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.


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>


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

Examples of amazon product URL formats

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"
  (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)))
        (setq $asin
               ((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))
                 (equal 10 (length $url ))
                 (string-match "\\`\\([[:alnum:]]\\{10\\}\\)\\'" $url))
               (t (error "no amazon ASIN found"))))

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

        (delete-region $p1 $p2)
         "<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.

Elisp HTML Commands

  1. wrap-url
  2. URL to Link
  3. Link to Dead Link
  4. Make Image Links
  5. Extract URL
  6. Word to Wikipedia Linkify
  7. Wikipedia URL Linkify
  8. URL Percent Decode/Encode
  9. Lines to HTML Table
  10. Markup Function Names
  11. Color Source Code
  12. Transform Text Under Cursor
  13. Chinese Char Reference Linkify
  14. HTML make-citation
  15. update-title
  16. Google Map Linkify 🌐
  17. CSS Compressor
  18. Make Ruby Annotation
  19. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp