;-*- coding: utf-8 -*- ; A collection of elisp functions ; 2008-11-20 ; Xah Lee ; ∑ http://xahlee.org/ ;; § ---------------------------------------- (require 'xfrp_find_replace_pairs "../../../ErgoEmacs_Source/ergoemacs/packages/xfrp_find_replace_pairs.elc") (require 'xeu_elisp_util "../../../ErgoEmacs_Source/ergoemacs/packages/xeu_elisp_util.elc") (defun local-url-to-file-path (localFileURL) "Turn a localhost file URL LOCALFILEURL into a file full path. For example, the following string shown in browser URL field: ; On Windows Vista 2009-06 〔C:\\Users\\xah\\web\\emacs\\emacs.html〕 IE 〔file:///C:/Users/xah/web/emacs/emacs.html〕 Firefox, Google Chrome, Safari 〔file://localhost/C:/Users/xah/web/emacs/emacs.html〕 Opera becomes 〔C:/Users/xah/web/emacs/emacs.html〕 On Mac 2009-06 〔file:///Users/xah/web/emacs/emacs.html〕 Safari, Firefox 〔file://localhost/Users/xah/web/emacs/emacs.html〕 Opera becomes 〔/Users/xah/web/emacs/emacs.html〕 On Ubuntu Linux, 2011-05 〔file:///media/HP/Users/xah/web/xahlee_org/index.html〕 firefox becomes 〔/media/HP/Users/xah/web/xahlee_org/index.html〕" (replace-regexp-pairs-in-string localFileURL [ ["^file://localhost" ""] ["^file://" ""] ["^/\\([A-Za-z]\\):" "\\1:"] ; Windows C:\\ ["\\\\" "/"] ; Windows \ → / ] ) ) (defun windows-style-path-to-unix (fpath) "Turn a MS Windows style full path FPATH to unix style. Note: This drops the drive letter. For example: C:\\Users\\xah\\web\\emacs\\emacs.html becomes /Users/xah/web/emacs/emacs.html TODO: The drive letter is removed. Not sure whether that should be part of this function. But emacs 23.2's `file-relative-name' does not work when there's a drive letter." (replace-regexp-in-string "^[A-Za-z]:" "" (replace-regexp-in-string "\\\\" "/" fpath t t)) ) (defun replace-html-named-entities (ξstring &optional ξfrom ξto) "Replace HTML entities to Unicode character. For example, “©” becomes “©”. When called interactively, work on current text block or text selection. (a “text block” is text between empty lines) When called in lisp code, if ξstring is non-nil, returns a changed string. If ξstring nil, change the text in the region between positions ξfrom ξto. The following HTML Entities are not replaced: & & < < > >" (interactive (if (region-active-p) (list nil (region-beginning) (region-end)) (let ((bds (get-selection-or-unit 'block)) ) (list nil (elt bds 1) (elt bds 2))) ) ) (let (workOnStringP inputStr outputStr) (setq workOnStringP (if ξstring t nil)) (setq inputStr (if workOnStringP ξstring (buffer-substring-no-properties ξfrom ξto))) (setq outputStr (let ((case-fold-search nil)) (replace-pairs-in-string inputStr [ [" " " "] [" " " "] [" " " "] [" " " "] ["‏" "‏"] ["‎" "‎"] ["‍" "‍"] ["‌" "‌"] ["¡" "¡"] ["¢" "¢"] ["£" "£"] ["¤" "¤"] ["¥" "¥"] ["¦" "¦"] ["§" "§"] ["¨" "¨"] ["©" "©"] ["ª" "ª"] ["«" "«"] ["¬" "¬"] ["­" "­"] ["®" "®"] ["¯" "¯"] ["°" "°"] ["±" "±"] ["²" "²"] ["³" "³"] ["´" "´"] ["µ" "µ"] ["¶" "¶"] ["·" "·"] ["¸" "¸"] ["¹" "¹"] ["º" "º"] ["»" "»"] ["¼" "¼"] ["½" "½"] ["¾" "¾"] ["¿" "¿"] ["À" "À"] ["Á" "Á"] ["Â" "Â"] ["Ã" "Ã"] ["Ä" "Ä"] ["Å" "Å"] ["Æ" "Æ"] ["Ç" "Ç"] ["È" "È"] ["É" "É"] ["Ê" "Ê"] ["Ë" "Ë"] ["Ì" "Ì"] ["Í" "Í"] ["Î" "Î"] ["Ï" "Ï"] ["Ð" "Ð"] ["Ñ" "Ñ"] ["Ò" "Ò"] ["Ó" "Ó"] ["Ô" "Ô"] ["Õ" "Õ"] ["Ö" "Ö"] ["×" "×"] ["Ø" "Ø"] ["Ù" "Ù"] ["Ú" "Ú"] ["Û" "Û"] ["Ü" "Ü"] ["Ý" "Ý"] ["Þ" "Þ"] ["ß" "ß"] ["à" "à"] ["á" "á"] ["â" "â"] ["ã" "ã"] ["ä" "ä"] ["å" "å"] ["æ" "æ"] ["ç" "ç"] ["è" "è"] ["é" "é"] ["ê" "ê"] ["ë" "ë"] ["ì" "ì"] ["í" "í"] ["î" "î"] ["ï" "ï"] ["ð" "ð"] ["ñ" "ñ"] ["ò" "ò"] ["ó" "ó"] ["ô" "ô"] ["õ" "õ"] ["ö" "ö"] ["÷" "÷"] ["ø" "ø"] ["ù" "ù"] ["ú" "ú"] ["û" "û"] ["ü" "ü"] ["ý" "ý"] ["þ" "þ"] ["ÿ" "ÿ"] ["ƒ" "ƒ"] ["Α" "Α"] ["Β" "Β"] ["Γ" "Γ"] ["Δ" "Δ"] ["Ε" "Ε"] ["Ζ" "Ζ"] ["Η" "Η"] ["Θ" "Θ"] ["Ι" "Ι"] ["Κ" "Κ"] ["Λ" "Λ"] ["Μ" "Μ"] ["Ν" "Ν"] ["Ξ" "Ξ"] ["Ο" "Ο"] ["Π" "Π"] ["Ρ" "Ρ"] ["Σ" "Σ"] ["Τ" "Τ"] ["Υ" "Υ"] ["Φ" "Φ"] ["Χ" "Χ"] ["Ψ" "Ψ"] ["Ω" "Ω"] ["α" "α"] ["β" "β"] ["γ" "γ"] ["δ" "δ"] ["ε" "ε"] ["ζ" "ζ"] ["η" "η"] ["θ" "θ"] ["ι" "ι"] ["κ" "κ"] ["λ" "λ"] ["μ" "μ"] ["ν" "ν"] ["ξ" "ξ"] ["ο" "ο"] ["π" "π"] ["ρ" "ρ"] ["ς" "ς"] ["σ" "σ"] ["τ" "τ"] ["υ" "υ"] ["φ" "φ"] ["χ" "χ"] ["ψ" "ψ"] ["ω" "ω"] ["ϑ" "ϑ"] ["ϒ" "ϒ"] ["ϖ" "ϖ"] ["•" "•"] ["…" "…"] ["′" "′"] ["″" "″"] ["‾" "‾"] ["⁄" "⁄"] ["℘" "℘"] ["ℑ" "ℑ"] ["ℜ" "ℜ"] ["™" "™"] ["ℵ" "ℵ"] ["←" "←"] ["↑" "↑"] ["→" "→"] ["↓" "↓"] ["↔" "↔"] ["↵" "↵"] ["⇐" "⇐"] ["⇑" "⇑"] ["⇒" "⇒"] ["⇓" "⇓"] ["⇔" "⇔"] ["∀" "∀"] ["∂" "∂"] ["∃" "∃"] ["∅" "∅"] ["∇" "∇"] ["∈" "∈"] ["∉" "∉"] ["∋" "∋"] ["∏" "∏"] ["∑" "∑"] ["−" "−"] ["∗" "∗"] ["√" "√"] ["∝" "∝"] ["∞" "∞"] ["∠" "∠"] ["∧" "∧"] ["∨" "∨"] ["∩" "∩"] ["∪" "∪"] ["∫" "∫"] ["∴" "∴"] ["∼" "∼"] ["≅" "≅"] ["≈" "≈"] ["≠" "≠"] ["≡" "≡"] ["≤" "≤"] ["≥" "≥"] ["⊂" "⊂"] ["⊃" "⊃"] ["⊄" "⊄"] ["⊆" "⊆"] ["⊇" "⊇"] ["⊕" "⊕"] ["⊗" "⊗"] ["⊥" "⊥"] ["⋅" "⋅"] ["⌈" "⌈"] ["⌉" "⌉"] ["⌊" "⌊"] ["⌋" "⌋"] ["⟨" "〈"] ["⟩" "〉"] ["◊" "◊"] ["♠" "♠"] ["♣" "♣"] ["♥" "♥"] ["♦" "♦"] [""" "\""] ["Œ" "Œ"] ["œ" "œ"] ["Š" "Š"] ["š" "š"] ["Ÿ" "Ÿ"] ["ˆ" "ˆ"] ["˜" "˜"] ["–" "–"] ["—" "—"] ["‘" "‘"] ["’" "’"] ["‚" "‚"] ["“" "“"] ["”" "”"] ["„" "„"] ["†" "†"] ["‡" "‡"] ["‰" "‰"] ["‹" "‹"] ["›" "›"] ["€" "€"] ] ) ) ) (if workOnStringP outputStr (save-excursion (delete-region ξfrom ξto) (goto-char ξfrom) (insert outputStr) )) ) ) (defun remove-vowel-old (&optional ξstring ξfrom ξto) "Remove the following letters: {a e i o u}. When called interactively, work on current text block or text selection. (a “text block” is text between empty lines) When called in lisp code, if ξstring is non-nil, returns a changed string. If ξstring nil, change the text in the region between positions ξfrom ξto." (interactive (if (region-active-p) (list nil (region-beginning) (region-end)) (let ((bds (bounds-of-thing-at-point 'paragraph)) ) (list nil (car bds) (cdr bds)) ) ) ) (let (workOnStringP inputStr outputStr) (setq workOnStringP (if ξstring t nil)) (setq inputStr (if workOnStringP ξstring (buffer-substring-no-properties ξfrom ξto))) (setq outputStr (let ((case-fold-search t)) (replace-regexp-in-string "a\\|e\\|i\\|o\\|u\\|" "" inputStr) ) ) (if workOnStringP outputStr (save-excursion (delete-region ξfrom ξto) (goto-char ξfrom) (insert outputStr) )) ) ) (defun remove-vowel (ξstring &optional ξfrom-to-pair) "Remove the following letters: {a e i o u}. When called interactively, work on current text block or text selection. (a “text block” is text between empty lines) When called in lisp code, if ξfrom-to-pair is non-nil, change the text in the region between positions [from to]. ξfrom-to-pair should be a list or vector pair. Else, returns a changed string." (interactive (if (region-active-p) (list nil (vector (region-beginning) (region-end))) (let ((bds (bounds-of-thing-at-point 'paragraph)) ) (list nil (vector (car bds) (cdr bds))) ) ) ) (let (workOnStringP inputStr outputStr ξfrom ξto ) (when ξfrom-to-pair (setq ξfrom (elt ξfrom-to-pair 0) ) (setq ξto (elt ξfrom-to-pair 1) ) ) (setq workOnStringP (if ξfrom-to-pair nil t)) (setq inputStr (if workOnStringP ξstring (buffer-substring-no-properties ξfrom ξto))) (setq outputStr (let ((case-fold-search t)) (replace-regexp-in-string "a\\|e\\|i\\|o\\|u\\|" "" inputStr) ) ) (if workOnStringP outputStr (save-excursion (delete-region ξfrom ξto) (goto-char ξfrom) (insert outputStr) )) ) ) (defun what-hexadecimal-value () "Prints the decimal value of a hexadecimal string under cursor. Samples of valid input: ffff 0xffff #xffff FFFF 0xFFFF #xFFFF Test cases 64*0xc8+#x12c 190*0x1f4+#x258 100 200 300 400 500 600" (interactive ) (let (inputStr tempStr p1 p2 ) (save-excursion (search-backward-regexp "[^0-9A-Fa-fx#]" nil t) (forward-char) (setq p1 (point) ) (search-forward-regexp "[^0-9A-Fa-fx#]" nil t) (backward-char) (setq p2 (point) ) ) (setq inputStr (buffer-substring-no-properties p1 p2) ) (let ((case-fold-search nil) ) (setq tempStr (replace-regexp-in-string "^0x" "" inputStr )) ; C, Perl, … (setq tempStr (replace-regexp-in-string "^#x" "" tempStr )) ; elisp … (setq tempStr (replace-regexp-in-string "^#" "" tempStr )) ; CSS … ) (message "Hex %s is %d" tempStr (string-to-number tempStr 16 ) ) ))