Emacs Lisp Power! Transform Text Under Cursor

By Xah Lee. Date: . Last updated: .

This page shows a example of turning a amazon product url into a customized link.

Problem

Given a URL of any of the following form:

Write a command to change it into this form:

<a class="amz" href="http://www.amazon.com/dp/B003CP0BHM/?tag=xahh-20" title="Cyborg R T Gaming Mouse">amazon</a>

Solution

(defun xah-amazon-linkify ()
  "Make the current amazon URL or selection into a link.

Examples of amazon product URL formats
http://www.amazon.com/Cyborg-R-T-Gaming-Mouse/dp/B003CP0BHM/ref=pd_sim_e_1
http://www.amazon.com/gp/product/B003CP0BHM
http://www.amazon.com/exec/obidos/ASIN/B003CP0BHM/xahh-20
http://www.amazon.com/exec/obidos/tg/detail/-/B003CP0BHM/
http://www.amazon.com/dp/B003CP0BHM?tag=xahhome-20
http://amzn.to/1F5M1hA
https://alexa.design/2okfMcj

Example output:
<a class=\"amz\" href=\"http://www.amazon.com/dp/B003CP0BHM/?tag=xahh-20\" title=\"Cyborg R T Gaming Mouse\">amazon</a>

ASIN is a 10 character string that's a product id.

URL `http://ergoemacs.org/emacs/elisp_amazon-linkify.html'
Version 2017-07-22"
  (interactive)
  (let (($bds (bounds-of-thing-at-point 'url))
        $p1 $p2 $url $asin $thingName )
    (if (use-region-p)
        (setq $p1 (region-beginning) $p2 (region-end))
      (setq $p1 (car $bds) $p2 (cdr $bds)))
    (setq $url (buffer-substring-no-properties $p1 $p2))
    (if (or (string-match "//amzn.to/" $url)
            (string-match "//alexa.design/" $url))
        (progn (delete-region $p1 $p2)
               (insert (format "<a class=\"amz_search\" href=\"%s\">amazon</a>" $url)))
      (progn
        (setq $asin
              (cond
               ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/?" $url) (match-string 1 $url))
               ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)\\?tag=" $url) (match-string 1 $url))
               ((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" $url) (match-string 1 $url))
               ((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" $url) (match-string 1 $url))
               ((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" $url) (match-string 1 $url))
               ((and
                 (equal 10 (length $url ))
                 (string-match "\\`\\([[:alnum:]]\\{10\\}\\)\\'" $url))
                $url)
               (t (error "no amazon ASIN found"))))

        (setq
         $thingName
         (replace-regexp-in-string
          "-" " "
          (if (string-match "amazon\.com/\\([^/]+?\\)/dp/" $url)
              (progn (match-string 1 $url))
            (progn
              (message "no product name found" ))
            ""
            )))

        (delete-region $p1 $p2)
        (insert
         "<a class=\"amz\" href=\"http://www.amazon.com/dp/"
         $asin "/?tag=xahh-20\" title=\"" $thingName "\">Buy at amazon</a>")
        (search-backward "\">")))))

Sample Use

For example, when i'm writing Keyboard reviews , i go to amazon and read reviews, then copy the URL, then, i press a button, it becomes a link, and with proper CSS, it shows in browser like this:

Buy at amazon

The uniform format is important, because it allows you to systematically process them in the future. For example, you can make all such links into some HTML Microformat, or get a list of all amazon product links.

Similar Uses

The “amazon-linkify” code is very simple. It shows a common and powerful use of elisp, and is great for learning elisp.

The following transform a URL into a link of particular format for my site:

For example, if the word under cursor is “emacs”, then calling “wikipedia-linkify” changes it to:

<a href="http://en.wikipedia.org/wiki/emacs">emacs</a>

Transform Words into Computer Language References

For example, the “elisp-ref-linkify” transform any of the following form:

Into this link of uniform format:

<span class="ref"><a href="../elisp/The-Mark.html">(info "(elisp) The Mark")</a></span>

The relative link path above is automatically generated. With CSS it is displayed in browser like this:

(info "(elisp) The Mark")

Transform URL Into Embedded Video

For example, the youtube-linkify command turns the YouTube URL under cursor into a embeded video html.

Power in Transform Text Under Cursor

The essense of power of these is that they transform the text under cursor or a text selection by one single button press, to anything you want, may it be a specialized HTML link, or customized programing function template, or even creating a file on the fly in the process (For example, Writing a google-earth Function). If any command you use often, just assign a keyboard key to them.

So, start learning elisp today!

If your elisp skill is young, you can still get this text transform power by writing the text transform part in a language you already know. See: Emacs Lisp Wrapper for Perl/Python/Ruby Scripts.

Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon