Updated: Emacs & Unicode Tips. Added info about how to insert a Unicode character by its decimal code point.
Updated a page that i wrote in 2006. The code was embarrassing. Here's the updated page: Emacs Lisp: Writing a Wrap-URL Function. Elisp level: beginner.
Here's a answer to last week's elisp quiz. Emacs Lisp: Command to Replace HTML Entities with Unicode Characters.
Emacs Regex Quirk: Matching Beginning/End of Line/String/Buffer
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 © 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.
Note: for those who know elisp well, your command should also replace all entities in decimal form (⁖ ©) or hexadecimal form ©.
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: ©;. It should not become ©.
Solution to yesterday's problem: Emacs Lisp: Replacing HTML Entities with Unicode Characters.
Emacs Quiz: Replacing HTML Entities with Unicode Char
See: http://xahlee.blogspot.com/2011/09/emacs-quiz-replacing-html-entities-with.html
Emacs Lisp Function to Trim String
Emacs lisp does not have a function that removes space from the beginning and end of a string. You must use replace-regexp-in-string for that. Here's a function that does it.
(defun trim-string (string) "Remove white spaces in beginning and ending of STRING. White space here is any of: space, tab, emacs newline (line feed, ASCII 10)." (replace-regexp-in-string "\\`[ \t\n]*" "" (replace-regexp-in-string "[ \t\n]*\\'" "" string)) )
See also:
few days ago i posted a article on string-rectangle, but it wasn't a finished article and was posted by mistake. Here's a more complete article: Emacs: Manipulate Column Text, string-rectangle, ASCII-Art
Programing Language: LISP Syntax Problem of Piping Functions
Updated: Emacs Lisp: print, princ, prin1, format, message
Emacs Lisp: Writing a Date Time String Parsing Function
Of those who bought my emacs tutorial recently, there's a new free update. (fixing a external link error) Just email to xah@xahlee.org with subject “xah emacs tutorial update”, then i'll email you the download location.
If you haven't bought it, you can buy at Buy Xah Emacs Tutorial.
Emacs Lisp: HTML Processing: Split Annotation
Emacs Lisp: Writing a make-citation Command
Emacs: Preserve Your Split-Panes Configuration with winner-mode
Thanks to Damon Haley, Lindner Technologies, Shuyang Zhao for donation. Very much appreciated.
Emacs Lisp vs Perl: Redux. Which Do You Prefer?
I wrote this article: Text Processing: Emacs Lisp vs Perl. Today, some discussion happened on Google's g+ social networking site, and Randal L Schwartz made the comment that he actually prefer Perl over Emacs Lisp for text processing. See the discussion at Source plus.google.com
Note: Randal is a well known expert in Perl, co-author of Perl (1st edition) and Learning Perl, and also has written at least one elisp package in GNU Emacs (a pretty printer for elisp at 〔lisp/emacs-lisp/pp.el〕)
So, if you are fluent in using both Perl and Emacs Lisp for TEXT PROCESSING, which one do you prefer? and why? (For this purpose, Perl here can mean any of Python, Ruby, Scheme.)
Comment or vote (upper right) at: http://xahlee.blogspot.com/2011/08/emacs-lisp-vs-perl-redux.html. Result will be posted here in a week.
Here's result:
If you are fluent in using both Perl and Emacs Lisp for TEXT PROCESSING, which one do you prefer? (Perl here can be any of Python, Ruby, Scheme.)
Updated and Moved into a article by itself: Emacs: How to Associate a File with a Major Mode?.
Emacs YASnippet Tip: Expand Whole hyphenated-word as Input
Updated: List Matching Lines and Delete Matching Lines in Emacs
Emacs Lisp: Chinese character Reference Linkify
Emacs Tip: Jump to Previous Marked Position
Often, you need to go to a previous position in a buffer. For example, you are editing a big source code that's few hundred lines. You want to look at some function's definition, but then you want to return to the current position.
Emacs has a buffer mark ring and global mark ring that records mark positions and lets you jump to it.
pop-global-mark) to go back previous position that may not be the current buffer.Jon Snader's Emacs Lisp tutorial for Validating Matching Pairs
Jon Snader (jcs) wrote 2 versions in elisp for our previous programing challenge on Validate Matching Brackets.
Ι'll be going over his code soon. His blog is well annotated, so is a good one if you are learning emacs lisp, from a different style than mine. His article is at: Xah's Challenge (Part 2) By Jon Snader (jcs). @ irreal.org….
Emacs Lisp: Getting Command Line Arguments
Answer to the challenge 2 days ago: Emacs Lisp: Batch Script to Validate Matching Brackets.
emacs tip: inserting source code in org-mode
When using org-mode, you can insert a snippet of programing language code.
Type <s then Tab ↹. It will insert this markup:
#+begin_src ▮ #+end_src
The above is the syntax for literal text. (similar to the concept of Perl and PHP'S Heredoc.)
Then, type “emacs-lisp” so you have:
#+begin_src emacs-lisp #+end_src
this tells org-mode this snippet is emacs-lisp code. The “emacs-lisp” there can be any mode. ⁖ “html”, “perl”,“haskell”, etc. (technically: the value of the variable major-mode, then minus the “-mode” string at the end.)
orgmode.org 15.2 Easy Templates
For basics of org-mode, see: Emacs: outline-mode and org-mode tutorial.
Emacs Lisp: Processing HTML: Transform Tags from <span> to <b>
Little Parser Problem Challenge: Matching Pairs Validation
See http://xahlee.blogspot.com/2011/07/little-parser-problem-challenge.html
Thanks to Xiang Xin Luo for donation.
For those of you who bought my Emacs Tutorial, just send me a email and i'll mail you a update. (xah@xahlee.org) In the subject, put “xah emacs tutorial update”.
Buy at Buy Xah Emacs Tutorial. Thank you for support!
Emacs: unique buffer names; auto-compile elisp files
If you work with files of the same name often, their buffers names are hard to distinguish. ⁖ {“index.html”, “index.html <2>”, “index.html <3>”}. You can make the buffer name display part of the dir name. Place the following in your emacs init file:
;; make buffer names easily identifiable (require 'uniquify) ; bundled with GNU Emacs 23.2.1 or earlier (setq uniquify-buffer-name-style 'forward)
auto byte-compile elisp files
If you code elisp, it's nice to have the elisp file automatically byte-compiled everytime you save it. Put the following in your emacs init file:
;; auto compile elisp files after save (add-hook 'emacs-lisp-mode-hook (lambda () (add-hook 'after-save-hook 'emacs-lisp-byte-compile t t)) )
Note that a byte-compiled lisp file will make emacs load it faster, but also run faster. The code run at least 6 times faster. However, for small elisp files (such as your 〔.emacs〕), the speed increase may not be noticeable.
Also note: if you want emacs to load the byte compiled file if it exists, you should not include the “.el” suffix. ⁖ do it like this (load "my_emacs_keys")
See also:
How to Install Emacs Packages ◇
Emacs Lisp's Library System: What's require, load, load-file, autoload, feature?.
I got the above tips today from Source tsengf.blogspot.com. Check it out, he's got many other emacs tips.
Addendum: Adolfo Benedetti wrote to add a code so that it'll byte-compile only if there already exist a byte-compiled file. Great improvement. Thanks.
(defun byte-compile-current-buffer () "`byte-compile' current buffer if it's emacs-lisp-mode and compiled file exists." (interactive) (when (and (eq major-mode 'emacs-lisp-mode) (file-exists-p (byte-compile-dest-file buffer-file-name))) (byte-compile-file buffer-file-name))) (add-hook 'after-save-hook 'byte-compile-current-buffer)
Update: Emacs dired Tips (File Management)
Emacs: perl-mode vs cperl-mode
Emacs: Defining Alias to Increase Productivity.
Emacs Lisp: Processing HTML: Transform Tags to HTML5 “figure” and “figcaption” Tags
blog comments powered by Disqus