dired-get-filenameのオプション
連休といえば.emacs.dの見直しのいい機会ですね。
今日はこちらを参考にDiredの挙動を
- ファイルを開くときは新規バッファで開く
- ディレクトリを開くときは同じバッファを更新する
になるようにしてみたのですが、途中ちょっとトラブルがあったのでメモします。
変更後トラブル
こちらの記事を参考に変更を施した後、Dired上の親ディレクトリ..を選択したところ、
Cannot operate on `.' or `..'
というエラーが出るようになってしまい、親ディレクトリに飛べなくなってしまいました。今までは普通にDired内で親ディレクトリに飛べていたのですが。
どうやら参考にした関数の中で用いられているdired-get-filenameがデフォルトでは.や..を特別扱いしていることが原因のようです。
これはdired-get-filenameはオプション引数の1つNO-ERROR-IF-NOT-FILEPをtにしてあげれば解決します。
上のブログで提案されている関数を以下のように修正します:
(defun dired-open-in-accordance-with-situation ()
  (interactive)
;; (let ((file (dired-get-filename)))
   (let ((file (dired-get-filename nil t)))
    (if (file-directory-p file)
        (dired-find-alternate-file)
      (dired-find-file))))
これで無事、親ディレクトリにもジャンプすることができるようになりました。
関数のドキュメント
ちなみにdired-get-filenameのオプションは以下のように定義されています。
1つ目の引数LOCALPは返り値の形式(ディレクトリの有無, 絶対パス/相対パス)を指定し、2つめのNO-ERROR-IF-NOT-FILEPは上記で述べたように.や..を特別扱いしないようにするオプションのようです。
"In Dired, return name of file mentioned on this line. Value returned normally includes the directory name. Optional arg LOCALP with value `no-dir' means don't include directory name in result. A value of `verbatim' means to return the name exactly as it occurs in the buffer, and a value of t means construct name relative to `default-directory', which still may contain slashes if in a subdirectory. Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as regular filenames and return nil if no filename on this line. Otherwise, an error occurs in these cases."
(dired.elより)