■ Windows版 Emacs 共通の設定 (要Cygwin)

【お知らせ】


<2017/09/04 追記>
次の設定をまとめました。本ページの設定よりもこちらの設定の利用をお勧めします。

<2017/04/06 追記>
Cygwin の cygutils package には readshortcut という Windowsショートカットのリンク先を表示するコマンドが含まれていることが分かりました。但し、readshortcut はファイルサーバなどの UNCパス上にあるショートカットや Cygwin の ln -s で作成したショートカット(CYGWIN環境変数が winsymlinks:lnk に設定された状態で ln -s をするとショートカットが作成される)のリンク先を表示することができないようです。このことから、本設定の sympath を置き換えるのではなく、sympath の中から readshortcut が使える場合には使うような設定に見直しをしてみました。この対応により、readshortcut が使える場合は、処理の高速化が図られています。

<2017/04/04 追記>
Cygwin版 Emacs をお使いの方は次の設定もお試しください。

【本題】


Windows ショートカットを dired から開くための設定です。

0) Cygwin に cygutils package がインストールされていなければ、インストールする。(必須ではありませんが、インストールすると処理の高速化が期待できます。)

1) ~/bin 等のパスがとおったディレクトリに、sympath というフィアル名で以下の内容を格納する。

sympath
#!/bin/sh

cscript="/c/WINDOWS/system32/cscript"

function usage_exit() {
    command_name=$(basename "$0")
    echo "Usage: $command_name shortcut"
    exit 1
}

if [ $# -ne 1 ]; then
    usage_exit
fi

if [[ ! "$1" =~ \.lnk$ ]]; then
    usage_exit
fi

if [ ! -e "$1" ]; then
    usage_exit
fi

shortcut_path=$(cygpath -a -w "$1")

if [[ ! "$shortcut_path" =~ \.lnk$ ]]; then
    output="$shortcut_path"
else
    if type readshortcut &> /dev/null; then
        if readshortcut "$shortcut_path" 2> /dev/null; then
            exit 0
        fi
    fi

    absolute_path=$(readlink -n -f "$0")
    directory_path=$(dirname "$absolute_path")

    cd "$directory_path"

    output=$($cscript //Nologo sympath.vbs "$shortcut_path" | tr -d '\r\n' | nkf -w)
fi

cygpath -u "$output"

2) sympath と同じディレクトリに、sympath.vbs という名前で以下の内容を格納する。

sympath.vbs
' http://www.atmarkit.co.jp/ait/articles/0712/27/news083_2.html

Wscript.Echo WScript.CreateObject("WScript.Shell").CreateShortcut(WScript.Arguments(0)).TargetPath

3) 以下の設定を行う。
(defun ad-dired-get-file-for-visit-3 (return-value)
  (if (string-match "\\.lnk$" return-value)
      (replace-regexp-in-string "\n" ""
                                (shell-command-to-string
                                 (concat "sympath "
                                         (shell-quote-argument return-value))))
    return-value))

(advice-add 'dired-get-file-for-visit :filter-return #'ad-dired-get-file-for-visit-3)


<変更履歴>
  • 2017/03/28 このページを作成した。


最終更新:2019年11月03日 09:39