位元詩人 技術雜談: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) 是資訊領域碩士,喜歡用開源技術來解決各式各樣的問題。這類技術跨平台、重用性高、技術生命長。

除了開源技術以外,位元詩人喜歡日本料理和黑咖啡,會一些日文,有時會自助旅行。