位元詩人 技術雜談:Call Online Perl Document from Emacs

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

Sometimes we want to browse online document for the usages of Perl subroutines or modules. It is boringly repetitive to open the browser and key in the keywords. Is there any way to automate the process? With some tricks, you can open online documents by hotkeys.

You may check this original Emacs script to utilize p3rl.org. p3rl.org is a web service providing shortened url for online Perl documents and Perl module documents. By using the service, you may access Perl online documents and Perl module documents with the same url. Here we provide this tip to directly access Perl documents from Emacs.

Check that whether thing-at-point is available in your Emacs before start the hack. thing-at-point is a built-in part of Emacs, so it should already be available. One way to check its existence is using M-x find-library RET thingatpt within Emacs. If not, install it.

Add all the following contents in ~/.emacs. First, register a new key word bounds at point.


;; extend thing-at-point to match a Perl subroutine or module
(defun perl-module-bounds-of-perl-module-at-point ()
  "Return the start and end point of a Perl module"
  (save-excursion
    (skip-chars-backward
     ":0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
    (if (looking-at "[A-Za-z0-9:]+")
    (cons (point) (match-end 0))
      nil)))

;; register to thing-at-point
(put 'perl-module 'bounds-of-thing-at-point
       'perl-module-bounds-of-perl-module-at-point)
{{< / highlight >}}

Next, set the command to browse the url from Emacs.

```lisp
;; set http://p3rl.org string
(defcustom perl-search-url "http://p3rl.org/"
  "URL at which to search for documentation on a word"
  :type 'string
  :group 'perl)

;; browse document with browse-url command
(defun perl-search-documentation ()
  "Search Perl documentation for the word at the point."
  (interactive)
  (browse-url (concat perl-search-url (thing-at-point 'perl-module))))

;; set hotkey, you may change to other hotkey
(global-set-key "\C-c\C-f"  'perl-search-documentation)
{{< / highlight >}}

Then, we can browse online Perl documents for subroutine and modules at the location of these keywords with `C-c C-f`, saving many repetitive keystrokes.
關於作者

身為資訊領域碩士,位元詩人 (ByteBard) 認為開發應用程式的目的是為社會帶來價值。如果在這個過程中該軟體能成為永續經營的項目,那就是開發者和使用者雙贏的局面。

位元詩人喜歡用開源技術來解決各式各樣的問題,但必要時對專有技術也不排斥。閒暇之餘,位元詩人將所學寫成文章,放在這個網站上和大家分享。