;-*- coding: utf-8 -*-
; Xah Lee's personal functions for transforming cursor location's text into HTML links.
; 2007-10
; Xah Lee,
; ∑ http://xahlee.org/
;----
(defun image-linkify ()
"Replace a path to image file with a HTML img tag.
Example, if cursor is on the word “emacs_logo.png”, then it will became
“
”.
If there's a text selection, use it as file name."
(interactive)
(let
(imgPath bds imgDimen width height altText myResult)
(setq bds
(if (and transient-mark-mode mark-active)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'filename)
))
(setq imgPath
(buffer-substring-no-properties (car bds) (cdr bds))
)
(if (file-exists-p imgPath)
(progn
(setq altText (file-name-nondirectory imgPath))
(setq altText (replace-regexp-in-string "\\.[A-Za-z]\\{3,4\\}$" "" altText t t))
(setq altText (replace-regexp-in-string "_" " " altText t t))
(setq imgDimen (get-image-dimensions imgPath))
(setq width (number-to-string (car imgDimen)))
(setq height (number-to-string (car (last imgDimen))))
(setq myResult (concat ""))
(delete-region (car bds) (cdr bds))
(insert myResult)
)
(error "File does not exist")
)
))
(defun full-size-img-linkify ()
"Make img file path at cursor point into a img link.
Example:
i/goddesses.jpg
becomes
❐
If region is active, use region as file name.
Note: in old version, it changes to:
\(Large Size: asian_goddesses.jpg (622x800))
"
(interactive)
(let
(imgPath imgfName linkText bds imgDimen width height myResult)
(setq bds
(if (and transient-mark-mode mark-active)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'filename)
))
(setq imgPath
(buffer-substring-no-properties (car bds) (cdr bds))
)
(setq imgfName (file-name-nondirectory imgPath))
;; (setq linkText
;; (if (< (length imgfName) 20)
;; imgfName
;; (concat (substring imgfName 0 5) "..." (substring imgfName -6) ) ))
(setq imgDimen (get-image-dimensions imgPath))
(setq width (number-to-string (car imgDimen)))
(setq height (number-to-string (car (last imgDimen))))
(setq myResult (concat "❐"))
(delete-region (car bds) (cdr bds))
(insert myResult)
))
(defun get-image-dimensions (img-file-relative-path)
"Returns a image file's width and height as a list."
(let (tmp dimen)
(clear-image-cache)
(setq tmp
(create-image (concat default-directory img-file-relative-path)))
(setq dimen (image-size tmp t))
(list (car dimen) (cdr dimen))
)
)
(defun get-image-dimensions2 (img-file-path)
"Returns a image file's width and height as a list.
This function requires ImageMagick's “identity” shell command."
(let (cmd-name sh-output width height)
(setq cmd-name "identify")
(setq sh-output (shell-command-to-string (concat cmd-name " " img-file-path)))
; sample output from “identify”:
; xyz.png PNG 520x429+0+0 DirectClass 8-bit 9.1k 0.0u 0:01
(string-match "^[^ ]+ [^ ]+ \\([0-9]+\\)x\\([0-9]+\\)" sh-output)
(setq width (match-string 1 sh-output))
(setq height (match-string 2 sh-output))
(list (string-to-number width) (string-to-number height))))
(defun wrap-url-string (url &optional raw)
"Returns url as a HTML link.
Example:
http://en.wikipedia.org/wiki/Emacs
becomes
Emacs↗.
If the url is a Wikipedia link, then the link text
is shortened to its title.
If the optional argument raw is non-nil, no shortening will happen."
(require 'gnus-util)
(let (linktext url2)
(setq url2 (replace-regexp-in-string "&" "&" url))
(setq linktext (gnus-url-unhex-string url2 nil))
(if (and (not raw) (string-match "wikipedia.org" url2))
(progn
(setq linktext (concat (car (last (split-string linktext "/"))) "↗") )
(setq linktext (replace-regexp-in-string "_" " " linktext))
)
)
(concat "" linktext "" )
)
)
(defun wrap-url ()
"Make the url at cursor point into a html link.
If there is a region, use the region as url instead.
This function is interface wrapper for `wrap-url-string'.
See that function for detail."
(interactive)
(let (bds p1 p2 url)
(if (and transient-mark-mode mark-active)
(progn
(setq p1 (region-beginning))
(setq p2 (region-end))
(setq url (buffer-substring-no-properties p1 p2))
)
(progn
;; (thing-at-point 'url) gets confused if the url contains a
;; parenthesis such as in
;; http://en.wikipedia.org/wiki/Oz_(programming_language)
;; (setq bds (bounds-of-thing-at-point 'url))
;; (setq p1 (car bds))
;; (setq p2 (cdr bds))
;; (setq url (buffer-substring-no-properties p1 p2))
(re-search-backward "[\n\t <>「」,]" nil t)
(looking-at "[\n\t <>「」,]?\\([^\n\t <>「」,]+\\)")
(setq p1 (match-beginning 1))
(setq p2 (match-end 1))
(setq url (match-string 1))
)
)
(delete-region p1 p2)
(goto-char p1)
(insert (wrap-url-string url))
)
)
; decode-coding-string str 'utf-8'
; http://wikipedia.org/wiki/emacs_%28computing%29
(defun source-linkify ()
"Make url at cursor point into a html link.
Example: http://example.com/xyz.htm
becomes
Source
"
(interactive)
(let (url str1 bds p1 p2)
(setq url
(if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'url)))
(setq bds (bounds-of-thing-at-point 'url))
(setq p1 (car bds))
(setq p2 (cdr bds))
(setq url (replace-regexp-in-string "&" "&" url))
(setq str1
(concat "Source"
))
(if (and transient-mark-mode mark-active)
(progn (delete-region (region-beginning) (region-end)))
(progn (delete-region p1 p2))
)
(insert str1)
))
(defun defunct ()
"Make the html link under cursor to a defunct form.
Example:
If cursor is on this line:
Source↗
It becomes:
Source
"
(interactive)
(let (p1 p2 linkText url)
(save-excursion
(search-backward "<" )
(setq p1 (point) )
(search-forward "\">Source")
(setq p2 (point) )
(setq datestring (format-time-string "%Y-%m-%d"))
(setq linkText (buffer-substring-no-properties p1 p2))
; (setq url (match-string 1))
;; (setq linkText (replace-regexp-in-string "\">Source"
;; (concat "; defunct:" (format-time-string "%Y-%m-%d") "; " url "\">Source")
;; linkText t t))
(with-temp-buffer
(setq linkText (replace-regexp-in-string "class=\"sorc\"" "class=\"sorcdd\"" linkText t t))
;; (setq linkText (replace-regexp-in-string "href=\"\\([^\"]+\\)\" +title=\"accessed:\\([^;]+\\)\""
;; (concat "href=\"#\" title=\"accessed:\1; defunct:" (format-time-string "%Y-%m-%d") ";\2\"" )
;; linkText t t))
(insert linkText)
(goto-char 0)
(search-forward-regexp "href=\"\\([^\"]+\\)\"")
(setq url (match-string 1))
(replace-match "href=\"#\"")
(search-forward "\"")
(search-forward "\"")
(backward-char 1)
(insert "; defunct:"
(format-time-string "%Y-%m-%d")
"; " url
)
(setq linkText (buffer-string))
)
)
(delete-region p1 p2)
(insert linkText)
))
(defun wikipedia-linkify ()
"Make the current word or text selection into a Wikipedia link.
For Example, if you cursor is on the word “Emacs”, then
it becomes
“Emacs↗”"
(interactive)
(let (word1 word2 word1Bounds str1)
(setq word1
(if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word)
))
(setq word2 (replace-regexp-in-string " " "_" word1) )
(setq str1
(concat "" word1 "↗"))
(if (and transient-mark-mode mark-active)
(progn (delete-region (region-beginning) (region-end))
(insert str1))
(progn
(setq word1Bounds (bounds-of-thing-at-point 'word))
(delete-region (car word1Bounds) (cdr word1Bounds))
(insert str1))
)
))
(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 (swd url)
(setq swd (thing-at-point 'line))
(setq swd (substring swd 0 -1))
(setq url
(concat "http://www.youporn.com/search?query=" swd) )
(setq url (replace-regexp-in-string " " "+" url ) )
(move-beginning-of-line 1)
(kill-line 1)
(insert "(YouPorn video: " swd "↗)\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 (bdr p1 p2 swd url)
(setq bdr
(if (and transient-mark-mode mark-active)
(cons (region-beginning) (1+ (region-end)))
(bounds-of-thing-at-point 'line)))
(setq p1 (car bdr))
(setq p2 (1- (cdr bdr)))
(setq swd (buffer-substring-no-properties p1 p2))
(setq url
(concat "http://youtube.com/results?search_query=" swd "&search=Search") )
(setq url (replace-regexp-in-string " " "+" url ) )
(setq url (replace-regexp-in-string "," "%2C" url ) )
(delete-region p1 p2)
(insert "" swd "↗")
))
(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
Google search: emacs lisp↗.
Warning: the line must end in a line return char else the result is wrong." (interactive) (let (swd url) (setq swd (thing-at-point 'line)) (setq swd (substring swd 0 -1)) (setq url (concat "http://www.google.com/search?q=" swd)) (setq url (replace-regexp-in-string " " "+" url ) ) (move-beginning-of-line 1) (kill-line 1) (insert "Google search: " swd "↗.
\n") )) (defun emacs-ref-linkify () "Make the current line into a elisp info node link. If there's a text selection, use that. For example, if the cursor is on the line: \(emacs)Dired Then it'll become: (info \"(emacs)Dired\") Warning: the line must end in a line return char else the result is wrong." (interactive) (let (bdr p1 p2 swd ) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (delete-region p1 p2) (insert "(info \"" swd "\")") )) (defun elisp-ref-linkify () "Make the current line into a elisp reference link. If there's a text selection, use that. For example, if the cursor is any one of the line: \(elisp)The Mark file:///Users/xah/web/elisp/The-Mark.html#The-Mark file:///Users/xah/web/elisp/The-Mark.html file:///Users/xah/web/elisp/The-Mark.html ~/xah/web/elisp/The-Mark.html Then it'll become: (info \"(elisp)The Mark\")" (interactive) (let (bdr p1 p2 swd linkText url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (region-end)) ;; return the boundary of line without including the eol (let ((bdr2 (bounds-of-thing-at-point 'line))) (if (string-match "\n" (buffer-substring-no-properties (car bdr2) (cdr bdr2))) (cons (car bdr2) (1- (cdr bdr2))) (cons (car bdr2) (cdr bdr2)) ) ) )) (setq p1 (car bdr)) (setq p2 (cdr bdr)) (setq swd (buffer-substring-no-properties p1 p2)) ;; convert to info's node syntax first (when (string-match "^file" swd) (setq swd (replace-regexp-in-string "^file:///Users/xah/web/elisp/" "" swd)) (setq swd (replace-regexp-in-string "\\..+" "" swd)) (setq swd (replace-regexp-in-string "-" " " swd)) (setq swd (replace-regexp-in-string "_002d" "-" swd)) (setq swd (concat "(elisp)" swd)) ) ;; convert info node string to relative file path (setq linkText (concat "(info \"" swd "\")")) (setq swd (replace-regexp-in-string "(elisp)" "" swd)) (setq swd (replace-regexp-in-string "-" "_002d" swd)) (setq swd (replace-regexp-in-string " " "-" swd)) (setq url (concat "../elisp/" swd ".html") ) (if (file-exists-p url) (progn (delete-region p1 p2) (insert "" linkText "") ) (error "Generated local url “%s” does not point to a file" url) ) )) (defun elisp-ref-linkify-old () "Make the current line into a elisp reference link. If there's a text selection, use that. For example, if the cursor is on the line: Control-Structures Then it'll become: Control-Structures" (interactive) (let (bdr p1 p2 swd url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (setq url (concat "../elisp/" swd ".html") ) (delete-region p1 p2) (insert "" swd "") )) (defun php-ref-linkify () "Make the current line into a PHP reference link. If there's a text selection, use that. For example, if the cursor is on the line: echo Then it'll become echo↗" (interactive) (let (bdr p1 p2 swd url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (setq url (concat "http://us.php.net/" swd) ) (delete-region p1 p2) (insert "" swd "↗") )) (defun povray-ref-linkify () "Make the current line into a POV-Ray reference link. If there's a text selection, use that. For example, if the cursor is on the line: http://www.povray.org/documentation/view/3.6.1/331/ Then it'll become: ↗ The cursor is moved to before “↗”." (interactive) (let (bdr p1 p2 url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (region-end)) (bounds-of-thing-at-point 'url))) (setq p1 (car bdr)) (setq p2 (cdr bdr)) (setq url (buffer-substring-no-properties p1 p2)) (delete-region p1 p2) (insert "↗") (search-backward "↗") )) (defun java-ref-linkify () "Make the current line into a Java reference link. If there's a text selection, use that. For example, if the cursor is on the line: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html Then it'll become Java Doc: String↗ The input string can be the url to the official Java API, Java Language Spec, the Java Tutorial." (interactive) (let (bdr p1 p2 url kword) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (region-end)) (bounds-of-thing-at-point 'url))) (setq p1 (car bdr)) (setq p2 (cdr bdr)) (setq url (buffer-substring-no-properties p1 p2)) (setq kword (file-name-sans-extension (file-name-nondirectory url))) (delete-region p1 p2) (cond ((string-match "j2se/1.5" url) (insert "Java Doc: " kword "↗")) ((string-match "tutorial" url) (insert "Java Tutorial: " kword "↗")) ((string-match "jls" url) (insert "Java Lang Spec: " kword "↗")) ) )) (defun perldoc-ref-linkify () "Make the current line into a link to Perl's doc site. For example, if the cursor is on the line: perlop Then it'll become perldoc perlop" (interactive) (let (bdr p1 p2 swd url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (setq url (concat "http://perldoc.perl.org/" swd ".html") ) (setq url (replace-regexp-in-string "::" "/" url )) (setq url (replace-regexp-in-string "-f " "functions/" url )) (delete-region p1 p2) (insert "" "perldoc " swd "↗") )) (defun python-ref-linkify () "Make the current line into a link to Python's doc site. For example, if the cursor is on the line: http://docs.python.org/lib/typesseq.html Then it'll become Python Doc↗. " (interactive) (let (bdr p1 p2 swd url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (setq url swd) (delete-region p1 p2) (insert "" "Python Doc↗") )) (defun mathematica-ref-linkify () "Make the current line into a link to Mathematica ref site. If there's a text selection, use that. For example, if the cursor is on the line: Table Then it'll become: Mathematica Ref: Table↗" (interactive) (let (bdr p1 p2 swd url) (setq bdr (if (and transient-mark-mode mark-active) (cons (region-beginning) (1+ (region-end))) (bounds-of-thing-at-point 'line))) (setq p1 (car bdr)) (setq p2 (1- (cdr bdr))) (setq swd (buffer-substring-no-properties p1 p2)) (setq url (concat "http://reference.wolfram.com/mathematica/ref/" swd ".html") ) (delete-region p1 p2) (insert "" "Mathematica: " swd "↗") )) ;---- ; 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 (word1 pagenum str1) (setq word1 (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'word) )) (setq pagenum (substring word1 1) ) (setq str1 (concat "p" pagenum "↗")) (if (and transient-mark-mode mark-active) (progn (delete-region (region-beginning) (region-end)) (insert str1)) (progn (backward-word) (kill-word 1) (insert str1)) ) )) (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:For comments, please go to http://xah-lee.blogspot.com/2007/12/graphics-programing-pains.html.
" (interactive) (let (url) (setq url (thing-at-point 'url)) (move-beginning-of-line 1) (kill-line 1) (insert "For comments, please go to: " url ".
\n") )) (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 cursorWord wordPath i testPaths foundq rpath linkWord) ;; grab the string of text selection or current word (setq bds (if (and transient-mark-mode mark-active) (cons (region-beginning) (region-end)) (bounds-of-thing-at-point 'symbol) )) (setq cursorWord (buffer-substring-no-properties (car bds) (cdr bds)) ) ;; word for constructing possible dir (setq wordPath (replace-regexp-in-string " " "_" (downcase cursorWord))) ;; the paths to test (setq testPaths (list (concat "/Users/xah/web/SpecialPlaneCurves_dir/" (upcase-initials wordPath) "_dir/" wordPath ".html") (concat "/Users/xah/web/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 (car bds) (cdr bds)) (insert (concat "" linkWord ""))) (progn (beep) (message "No file found")) )))