;; -*- coding: utf-8 -*- ;; part of Xah Lee's emacs init file. ;; 2007-06, 2011-01-24 ;; 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 browse-url-of-buffer-with-firefox () "Same as `browse-url-of-buffer' but using Firefox. You need Firefox's path in the path environment variable within emacs. e.g. (setenv \"PATH\" (concat \"C:/Program Files (x86)/Mozilla Firefox/\" \";\" (getenv \"PATH\") ) ) On Mac OS X, you don't need to. This command makes this shell call: 「open -a Firefox.app http://example.com/」" (interactive) (let () (cond ((string-equal system-type "windows-nt") ; Windows (shell-command (concat "firefox file://" buffer-file-name)) ) ((string-equal system-type "gnu/linux") (shell-command (concat "firefox file://" buffer-file-name)) ) ((string-equal system-type "darwin") ; Mac (shell-command (concat "open -a Firefox.app file://" buffer-file-name)) ) ) )) (defun browse-url-Google-Chrome (uri) "Same as `browse-url' but using Google Chrome." (interactive) (let () (shell-command (concat "chrome " uri)) )) (defun browse-url-Opera (uri) "Same as `browse-url' but using Opera browser." (interactive) (let () (shell-command (concat "opera " uri)) )) (defun browse-url-of-buffer-with-firefox-2 () "Same as `browse-url-of-buffer' but using Firefox. You need to have the firefox path in `exec-path'. e.g.: (add-to-list 'exec-path \"c:/Program Files (x86)/Mozilla Firefox/\")" (interactive) (let () (require 'browse-url) (browse-url-firefox (concat "file:///" buffer-file-name)) )) ;; § ---------------------------------------- (defun yellowMe () "temp function. change background color of current frame to light yellow.." (interactive) (set-background-color "cornsilk") ) (defun pinkMe () "temp function. change background color of current frame to light pink.." (interactive) (set-background-color "lavender blush") ) (defun list-matching-lines2 () "Show lines in the current buffer matching current word or text selection. This command is the similar to `list-matching-lines'. The differences are: • The input of this command is the current word. • If there is a text selection, that is used as input. • The input is plain text, not regex." (interactive) (let (bds p1 p2 myStr ) (setq bds (get-selection-or-unit 'glyphs)) (setq myStr (elt bds 0) ) (setq p1 (elt bds 1) ) (setq p2 (elt bds 2) ) (list-matching-lines (regexp-quote myStr)) ) ) (defun xah-new-empty-buffer () "Opens a new empty file in a particular dir. Specific to Xah Lee." (interactive) (let () (find-file "~/Documents/ciska/") (switch-to-buffer (generate-new-buffer "untitled")) (funcall (and initial-major-mode)) (setq buffer-offer-save t) ) ) (defun delete-secondlife-cache () "Delete Second Life's cache directory." (interactive) (when (string-equal system-type "windows-nt") (w32-shell-execute "explore" "c:/Users/xah/AppData/Local/SecondLife/") (w32-shell-execute "explore" "c:/Users/xah/AppData/Local/PhoenixViewer/") ;; "~/Documents/autohotkey/delete Second Life cache.ahk" ;; (shell-command "rm -R c:/Users/xah/AppData/Local/SecondLife/ &") ) ) (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 "
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. If there's no current text selection, this command uses current word as input. If there is a text selection, it uses the text selection for path. (convenient if the file path contains space) The following path all works: /Users/xah/web/xahlee_org/emacs/emacs.html c:/Users/xah/web/xahlee_org/emacs/emacs.html ~/web/xahlee_org/emacs/emacs.html file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html http://xahlee.org/emacs/emacs.html" (interactive) (let (inputStr f2 ff ) ;; (setq ff (elt (get-selection-or-unit 'glyphs) 0) ) (setq inputStr (elt (get-selection-or-unit ["^ \t\n,()[]{}<>〔〕“”\"" "^ \t\n,()[]{}<>〔〕“”\""]) 0) ) ;; (insert inputStr) (setq f2 (cond ((string-match "^xahlee\\.org" inputStr ) (concat "http://" inputStr)) ((string-match "^xahlee\\.info" inputStr ) (concat "http://" inputStr)) ((string-match "^ergoemacs\\.org" inputStr ) (concat "http://" inputStr)) ((string-match "^xahporn\\.org" inputStr ) (concat "http://" inputStr)) (t inputStr) ) ) (setq ff (cond ((string-match "^http://xah" f2 ) (xahsite-url-to-filepath f2)) ((string-match "^http://www\\.xah" f2 ) (xahsite-url-to-filepath f2)) ((string-match "^http://ergoemacs\\.org" f2 ) (xahsite-url-to-filepath f2)) ((string-match "^http://www\\.ergoemacs\\.org" f2 ) (xahsite-url-to-filepath f2)) ((string-match "^file://" f2 ) (local-url-to-file-path f2)) ((string-match "^/cygdrive/" f2 ) (replace-regexp-in-string "/cygdrive/c/Users/xah/" "~/" f2)) (t f2) ) ) ;; ; (setq ff (windows-style-path-to-unix ff)) ;; ;; make some path starting with “/” to be “~/web/xahlee_org” ;; (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/xahlee_org/" ff))) (message "%s" ff) (if (not (file-exists-p ff)) (if (file-exists-p (concat ff ".el")) (find-file-at-point (concat ff ".el")) (find-file-at-point 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 (bds p1 p2 myStr) (setq bds (get-selection-or-unit 'glyphs)) (setq myStr (elt bds 0) ) (setq p1 (elt bds 1) ) (setq p2 (elt bds 2) ) (setq myStr (replace-regexp-in-string "&" "&" myStr)) ;; on Mac, map specific links to particular browser ;; (cond ;; ((string-match "flickr.com/" myStr) (shell-command (concat "open -a safari " "\"" myStr "\""))) ;; ((string-match "blogspot.com/" myStr) (shell-command (concat "open -a safari " "\"" myStr "\""))) ;; ((string-match "livejournal.com/" myStr) (shell-command (concat "open -a safari " "\"" myStr "\""))) ;; ((string-match "yahoo.com/" myStr) (shell-command (concat "open -a safari " "\"" myStr "\""))) ;; (t (browse-url myStr))) (browse-url myStr) )) (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 URL. For example, this line /Users/xah/web/xahlee_org/emacs/emacs.html becomes 〈Xah's Emacs Tutorial〉 http://xahlee.org/emacs/emacs.html The title came from HTML file's title tag. File path must be a URL scheme, full path, or relative path. Example URL schemes among browsers and OSes: C:\\Users\\xah\\web\\xahlee_org\\emacs\\emacs.html ; IE file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; Windows: firefox, chrome, safari file://localhost/C:/Users/xah/web/xahlee_org/emacs/emacs.html ; Windows opera file:///Users/xah/web/xahlee_org/emacs/emacs.html ; osx: safari, firefox file://localhost/Users/xah/web/xahlee_org/emacs/emacs.html ; opera Example path variations: c:/Users/xah/web/xahlee_org/emacs/emacs.html /Users/xah/web/xahlee_org/emacs/emacs.html ~/web/xahlee_org/emacs/emacs.html emacs.html ; relative path This is Xah Lee's personal command assuming a particular dir structure." (interactive) (let (bds p1 p2 inputStr myFile myTitle) (setq bds (get-selection-or-unit 'glyphs)) (setq inputStr (elt bds 0) ) (setq p1 (elt bds 1) ) (setq p2 (elt bds 2) ) ;; the input is either a file path or uri ;; file path may start with “/”, “C:”, “~” ;; URL start with “http”, “file://” ;; change to file path (setq myFile (cond ((string-match "^file://" inputStr ) (local-url-to-file-path inputStr)) ((string-match "^http" inputStr ) (xahsite-url-to-filepath inputStr)) ((string-match "^C:" inputStr ) inputStr) ((string-match "^c:" inputStr ) inputStr) ((string-match "^/" inputStr ) inputStr) ((string-match "^~" inputStr ) inputStr) (t inputStr) ) ) (setq myFile (expand-file-name myFile ) ) (message "myFile: %s" myFile) (setq myTitle (if (string-match ".+html\\'" myFile) (get-html-file-title myFile) (file-name-nondirectory myFile))) (setq myTitle (replace-pairs-in-string myTitle [["&" "&"] ["<" "<"] [">" ">" ]]) ) (delete-region p1 p2) (insert "〈" myTitle "〉\n" (xahsite-filepath-to-url myFile)) )) (defun xah-copy-url-current-file () "Put the current file's URL into the kill-ring." (interactive) (let (ξurl) (setq ξurl (xahsite-filepath-to-url (buffer-file-name)) ) (kill-new ξurl) (message "URL copied %s" ξurl) ) ) ;; § ---------------------------------------- (defun xah-browse-url-of-buffer () "Similar to browse-url-of-buffer, but go to xahlee.org instead. For example, if current buffer is of this file: c:/Users/xah/web/xahlee_org/Periodic_dosage_dir/pd.html then after calling this function, default browser will be launched and opening this URL: http://xahlee.org/Periodic_dosage_dir/pd.html" (interactive) (let ( (ξurl (buffer-file-name)) ) (setq ξurl (replace-regexp-in-string "c:/Users/xah/web/xahlee_org/" "http://xahlee.org/" ξurl) ) (setq ξurl (replace-regexp-in-string "c:/Users/h3/web/xahlee_org/" "http://xahlee.org/" ξurl) ) (browse-url ξurl) ;; (shell-command (concat "C:/Program\ Files\ (x86)/Internet\ Explorer/iexplore.exe " ξurl)) ) ) ;; § ---------------------------------------- (defun x-query-find-then-bold () "personal to xahlee.org's vocabulary pages. Search forward a word enclosed by “
” and “
”, then search forward it inside the example body, only if it is not already bold. Then, ask user whether that should be bold." (interactive) (let () (goto-char (point-min)) (while (search-forward-regexp "\\([^\<]+\\)
" nil t) (search-forward-regexp (match-string 1)) (when (y-or-n-p "Do you want to bold the word?") (wrap-html-tag "span" "w") ;;(replace-match "\\1" t) )) )) (defun process-image (fileList argsString newNameSuffix newNameFileSuffix ) "Create Requires ImageMagick shell tool." (require 'dired) (mapc (lambda (ξf) (let ( newName cmdStr ) (setq newName (concat (file-name-sans-extension ξf) newNameSuffix newNameFileSuffix) ) (while (file-exists-p newName) (setq newName (concat (file-name-sans-extension newName) newNameSuffix (file-name-extension newName t))) ) ;; relative paths used to get around Windows/Cygwin path remapping problem (setq cmdStr (concat "convert " argsString " " (file-relative-name ξf) " " (file-relative-name newName)) ) (shell-command cmdStr) )) fileList )) (defun scale-image (fileList scalePercentage) "Create a scaled jpg version of images of marked files in dired. The new names have “-s” appended before the file name extension. Requires ImageMagick shell tool." (interactive (let ( (myFileList (cond ((string-equal major-mode "dired-mode") (dired-get-marked-files)) ((string-equal major-mode "image-mode") (list (buffer-file-name))) (t (list (read-from-minibuffer "file name:") )) ) ) ) (list myFileList (read-from-minibuffer "scale percentage:")) ) ) (process-image fileList (concat "-scale " scalePercentage "% -quality 85% " ) "-s" ".jpg" ) ) (defun image-autocrop (fileList) "Create a new auto-cropped jpg version of images of marked files in dired. Requires ImageMagick shell tool." (interactive (let ( (myFileList (cond ((string-equal major-mode "dired-mode") (dired-get-marked-files)) ((string-equal major-mode "image-mode") (list (buffer-file-name))) (t (list (read-from-minibuffer "file name:") )) ) ) ) (list myFileList) ) ) (process-image fileList "-trim" "-c" ".jpg" ) ) (defun 2png (fileList) "Create a png version of images of marked files in dired. Requires ImageMagick shell tool." (interactive (let ( (myFileList (cond ((string-equal major-mode "dired-mode") (dired-get-marked-files)) ((string-equal major-mode "image-mode") (list (buffer-file-name))) (t (list (read-from-minibuffer "file name:") )) ) ) ) (list myFileList) ) ) (process-image fileList "" "-2" ".png" ) ) (defun 2jpg (fileList) "Create a jpg version of images of marked files in dired. Requires ImageMagick shell tool." (interactive (let ( (myFileList (cond ((string-equal major-mode "dired-mode") (dired-get-marked-files)) ((string-equal major-mode "image-mode") (list (buffer-file-name))) (t (list (read-from-minibuffer "file name:") )) ) ) ) (list myFileList) ) ) (process-image fileList "" "-2" ".jpg" ))