This page showcases the power of emacs.
Just updated a elisp command.
(defun amazon-linkify () "Change current URL or text selection into a Amazon.com 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 inputText 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 inputText (buffer-substring-no-properties p1 p2) ) ;; extract the id from text (cond ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/" inputText) (setq asin (match-string 1 inputText) )) ((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" inputText) (setq asin (match-string 1 inputText) )) ((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" inputText) (setq asin (match-string 1 inputText) )) ((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" inputText) (setq asin (match-string 1 inputText) )) ((and (equal 10 (length inputText ) ) (string-match "^\\([[:alnum:]]\\{10\\}\\)$" inputText) ) (setq asin inputText )) (t (error "no amazon ASIN found")) ) ;; extract the product name from URL, if any (cond ((string-match "amazon\.com/\\([^/]+?\\)/dp/" inputText) (setq productName (match-string 1 inputText) )) (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 "\">") ))
For example, when i'm writing about keyboards or any tech product or books, i go to amazon and read reviews, then copy the URL. The URL 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 becomes 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.
The “amazon-linkify” code is very simple. It shows a common and powerful use of elisp, and is great for learning elisp. I have 30 other similar “linkify” commands. They are:
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>
The following transform words into search link:
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&q=world+juggling+federation">world juggling federation</a>
And with CSS it shows in browser like this: world juggling federation.
For example, the “elisp-ref-linkify” transform any of the following form:
• (elisp) The Mark • ~/web/elisp/The-Mark.html • c:/Users/h3/web/xahlee_org/emacs_manual/elisp/The-Mark.html • file:///Users/xah/web/xahlee_org/elisp/The-Mark.html • file:///Users/xah/web/xahlee_org/elisp/The-Mark.html#The-Mark
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").
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. Most of them have a keybinding, or a 2-letter alias. You can see the source code at Xah Lee's Emacs Customization Files.
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 (⁖ 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 language you already know. See: Emacs Lisp Wrapper for Perl/Python/Ruby Scripts.