;; 2010-06-07
;; ∑ http://xahlee.org/
(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 (region-active-p)
(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/xahlee_org/elisp/The-Mark.html#The-Mark
file:///Users/xah/web/xahlee_org/elisp/The-Mark.html
~/web/elisp/The-Mark.html
Then it'll become:
(info \"(elisp) The Mark\")"
(interactive)
(let (bdr p1 p2 meatstr linkText url)
(setq bdr
(if (region-active-p)
(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 meatstr (buffer-substring-no-properties p1 p2))
;; convert to info's node syntax first
(when (string-match "^file" meatstr)
(setq meatstr (local-url-to-file-path meatstr)) ;convert url style path to os's file path
(setq meatstr (file-name-sans-extension (file-name-nondirectory meatstr)))
(setq meatstr (replace-regexp-in-string "-" " " meatstr))
(setq meatstr (replace-regexp-in-string "_002d" "-" meatstr))
(setq meatstr (concat "(elisp) " meatstr))
)
(when (not (string-match "(elisp)" meatstr))
(setq meatstr )
)
;; convert info node string to relative file path
(setq linkText (concat "(info \"" meatstr "\")"))
(setq meatstr (replace-regexp-in-string "(elisp) " "" meatstr))
(setq meatstr (replace-regexp-in-string "-" "_002d" meatstr))
(setq meatstr (replace-regexp-in-string " " "-" meatstr))
(setq url (concat "../elisp/" meatstr ".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 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 (region-active-p)
(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 (region-active-p)
(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 "")
(backward-char 11)))
(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 (region-active-p)
(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 (region-active-p)
(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 (region-active-p)
(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 (region-active-p)
(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 "")))