■ Windows版 Emacs 共通の設定


【お知らせ】


<2018/08/28 追記>
WSL での locate の利用については、こちらを参照ください。

<2018/08/26 追記>
locate コマンドで and 検索ができるようにあ、-A オプションを追加しました。

<2015/07/22 追記>
cron-config の実行時の挙動が変わったようなので、コマンドの指定方法を変更しました。

【本題】


Cygwin および NTEmacs から locate を利用するための設定です。

まず、locateを使うためのDBを次のようなスクリプトを実行して作成します。updatedb のオプションは、適当に修正してください。

~/bin/updatedb.sh
#!/bin/sh

export LC_ALL=C
export CYGWIN="nodosfilewarning"

updatedb \
    --localpaths='
c:/
' \
    --findoptions='
-type d -name .svn -prune -o
-type d -name .git -prune -o
-path c:/\$RECYCLE.BIN -prune -o
-path c:/Windows -prune
'

c:/ の並び(下)に別なドライブを追加することができます。
「//<コンピュータ名またはIPアドレス>/<共有名>/」のようなUNCパスを登録することでファイルサーバのファイル情報を登録することもできます。
ファイルサーバの認証が発生する場合は、次の方法で認証情報を記憶させてください。

findoptions の設定の意味は、次のとおりです。

 一行目:ディレクトリ.svnは含めない
 三行目:c:/$RECYCLE.BIN で始まるパスは含めない

これで、shell から locate <検索文字列> と入力して、検索が可能となります。
Emacs からは、M-x locate で実行できます。

locate の検索に時間が掛かる場合は、データベース化するディレクトリの範囲を絞ってください。

検索結果は、dired のように、Enter や f が利用可能ですが、次の設定を行っておくと、より便利に利用できます。
(require 'locate)

;; ファイルのあるディレクトリを OS で開く
(define-key locate-mode-map (kbd "E")
  (lambda ()
    (interactive)
    (os-open-command (locate-get-dirname))))

;; ファイルのあるディレクトリを dired で開く
(define-key locate-mode-map (kbd "d")
  (lambda ()
    (interactive)
    (dired (locate-get-dirname))))

;; C-c l で locate を起動する
(global-set-key (kbd "C-c l") 'locate)
※ 上記の設定の os-open-command は、「dired を OS と連携して使うための設定」で紹介したものと同じものを利用しています。
 「dired を OS と連携して使うための設定」で行った W キーや find-file の設定も、locate の検索結果の画面で利用可能です。

locate は anything とも連携できます。
;; locateコマンドのパラメータを指定する
(setq anything-c-locate-command "locate -i -A --regex %s")

helmと連携する場合は次のとおりとなります。
;; locateコマンドのパラメータを指定する
(setq helm-locate-command "locate %s -A --regex %s")
※ helm の設定については、次のページを参照してください。

また、上記で紹介した updatedb.sh は、cron を使って夜間等に毎日実行すると便利です。
その場合の設定を以下に紹介します。

0) Cygwin に cygrunsrv、cron がインストールされていなければ、インストールする。
  ※ インストールされているかどうかは、cygcheck -c cygrunsrv 、cygcheck -c cron で確認できる。
1) mintty を管理者権限で立ち上げ、cron-config を実行する。"<username>"の箇所には、ログインしているユーザ名を指定してください。
# cyg_username=<username> cron-config
Do you want to install the cron daemon as a service? (yes/no) yes
Enter the value of CYGWIN for the daemon: [ ]  ← Returnのみ押下

You must decide under what account the cron daemon will run.
If you are the only user on this machine, the daemon can run as yourself.
   This gives access to all network drives but only allows you as user.
To run multiple users, cron must change user context without knowing
  the passwords. There are three methods to do that, as explained in
  http://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-nopasswd1
If all the cron users have executed "passwd -R" (see man passwd),
  which provides access to network drives, or if you are using the
  cyglsa package, then cron should run under the local system account.
Otherwise you need to have or to create a privileged account.
  This script will help you do so.
Do you want the cron daemon to run as yourself? (yes/no) yes

Please enter the password for user '<username>': ← ログインユーザが <username> の部分に表示されるので、そのパスワードを入力する
Reenter: ← パスワードを再入力する
Running cron_diagnose ...
... no problem found.

Do you want to start the cron daemon as a service now? (yes/no) yes
OK. The cron daemon is now running.

In case of problem, examine the log file for cron,
/var/log/cron.log, and the Windows event log (using /usr/bin/cronevents)
for information about the problem cron is having.

Examine also any cron.log file in the HOME directory
(or the file specified in MAILTO) and cron related files in /tmp.

If you cannot fix the problem, then report it to cygwin@cygwin.com.
Please run the script /usr/bin/cronbug and ATTACH its output
(the file cronbug.txt) to your e-mail.

WARNING: PATH may be set differently under cron than in interactive shells.
         Names such as "find" and "date" may refer to Windows programs.

2) cron が正常に起動したかを、cygrunsrv -Q cron を実行することで確認する。
3) うまく動作しない場合は、cronevents コマンドでログを確認する。
4) cron が正常に起動していることを確認したら、crontab に次を設定する。ユーザ権限(管理者権限でなく)で実行した mintty から、crontab -e で設定する。確認は、crontab -l。
0 3 * * 1,2,3,4,5 /bin/sh /home/bin/updatedb.sh > /tmp/updatedb.log 2>&1
 ※ この設定は、平日の深夜3時に ~/bin に格納されている updatedb.sh を実行するものとなります。

5) 登録した cron サービスを削除するには、、cygrunsrv -R cron を実行する。
6) Cygwin のアップデートを行うとき、プログラムが使用されているというエラーとなる場合があるので、そのときは Windows のコントロールパネルのサービスの機能を使って、Cron daemon のサービスを一時的に停止する。
 ※ 管理権限で立ち上げた mintty から、cygrunsrv -E cron(停止)、cygrunsrv -S cron(起動)を行うことも可能

上記は、次のページを参考としました。



<変更履歴>
  • 2012/09/25 anything から locate を使う場合の説明を追加した。
  • 2012/12/27 lambda関数のクォートをとった。
  • 2014/04/30 updatedb のパラメータの説明文を追加した。
  • 2015/07/22 cron-config の実行時の挙動が変わったようなので、コマンの指定方法を変更した。
  • 2018/08/26 locate コマンドで and 検索ができるようにあ、-A オプションを追加した。


最終更新:2019年10月31日 22:26