ttt
") (setq p1 (point)) (search-forward "
") (backward-char 4) (setq p2 (point)) (setq wd (buffer-substring-no-properties p1 p2)) ;; grab the example text (search-forward "renro = throw = 丢 diu1
bolci = ball = 球. 给 = give.
• http://en.wiktionary.org/wiki/丢 • http://en.wiktionary.org/wiki/给" ) ) (re-search-backward "
" nil t) (re-search-forward "
" nil t)
)
(defun make-wiki-entry ()
"Open pd.html, and at the right place, paste (a Wikipedia link), and save."
(interactive)
(find-file "~/web/Periodic_dosage_dir/pd.html")
(goto-char (point-min))
(re-search-forward "wikime\n" nil t)
(yank)
(insert "\n")
(save-buffer)
)
;----
(defun xah-find-file-at-point ()
"Open the file path under cursor.
This is similar to `find-file-at-point' but
customized for Xah Lee."
(interactive)
(let (ff)
(setq ff (thing-at-point 'filename))
;; make some path starting with “/” to be “~/web”
(when (and
(string-equal "/" (substring ff 0 1))
(not (string-equal "/Deskto" (substring ff 0 7)))
(not (string-equal "/Developer" (substring ff 0 10)))
(not (string-equal "/Library" (substring ff 0 8)))
(not (string-equal "/Network" (substring ff 0 8)))
(not (string-equal "/System" (substring ff 0 7)))
(not (string-equal "/TheVol" (substring ff 0 7)))
(not (string-equal "/Volumes" (substring ff 0 8)))
(not (string-equal "/bin" (substring ff 0 4)))
(not (string-equal "/cores" (substring ff 0 6)))
(not (string-equal "/dev" (substring ff 0 4)))
(not (string-equal "/opt" (substring ff 0 4)))
(not (string-equal "/private" (substring ff 0 8)))
(not (string-equal "/sbin" (substring ff 0 5)))
(not (string-equal "/sw" (substring ff 0 3)))
(not (string-equal "/usr" (substring ff 0 4)))
(not (string-equal "/var" (substring ff 0 4)))
(not (string-equal "/Users/" (substring ff 0 7)))
)
(setq ff (concat "~/web" ff)))
;; if the file path is a url, change it
;; else find-file-at-point launch into browser
(setq ff (replace-regexp-in-string "^file://localhost" "" ff))
(setq ff (replace-regexp-in-string "^file://" "" ff))
(setq ff (replace-regexp-in-string "^http://xahlee.org/" "~/web/" ff))
(find-file-at-point ff)
)
)
(defun xah-browse-url-at-point ()
"Switch to web browser and load the URL at point.
This code is designed to work on Mac OS X only.
If the cursor is on a url, visit it
http://mathforum.org/library/topics/conic_g/
for certain domain, use particular browser.
If the cursor is on like one of the following
/somedir/somefile.html or
~/web/somedir/somefile.html
use FireFox to visit it as local file (construct the proper url)."
(interactive)
(let (posOrig pos1 pos2 myurl)
(setq myurl
(if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning) (region-end))
(progn ;; not using thing-at-point because the thing i need to grab may be symbol, url, file. The thing-at-point will actually transform the thing grabbed. e.g. if the thing is url, it'll add http prefix or such.
(setq posOrig (point))
(skip-chars-backward "^\n\t\" ")
(setq pos1 (point))
(skip-chars-forward "^\n\t\" ")
(setq pos2 (point))
(goto-char posOrig)
(buffer-substring-no-properties pos1 pos2)
)))
(setq myurl (replace-regexp-in-string "&" "&" myurl))
(cond
((string-match "stumbleupon.com/" myurl) (shell-command (concat "open -a firefox " "\"" myurl "\"")))
((string-match "delicious.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "youtube.com/" myurl)
(setq myurl (replace-regexp-in-string "youtube.com/v/\\(.+\\)" "youtube.com/watch?v=\\1" myurl))
(shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "flickr.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "youporn.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "blogspot.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "livejournal.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "yahoo.com/" myurl) (shell-command (concat "open -a safari " "\"" myurl "\"")))
((string-match "~/web/" myurl) (shell-command (concat "open -a firefox " "\"" myurl "\"")))
((string-match "^/" myurl) (shell-command (concat "open -a firefox " "file:///Users/xah/web" "\"" myurl "\"")))
((and t) (browse-url myurl))
)))
(defun xah-find-word-usage (myword)
"Grep a dir for a word's usage."
(interactive "sWord to search: ")
(require 'grep)
(grep-compute-defaults)
(rgrep myword "*html" "~/web/p")
;; ~/web/p
;; ~/web/flatland/
;; ~/web/Periodic_dosage_dir/_p2/russell-lecture.html
;; ~/web/Periodic_dosage_dir/_p2/why_not_christian.html
)
(defun xah-cite ()
"Change the file path under cursor into title and path.
For example:
file:///Users/xah/web/emacs/apple_pc_kb_diff.html
becomes
• Difference Between Apple and PC keyboards
http://xahlee.org/emacs/apple_pc_kb_diff.html
The title came from HTML file's title tag.
File path must be a url scheme, full path, or relative path.
This is Xah Lee's personal command assuming a particular dir structure."
(interactive)
(let (bds ff title)
(setq bds (bounds-of-thing-at-point 'filename))
(setq ff (buffer-substring-no-properties (car bds) (cdr bds)))
;; change file path to full path
(setq ff (replace-regexp-in-string "^file://localhost/" "" ff))
(setq ff (replace-regexp-in-string "^file://" "" ff))
(setq ff (replace-regexp-in-string "^http://xahlee\\.org/" "/Users/xah/web/" ff))
(setq ff (replace-regexp-in-string "^~/web/" "/Users/xah/web/" ff))
(setq ff (expand-file-name ff)) ; if its relative path
(setq title (get-html-file-title ff))
(delete-region (car bds) (cdr bds))
(insert "• " title "\n")
(insert (replace-regexp-in-string "/Users/xah/web" " http://xahlee.org" ff))
))
(defun new-temp-file ()
"Open a new temp file in “~/Document/temp/”"
(interactive)
(random t)
(find-file
(concat "~/Documents/temp/" "tmp_"
(number-to-string (random 999)))))
(defun copy-to-register-1 (p1 p2)
"Copy text selection to register named “1”."
(interactive "r")
(copy-to-register ?1 p1 p2 )
)
(defun copy-to-register-2 (p1 p2)
"Copy text selection to register named “2”."
(interactive "r")
(copy-to-register ?2 p1 p2 )
)
(defun insert-register-content-1 ()
"Insert register named “1”'s content."
(interactive)
(insert-register ?1)
)
(defun insert-register-content-2 ()
"Insert register named “1”'s content."
(interactive)
(insert-register ?2)
)
(defun xah-insert-dstp ()
"Insert the file creation date in this format:
“