2011-02-16

EmacsのバッファやDiredでカーソル位置のファイルを Quick Look

Mac OS X には qlmanage という便利なコマンドがあるのでそれを使う。

C-c y で Quick Look。もう一度 C-c y で、Quick Look を閉じる。
call-process を使って C-g で閉じるのもいいですが、やや反応が鈍いので下の例では start-process を使っています。

(defun my-quicklook-at-point ()
  "Preview a file at point with Quick Look."
  (interactive)
  (require 'ffap)
  (let ((url (ffap-url-at-point))
        (file (ffap-file-at-point))
        (process (get-process "qlmanage_ps")))
    (when url
      (if (string-match "\\`file://\\(.*\\)\\'" url)
          (setq file (match-string 1 url))
        (setq file nil)))
    (when (or (not (stringp file))
              (not (file-exists-p (setq file (expand-file-name file)))))
      (when process
        (kill-process process))
      (error "No file found"))
    (if process
        (kill-process process)
      (message "Quick Look: %s" file)
      (start-process "qlmanage_ps" nil "qlmanage" "-p" file))))

(global-set-key "\C-cy" 'my-quicklook-at-point)

Dired の設定。

(defun my-dired-do-quicklook ()
  "In dired, preview with Quick Look."
  (interactive)
  (let ((file (dired-get-filename))
        (process (get-process "qlmanage_ps")))
    (if process
        (kill-process process)
      (start-process "qlmanage_ps" nil "qlmanage" "-p" file))))

(eval-after-load "dired"
  '(define-key dired-mode-map "\C-cy" 'my-dired-do-quicklook))

キーバインドは適当に変えてください。

動作確認:GNU Emacs 22, Mac OS X 10.6