Emacs Lisp Power! amazon-linkify

Perm url with updates: http://xahlee.org/emacs/elisp_amazon-linkify.html

Emacs Lisp Power! Transform Text Under Cursor

Xah Lee, 2010-11-03

This pages showcases the power of emacs.

Just updated a elisp command.

(defun amazon-linkify ()
  "Make the current url or region into a Amazon 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/-/0877796335/

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

For info about the amazon id in url, see:
URL `http://en.wikipedia.org/wiki/Amazon_Standard_Identification_Number'"
  (interactive)
  (let (asin p1 p2 meat productName tmpBds)
    (if (region-active-p)
        (setq p1 (region-beginning) p2 (region-end))
      (progn
        (setq tmpBds (bounds-of-thing-at-point 'url) )
        (setq p1 (car tmpBds) )
        (setq p2 (cdr tmpBds) )
        ))
    ;; get the text
    (setq meat (buffer-substring-no-properties p1 p2) )

    ;; extract the id from text
    (cond
     ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/" meat) (setq asin (match-string 1 meat) ))
     ((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" meat) (setq asin (match-string 1 meat) ))
     ((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" meat) (setq asin (match-string 1 meat) ))
     ((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" meat) (setq asin (match-string 1 meat) ))
     ((and 
       (equal 10 (length meat ) )
       (string-match "^\\([[:alnum:]]\\{10\\}\\)$" meat)
       )
      (setq asin meat ))
     (t (error "no amazon ASIN found"))
     )

    ;; extract the product name from url, if any
    (cond
     ((string-match "amazon\.com/\\([^/]+?\\)/dp/" meat) (setq productName (match-string 1 meat) ))
     (t (setq productName "") (message "no product name found" ) (ding))
     )

    ;; replace dash to space in productName
    (setq productName (replace-regexp-in-string "-" " " productName) )
 
    (delete-region p1 p2)
    (insert
     "<a class=\"amz\" href=\"http://www.amazon.com/dp/"
     asin "/?tag=xahh-20\" title=\"" productName "\">amazon</a>")
    (search-backward "\">")
    ))

Sample Use

For example, when i'm writing about keyboards or any tech product Kindle, Apple Macbook, iPad, or movie reviews, i go to amazon and read reviews, then press 【Ctrl+l】 to place cursor on the url field, 【Ctrl+c】 copy, press F6 to switch me to emacs, paste. I get a url that may be in any of the following form:

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/
http://www.amazon.com/exec/obidos/tg/detail/-/B003CP0BHM/

then, i press a button, it become a link with this uniform format:

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

With proper CSS it shows in browser like this: amazon.

The uniform format is important, because it allows me to systematically process them in the future. For example, possibly one day i need to make them into some particular HTML Microformat, or get a list of all amazon links with product names.

Similar Uses

This snippet is simple to understand, shows a common and powerful use of elisp, is great for learning elisp. I have 30 other similar “linkify” commands. They are:

Transform URL Into Links

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

  • amazon-linkify
  • blogger-linkify
  • wikipedia-linkify
  • full-size-img-linkify
  • image-linkify
  • nks-linkify
  • source-linkify
  • xah-all-linkify
  • xah-curve-linkify
  • xah-file-linkify
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>

The following transform a URL into search link:

  • amazon-search-linkify
  • amazon-search-linkify-url
  • youporn-search-linkify
  • youtube-search-linkify
  • video-search-linkify
  • google-search-linkify

For example, if text selection is “world juggling federation”, then calling “video-search-linkify” it becomes:

<a class="gvidsr" href="http://www.google.com/search?tbs=vid%3A1&amp;q=world+juggling+federation">world juggling federation</a>

And with CSS it shows in browser like this:

world juggling federation

Transform URL Into Computer Language References

  • elisp-ref-linkify
  • emacs-ref-linkify
  • java-ref-linkify
  • mathematica-ref-linkify
  • perldoc-ref-linkify
  • php-ref-linkify
  • povray-ref-linkify
  • python-ref-linkify

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

(elisp) The Mark
file:///Users/xah/web/xahlee_org/elisp/The-Mark.html#The-Mark
file:///Users/xah/web/xahlee_org/elisp/The-Mark.html
~/web/elisp/The-Mark.html

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

  • youtube-linkify
  • dailymotion-video-linkify
  • google-video-linkify
  • redtube-linkify
  • break-video-linkify
  • tudou-video-linkify

For example, the “youtube-linkify” command turns the url under cursor, grabbed from the url field in a browser, like this:

http://www.youtube.com/watch?v=fjml8K7_Xfk

into a embedded video with VALID HTML and works in all browsers, like this:

<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/fjml8K7_Xfk" width="480" height="385"><param name="movie" value="http://www.youtube.com/v/fjml8K7_Xfk"></object>

About 10 of these linkify commands i use few times per hour, each, for writing my website. You can see the source code at Xah Lee's Emacs Customization Files.

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 (e.g. Writing a google-earth Function). If any command you use often, just assign a keyboard key to them.

So, start learning elisp today. If you are a complete newb, see: Emacs Lisp Basics and Emacs Lisp Examples. If you have some elisp experience, you can read Emacs Lisp Idioms and the 10 or so detail example tutorial at Emacs Lisp Tutorial.

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

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs