Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

EmacsでPerlライブラリのソースファイルを開く

デバッグしてる時などに、CPANからインストールしたモジュールを直接いじりたくなったりすることがあるので、以下のコマンドで開けるようにした。

(put 'perl-module-thing 'end-op
     (lambda ()
       (re-search-forward "\\=[a-zA-Z][a-zA-Z0-9_:]*" nil t)))
(put 'perl-module-thing 'beginning-op
     (lambda ()
       (if (re-search-backward "[^a-zA-Z0-9_:]" nil t)
           (forward-char)
         (goto-char (point-min)))))

(defun find-perl-module-file ()
  (interactive)
  (let ((module (thing-at-point 'perl-module-thing))
        (file ""))
    (when (string= module "")
      (setq module (read-string "Module Name: ")))
    (setq file (replace-regexp-in-string "\n+$" ""
                (shell-command-to-string (format "perldoc -l %s" module))))
    (if (string-match "^No documentation" file)
        (error (format "module not found: %s" module))
      (find-file file))))

追記

id:dankogaiさんのご指摘を受けて、ちょっと書き直しました。