;-*- coding: utf-8 -*- ; part of Xah Lee's emacs init file. ; 2007-06 ; Xah Lee, ; ∑ http://xahlee.org/ ;---- ; some functions personal to working on XahLee.org's website ; many of these opens a particular file and insert a string (defun insert-atom-entry () "Insert a blank Atom RSS entry template." (interactive) (insert (concat " ttt " (format-time-string "tag:xahlee.org,%Y-%m-%d:%H%M%S" (current-time) 1) " " (concat (format-time-string "%Y-%m-%dT%T") ((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5))) (format-time-string "%z")) ) " ttt
\n\n" )) ) (defun update-pdxml-date () "Update the Atom RSS updated tag in pd.xml.\n That is, the first occurance of: 2006-10-10T22:58:42-07:00" (interactive) (find-file "~/web/Periodic_dosage_dir/pd.xml") (goto-char (point-min)) (let (x1) (setq x1 (re-search-forward "" nil t)) (delete-region x1 (+ x1 25)) (insert-date-time))) (defun make-pdxml-entry (begin end) "Insert current region to as a Atom RSS entry in pd.xml.\n Detail: create a new Atom entry in pd.xml, with the current region as its content, update the Atom file's update date." (interactive "r") (let ((meat (buffer-substring-no-properties begin end))) (find-file "~/web/Periodic_dosage_dir/pd.xml") (goto-char (point-min)) (re-search-forward "\n" nil t) (insert-atom-entry) (re-search-backward "
" nil t) (re-search-forward "
" nil t) (insert meat) (update-pdxml-date) (find-file "~/web/Periodic_dosage_dir/pd.xml") (goto-char (point-min)) (re-search-forward ">ttt" nil t))) (defun make-word-entry () "Insert a blank a-word-a-day html template in a paritcular file." (interactive) (find-file "~/web/PageTwo_dir/Vocabulary_dir/new.html") (goto-char (point-min)) (re-search-forward "\n" nil t) (insert-word-entry) (re-search-backward "\"ex\">" nil t) (re-search-forward ">ttt" nil t) ) (defun insert-word-entry () "Insert a blank a-word-a-day html template." (interactive) (insert (concat "
" (format-time-string "%Y-%m-%d") "

ttt

ttt
ttt
\n
\n\n" ) ) (re-search-backward "\"ex\">" nil t) (re-search-forward ">ttt" nil t) ) (defun insert-definition-entry () "Insert a word definition entry template. Using current word or current text selection." (interactive) (require 'dictionary) (let (meat) (setq meat (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (let ((bdr (get-english-word-boundary) )) (buffer-substring-no-properties (car bdr) (cdr bdr)) ) )) (search-forward "\n\n" nil t) (search-backward "
") (insert "
\n") (search-backward "
") (insert meat) (insert " = ") (lookup-word-def meat) ;; (dictionary-new-search (cons meat dictionary-default-dictionary)) ) ) (defun xah-search-next-unbold () "search the next word block that isn't bolded. Used for the files in ~/web/PageTwo_dir/Vocabulary_dir/ ." (interactive) (let ( wd egText p1 p2 p3 p4 notBolded-p) ;; grab the word (search-forward "

") (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 "
") (setq p3 (point)) (search-forward "
") (backward-char 6) (setq p4 (point)) (setq egText (buffer-substring-no-properties p3 p4)) ;; check if word is bolded in example text (setq notBolded-p (not (string-match (concat ">" wd) egText)) ) ;; (setq notBolded-p (string-match ">harrowing" egText) ) (if notBolded-p (progn (goto-char p3) (search-forward wd p4) ) (xah-search-next-unbold) ;; (when (y-or-n-p "Do you want to bold it?") ;; (goto-char p3) ;; (search-forward wd p4) ;; (search-backward wd p3) ;; (insert "") ;; (search-forward wd p4) ;; (insert "") ;; ) ) ) ) (defun make-lojban-entry () "Insert a blank a-lojban-a-day html template in a paritcular file." (interactive) (find-file "~/web/lojban/valsi_dikni/valsi_dikni.html") (goto-char (point-min)) (re-search-forward "\n" nil t) (insert (concat "
" (format-time-string "%Y-%m-%d") "

renro = throw = 丢 diu1

renro:=x1 throws/launches/casts/hurls x2 to/at/in direction x3 (propulsion derives internally to x1)
mi renro (le bolci ku) do = i throw ball to you = 我 丢 球qiu2 给gei3 你

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: “

2008-12
” at cursor position. This command requires the GetFileInfo command line util in OS X. " (interactive) (let (cmdStr resultStr mydate) (setq cmdStr (concat "GetFileInfo -d " (buffer-file-name))) (setq resultStr (shell-command-to-string cmdStr)) (setq mydate (replace-regexp-in-string "\\([0-9][0-9]\\)/\\([0-9][0-9]\\)/\\([0-9][0-9][0-9][0-9]\\) .+\n" "\\3-\\1" resultStr)) (insert "
" mydate "
") ) ) (defun xah-put-dstp () "Insert a file creation date like “
2008-12.
” in the appropriate footer location of the current XahLee.org html file. This command requires the GetFileInfo command line util in OS X. " (interactive) (let (cmdStr resultStr mydate) (setq cmdStr (concat "GetFileInfo -d " (buffer-file-name))) (setq resultStr (shell-command-to-string cmdStr)) (setq mydate (replace-regexp-in-string "\\([0-9][0-9]\\)/\\([0-9][0-9]\\)/\\([0-9][0-9][0-9][0-9]\\) .+\n" "\\3-\\1" resultStr)) (goto-char (point-max)) (search-backward "
") (insert "
" mydate "
\n") ) ) (defun xah-fix-dstp (fpath) "Insert a file creation date like “
2008-12.
” to file at FPATH. " (let (mybuffer) (setq mybuffer (find-file fpath)) (xah-put-dstp) ;; (goto-char (point-max)) ;; (search-backward "
") ;; (insert "
1997
\n") ;; (save-buffer) ;; (kill-buffer mybuffer) ) )