;;-*- coding: utf-8 -*-
;; Xah Lee's personal functions for transforming cursor location's text into HTML links.
;; 2007-10, 2011-05-29
;; ∑ http://xahlee.org/
;; § ----------------------------------------
(defun image-linkify ()
"Replace a image file's path under cursor with a HTML img tag,
If there's a text selection, use that as path.
For example, if cursor is on the string
i/cat.png
then it will became
Image path can be a URL or local file. Supported file suffix are
{.gif, .png, .svg}. If it is URL (starting with “http”), then no
“width” and “height” attribute will be added."
(interactive)
(let ( bds p1 p2 ξpath ξwidthHeight ξwidth ξheight altText)
(setq bds (get-selection-or-unit 'glyphs))
(setq ξpath (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq altText (file-name-sans-extension (file-name-nondirectory ξpath)))
(setq altText (replace-regexp-in-string "_" " " altText t t))
(setq altText (replace-regexp-in-string "-s$" "" altText))
(if (string-match "^http" ξpath)
(progn
(delete-region p1 p2)
(insert "") )
(progn
(setq ξpath (windows-style-path-to-unix (local-url-to-file-path ξpath)))
(if (file-exists-p ξpath)
(progn
(setq ξwidthHeight (get-image-dimensions ξpath) )
(setq ξwidth (number-to-string (elt ξwidthHeight 0)))
(setq ξheight (number-to-string (elt ξwidthHeight 1)))
(delete-region p1 p2)
(insert "")
)
(error "File does not exist")) ) ) ))
(defun image-wrap ()
"Replace a image file's path under cursor with a HTML img tag,
and wrap it with “figure” and “figcaption” tags.
Example, if cursor is on the word “i/cat.png”, then it will became
▮
If there's a text selection, use that as image path.
This function calls `image-linkify' to do its work."
(interactive)
(let (myStr)
(image-linkify)
(search-backward "<")
(insert "\n")
(search-forward ">")
(insert "
")
(search-backward "")
(backward-char)
))
(defun full-size-img-linkify ()
"Make image file path at cursor point into a img link.
Example:
i/goddess.jpg
becomes
❐
If region is active, use region as file name."
(interactive)
(let
(bds p3 p4 inputStr imgPath
;; imgFileName linkText
ξdimension ξwidth ξheight resultStr)
(setq bds (get-selection-or-unit 'glyphs))
(setq inputStr (elt bds 0) p3 (elt bds 1) p4 (elt bds 2) )
(setq imgPath (local-url-to-file-path inputStr))
;; (setq imgPath (windows-style-path-to-unix imgPath))
;; (message "ttt is : %s" imgPath)
;; (setq imgFileName (file-name-nondirectory imgPath))
;; (setq linkText
;; (if (< (length imgFileName) 20)
;; imgFileName
;; (concat (substring imgFileName 0 5) "…" (substring imgFileName -6) ) ))
(setq ξdimension (get-image-dimensions-imk imgPath))
(setq ξwidth (number-to-string (elt ξdimension 0)))
(setq ξheight (number-to-string (elt ξdimension 1)))
(setq resultStr
(concat "❐"))
(delete-region p3 p4)
(insert resultStr)))
(defun url-percent-encode-string (ξurl)
"Returns URL percent-encoded
Example:
http://en.wikipedia.org/wiki/Python_(programming_language)
⇒
http://en.wikipedia.org/wiki/Python_%28programming_language%29
"
(let (ξurl2)
;; (require 'url-util)
;; (url-hexify-string "http://xahlee.org/emacs/emacs.html")
;; todo: needs to properly do URL percent encoding
(setq ξurl2 (replace-regexp-in-string "&" "&" ξurl))
(setq ξurl2 (replace-regexp-in-string "," "%2C" ξurl2))
(setq ξurl2 (replace-regexp-in-string "\"" "%22" ξurl2))
))
(defun wrap-wikipedia-url (ξstring &optional ξfrom-to-pair)
"Make the URL at cursor point into a html link.
If there is a text selection, use that as input.
Example:
http://en.wikipedia.org/wiki/Emacs
⇒
Emacs.
When called interactively, work on current URL or text selection.
When called in lisp code, if ξstring is non-nil, returns a changed string. If ξstring nil, change the text in the region between positions in sequence ξfrom-to-pair."
(interactive
(if (region-active-p)
(list nil (vector (region-beginning) (region-end)))
(let ((bds (get-selection-or-unit ["^ \t\n,([{<>〔“\"" "^ \t\n,)]}<>〕\"”"])) )
(list nil (vector (aref bds 1) (aref bds 2))) ) ) )
(let (workOnStringP inputStr outputStr
(ξfrom (elt ξfrom-to-pair 0))
(ξto (elt ξfrom-to-pair 1)))
(setq workOnStringP (if () t nil))
(setq inputStr (if workOnStringP ξstring (buffer-substring-no-properties ξfrom ξto)))
(require 'gnus-util) ; url-percent-encode-string
(setq outputStr
(let ((linkText inputStr))
(setq linkText
(progn
(setq linkText (substring (replace-regexp-in-string "\\([^/]+\\)/" "" inputStr) 1)) ; get the last part after /
(setq linkText (replace-regexp-in-string "_" " " linkText))
(setq linkText (replace-regexp-in-string "%27" "'" linkText))
(setq linkText (replace-regexp-in-string "%28" "(" linkText))
(setq linkText (replace-regexp-in-string "%29" ")" linkText)) )
)
(concat "" linkText "" ))
)
(if workOnStringP
outputStr
(progn
(delete-region ξfrom ξto)
(goto-char ξfrom)
(insert outputStr) )) ) )
(defun wrap-url (ξstring &optional ξfrom ξto)
"Make the URL at cursor point into a html link.
When called interactively, work on current letter sequence or text selection.
When called in lisp code, if ξstring is non-nil, returns a changed string. If ξstring nil, change the text in the region between positions ξfrom ξto."
(interactive
(if (region-active-p)
(list nil (region-beginning) (region-end))
(let ((bds (unit-at-cursor 'glyphs)) )
(list nil (elt bds 1) (elt bds 2)) ) ) )
(let (workOnStringP inputStr outputStr)
(setq workOnStringP (if ξstring t nil))
(setq inputStr (if workOnStringP ξstring (buffer-substring-no-properties ξfrom ξto)))
(setq outputStr (concat "" inputStr "" ) )
(if workOnStringP
outputStr
(save-excursion
(delete-region ξfrom ξto)
(goto-char ξfrom)
(insert outputStr) )) )
)
(defun blogger-linkify ()
"Make URL at cursor point into a html link.
Example: http://xahlee.blogspot.com/2010/03/some.html
becomes
"
)
(setq resultStr (replace-regexp-in-string "乔" ξchar templateStr))
(delete-region p1 p2)
(insert resultStr) ))
(defun word-etymology-linkify ()
"Make the current word into a etymology reference link.
."
(interactive)
(let ( bds p1 p2 inputstr resultStr)
(setq bds (get-selection-or-unit 'line))
(setq inputstr (elt bds 0) p1 (elt bds 1) p2 (elt bds 2) )
(setq resultStr (concat "" inputstr "") )
(delete-region p1 p2)
(insert resultStr) ))
;; § ----------------------------------------
(defun youporn-search-linkify ()
"Make the current line into a YouPorn.com link.
For example, if the cursor is on the line:
anal
Then it'll become
\(YouPorn video: anal\)"
(interactive)
(let (bds p1 p2 ξword ξurl)
(setq bds (get-selection-or-unit 'line))
(setq ξword (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq ξurl (concat "http://www.youporn.com/search?query=" ξword) )
(setq ξurl (replace-regexp-in-string " " "+" ξurl ) )
(delete-region p1 p2)
(insert "(YouPorn video: " ξword ")\n")))
(defun youtube-search-linkify ()
"Make the current line into a YouTube link.
If there's a text selection, use that.
For example, if the cursor is on the line:
David Bowie
Then it'll become
David Bowie
Warning: the line must end in a line return char else the result is wrong.
Note: old version returns this form:
David Bowie
"
(interactive)
(let (bds p1 p2 ξword ξurl)
(setq bds (get-selection-or-unit 'line))
(setq ξword (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq ξurl (concat "http://youtube.com/results?search_query=" ξword "&search=Search") )
(setq ξurl (replace-regexp-in-string " " "+" ξurl ) )
(setq ξurl (replace-regexp-in-string "," "%2C" ξurl ) )
(delete-region p1 p2)
(insert "" ξword "")))
(defun video-search-string (searchString)
"Return a Google video search string URL of SEARCHSTRING.
Example:
「(video-search-string \"White Rabbit, Jefferson Airplane\")」 ⇒
「http://www.google.com/search?tbs=vid%3A1&q=White+Rabbit%2C+Jefferson+Airplane」
This command is called by `video-search-linkify'."
(let (strEncoded)
(setq strEncoded searchString )
(setq strEncoded (replace-regexp-in-string " " "+" strEncoded ) )
(setq strEncoded (url-percent-encode-string strEncoded ) )
(concat "http://www.google.com/search?tbs=vid%3A1&q=" strEncoded)
))
(defun video-search-linkify ()
"Make the current line into a Google video search link.
If there's a text selection, use that.
For example, if the cursor is on the line:
White Rabbit, Jefferson Airplane
Then it'll become
White Rabbit, Jefferson Airplane
Warning: the line must end in a line return char else the result is wrong.
This command calls `video-search-string'"
(interactive)
(let (bds p1 p2 ξword ξurl)
(setq bds (get-selection-or-unit 'line))
(setq ξword (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq ξurl (video-search-string ξword) )
(delete-region p1 p2)
(insert "" ξword "")))
(defun google-search-linkify ()
"Make the current line into a Google search link.
For example, if the cursor is on the line:
emacs lisp
Then it'll become
\n")))
;; § ----------------------------------------
;; some custom html markup and functions for working with html
(defun nks-linkify ()
"Make the current word into into a link to Wolfram Science site.
For Example, if you cursor is on the word “p123”, then
it becomes
“p123”"
(interactive)
(let (bds p1 p2 inputStr pagenum myresult)
(setq bds (get-selection-or-unit 'glyphs))
(setq inputStr (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq pagenum (substring inputStr 1) )
(setq myresult
(concat
"p" pagenum ""))
(delete-region p1 p2)
(insert myresult)
))
(defun listify-block ()
"Make the current block of lines into a HTML list.
Any URL in the line will be turned into links.
Example:
If your cursor is in the following block of text:
Castratos are castrated males made for singing: http://en.wikipedia.org/wiki/Castrato , record of the last castrato: http://www.archive.org/details/AlessandroMoreschi
human vocal range: http://en.wikipedia.org/wiki/Vocal_range
It will become:
")
(buffer-string)
) )
)
(delete-region p1 p2)
(insert resultStr)
) )
(defun listify-block_old_2011-02-01 ()
"Make the current block of lines into a HTML list.
Any URL in the line will be turned into links.
Example:
If your cursor is in the following block of text:
Castratos are castrated males made for singing: http://en.wikipedia.org/wiki/Castrato , record of the last castrato: http://www.archive.org/details/AlessandroMoreschi
human vocal range: http://en.wikipedia.org/wiki/Vocal_range
It will become:
")))
;; § ----------------------------------------
;; more specific to Xah Lee
(defun amazon-search-linkify-url (sString productCat assid)
"Returns a URL of amazon search based on search string and product category.
sString is the search string. e.g. “deep throat”
productCat is a short code for amazon's product category.
See `amazon-search-linkify' for the possible code string.
Sample call:
(amazon-search-linkify-url \"debbie does dollas\" \"dvd\" \"xahh-20\")"
(interactive)
(let (sStrPercent)
(setq sStrPercent sString)
(setq sStrPercent (replace-regexp-in-string " " "%20" sStrPercent) )
(setq sStrPercent (replace-regexp-in-string "," "%2c" sStrPercent) )
(concat
""
sString
""
) ) )
(defun amazon-search-linkify ()
"Make the current line or region into a Amazon product search link.
The current line must have this format:
search word;code
The “search word” is any letter and space.
the “code” is one of the following:
a = “blended” = all categories.
d = “dvd” = movies and tv.
b = “books”
c = “classical” = classical music
p = “pc-hardware”
e = “electronics”
m = “music”
s = “software”
There are other amazon categories, but not supported by this function."
(interactive)
(let (p1 p2 mainText tmplist sstr pcato pcc)
(if (region-active-p)
(setq p1 (region-beginning) p2 (region-end))
(progn
(setq p1 (line-beginning-position) )
(setq p2 (line-end-position) )
))
;; get the text
(setq mainText (buffer-substring-no-properties p1 p2) )
(setq tmplist (split-string mainText ";") )
(setq sstr (nth 0 tmplist ) )
(setq pcato (nth 1 tmplist ) )
(message "%s , %s" sstr pcato)
(cond
((string= pcato "a") (setq pcc "blended"))
((string= pcato "d") (setq pcc "dvd"))
((string= pcato "b") (setq pcc "books"))
((string= pcato "c") (setq pcc "classical"))
((string= pcato "p") (setq pcc "pc-hardware"))
((string= pcato "e") (setq pcc "electronics"))
((string= pcato "m") (setq pcc "music"))
((string= pcato "s") (setq pcc "software"))
(t (error "Code does not match"))
)
(delete-region p1 p2)
(insert (amazon-search-linkify-url sstr pcc "xahh-20"))
))
(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/-/B003CP0BHM/
http://www.amazon.com/dp/B003CP0BHM?tag=xahhome-20
Example output:
amazon
For info about the amazon id in URL, see:
URL `http://en.wikipedia.org/wiki/Amazon_Standard_Identification_Number'"
(interactive)
(let (bds p1 p2 mainText asin productName
;; tmpBds
)
(setq bds (get-selection-or-unit 'glyphs))
(setq mainText (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
;; extract the id from text
(cond
((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/" mainText) (setq asin (match-string 1 mainText) ))
((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)\\?tag=" mainText) (setq asin (match-string 1 mainText) ))
((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" mainText) (setq asin (match-string 1 mainText) ))
((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" mainText) (setq asin (match-string 1 mainText) ))
((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" mainText) (setq asin (match-string 1 mainText) ))
((and
(equal 10 (length mainText ) )
(string-match "^\\([[:alnum:]]\\{10\\}\\)$" mainText)
)
(setq asin mainText ))
(t (error "no amazon ASIN found"))
)
;; extract the product name from URL, if any
(cond
((string-match "amazon\.com/\\([^/]+?\\)/dp/" mainText) (setq productName (match-string 1 mainText) ))
(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
"amazon")
(search-backward "\">")
))
;; (defun local-linkify ()
;; "Make the path under cursor into a local link.\n
;; For Example, if you cursor is on the text “../emacs/emacs.html”,
;; then it'll become:
;; “Xah's Emacs Tutorial”.
;; The link text is pulled from the file's
tag.
;; If a region is active, use the region as file path."
;; (interactive)
;; (let (myPath bounds tempBuff x1 x2 titleText resultStr)
;; (setq myPath
;; (if (region-active-p)
;; (buffer-substring-no-properties (region-beginning) (region-end))
;; (thing-at-point 'filename)
;; ))
;; (setq bounds (bounds-of-thing-at-point 'filename))
;; (setq tempBuff (generate-new-buffer-name " temp"))
;; (when (file-exists-p myPath)
;; (progn
;; (save-current-buffer
;; (message myPath)
;; (set-buffer (get-buffer-create tempBuff))
;; (goto-char (point-min))
;; (insert-file-contents myPath nil nil nil t)
;; (setq x1 (search-forward ""))
;; (search-forward "")
;; (setq x2 (search-backward "<"))
;; (setq titleText (buffer-substring-no-properties x1 x2))
;; (kill-buffer tempBuff))
;; (setq resultStr (concat "" titleText ""))
;; (save-excursion
;; (delete-region (car bounds) (cdr bounds))
;; (insert resultStr))))
;; ))
(defun xah-file-linkify ()
"Make the path under cursor into a HTML link for xahlee.org.
For Example, if you cursor is on the text “../emacs/emacs.html”,
then it'll become:
“Xah's Emacs Tutorial”.
The link text is pulled from the file's
tag.
The file path can also be a full path or URL, e.g.:
/Users/xah/web/xahlee_org/web/emacs/emacs.html
file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html
file:///C:/Users/h3/web/xahlee_org/emacs/keyboards_hacker_idiocy.html
file://localhost/C:/Users/xah/web/xahlee_org/emacs/emacs.html
If there is text selection, use it as file path."
(interactive)
(let (bds p1 p2 inputStr fPath rltvPath titleText resultStr )
(setq bds (get-selection-or-unit ["^ \t\n,()[]{}<>〔〕“”\"" "^ \t\n,()[]{}<>〔〕“”\""]))
(setq inputStr (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(setq fPath
(cond
((string-match "^file://" inputStr ) (local-url-to-file-path inputStr))
((string-match "^[A-Za-z]:" inputStr ) (windows-style-path-to-unix inputStr))
((string-match "^http://xah" inputStr ) (xahsite-url-to-filepath inputStr))
(t inputStr)
)
)
(setq fPath (replace-regexp-in-string "/$" "/index.html" fPath)) ; add file name if not there
(setq fPath (replace-regexp-in-string "^C:" "" fPath)) ; remove the C:
(message "fpath: %s" fPath)
(setq rltvPath (file-relative-name fPath))
(message "rltvPath: %s" rltvPath)
(if (file-exists-p rltvPath)
(progn
(setq titleText
(if (string-match ".+html\\'" rltvPath)
(get-html-file-title rltvPath)
(file-name-nondirectory rltvPath)))
(setq resultStr (concat "" titleText ""))
(delete-region p1 p2)
(insert resultStr)
)
(progn (message
(concat "Cannot locate the file: " rltvPath)
))
)
))
(defun xah-curve-linkify ()
"Make the current word or text selection into a html link.
This function works on Xah Lee's website only.
Example:
“parabola” becomes
“parabola”.
The directory to search includes:
“SpecialPlaneCurves_dir” and “surface”."
(interactive)
(let (bds p1 p2 cursorWord wordPath i testPaths foundq rpath linkWord)
(setq bds (get-selection-or-unit 'glyphs))
(setq cursorWord (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
;; word for constructing possible dir
(setq wordPath (replace-regexp-in-string " " "_" (downcase cursorWord)))
;; the paths to test
(setq testPaths
(list
(concat "~/web/xahlee_org/SpecialPlaneCurves_dir/" (upcase-initials wordPath) "_dir/" wordPath ".html")
(concat "~/web/xahlee_org/surface/" wordPath "/" wordPath ".html")))
;; loop thru the paths until a file is found
(setq foundq nil)
(setq i 0)
(while (and (not foundq) (< i (length testPaths)))
(setq rpath (nth i testPaths))
(message rpath)
(setq foundq (file-exists-p rpath))
(setq i (1+ i)))
(if foundq
(progn
(setq linkWord (replace-regexp-in-string "_" " " cursorWord))
(delete-region p1 p2)
(insert (concat "" linkWord "")))
(progn (beep) (message "No file found")))))
(defun xah-all-linkify ()
"Make the text under cursor into a HTML link for xahlee.org.
text can be any of:
• relative path (file, image, or anything)
• Wikipedia link
• any URL
They will be changed into a html link in various formats, depending on the input.
If there is text selection, use it as input."
(interactive)
(let (bds p1 p2 myPath titleText resultStr )
;; (setq bds (get-selection-or-unit 'glyphs))
(setq bds (get-selection-or-unit ["^ \t\n,([{<>〔“\"" "^ \t\n,)]}<>〕\"”"]))
(setq myPath (elt bds 0) )
(setq p1 (aref bds 1) )
(setq p2 (aref bds 2) )
(cond
((and (string-match "^http://xahlee\.blogspot\.com/" myPath)) (blogger-linkify))
((and (string-match "^http://wordy-english\.blogspot\.com/" myPath)) (blogger-linkify))
((and (string-match "www\.amazon\.com/" myPath)) (amazon-linkify))
((and (string-match "www\.youtube\.com/" myPath)) (youtube-linkify))
((or (string-match "^http://xahlee.org" myPath)
(string-match "^http://www.xahlee.org" myPath)) (xah-file-linkify))
((string-match "wikipedia.org/" myPath)
(let ((case-fold-search nil))
(if (string-match "\\(\\.jpg$\\)\\|\\(\\.png$\\)" myPath)
(source-linkify)
(call-interactively 'wrap-wikipedia-url) ) ) )
((and (string-match "^https?://" myPath)) (wrap-url nil p1 p2)) ; generic URL
((or
(string-match "\.jpg$" myPath)
(string-match "\.png$" myPath)
(string-match "\.gif$" myPath)
(string-match "\.svg$" myPath)
) (image-wrap))
((or (string-match "^\.\." myPath)
(string-match "^c:" myPath)
(string-match "^/" myPath)
(string-match "file://" myPath)
) (xah-file-linkify))
(t (error "doesn't know what type of file this is."))
) ))