<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wikemacs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=187.41.158.248</id>
	<title>WikEmacs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wikemacs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=187.41.158.248"/>
	<link rel="alternate" type="text/html" href="https://wikemacs.org/wiki/Special:Contributions/187.41.158.248"/>
	<updated>2026-04-25T07:27:34Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>https://wikemacs.org/index.php?title=Emacs_Lisp_Cookbook&amp;diff=2618</id>
		<title>Emacs Lisp Cookbook</title>
		<link rel="alternate" type="text/html" href="https://wikemacs.org/index.php?title=Emacs_Lisp_Cookbook&amp;diff=2618"/>
		<updated>2012-04-07T14:29:53Z</updated>

		<summary type="html">&lt;p&gt;187.41.158.248: /* Serialization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains snippets of code that demonstrate basic [[Emacs Lisp]]&lt;br /&gt;
programming operations in the spirit of the O'Reilly Cookbook&lt;br /&gt;
series of books. For every task addressed, a worked-out solution&lt;br /&gt;
is presented as a short, focused, directly usable piece of code.&lt;br /&gt;
&lt;br /&gt;
All this stuff can be found elsewhere, but it is scattered about in&lt;br /&gt;
libraries, manuals, etc.  It would be helpful to have here in one&lt;br /&gt;
spot.&lt;br /&gt;
&lt;br /&gt;
These recipes should be pastable into the '''*scratch*''' buffer so that&lt;br /&gt;
users can hit '''C-j''' and evaluate them step by step.&lt;br /&gt;
&lt;br /&gt;
== Strings ==&lt;br /&gt;
&lt;br /&gt;
The empty string (zero-length string, null string, ...):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(zerop (string-match &amp;quot;&amp;quot; &amp;quot;&amp;quot;)) ;; O(n)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-equal &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(n)?&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(equal &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(n)?&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(zerop (length &amp;quot;&amp;quot;)) ;; O(1)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(eq &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(1)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a space and performance optimization, Emacs keeps an intern-ed copy&lt;br /&gt;
of the empty string as a single object&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(eq &amp;quot;&amp;quot; (purecopy &amp;quot;&amp;quot;))&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(eq &amp;quot;&amp;quot; (propertize &amp;quot;&amp;quot; 'face 'italic))&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Strings vs buffer content ===&lt;br /&gt;
&lt;br /&gt;
While it is quite common in other programming languages to work on&lt;br /&gt;
strings contained in variables in Emacs it is even more idiomatic to&lt;br /&gt;
work on strings in buffers. That's why the following contains examples&lt;br /&gt;
of both.&lt;br /&gt;
&lt;br /&gt;
=== Substrings ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; 0 3)&lt;br /&gt;
==&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; -3 -1)&lt;br /&gt;
==&amp;gt; &amp;quot;ef&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The TO argument is optional:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; -3)&lt;br /&gt;
==&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(with-temp-buffer&lt;br /&gt;
  (insert &amp;quot;abcdefg&amp;quot;)&lt;br /&gt;
  (buffer-substring 2 4))&lt;br /&gt;
==&amp;gt; &amp;quot;bc&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Processing characters ===&lt;br /&gt;
&lt;br /&gt;
Reversing a string:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(string-to-list &amp;quot;foo&amp;quot;)&lt;br /&gt;
==&amp;gt; (102 111 111)&lt;br /&gt;
(reverse (string-to-list &amp;quot;foo&amp;quot;))&lt;br /&gt;
==&amp;gt; (111 111 102)&lt;br /&gt;
(apply 'string (reverse (string-to-list &amp;quot;foo&amp;quot;)))&lt;br /&gt;
==&amp;gt; &amp;quot;oof&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See CharacterProcessing and StringModification. See [[tr]] for an example of you sometimes need to mix strings and characters.&lt;br /&gt;
&lt;br /&gt;
Looking at characters in buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(with-temp-buffer&lt;br /&gt;
  (insert &amp;quot;abcdefg&amp;quot;)&lt;br /&gt;
  (goto-char (point-min))&lt;br /&gt;
  (while (not (= (char-after) ?b))&lt;br /&gt;
    (forward-char))&lt;br /&gt;
  (point))&lt;br /&gt;
==&amp;gt; 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Trim whitespace ===&lt;br /&gt;
&lt;br /&gt;
Trim whitespace from the end of a string:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(setq test-str &amp;quot;abcdefg  &amp;quot;)&lt;br /&gt;
(when (string-match &amp;quot;[ \t]*$&amp;quot; test-str)&lt;br /&gt;
  (message (concat &amp;quot;[&amp;quot; (replace-match &amp;quot;&amp;quot; nil nil test-str) &amp;quot;]&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trim whitespace from a string with a Perl-like chomp function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun chomp (str)&lt;br /&gt;
  &amp;quot;Chomp leading and tailing whitespace from STR.&amp;quot;&lt;br /&gt;
  (while (string-match &amp;quot;\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'&amp;quot;&lt;br /&gt;
                       str)&lt;br /&gt;
    (setq str (replace-match &amp;quot;&amp;quot; t t str)))&lt;br /&gt;
  str)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Splitting strings ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== Joining strings ===&lt;br /&gt;
&lt;br /&gt;
Use `mapconcat' to join a list into a string using a separator (&amp;quot;glue&amp;quot;) between elements in the string.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(mapconcat 'identity '(&amp;quot;&amp;quot; &amp;quot;home&amp;quot; &amp;quot;alex &amp;quot; &amp;quot;elisp&amp;quot; &amp;quot;erc&amp;quot;) &amp;quot;/&amp;quot;)&lt;br /&gt;
==&amp;gt; &amp;quot;/home/alex /elisp/erc&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Serialization ===&lt;br /&gt;
&lt;br /&gt;
The basic idea is to convert forms to strings with `prin1-to-string' and convert it back from a string with `read'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(length (read (prin1-to-string (make-list 1000000 '(x)))))&lt;br /&gt;
==&amp;gt; 1000000&lt;br /&gt;
&lt;br /&gt;
(read (prin1-to-string &amp;quot;Hello World!&amp;quot;))&lt;br /&gt;
==&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This only works in the simplest cases.  Unfortunately, this doesn't work for all Emacs data types for programming or the editor.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(read (prin1-to-string (make-hash-table))) ;; Error before Emacs 23.&lt;br /&gt;
==&amp;gt; #s(hash-table size 65 test eql rehash-size 1.5 [...] data ())&lt;br /&gt;
&lt;br /&gt;
(read (prin1-to-string (current-buffer)))&lt;br /&gt;
==&amp;gt; Lisp error: (invalid-read-syntax &amp;quot;#&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Formatting ===&lt;br /&gt;
&lt;br /&gt;
== Killing text ==&lt;br /&gt;
&lt;br /&gt;
As the Emacs Lisp Manual says, &amp;quot;Most of the kill commands are primarily for&lt;br /&gt;
interactive use [...]  When you need to delete text for internal&lt;br /&gt;
purposes within a Lisp function, you should normally use deletion&lt;br /&gt;
functions, so as not to disturb the kill ring contents.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following mimic the `kill-' commands but without disturbing the kill ring.&lt;br /&gt;
&lt;br /&gt;
=== Delete region ===&lt;br /&gt;
&lt;br /&gt;
The Lisp equivalent of `kill-region' (`C-w') but without kill ring side effects::&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (region-beginning) (region-end))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
According to the ElispManual, &amp;quot;Few programs need to use the&lt;br /&gt;
`region-beginning' and `region-end' functions.&amp;quot;  This is because&lt;br /&gt;
Lisp code should not rely on nor &amp;quot;alter the mark unless altering&lt;br /&gt;
the mark is part of the user-level functionality of the&lt;br /&gt;
command.  (And, in that case, this effect should be documented.)&lt;br /&gt;
To remember a location for internal use in the Lisp program,&lt;br /&gt;
store it in a Lisp variable.  For example: [...]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Delete line ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-line' (`C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (forward-char -1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (forward-char -1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point) (line-end-position))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
The examples with `forward-line' are shown because the paradigm is used later, see below.&lt;br /&gt;
&lt;br /&gt;
=== Delete line backwards ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of killing the line backwards (`C-0 C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 0)&lt;br /&gt;
  (delete-region (point) beg))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (save-excursion&lt;br /&gt;
                 (forward-line 0)&lt;br /&gt;
                 (point))&lt;br /&gt;
               (point))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (line-beginning-position) (point))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Delete line to next line  ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of killing the line and the newline (`C-1 C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
=== Delete whole line ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-whole-line' (`C-S-DEL') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (progn (forward-line 0)&lt;br /&gt;
                  (point))))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (save-excursion&lt;br /&gt;
                 (forward-line 0)&lt;br /&gt;
                 (point))&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (line-beginning-position) (line-end-position))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
=== Delete word ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-word' (`M-d') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-word 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-word 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
=== Delete sentence ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-sentence' (`M-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-sentence 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (delete-region (point)&lt;br /&gt;
                 (save-excursion&lt;br /&gt;
                   (forward-sentence 1)&lt;br /&gt;
                   (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
                   &lt;br /&gt;
== Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== String a number? ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun string-integer-p (string)&lt;br /&gt;
  (if (string-match &amp;quot;\\`[-+]?[0-9]+\\'&amp;quot; string)&lt;br /&gt;
      t&lt;br /&gt;
    nil))&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;1234&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;x1234&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(defun string-float-p (string)&lt;br /&gt;
  (if (string-match &amp;quot;\\`[-+]?[0-9]+\\.[0-9]*\\'&amp;quot; string)&lt;br /&gt;
      t&lt;br /&gt;
    nil))&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;1234&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;.1&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;1.&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
=== String to number ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun decimal-number (string)&lt;br /&gt;
  (let ((n (string-to-number string)))&lt;br /&gt;
    (if (and (zerop n)&lt;br /&gt;
             (not (string-match &amp;quot;\\`\\s-*0+\\.?0*\\s-*\\'&amp;quot; string)))&lt;br /&gt;
        nil&lt;br /&gt;
      n)))&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;536870911&amp;quot;)&lt;br /&gt;
==&amp;gt; 536870911&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;536870912&amp;quot;)&lt;br /&gt;
==&amp;gt; 536870912.0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; 3.141592653589793&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;042&amp;quot;)&lt;br /&gt;
==&amp;gt; 42&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot; 0 &amp;quot;)&lt;br /&gt;
==&amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;000&amp;quot;)&lt;br /&gt;
==&amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;0.0&amp;quot;)&lt;br /&gt;
==&amp;gt; 0.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Random numbers ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (random 2)  ;coin toss (0 or 1)&lt;br /&gt;
  (+ (random 6) 1)  ;dice&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Put commas in numbers ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun group-number (num &amp;amp;optional size char)&lt;br /&gt;
    &amp;quot;Format NUM as string grouped to SIZE with CHAR.&amp;quot;&lt;br /&gt;
    ;; Based on code for `math-group-float' in calc-ext.el&lt;br /&gt;
    (let* ((size (or size 3))&lt;br /&gt;
           (char (or char &amp;quot;,&amp;quot;))&lt;br /&gt;
           (str (if (stringp num)&lt;br /&gt;
                    num&lt;br /&gt;
                  (number-to-string num)))&lt;br /&gt;
           (pt (or (string-match &amp;quot;[^0-9a-zA-Z]&amp;quot; str) (length str))))&lt;br /&gt;
      (while (&amp;gt; pt size)&lt;br /&gt;
        (setq str (concat (substring str 0 (- pt size))&lt;br /&gt;
                          char&lt;br /&gt;
                          (substring str (- pt size)))&lt;br /&gt;
              pt (- pt size)))&lt;br /&gt;
      str))&lt;br /&gt;
&lt;br /&gt;
  (group-number 299792458)&lt;br /&gt;
  ==&amp;gt; &amp;quot;299,792,458&amp;quot;&lt;br /&gt;
  (group-number &amp;quot;149597870691&amp;quot; 4 &amp;quot; &amp;quot;)&lt;br /&gt;
  ==&amp;gt; &amp;quot;1495 9787 0691&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Incrementing numbers ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
== Dates and times ==&lt;br /&gt;
&lt;br /&gt;
=== Get today's date ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== Formatting dates ===&lt;br /&gt;
&lt;br /&gt;
Use the function `format-time-string' which is a build in function in both Emacsen and works like `strftime':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    ;; Year-Month-Day:&lt;br /&gt;
    (insert (format-time-string &amp;quot;%Y-%m-%d&amp;quot;))&lt;br /&gt;
    ;; Hour:Minutes:Seconds&lt;br /&gt;
    (insert (format-time-string &amp;quot;%H-%M-%S&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Conversions ===&lt;br /&gt;
&lt;br /&gt;
Read a date from a string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((time (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;)))&lt;br /&gt;
    (set-time-zone-rule t) ;; Use Universal time.&lt;br /&gt;
    (prog1 (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; time)&lt;br /&gt;
      (set-time-zone-rule nil))) ;; Reset to default time zone.&lt;br /&gt;
  ==&amp;gt; &amp;quot;1983-09-27 17:35:59 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Decode a time object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (decode-time (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;))&lt;br /&gt;
  ==&amp;gt; (59 35 13 27 9 1983 2 t -14400)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Get the seconds from the unix epoch.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((time (date-to-time &amp;quot;13 Feb 2009 23:31:30 UTC&amp;quot;)))&lt;br /&gt;
    (float-time time))&lt;br /&gt;
  ==&amp;gt; 1234585890.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the date for seconds from the unix epoch.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; (seconds-to-time 1234585890))&lt;br /&gt;
  ==&amp;gt; &amp;quot;2009-02-13 23:31:30 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the date 30 seconds in the future.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; (time-add (current-time)&lt;br /&gt;
                                                  (seconds-to-time 30)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;2012-02-13 10:07:11 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Formatting elapsed time in years, days, hours, minutes and seconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-seconds &amp;quot;%Y %D %h:%m:%s&amp;quot; (1- (* 367 24 3600)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;1 year 1 day 23:59:59&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the days between two dates.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((days1 (time-to-days (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;)))&lt;br /&gt;
        (days2 (time-to-days (date-to-time &amp;quot;2009-02-13 23:31:30 UTC&amp;quot;))))&lt;br /&gt;
    (- days2 days1))&lt;br /&gt;
  ==&amp;gt; 9271&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Getting the day in the year.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (time-to-day-in-year (current-time))&lt;br /&gt;
  ==&amp;gt; 44&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Build a date based on the day of the year.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%j&amp;quot;&lt;br /&gt;
                      (encode-time 0 0 0 44 1 2012))&lt;br /&gt;
  ==&amp;gt; &amp;quot;044&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Timers ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
== Pattern matching ==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Patterns&amp;quot; refers to RegularExpression&amp;quot;&amp;quot;s.&lt;br /&gt;
&lt;br /&gt;
There's a set of functions that work in strings, and a set that work in buffers.&lt;br /&gt;
&lt;br /&gt;
=== Finding ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (string-match &amp;quot;foo*&amp;quot; &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
    ==&amp;gt; 6&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
      (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
      (goto-char (point-min))&lt;br /&gt;
      (re-search-forward &amp;quot;foo*&amp;quot;)&lt;br /&gt;
      (point))&lt;br /&gt;
    ==&amp;gt; 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Alternative without regular expressions: `search-forward'.&lt;br /&gt;
&lt;br /&gt;
Note that the functions working on buffers move point to the end of the occurrence found and return it.&lt;br /&gt;
That's why the result is 10 instead of 6!&lt;br /&gt;
&lt;br /&gt;
=== Comments ===&lt;br /&gt;
&lt;br /&gt;
Move to the beginning of the current comment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (require 'newcomment)&lt;br /&gt;
    (comment-beginning)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Move to the text after a comment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (comment-search-forward (line-end-position) t)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
See also EndOfLineNoComments.&lt;br /&gt;
&lt;br /&gt;
=== Search and replace ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (replace-regexp-in-string &amp;quot;foo*&amp;quot; &amp;quot;fu&amp;quot; &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
    ==&amp;gt; &amp;quot;Fight fu fur fud!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
       (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
       (goto-char (point-min))&lt;br /&gt;
       (while (re-search-forward &amp;quot;foo*&amp;quot; nil t)&lt;br /&gt;
         (replace-match &amp;quot;fu&amp;quot;))&lt;br /&gt;
       (buffer-string))&lt;br /&gt;
    ==&amp;gt; &amp;quot;Fight fu fur fud!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Alternative without regular expressions: `search-forward'.&lt;br /&gt;
&lt;br /&gt;
See also StringSearchAndReplace.&lt;br /&gt;
&lt;br /&gt;
=== Verifying ===&lt;br /&gt;
&lt;br /&gt;
Sometimes you just want to check whether you're at the right place:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
      (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
      (goto-char (point-min))&lt;br /&gt;
      (looking-at &amp;quot;fight&amp;quot;))&lt;br /&gt;
    ==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
== Sequences ==&lt;br /&gt;
&lt;br /&gt;
Datatypes used to represent sequences of things:&lt;br /&gt;
&lt;br /&gt;
     _____________________________________________&lt;br /&gt;
    |                                             |&lt;br /&gt;
    |          Sequence                           |&lt;br /&gt;
    |  ______   ________________________________  |&lt;br /&gt;
    | |      | |                                | |&lt;br /&gt;
    | | List | |             Array              | |&lt;br /&gt;
    | |      | |    ________       ________     | |&lt;br /&gt;
    | |______| |   |        |     |        |    | |&lt;br /&gt;
    |          |   | Vector |     | String |    | |&lt;br /&gt;
    |          |   |________|     |________|    | |&lt;br /&gt;
    |          |  ____________   _____________  | |&lt;br /&gt;
    |          | |            | |             | | |&lt;br /&gt;
    |          | | Char-table | | Bool-vector | | |&lt;br /&gt;
    |          | |____________| |_____________| | |&lt;br /&gt;
    |          |________________________________| |&lt;br /&gt;
    |_____________________________________________|&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
&lt;br /&gt;
List basics are explained on ListStructure.&lt;br /&gt;
Lists can shrink and grow, but access to elements towards the end of the list is slow if the list is long.&lt;br /&gt;
&lt;br /&gt;
Use `cons' to append a new element to the front of a list.&lt;br /&gt;
Use `nth' to access an element of the list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
      (when (string= &amp;quot;foo&amp;quot; (nth 1 words))&lt;br /&gt;
        (setq words (cons &amp;quot;bar&amp;quot; words)))&lt;br /&gt;
      words)&lt;br /&gt;
    ==&amp;gt; (&amp;quot;bar&amp;quot; &amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
See ListModification for more ways of changing a list.&lt;br /&gt;
&lt;br /&gt;
Iteration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((result))&lt;br /&gt;
      (dolist (word '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;))&lt;br /&gt;
        (when (string-match &amp;quot;o&amp;quot; word)&lt;br /&gt;
          (setq result (cons word result))))&lt;br /&gt;
      (nreverse result))&lt;br /&gt;
    ==&amp;gt; (&amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Note how `cons' adds an element to the front of the list,&lt;br /&gt;
so that usually the list has to be reversed after the loop.&lt;br /&gt;
`nreverse' is particularly efficient because it does this&lt;br /&gt;
destructively by swiveling pointers around. See&lt;br /&gt;
DestructiveOperations for more about this.&lt;br /&gt;
&lt;br /&gt;
Copying:&lt;br /&gt;
&lt;br /&gt;
Use `copy-sequence' to make a copy of a list that won't change the&lt;br /&gt;
elements of the original.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-sequence orig)))&lt;br /&gt;
      (setcdr copy '((5 6)))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 4)) ((1 2) (5 6)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
However, the elements in the copy are still from the original.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-sequence orig)))&lt;br /&gt;
      (setcdr (cadr copy) '(0))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 0)) ((1 2) (3 0)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
The function `copy-tree' is the recursive version of `copy-sequence'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-tree orig)))&lt;br /&gt;
      (setcdr (cadr copy) '(0))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 4)) ((1 2) (3 0)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Filtering:&lt;br /&gt;
&lt;br /&gt;
Emacs Lisp doesn't come with a `filter' function to keep elements that satisfy a conditional and excise the elements that do not satisfy it.  One can use `mapcar' to iterate over a list with a conditional, and then use `delq' to remove the `nil' values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun my-filter (condp lst)&lt;br /&gt;
    (delq nil&lt;br /&gt;
          (mapcar (lambda (x) (and (funcall condp x) x)) lst)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;          &lt;br /&gt;
&lt;br /&gt;
Therefore,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (my-filter 'identity my-list)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (delq nil my-list)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (my-filter 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; (1 2 3 4)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Actually the package cl-seq contains the functions `remove-if' and `remove-if-not'.  The latter can be used instead of `my-filter'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (remove-if-not 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; (1 2 3 4)&lt;br /&gt;
&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (remove-if 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; ((quote a) &amp;quot;nil&amp;quot; nil)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
As an example here is the quick sort algorithm:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun quicksort (lst)&lt;br /&gt;
    &amp;quot;Implement the quicksort algorithm.&amp;quot;&lt;br /&gt;
    (if (null lst) nil&lt;br /&gt;
      (let* ((spl (car lst))&lt;br /&gt;
             (rst (cdr lst))&lt;br /&gt;
             (smalp (lambda (x)&lt;br /&gt;
                   (&amp;lt; x spl))))&lt;br /&gt;
        (append (quicksort (remove-if-not smalp rst))&lt;br /&gt;
                (list spl)&lt;br /&gt;
                (quicksort (remove-if smalp rst))))))&lt;br /&gt;
&lt;br /&gt;
  (quicksort '(5 7 1 3 -9 8 7 -4 0))&lt;br /&gt;
  ==&amp;gt; (-9 -4 0 1 3 5 7 7 8)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Tranposing:&lt;br /&gt;
&lt;br /&gt;
Convert multiple lists into a list &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 ((lambda (&amp;amp;rest args)&lt;br /&gt;
    (mapcar (lambda (n)&lt;br /&gt;
              (delq nil (mapcar (lambda (arg) (nth n arg)) args)))&lt;br /&gt;
            (number-sequence 0 (1- (apply 'max (mapcar 'length args))))))&lt;br /&gt;
  '(1 2 3) '(a b c) '(A B C))&lt;br /&gt;
  ==&amp;gt; ((1 a A) (2 b B) (3 c C))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
A more concise version is possible with the the higher-arity version of mapcar available with the `cl' library.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  ((lambda (&amp;amp;rest args)&lt;br /&gt;
     (apply (function mapcar*) (function list) args))&lt;br /&gt;
   '(1 2 3) '(a b c) '(A B C))&lt;br /&gt;
  ==&amp;gt; ((1 a A) (2 b B) (3 c C))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Searching:&lt;br /&gt;
&lt;br /&gt;
Simply checking for existence of a value in a list can be done with&lt;br /&gt;
`member' or `memq'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
    (car (member &amp;quot;for&amp;quot; words)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;for&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  (let ((re &amp;quot;\\wo\\b&amp;quot;)&lt;br /&gt;
        (words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
    (consp (memq t&lt;br /&gt;
             (mapcar (lambda (s) (numberp (string-match re s))) words))))&lt;br /&gt;
  ==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
In the latter, a more efficient algorithm would use a loop (a non-local exit).&lt;br /&gt;
&lt;br /&gt;
=== Association lists ===&lt;br /&gt;
&lt;br /&gt;
The ElispManual has examples of finding and deleting values in an&lt;br /&gt;
association list.  Here are cases when the car values are strings.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (assoc &amp;quot;2&amp;quot; '((&amp;quot;2&amp;quot; . 2) (&amp;quot;1&amp;quot; . 1) (&amp;quot;2&amp;quot;) (&amp;quot;3&amp;quot; . 3)))&lt;br /&gt;
  ==&amp;gt; (&amp;quot;2&amp;quot; . 2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Deleting:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((alist '((&amp;quot;a&amp;quot; . 1) (&amp;quot;b&amp;quot; . 2))))&lt;br /&gt;
    (delq (assoc &amp;quot;a&amp;quot; alist) alist))&lt;br /&gt;
  ==&amp;gt; ((&amp;quot;b&amp;quot; . 2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Matches with a test function other than `equal':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((alist '((&amp;quot;ab&amp;quot; . 1) (&amp;quot;bc&amp;quot; . 2) (&amp;quot;cd&amp;quot; . 3))))&lt;br /&gt;
    (assoc-default &amp;quot;c&amp;quot; alist (lambda (x y) (string-match y x))))&lt;br /&gt;
  ==&amp;gt; 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Vectors ===&lt;br /&gt;
&lt;br /&gt;
Vectors are fixed in size but elements can be accessed in constant time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((words [&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;]))&lt;br /&gt;
      (when (string= &amp;quot;foo&amp;quot; (aref words 1))&lt;br /&gt;
        (aset words 1 &amp;quot;bar&amp;quot;))&lt;br /&gt;
      words)&lt;br /&gt;
    ==&amp;gt; [&amp;quot;fight&amp;quot; &amp;quot;bar&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
== Hashes ==&lt;br /&gt;
&lt;br /&gt;
Hashes map keys to values. In a way they are similar to alists, except&lt;br /&gt;
they are more efficient for a large number of keys.&lt;br /&gt;
&lt;br /&gt;
More info is available on the HashMap page.&lt;br /&gt;
&lt;br /&gt;
=== Storing and retrieving keys and values ===&lt;br /&gt;
&lt;br /&gt;
By default, hash tables use `eql' to compare keys. This is not appropriate&lt;br /&gt;
for strings: ##(eql &amp;quot;alex&amp;quot; &amp;quot;alex&amp;quot;)## ==&amp;gt; nil. Thus, use `equal' in these cases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal)))&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (gethash &amp;quot;kensanata&amp;quot; nick-table))&lt;br /&gt;
    ==&amp;gt; &amp;quot;Alex Schroeder&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Iterate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal))&lt;br /&gt;
          nicks)&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;e1f&amp;quot; &amp;quot;Luis Fernandes&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;pjb&amp;quot; &amp;quot;Pascal J. Bourguignon&amp;quot; nick-table)&lt;br /&gt;
      (maphash (lambda (nick real-name)&lt;br /&gt;
                 (setq nicks (cons nick nicks)))&lt;br /&gt;
               nick-table)&lt;br /&gt;
      nicks)&lt;br /&gt;
      ==&amp;gt; (&amp;quot;pjb&amp;quot; &amp;quot;e1f&amp;quot; &amp;quot;kensanata&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Sorting keys  ===&lt;br /&gt;
&lt;br /&gt;
Use `maphash' to build up a list of keys, sort it, and then loop through&lt;br /&gt;
the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal))&lt;br /&gt;
          nicks)&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;e1f&amp;quot; &amp;quot;Luis Fernandes&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;pjb&amp;quot; &amp;quot;Pascal J. Bourguignon&amp;quot; nick-table)&lt;br /&gt;
      (maphash (lambda (nick real-name)&lt;br /&gt;
                 (setq nicks (cons nick nicks)))&lt;br /&gt;
               nick-table)&lt;br /&gt;
      (mapcar (lambda (nick)&lt;br /&gt;
                (concat nick &amp;quot; =&amp;gt; &amp;quot; (gethash nick nick-table)))&lt;br /&gt;
              (sort nicks 'string&amp;lt;)))&lt;br /&gt;
      ==&amp;gt; (&amp;quot;e1f =&amp;gt; Luis Fernandes&amp;quot;&lt;br /&gt;
           &amp;quot;kensanata =&amp;gt; Alex Schroeder&amp;quot;&lt;br /&gt;
           &amp;quot;pjb =&amp;gt; Pascal J. Bourguignon&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;           &lt;br /&gt;
&lt;br /&gt;
== Files ==&lt;br /&gt;
&lt;br /&gt;
=== Read ===&lt;br /&gt;
&lt;br /&gt;
Processing a file is usually done with a temporary buffer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 (defun process-file (file)&lt;br /&gt;
   &amp;quot;Read the contents of a file into a temp buffer and then do&lt;br /&gt;
 something there.&amp;quot;&lt;br /&gt;
   (when (file-readable-p file)&lt;br /&gt;
     (with-temp-buffer&lt;br /&gt;
       (insert-file-contents file)&lt;br /&gt;
       (goto-char (point-min))&lt;br /&gt;
       (while (not (eobp))&lt;br /&gt;
       ;; do something here with buffer content&lt;br /&gt;
         (forward-line)))))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;         &lt;br /&gt;
&lt;br /&gt;
On the chance that a buffer may already be actively visiting the file,&lt;br /&gt;
consider using `find-file-noselect'&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun file-string (file)&lt;br /&gt;
    &amp;quot;Read the contents of a file and return as a string.&amp;quot;&lt;br /&gt;
    (with-current-buffer (find-file-noselect file)&lt;br /&gt;
      (buffer-string)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Write ===&lt;br /&gt;
&lt;br /&gt;
To write something to a file you can create a temporary buffer, insert the things to write there and write the buffer contents to a file.  The following example read a string and a filename (with completion, but doesn't need to exist, see InteractiveCodeChar F) and write the string to that file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 (defun write-string-to-file (string file)&lt;br /&gt;
   (interactive &amp;quot;sEnter the string: \nFFile to save to: &amp;quot;)&lt;br /&gt;
   (with-temp-buffer&lt;br /&gt;
     (insert string)&lt;br /&gt;
     (when (file-writable-p file)&lt;br /&gt;
       (write-region (point-min)&lt;br /&gt;
                     (point-max)&lt;br /&gt;
                     file))))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                     &lt;br /&gt;
&lt;br /&gt;
=== Input and output (I/O) ===&lt;br /&gt;
&lt;br /&gt;
This is a nice and simple way to edit a file with Emacs Lisp,&lt;br /&gt;
especially when there's a chance the file might be already be visited&lt;br /&gt;
in Emacs and its ok to edit the existing buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (with-current-buffer (find-file-noselect &amp;quot;~/logfile&amp;quot;)&lt;br /&gt;
    (goto-char (point-max))&lt;br /&gt;
    (insert (format &amp;quot;Hash of last line: %s&amp;quot;&lt;br /&gt;
             (md5&lt;br /&gt;
              (save-excursion&lt;br /&gt;
                (forward-line -1)&lt;br /&gt;
                (buffer-substring-no-properties (point)&lt;br /&gt;
                                                (line-end-position))))))&lt;br /&gt;
    (newline)&lt;br /&gt;
    (with-temp-message &amp;quot;Writing file...&amp;quot;&lt;br /&gt;
      (save-buffer))&lt;br /&gt;
    (message &amp;quot;Writing file...done&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Searching ===&lt;br /&gt;
&lt;br /&gt;
If you don't have grep, then you may need to write some Lisp which can find a match in a file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  ;; Visit file unless its already open.&lt;br /&gt;
  (with-current-buffer (find-file-noselect &amp;quot;~/.emacs&amp;quot;)&lt;br /&gt;
    (save-excursion ;; Don't change location of point.&lt;br /&gt;
      (goto-char (point-min)) ;; From the beginning...&lt;br /&gt;
      (if (re-search-forward &amp;quot;.*load-path.*&amp;quot; nil t 1)&lt;br /&gt;
          (match-string-no-properties 0)&lt;br /&gt;
        (error &amp;quot;Search failed&amp;quot;))))&lt;br /&gt;
  ==&amp;gt; &amp;quot;(add-to-list 'load-path \&amp;quot;/usr/share/emacs/site-lisp/\&amp;quot;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Filter ===&lt;br /&gt;
&lt;br /&gt;
=== Locking ===&lt;br /&gt;
&lt;br /&gt;
=== Stat ===&lt;br /&gt;
&lt;br /&gt;
An interface to the kernel's stat(2) is provided by the function file-attributes. The way times are represented may be a bit unexpected, though.&lt;br /&gt;
&lt;br /&gt;
=== Deleting ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (if (file-exists-p filename)&lt;br /&gt;
      (delete-file filename))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Copy, move and rename ===&lt;br /&gt;
&lt;br /&gt;
== Directories ==&lt;br /&gt;
&lt;br /&gt;
=== Traversing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (defun walk-path (dir action)&lt;br /&gt;
       &amp;quot;walk DIR executing ACTION with (dir file)&amp;quot;&lt;br /&gt;
       (cond ((file-directory-p dir)&lt;br /&gt;
              (or (char-equal ?/ (aref dir(1- (length dir))))&lt;br /&gt;
                  (setq dir (file-name-as-directory dir)))&lt;br /&gt;
              (let ((lst (directory-files dir nil nil t))&lt;br /&gt;
                     fullname file)&lt;br /&gt;
                (while lst&lt;br /&gt;
                  (setq file (car lst))&lt;br /&gt;
                  (setq lst (cdr lst))&lt;br /&gt;
                  (cond ((member file '(&amp;quot;.&amp;quot; &amp;quot;..&amp;quot;)))&lt;br /&gt;
                        (t&lt;br /&gt;
                         (and (funcall action dir file)&lt;br /&gt;
                              (setq fullname (concat dir file))&lt;br /&gt;
                              (file-directory-p fullname)&lt;br /&gt;
                              (walk-path fullname action)))))))&lt;br /&gt;
             (t&lt;br /&gt;
              (funcall action&lt;br /&gt;
                       (file-name-directory dir)&lt;br /&gt;
                       (file-name-nondirectory dir)))))&lt;br /&gt;
&lt;br /&gt;
    (defun walk-path-visitor (dir file)&lt;br /&gt;
       &amp;quot;Called by walk-path for each file found&amp;quot;&lt;br /&gt;
       (message (concat  dir file)))&lt;br /&gt;
&lt;br /&gt;
    (walk-path &amp;quot;~/&amp;quot; 'walk-path-visitor)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Path splitting ===&lt;br /&gt;
&lt;br /&gt;
Splitting the path can be done with `split-string' and with the slash. Previously, Emacs would determine the character separating directory names with `directory-sep-char'.  However, the variable is obselete with Emacs 21.1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(split-string default-directory &amp;quot;/&amp;quot;)&lt;br /&gt;
==&amp;gt; (&amp;quot;&amp;quot; &amp;quot;usr&amp;quot; &amp;quot;share&amp;quot; &amp;quot;emacs&amp;quot; &amp;quot;22.2&amp;quot; &amp;quot;lisp&amp;quot; &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For splitting a path variable, Emacs already has the `parse-colon-path' function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(parse-colon-path (getenv &amp;quot;PATH&amp;quot;))&lt;br /&gt;
==&amp;gt; (&amp;quot;/usr/lib/qt-3.3/bin/&amp;quot; &amp;quot;/usr/kerberos/bin/&amp;quot; &amp;quot;/usr/local/bin/&amp;quot;&lt;br /&gt;
&amp;quot;/usr/bin/&amp;quot; &amp;quot;/bin/&amp;quot; &amp;quot;/usr/local/sbin/&amp;quot; &amp;quot;/usr/sbin/&amp;quot; &amp;quot;/sbin/&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Processes ==&lt;br /&gt;
&lt;br /&gt;
=== Running a program ===&lt;br /&gt;
&lt;br /&gt;
Run a command without caring about its output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (async-shell-command &amp;quot;emacs&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Run a command and put its output in the current buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command &amp;quot;seq 8 12 | sort&amp;quot; t)&lt;br /&gt;
  10&lt;br /&gt;
  11&lt;br /&gt;
  12&lt;br /&gt;
  8&lt;br /&gt;
  9&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Run a command and put its output in a new buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command &amp;quot;seq 8 12 | sort&amp;quot;&lt;br /&gt;
                 (get-buffer-create &amp;quot;*Standard output*&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
Run a command return its output as a string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command-to-string &amp;quot;seq 8 12 | sort&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
XEmacs also comes with `exec-to-string'.&lt;br /&gt;
&lt;br /&gt;
=== Handling signals ===&lt;br /&gt;
&lt;br /&gt;
== Sockets ==&lt;br /&gt;
&lt;br /&gt;
=== Tcp client ===&lt;br /&gt;
&lt;br /&gt;
=== Tcp server ===&lt;br /&gt;
&lt;br /&gt;
Perhaps EmacsEchoServer and EmacsDaytimeServer can be useful here.&lt;br /&gt;
&lt;br /&gt;
== Keyboard events ==&lt;br /&gt;
&lt;br /&gt;
* Call function bound to key&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(funcall (key-binding (kbd &amp;quot;M-TAB&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(call-interactively (key-binding (kbd &amp;quot;M-TAB&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
[[Category:Customization]]&lt;br /&gt;
[[Category:Intermediate]]&lt;/div&gt;</summary>
		<author><name>187.41.158.248</name></author>
	</entry>
	<entry>
		<id>https://wikemacs.org/index.php?title=Emacs_Lisp_Cookbook&amp;diff=2617</id>
		<title>Emacs Lisp Cookbook</title>
		<link rel="alternate" type="text/html" href="https://wikemacs.org/index.php?title=Emacs_Lisp_Cookbook&amp;diff=2617"/>
		<updated>2012-04-07T14:27:40Z</updated>

		<summary type="html">&lt;p&gt;187.41.158.248: /* Trim whitespace */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page contains snippets of code that demonstrate basic [[Emacs Lisp]]&lt;br /&gt;
programming operations in the spirit of the O'Reilly Cookbook&lt;br /&gt;
series of books. For every task addressed, a worked-out solution&lt;br /&gt;
is presented as a short, focused, directly usable piece of code.&lt;br /&gt;
&lt;br /&gt;
All this stuff can be found elsewhere, but it is scattered about in&lt;br /&gt;
libraries, manuals, etc.  It would be helpful to have here in one&lt;br /&gt;
spot.&lt;br /&gt;
&lt;br /&gt;
These recipes should be pastable into the '''*scratch*''' buffer so that&lt;br /&gt;
users can hit '''C-j''' and evaluate them step by step.&lt;br /&gt;
&lt;br /&gt;
== Strings ==&lt;br /&gt;
&lt;br /&gt;
The empty string (zero-length string, null string, ...):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(zerop (string-match &amp;quot;&amp;quot; &amp;quot;&amp;quot;)) ;; O(n)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-equal &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(n)?&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(equal &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(n)?&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(zerop (length &amp;quot;&amp;quot;)) ;; O(1)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(eq &amp;quot;&amp;quot; &amp;quot;&amp;quot;) ;; O(1)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a space and performance optimization, Emacs keeps an intern-ed copy&lt;br /&gt;
of the empty string as a single object&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(eq &amp;quot;&amp;quot; (purecopy &amp;quot;&amp;quot;))&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(eq &amp;quot;&amp;quot; (propertize &amp;quot;&amp;quot; 'face 'italic))&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Strings vs buffer content ===&lt;br /&gt;
&lt;br /&gt;
While it is quite common in other programming languages to work on&lt;br /&gt;
strings contained in variables in Emacs it is even more idiomatic to&lt;br /&gt;
work on strings in buffers. That's why the following contains examples&lt;br /&gt;
of both.&lt;br /&gt;
&lt;br /&gt;
=== Substrings ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; 0 3)&lt;br /&gt;
==&amp;gt; &amp;quot;abc&amp;quot;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; -3 -1)&lt;br /&gt;
==&amp;gt; &amp;quot;ef&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The TO argument is optional:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(substring &amp;quot;abcdefg&amp;quot; -3)&lt;br /&gt;
==&amp;gt; &amp;quot;efg&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(with-temp-buffer&lt;br /&gt;
  (insert &amp;quot;abcdefg&amp;quot;)&lt;br /&gt;
  (buffer-substring 2 4))&lt;br /&gt;
==&amp;gt; &amp;quot;bc&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Processing characters ===&lt;br /&gt;
&lt;br /&gt;
Reversing a string:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(string-to-list &amp;quot;foo&amp;quot;)&lt;br /&gt;
==&amp;gt; (102 111 111)&lt;br /&gt;
(reverse (string-to-list &amp;quot;foo&amp;quot;))&lt;br /&gt;
==&amp;gt; (111 111 102)&lt;br /&gt;
(apply 'string (reverse (string-to-list &amp;quot;foo&amp;quot;)))&lt;br /&gt;
==&amp;gt; &amp;quot;oof&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See CharacterProcessing and StringModification. See [[tr]] for an example of you sometimes need to mix strings and characters.&lt;br /&gt;
&lt;br /&gt;
Looking at characters in buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(with-temp-buffer&lt;br /&gt;
  (insert &amp;quot;abcdefg&amp;quot;)&lt;br /&gt;
  (goto-char (point-min))&lt;br /&gt;
  (while (not (= (char-after) ?b))&lt;br /&gt;
    (forward-char))&lt;br /&gt;
  (point))&lt;br /&gt;
==&amp;gt; 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Trim whitespace ===&lt;br /&gt;
&lt;br /&gt;
Trim whitespace from the end of a string:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(setq test-str &amp;quot;abcdefg  &amp;quot;)&lt;br /&gt;
(when (string-match &amp;quot;[ \t]*$&amp;quot; test-str)&lt;br /&gt;
  (message (concat &amp;quot;[&amp;quot; (replace-match &amp;quot;&amp;quot; nil nil test-str) &amp;quot;]&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trim whitespace from a string with a Perl-like chomp function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun chomp (str)&lt;br /&gt;
  &amp;quot;Chomp leading and tailing whitespace from STR.&amp;quot;&lt;br /&gt;
  (while (string-match &amp;quot;\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'&amp;quot;&lt;br /&gt;
                       str)&lt;br /&gt;
    (setq str (replace-match &amp;quot;&amp;quot; t t str)))&lt;br /&gt;
  str)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Splitting strings ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== Joining strings ===&lt;br /&gt;
&lt;br /&gt;
Use `mapconcat' to join a list into a string using a separator (&amp;quot;glue&amp;quot;) between elements in the string.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(mapconcat 'identity '(&amp;quot;&amp;quot; &amp;quot;home&amp;quot; &amp;quot;alex &amp;quot; &amp;quot;elisp&amp;quot; &amp;quot;erc&amp;quot;) &amp;quot;/&amp;quot;)&lt;br /&gt;
==&amp;gt; &amp;quot;/home/alex /elisp/erc&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Serialization ===&lt;br /&gt;
&lt;br /&gt;
The basic idea is to convert forms to strings with `prin1-to-string' and convert it back from a string with `read'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(length (read (prin1-to-string (make-list 1000000 '(x)))))&lt;br /&gt;
==&amp;gt; 1000000&lt;br /&gt;
&lt;br /&gt;
(read (prin1-to-string &amp;quot;Hello World!&amp;quot;))&lt;br /&gt;
==&amp;gt; &amp;quot;Hello World!&amp;quot;&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This only works in the simplest cases.  Unfortunately, this doesn't work for all Emacs data types for programming or the editor.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(read (prin1-to-string (make-hash-table))) ;; Error before Emacs 23.&lt;br /&gt;
==&amp;gt; #s(hash-table size 65 test eql rehash-size 1.5 [...] data ())&lt;br /&gt;
&lt;br /&gt;
(read (prin1-to-string (current-buffer)))&lt;br /&gt;
==&amp;gt; Lisp error: (invalid-read-syntax &amp;quot;#&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Formatting ===&lt;br /&gt;
&lt;br /&gt;
== Killing text ==&lt;br /&gt;
&lt;br /&gt;
As the Emacs Lisp Manual says, &amp;quot;Most of the kill commands are primarily for&lt;br /&gt;
interactive use [...]  When you need to delete text for internal&lt;br /&gt;
purposes within a Lisp function, you should normally use deletion&lt;br /&gt;
functions, so as not to disturb the kill ring contents.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following mimic the `kill-' commands but without disturbing the kill ring.&lt;br /&gt;
&lt;br /&gt;
=== Delete region ===&lt;br /&gt;
&lt;br /&gt;
The Lisp equivalent of `kill-region' (`C-w') but without kill ring side effects::&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (region-beginning) (region-end))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
According to the ElispManual, &amp;quot;Few programs need to use the&lt;br /&gt;
`region-beginning' and `region-end' functions.&amp;quot;  This is because&lt;br /&gt;
Lisp code should not rely on nor &amp;quot;alter the mark unless altering&lt;br /&gt;
the mark is part of the user-level functionality of the&lt;br /&gt;
command.  (And, in that case, this effect should be documented.)&lt;br /&gt;
To remember a location for internal use in the Lisp program,&lt;br /&gt;
store it in a Lisp variable.  For example: [...]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Delete line ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-line' (`C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (forward-char -1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (forward-char -1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point) (line-end-position))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
The examples with `forward-line' are shown because the paradigm is used later, see below.&lt;br /&gt;
&lt;br /&gt;
=== Delete line backwards ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of killing the line backwards (`C-0 C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 0)&lt;br /&gt;
  (delete-region (point) beg))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (save-excursion&lt;br /&gt;
                 (forward-line 0)&lt;br /&gt;
                 (point))&lt;br /&gt;
               (point))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (line-beginning-position) (point))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Delete line to next line  ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of killing the line and the newline (`C-1 C-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
=== Delete whole line ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-whole-line' (`C-S-DEL') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (progn (forward-line 0)&lt;br /&gt;
                  (point))))&lt;br /&gt;
  (forward-line 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (save-excursion&lt;br /&gt;
                 (forward-line 0)&lt;br /&gt;
                 (point))&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-line 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
Or simplest of all,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (line-beginning-position) (line-end-position))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
=== Delete word ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-word' (`M-d') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-word 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(delete-region (point)&lt;br /&gt;
               (save-excursion&lt;br /&gt;
                 (forward-word 1)&lt;br /&gt;
                 (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                   &lt;br /&gt;
&lt;br /&gt;
=== Delete sentence ===&lt;br /&gt;
&lt;br /&gt;
The equivalent of `kill-sentence' (`M-k') but without kill ring side effects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(let ((beg (point)))&lt;br /&gt;
  (forward-sentence 1)&lt;br /&gt;
  (delete-region beg (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
Alternatively, replacing the `let' with `save-excursion'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (delete-region (point)&lt;br /&gt;
                 (save-excursion&lt;br /&gt;
                   (forward-sentence 1)&lt;br /&gt;
                   (point)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
                   &lt;br /&gt;
== Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== String a number? ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun string-integer-p (string)&lt;br /&gt;
  (if (string-match &amp;quot;\\`[-+]?[0-9]+\\'&amp;quot; string)&lt;br /&gt;
      t&lt;br /&gt;
    nil))&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;1234&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;x1234&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-integer-p &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(defun string-float-p (string)&lt;br /&gt;
  (if (string-match &amp;quot;\\`[-+]?[0-9]+\\.[0-9]*\\'&amp;quot; string)&lt;br /&gt;
      t&lt;br /&gt;
    nil))&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;1234&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;.1&amp;quot;)&lt;br /&gt;
==&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
(string-float-p &amp;quot;1.&amp;quot;)&lt;br /&gt;
==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
=== String to number ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun decimal-number (string)&lt;br /&gt;
  (let ((n (string-to-number string)))&lt;br /&gt;
    (if (and (zerop n)&lt;br /&gt;
             (not (string-match &amp;quot;\\`\\s-*0+\\.?0*\\s-*\\'&amp;quot; string)))&lt;br /&gt;
        nil&lt;br /&gt;
      n)))&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;536870911&amp;quot;)&lt;br /&gt;
==&amp;gt; 536870911&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;536870912&amp;quot;)&lt;br /&gt;
==&amp;gt; 536870912.0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;3.141592653589793&amp;quot;)&lt;br /&gt;
==&amp;gt; 3.141592653589793&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;042&amp;quot;)&lt;br /&gt;
==&amp;gt; 42&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot; 0 &amp;quot;)&lt;br /&gt;
==&amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;000&amp;quot;)&lt;br /&gt;
==&amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
(decimal-number &amp;quot;0.0&amp;quot;)&lt;br /&gt;
==&amp;gt; 0.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Random numbers ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (random 2)  ;coin toss (0 or 1)&lt;br /&gt;
  (+ (random 6) 1)  ;dice&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Put commas in numbers ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun group-number (num &amp;amp;optional size char)&lt;br /&gt;
    &amp;quot;Format NUM as string grouped to SIZE with CHAR.&amp;quot;&lt;br /&gt;
    ;; Based on code for `math-group-float' in calc-ext.el&lt;br /&gt;
    (let* ((size (or size 3))&lt;br /&gt;
           (char (or char &amp;quot;,&amp;quot;))&lt;br /&gt;
           (str (if (stringp num)&lt;br /&gt;
                    num&lt;br /&gt;
                  (number-to-string num)))&lt;br /&gt;
           (pt (or (string-match &amp;quot;[^0-9a-zA-Z]&amp;quot; str) (length str))))&lt;br /&gt;
      (while (&amp;gt; pt size)&lt;br /&gt;
        (setq str (concat (substring str 0 (- pt size))&lt;br /&gt;
                          char&lt;br /&gt;
                          (substring str (- pt size)))&lt;br /&gt;
              pt (- pt size)))&lt;br /&gt;
      str))&lt;br /&gt;
&lt;br /&gt;
  (group-number 299792458)&lt;br /&gt;
  ==&amp;gt; &amp;quot;299,792,458&amp;quot;&lt;br /&gt;
  (group-number &amp;quot;149597870691&amp;quot; 4 &amp;quot; &amp;quot;)&lt;br /&gt;
  ==&amp;gt; &amp;quot;1495 9787 0691&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Incrementing numbers ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
== Dates and times ==&lt;br /&gt;
&lt;br /&gt;
=== Get today's date ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== Formatting dates ===&lt;br /&gt;
&lt;br /&gt;
Use the function `format-time-string' which is a build in function in both Emacsen and works like `strftime':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    ;; Year-Month-Day:&lt;br /&gt;
    (insert (format-time-string &amp;quot;%Y-%m-%d&amp;quot;))&lt;br /&gt;
    ;; Hour:Minutes:Seconds&lt;br /&gt;
    (insert (format-time-string &amp;quot;%H-%M-%S&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Conversions ===&lt;br /&gt;
&lt;br /&gt;
Read a date from a string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((time (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;)))&lt;br /&gt;
    (set-time-zone-rule t) ;; Use Universal time.&lt;br /&gt;
    (prog1 (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; time)&lt;br /&gt;
      (set-time-zone-rule nil))) ;; Reset to default time zone.&lt;br /&gt;
  ==&amp;gt; &amp;quot;1983-09-27 17:35:59 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Decode a time object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (decode-time (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;))&lt;br /&gt;
  ==&amp;gt; (59 35 13 27 9 1983 2 t -14400)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Get the seconds from the unix epoch.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((time (date-to-time &amp;quot;13 Feb 2009 23:31:30 UTC&amp;quot;)))&lt;br /&gt;
    (float-time time))&lt;br /&gt;
  ==&amp;gt; 1234585890.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the date for seconds from the unix epoch.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; (seconds-to-time 1234585890))&lt;br /&gt;
  ==&amp;gt; &amp;quot;2009-02-13 23:31:30 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the date 30 seconds in the future.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%Y-%m-%d %T UTC&amp;quot; (time-add (current-time)&lt;br /&gt;
                                                  (seconds-to-time 30)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;2012-02-13 10:07:11 UTC&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Formatting elapsed time in years, days, hours, minutes and seconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-seconds &amp;quot;%Y %D %h:%m:%s&amp;quot; (1- (* 367 24 3600)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;1 year 1 day 23:59:59&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Find the days between two dates.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((days1 (time-to-days (date-to-time &amp;quot;Tue, 27-Sep-83 12:35:59 EST&amp;quot;)))&lt;br /&gt;
        (days2 (time-to-days (date-to-time &amp;quot;2009-02-13 23:31:30 UTC&amp;quot;))))&lt;br /&gt;
    (- days2 days1))&lt;br /&gt;
  ==&amp;gt; 9271&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Getting the day in the year.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (time-to-day-in-year (current-time))&lt;br /&gt;
  ==&amp;gt; 44&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Build a date based on the day of the year.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (format-time-string &amp;quot;%j&amp;quot;&lt;br /&gt;
                      (encode-time 0 0 0 44 1 2012))&lt;br /&gt;
  ==&amp;gt; &amp;quot;044&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Timers ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
== Pattern matching ==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Patterns&amp;quot; refers to RegularExpression&amp;quot;&amp;quot;s.&lt;br /&gt;
&lt;br /&gt;
There's a set of functions that work in strings, and a set that work in buffers.&lt;br /&gt;
&lt;br /&gt;
=== Finding ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (string-match &amp;quot;foo*&amp;quot; &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
    ==&amp;gt; 6&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
      (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
      (goto-char (point-min))&lt;br /&gt;
      (re-search-forward &amp;quot;foo*&amp;quot;)&lt;br /&gt;
      (point))&lt;br /&gt;
    ==&amp;gt; 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Alternative without regular expressions: `search-forward'.&lt;br /&gt;
&lt;br /&gt;
Note that the functions working on buffers move point to the end of the occurrence found and return it.&lt;br /&gt;
That's why the result is 10 instead of 6!&lt;br /&gt;
&lt;br /&gt;
=== Comments ===&lt;br /&gt;
&lt;br /&gt;
Move to the beginning of the current comment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (require 'newcomment)&lt;br /&gt;
    (comment-beginning)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Move to the text after a comment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (comment-search-forward (line-end-position) t)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
See also EndOfLineNoComments.&lt;br /&gt;
&lt;br /&gt;
=== Search and replace ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (replace-regexp-in-string &amp;quot;foo*&amp;quot; &amp;quot;fu&amp;quot; &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
    ==&amp;gt; &amp;quot;Fight fu fur fud!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Buffers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
       (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
       (goto-char (point-min))&lt;br /&gt;
       (while (re-search-forward &amp;quot;foo*&amp;quot; nil t)&lt;br /&gt;
         (replace-match &amp;quot;fu&amp;quot;))&lt;br /&gt;
       (buffer-string))&lt;br /&gt;
    ==&amp;gt; &amp;quot;Fight fu fur fud!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Alternative without regular expressions: `search-forward'.&lt;br /&gt;
&lt;br /&gt;
See also StringSearchAndReplace.&lt;br /&gt;
&lt;br /&gt;
=== Verifying ===&lt;br /&gt;
&lt;br /&gt;
Sometimes you just want to check whether you're at the right place:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (with-temp-buffer&lt;br /&gt;
      (insert &amp;quot;Fight foo for food!&amp;quot;)&lt;br /&gt;
      (goto-char (point-min))&lt;br /&gt;
      (looking-at &amp;quot;fight&amp;quot;))&lt;br /&gt;
    ==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
== Sequences ==&lt;br /&gt;
&lt;br /&gt;
Datatypes used to represent sequences of things:&lt;br /&gt;
&lt;br /&gt;
     _____________________________________________&lt;br /&gt;
    |                                             |&lt;br /&gt;
    |          Sequence                           |&lt;br /&gt;
    |  ______   ________________________________  |&lt;br /&gt;
    | |      | |                                | |&lt;br /&gt;
    | | List | |             Array              | |&lt;br /&gt;
    | |      | |    ________       ________     | |&lt;br /&gt;
    | |______| |   |        |     |        |    | |&lt;br /&gt;
    |          |   | Vector |     | String |    | |&lt;br /&gt;
    |          |   |________|     |________|    | |&lt;br /&gt;
    |          |  ____________   _____________  | |&lt;br /&gt;
    |          | |            | |             | | |&lt;br /&gt;
    |          | | Char-table | | Bool-vector | | |&lt;br /&gt;
    |          | |____________| |_____________| | |&lt;br /&gt;
    |          |________________________________| |&lt;br /&gt;
    |_____________________________________________|&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Lists ===&lt;br /&gt;
&lt;br /&gt;
List basics are explained on ListStructure.&lt;br /&gt;
Lists can shrink and grow, but access to elements towards the end of the list is slow if the list is long.&lt;br /&gt;
&lt;br /&gt;
Use `cons' to append a new element to the front of a list.&lt;br /&gt;
Use `nth' to access an element of the list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
      (when (string= &amp;quot;foo&amp;quot; (nth 1 words))&lt;br /&gt;
        (setq words (cons &amp;quot;bar&amp;quot; words)))&lt;br /&gt;
      words)&lt;br /&gt;
    ==&amp;gt; (&amp;quot;bar&amp;quot; &amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
See ListModification for more ways of changing a list.&lt;br /&gt;
&lt;br /&gt;
Iteration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((result))&lt;br /&gt;
      (dolist (word '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;))&lt;br /&gt;
        (when (string-match &amp;quot;o&amp;quot; word)&lt;br /&gt;
          (setq result (cons word result))))&lt;br /&gt;
      (nreverse result))&lt;br /&gt;
    ==&amp;gt; (&amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Note how `cons' adds an element to the front of the list,&lt;br /&gt;
so that usually the list has to be reversed after the loop.&lt;br /&gt;
`nreverse' is particularly efficient because it does this&lt;br /&gt;
destructively by swiveling pointers around. See&lt;br /&gt;
DestructiveOperations for more about this.&lt;br /&gt;
&lt;br /&gt;
Copying:&lt;br /&gt;
&lt;br /&gt;
Use `copy-sequence' to make a copy of a list that won't change the&lt;br /&gt;
elements of the original.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-sequence orig)))&lt;br /&gt;
      (setcdr copy '((5 6)))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 4)) ((1 2) (5 6)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
However, the elements in the copy are still from the original.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-sequence orig)))&lt;br /&gt;
      (setcdr (cadr copy) '(0))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 0)) ((1 2) (3 0)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
The function `copy-tree' is the recursive version of `copy-sequence'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let* ((orig '((1 2) (3 4)))&lt;br /&gt;
           (copy (copy-tree orig)))&lt;br /&gt;
      (setcdr (cadr copy) '(0))&lt;br /&gt;
      (list orig copy))&lt;br /&gt;
    ==&amp;gt; (((1 2) (3 4)) ((1 2) (3 0)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Filtering:&lt;br /&gt;
&lt;br /&gt;
Emacs Lisp doesn't come with a `filter' function to keep elements that satisfy a conditional and excise the elements that do not satisfy it.  One can use `mapcar' to iterate over a list with a conditional, and then use `delq' to remove the `nil' values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun my-filter (condp lst)&lt;br /&gt;
    (delq nil&lt;br /&gt;
          (mapcar (lambda (x) (and (funcall condp x) x)) lst)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;          &lt;br /&gt;
&lt;br /&gt;
Therefore,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (my-filter 'identity my-list)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (delq nil my-list)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (my-filter 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; (1 2 3 4)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Actually the package cl-seq contains the functions `remove-if' and `remove-if-not'.  The latter can be used instead of `my-filter'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (remove-if-not 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; (1 2 3 4)&lt;br /&gt;
&lt;br /&gt;
  (let ((num-list '(1 'a 2 &amp;quot;nil&amp;quot; 3 nil 4)))&lt;br /&gt;
    (remove-if 'numberp num-list))&lt;br /&gt;
  ==&amp;gt; ((quote a) &amp;quot;nil&amp;quot; nil)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
As an example here is the quick sort algorithm:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun quicksort (lst)&lt;br /&gt;
    &amp;quot;Implement the quicksort algorithm.&amp;quot;&lt;br /&gt;
    (if (null lst) nil&lt;br /&gt;
      (let* ((spl (car lst))&lt;br /&gt;
             (rst (cdr lst))&lt;br /&gt;
             (smalp (lambda (x)&lt;br /&gt;
                   (&amp;lt; x spl))))&lt;br /&gt;
        (append (quicksort (remove-if-not smalp rst))&lt;br /&gt;
                (list spl)&lt;br /&gt;
                (quicksort (remove-if smalp rst))))))&lt;br /&gt;
&lt;br /&gt;
  (quicksort '(5 7 1 3 -9 8 7 -4 0))&lt;br /&gt;
  ==&amp;gt; (-9 -4 0 1 3 5 7 7 8)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Tranposing:&lt;br /&gt;
&lt;br /&gt;
Convert multiple lists into a list &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 ((lambda (&amp;amp;rest args)&lt;br /&gt;
    (mapcar (lambda (n)&lt;br /&gt;
              (delq nil (mapcar (lambda (arg) (nth n arg)) args)))&lt;br /&gt;
            (number-sequence 0 (1- (apply 'max (mapcar 'length args))))))&lt;br /&gt;
  '(1 2 3) '(a b c) '(A B C))&lt;br /&gt;
  ==&amp;gt; ((1 a A) (2 b B) (3 c C))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
A more concise version is possible with the the higher-arity version of mapcar available with the `cl' library.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  ((lambda (&amp;amp;rest args)&lt;br /&gt;
     (apply (function mapcar*) (function list) args))&lt;br /&gt;
   '(1 2 3) '(a b c) '(A B C))&lt;br /&gt;
  ==&amp;gt; ((1 a A) (2 b B) (3 c C))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Searching:&lt;br /&gt;
&lt;br /&gt;
Simply checking for existence of a value in a list can be done with&lt;br /&gt;
`member' or `memq'.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
    (car (member &amp;quot;for&amp;quot; words)))&lt;br /&gt;
  ==&amp;gt; &amp;quot;for&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  (let ((re &amp;quot;\\wo\\b&amp;quot;)&lt;br /&gt;
        (words '(&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;)))&lt;br /&gt;
    (consp (memq t&lt;br /&gt;
             (mapcar (lambda (s) (numberp (string-match re s))) words))))&lt;br /&gt;
  ==&amp;gt; t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
In the latter, a more efficient algorithm would use a loop (a non-local exit).&lt;br /&gt;
&lt;br /&gt;
=== Association lists ===&lt;br /&gt;
&lt;br /&gt;
The ElispManual has examples of finding and deleting values in an&lt;br /&gt;
association list.  Here are cases when the car values are strings.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (assoc &amp;quot;2&amp;quot; '((&amp;quot;2&amp;quot; . 2) (&amp;quot;1&amp;quot; . 1) (&amp;quot;2&amp;quot;) (&amp;quot;3&amp;quot; . 3)))&lt;br /&gt;
  ==&amp;gt; (&amp;quot;2&amp;quot; . 2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Deleting:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((alist '((&amp;quot;a&amp;quot; . 1) (&amp;quot;b&amp;quot; . 2))))&lt;br /&gt;
    (delq (assoc &amp;quot;a&amp;quot; alist) alist))&lt;br /&gt;
  ==&amp;gt; ((&amp;quot;b&amp;quot; . 2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Matches with a test function other than `equal':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (let ((alist '((&amp;quot;ab&amp;quot; . 1) (&amp;quot;bc&amp;quot; . 2) (&amp;quot;cd&amp;quot; . 3))))&lt;br /&gt;
    (assoc-default &amp;quot;c&amp;quot; alist (lambda (x y) (string-match y x))))&lt;br /&gt;
  ==&amp;gt; 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Vectors ===&lt;br /&gt;
&lt;br /&gt;
Vectors are fixed in size but elements can be accessed in constant time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((words [&amp;quot;fight&amp;quot; &amp;quot;foo&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;]))&lt;br /&gt;
      (when (string= &amp;quot;foo&amp;quot; (aref words 1))&lt;br /&gt;
        (aset words 1 &amp;quot;bar&amp;quot;))&lt;br /&gt;
      words)&lt;br /&gt;
    ==&amp;gt; [&amp;quot;fight&amp;quot; &amp;quot;bar&amp;quot; &amp;quot;for&amp;quot; &amp;quot;food!&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
== Hashes ==&lt;br /&gt;
&lt;br /&gt;
Hashes map keys to values. In a way they are similar to alists, except&lt;br /&gt;
they are more efficient for a large number of keys.&lt;br /&gt;
&lt;br /&gt;
More info is available on the HashMap page.&lt;br /&gt;
&lt;br /&gt;
=== Storing and retrieving keys and values ===&lt;br /&gt;
&lt;br /&gt;
By default, hash tables use `eql' to compare keys. This is not appropriate&lt;br /&gt;
for strings: ##(eql &amp;quot;alex&amp;quot; &amp;quot;alex&amp;quot;)## ==&amp;gt; nil. Thus, use `equal' in these cases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal)))&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (gethash &amp;quot;kensanata&amp;quot; nick-table))&lt;br /&gt;
    ==&amp;gt; &amp;quot;Alex Schroeder&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
Iterate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal))&lt;br /&gt;
          nicks)&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;e1f&amp;quot; &amp;quot;Luis Fernandes&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;pjb&amp;quot; &amp;quot;Pascal J. Bourguignon&amp;quot; nick-table)&lt;br /&gt;
      (maphash (lambda (nick real-name)&lt;br /&gt;
                 (setq nicks (cons nick nicks)))&lt;br /&gt;
               nick-table)&lt;br /&gt;
      nicks)&lt;br /&gt;
      ==&amp;gt; (&amp;quot;pjb&amp;quot; &amp;quot;e1f&amp;quot; &amp;quot;kensanata&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Sorting keys  ===&lt;br /&gt;
&lt;br /&gt;
Use `maphash' to build up a list of keys, sort it, and then loop through&lt;br /&gt;
the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (let ((nick-table (make-hash-table :test 'equal))&lt;br /&gt;
          nicks)&lt;br /&gt;
      (puthash &amp;quot;kensanata&amp;quot; &amp;quot;Alex Schroeder&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;e1f&amp;quot; &amp;quot;Luis Fernandes&amp;quot; nick-table)&lt;br /&gt;
      (puthash &amp;quot;pjb&amp;quot; &amp;quot;Pascal J. Bourguignon&amp;quot; nick-table)&lt;br /&gt;
      (maphash (lambda (nick real-name)&lt;br /&gt;
                 (setq nicks (cons nick nicks)))&lt;br /&gt;
               nick-table)&lt;br /&gt;
      (mapcar (lambda (nick)&lt;br /&gt;
                (concat nick &amp;quot; =&amp;gt; &amp;quot; (gethash nick nick-table)))&lt;br /&gt;
              (sort nicks 'string&amp;lt;)))&lt;br /&gt;
      ==&amp;gt; (&amp;quot;e1f =&amp;gt; Luis Fernandes&amp;quot;&lt;br /&gt;
           &amp;quot;kensanata =&amp;gt; Alex Schroeder&amp;quot;&lt;br /&gt;
           &amp;quot;pjb =&amp;gt; Pascal J. Bourguignon&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;           &lt;br /&gt;
&lt;br /&gt;
== Files ==&lt;br /&gt;
&lt;br /&gt;
=== Read ===&lt;br /&gt;
&lt;br /&gt;
Processing a file is usually done with a temporary buffer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 (defun process-file (file)&lt;br /&gt;
   &amp;quot;Read the contents of a file into a temp buffer and then do&lt;br /&gt;
 something there.&amp;quot;&lt;br /&gt;
   (when (file-readable-p file)&lt;br /&gt;
     (with-temp-buffer&lt;br /&gt;
       (insert-file-contents file)&lt;br /&gt;
       (goto-char (point-min))&lt;br /&gt;
       (while (not (eobp))&lt;br /&gt;
       ;; do something here with buffer content&lt;br /&gt;
         (forward-line)))))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;         &lt;br /&gt;
&lt;br /&gt;
On the chance that a buffer may already be actively visiting the file,&lt;br /&gt;
consider using `find-file-noselect'&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (defun file-string (file)&lt;br /&gt;
    &amp;quot;Read the contents of a file and return as a string.&amp;quot;&lt;br /&gt;
    (with-current-buffer (find-file-noselect file)&lt;br /&gt;
      (buffer-string)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Write ===&lt;br /&gt;
&lt;br /&gt;
To write something to a file you can create a temporary buffer, insert the things to write there and write the buffer contents to a file.  The following example read a string and a filename (with completion, but doesn't need to exist, see InteractiveCodeChar F) and write the string to that file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
 (defun write-string-to-file (string file)&lt;br /&gt;
   (interactive &amp;quot;sEnter the string: \nFFile to save to: &amp;quot;)&lt;br /&gt;
   (with-temp-buffer&lt;br /&gt;
     (insert string)&lt;br /&gt;
     (when (file-writable-p file)&lt;br /&gt;
       (write-region (point-min)&lt;br /&gt;
                     (point-max)&lt;br /&gt;
                     file))))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                     &lt;br /&gt;
&lt;br /&gt;
=== Input and output (I/O) ===&lt;br /&gt;
&lt;br /&gt;
This is a nice and simple way to edit a file with Emacs Lisp,&lt;br /&gt;
especially when there's a chance the file might be already be visited&lt;br /&gt;
in Emacs and its ok to edit the existing buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (with-current-buffer (find-file-noselect &amp;quot;~/logfile&amp;quot;)&lt;br /&gt;
    (goto-char (point-max))&lt;br /&gt;
    (insert (format &amp;quot;Hash of last line: %s&amp;quot;&lt;br /&gt;
             (md5&lt;br /&gt;
              (save-excursion&lt;br /&gt;
                (forward-line -1)&lt;br /&gt;
                (buffer-substring-no-properties (point)&lt;br /&gt;
                                                (line-end-position))))))&lt;br /&gt;
    (newline)&lt;br /&gt;
    (with-temp-message &amp;quot;Writing file...&amp;quot;&lt;br /&gt;
      (save-buffer))&lt;br /&gt;
    (message &amp;quot;Writing file...done&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Searching ===&lt;br /&gt;
&lt;br /&gt;
If you don't have grep, then you may need to write some Lisp which can find a match in a file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  ;; Visit file unless its already open.&lt;br /&gt;
  (with-current-buffer (find-file-noselect &amp;quot;~/.emacs&amp;quot;)&lt;br /&gt;
    (save-excursion ;; Don't change location of point.&lt;br /&gt;
      (goto-char (point-min)) ;; From the beginning...&lt;br /&gt;
      (if (re-search-forward &amp;quot;.*load-path.*&amp;quot; nil t 1)&lt;br /&gt;
          (match-string-no-properties 0)&lt;br /&gt;
        (error &amp;quot;Search failed&amp;quot;))))&lt;br /&gt;
  ==&amp;gt; &amp;quot;(add-to-list 'load-path \&amp;quot;/usr/share/emacs/site-lisp/\&amp;quot;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
=== Filter ===&lt;br /&gt;
&lt;br /&gt;
=== Locking ===&lt;br /&gt;
&lt;br /&gt;
=== Stat ===&lt;br /&gt;
&lt;br /&gt;
An interface to the kernel's stat(2) is provided by the function file-attributes. The way times are represented may be a bit unexpected, though.&lt;br /&gt;
&lt;br /&gt;
=== Deleting ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (if (file-exists-p filename)&lt;br /&gt;
      (delete-file filename))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;      &lt;br /&gt;
&lt;br /&gt;
=== Copy, move and rename ===&lt;br /&gt;
&lt;br /&gt;
== Directories ==&lt;br /&gt;
&lt;br /&gt;
=== Traversing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
    (defun walk-path (dir action)&lt;br /&gt;
       &amp;quot;walk DIR executing ACTION with (dir file)&amp;quot;&lt;br /&gt;
       (cond ((file-directory-p dir)&lt;br /&gt;
              (or (char-equal ?/ (aref dir(1- (length dir))))&lt;br /&gt;
                  (setq dir (file-name-as-directory dir)))&lt;br /&gt;
              (let ((lst (directory-files dir nil nil t))&lt;br /&gt;
                     fullname file)&lt;br /&gt;
                (while lst&lt;br /&gt;
                  (setq file (car lst))&lt;br /&gt;
                  (setq lst (cdr lst))&lt;br /&gt;
                  (cond ((member file '(&amp;quot;.&amp;quot; &amp;quot;..&amp;quot;)))&lt;br /&gt;
                        (t&lt;br /&gt;
                         (and (funcall action dir file)&lt;br /&gt;
                              (setq fullname (concat dir file))&lt;br /&gt;
                              (file-directory-p fullname)&lt;br /&gt;
                              (walk-path fullname action)))))))&lt;br /&gt;
             (t&lt;br /&gt;
              (funcall action&lt;br /&gt;
                       (file-name-directory dir)&lt;br /&gt;
                       (file-name-nondirectory dir)))))&lt;br /&gt;
&lt;br /&gt;
    (defun walk-path-visitor (dir file)&lt;br /&gt;
       &amp;quot;Called by walk-path for each file found&amp;quot;&lt;br /&gt;
       (message (concat  dir file)))&lt;br /&gt;
&lt;br /&gt;
    (walk-path &amp;quot;~/&amp;quot; 'walk-path-visitor)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;    &lt;br /&gt;
&lt;br /&gt;
=== Path splitting ===&lt;br /&gt;
&lt;br /&gt;
Splitting the path can be done with `split-string' and with the slash. Previously, Emacs would determine the character separating directory names with `directory-sep-char'.  However, the variable is obselete with Emacs 21.1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(split-string default-directory &amp;quot;/&amp;quot;)&lt;br /&gt;
==&amp;gt; (&amp;quot;&amp;quot; &amp;quot;usr&amp;quot; &amp;quot;share&amp;quot; &amp;quot;emacs&amp;quot; &amp;quot;22.2&amp;quot; &amp;quot;lisp&amp;quot; &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For splitting a path variable, Emacs already has the `parse-colon-path' function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(parse-colon-path (getenv &amp;quot;PATH&amp;quot;))&lt;br /&gt;
==&amp;gt; (&amp;quot;/usr/lib/qt-3.3/bin/&amp;quot; &amp;quot;/usr/kerberos/bin/&amp;quot; &amp;quot;/usr/local/bin/&amp;quot;&lt;br /&gt;
&amp;quot;/usr/bin/&amp;quot; &amp;quot;/bin/&amp;quot; &amp;quot;/usr/local/sbin/&amp;quot; &amp;quot;/usr/sbin/&amp;quot; &amp;quot;/sbin/&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Processes ==&lt;br /&gt;
&lt;br /&gt;
=== Running a program ===&lt;br /&gt;
&lt;br /&gt;
Run a command without caring about its output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (async-shell-command &amp;quot;emacs&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Run a command and put its output in the current buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command &amp;quot;seq 8 12 | sort&amp;quot; t)&lt;br /&gt;
  10&lt;br /&gt;
  11&lt;br /&gt;
  12&lt;br /&gt;
  8&lt;br /&gt;
  9&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Run a command and put its output in a new buffer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command &amp;quot;seq 8 12 | sort&amp;quot;&lt;br /&gt;
                 (get-buffer-create &amp;quot;*Standard output*&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;                 &lt;br /&gt;
&lt;br /&gt;
Run a command return its output as a string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
  (shell-command-to-string &amp;quot;seq 8 12 | sort&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
XEmacs also comes with `exec-to-string'.&lt;br /&gt;
&lt;br /&gt;
=== Handling signals ===&lt;br /&gt;
&lt;br /&gt;
== Sockets ==&lt;br /&gt;
&lt;br /&gt;
=== Tcp client ===&lt;br /&gt;
&lt;br /&gt;
=== Tcp server ===&lt;br /&gt;
&lt;br /&gt;
Perhaps EmacsEchoServer and EmacsDaytimeServer can be useful here.&lt;br /&gt;
&lt;br /&gt;
== Keyboard events ==&lt;br /&gt;
&lt;br /&gt;
* Call function bound to key&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(funcall (key-binding (kbd &amp;quot;M-TAB&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(call-interactively (key-binding (kbd &amp;quot;M-TAB&amp;quot;)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
[[Category:Customization]]&lt;br /&gt;
[[Category:Intermediate]]&lt;/div&gt;</summary>
		<author><name>187.41.158.248</name></author>
	</entry>
</feed>