Read CPAN module source code

有了cperl-perldoc,在Emacs里面看perl文档很方便,但有时候想看看代码,只好切换到命令行下面执行:

$ emacsclient --no-wait `perldoc -l A::Module`

如果使用下面的Emacs Lisp代码,可以省掉切换的麻烦,像使用cperl-perldoc看文档一样,直接在Emacs里面代开源代码文件。

(defun wl-cperl-find-module (module)
  "View source code of CPAN module."
  (interactive
   (list (let* ((default-module (cperl-word-at-point))
                (input (read-string
                        (format "CPAN module%s: "
                                (if (string= default-module "")
                                    ""
                                  (format " (default %s)" default-module))))))
           (if (string= input "")
               (if (string= default-module "")
                   (error "No module given")
                 default-module)
             input))))
  (let ((perldoc-output
         (with-temp-buffer
           (call-process "perldoc" nil t nil "-l" module)
           (buffer-substring-no-properties (point-min) (1- (point-max))))))
    (if (string-match "no documentation found" perldoc-output)
        (message "%s" perldoc-output)
      (find-file-other-window perldoc-output))))

(cperl-define-key (kbd "C-c m") 'wl-cperl-find-module)

Pretty print macro expansion

*scratch* buffer里面运行macroexpand展开宏后的程序全在一行,不好读,使用pp可以获得更好的效果。

(defun wl-pp-macroexpand-at-point ()
  (interactive)
  (pp-eval-expression '(macroexpand (read (thing-at-point 'sexp)))))

(define-key emacs-lisp-mode-map (kbd "C-c C-c") 'wl-pp-macroexpand-at-point)

将光标放在想要展开的宏调用的左括号前,调用该程序,宏展开的结果显示在*Pp Eval Output* buffer里。

如果想看宏调用执行后的结果,可以使用下面的函数。

(defun wl-pp-evaluate-at-point ()
  (interactive)
  (pp-eval-expression (macroexpand (read (thing-at-point 'sexp)))))

(define-key emacs-lisp-mode-map (kbd "C-c C-e") 'wl-pp-evaluate-at-point)