Showing posts from September 25, 2011

Emacs: Converting Decimal and Hexadecimal

Sometimes i need to convert between decimal and hexadecimal. Here's how to do that using emacs's built-in calculator.Call “calc” 【Alt+x】.Type any number. For example, 10.Type “d6” to turn the display into hexadecimal form.Type “d0” to turn the display into decimal form.To type a hex number, type #, then type “16#aa” for the hex “aa”.Another way, i find simpler, is using elisp. Open a new file, then type the following: (format "%x" 10) ; decimal to hex. Returns 「a」 (format "%d" #xa) ; hex 「a」 to decimal. Returns 「10」. Select the code, then call “eval-region” 【Alt+x】. Or, put cursor at the end of the right parenthesis, then call “eval-last-sexp” 【Ctrl+xCtrl+e】 (See: Emacs: How to Eval Elisp Code, Find Functions, Search Documentation.) To open a new file in ErgoEmacs, press 【Ctrl+n】. In GNU Emacs, call “switch-to-buffer” 【Ctrl+xb】 then type a new name.

emacs lisp if function's grammar

Emacs Lisp's “if” function has a annoying form.One would expect it to take 3 arguments, no more and no less, like this: (if ‹test› ‹expression for true› ‹expression for false› ) But it's actually takes many more args. From the 3rd args onward are all expressions for false. Like this: (if ‹test› ‹expression for true› ‹expression for false› ‹expression for false 2› ‹expression for false 3› … ) Here's a test code you can check: (if nil (message "false") (message "true") (message "so true") (message "yes really") ) You can run the code by selecting it, then call “eval-region” 【Alt+x】. You can switch to the “*Messages*” buffer by 【Ctrl+he】.Is Common Lisp and Scheme Lisp the same way?I think i'd prefer the simpler, logical, form: (if test true false).

Oxford Comma and Strippers

Emacs Lisp: Command to Replace HTML Entities with Unicode Characters

Perm url with updates: Lisp: Command to Replace HTML Entities with Unicode CharactersXah Lee, 2011-09-27This page shows you how to write a elisp command to replace HTML entities such as é by its unicode character é.The ProblemI have many HTML files from existing sources that contain many HTML Entities. I want to have a command that automatically change them to Unicode characters. Example:‘ ⇒ ‘’ ⇒ ’“ ⇒ “” ⇒ ”é ⇒ é(For more about HTML entities, see: Character Sets and Encoding in HTMLHTML/XML Entities List.)The command should work on the current paragraph, or text selection.SolutionThis is easy to write. One of the basic elisp idiom is find & replace on a region, like this: (defunreplace-html-chars-region (start end) "Replace some html entities in region …." (interactive "r") (save-restriction (narrow-to-region start end) …

Emacs Regex Quirk: Matching beginning/end of line/string

Perm URL with updates:, 2011-09-29, …, 2011-11-28This page is a tutorial on emacs regex. Suppose you want to write a function that removes spaces in front of a string. You'd use a regex like this: (replace-regexp-in-string "^ +""" myString) Here, the ^ means beginning of string, right?WRONG!In emacs regex, ^ matches beginning of string, but also beginning of each line in the string. Try to evaluate the following (place cursor at end then call eval-last-sexp.): (replace-regexp-in-string "^ +""•"" like (1) this (2) that ") Here's the result: " •like •(1) this •(2) that " To match just the beginning of a string, use \`. Like this:;; Remove space/tab/newline in beginning myStr (replace-regexp-in-string "\\`[ \t\n]*""" myStr) Similarly, the $ matches the endings of {buffer, string, line}. To just match ending of {buffer, str…

Rhythmic Gymnastics videos


Emacs Lisp: Fixing Dead Links

Perm url with updates: Lisp: Fixing Dead LinksXah Lee, 2011-09-25This page shows you how to write a elisp script that checks thousands of HTML files and fix dead links.The ProblemSummaryI have 2 thousands HTML files that contains about 70 dead local links. I need to write a elisp script to change these links to non-links. For example, this is a dead link: <ahref="../widget/index.html#Top">Introduction</a> I need it to be: <spanclass="εlink"title="../widget/index.html#Top">Introduction</span> The script should run in batch. And it should generate a report.DetailI have copy of the emacs manuals, at:GNU Emacs Manual (~690 files)GNU Emacs Lisp Reference Manual (~900 files)These manual sometimes have links to other info files that's not emacs. For example, on this page Changing Files - GNU Emacs Lisp Reference Manual, it contains a link to GNU coreutils like this: <ahref=&quo…

Emacs Quiz of the Day: replace-html-entities

Write a function “replace-html-entities”. If there is a text selection, work on the selection. Else, work on the current paragraph (defined by 2 line breaks)Replace all named html entities such as &copy; to ©. (see entity list here: HTML/XML Entities List.)i'll post a answer on Monday.If you are new to elisp, the following articles will be helpful. One of the article basically spills out the solution.Emacs Lisp BasicsEmacs Lisp Idioms (for writing interactive commands)Emacs Lisp Examples (Page 1)Note: for those who know elisp well, your command should also replace all entities in decimal form (e.g. &#169;) or hexadecimal form &#xa9;. There's a tricky part in this problem. Your code should not introduce extraneous transformation. For example, suppose the input file discusses HTML language, and it has this text in it: &copy&#59;. It should not become ©.