https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&feed=atom&action=history
Emacs Lisp in 15 minutes - Revision history
2024-03-28T17:38:56Z
Revision history for this page on the wiki
MediaWiki 1.35.14
https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&diff=47920&oldid=prev
Elvince: B. Guery, org maintainer
2017-05-18T20:48:26Z
<p>B. Guery, org maintainer</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 20:48, 18 May 2017</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l2" >Line 2:</td>
<td colspan="2" class="diff-lineno">Line 2:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This gives an introduction to Emacs Lisp in 15 minutes (v0.2d)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This gives an introduction to Emacs Lisp in 15 minutes (v0.2d)</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Author: Bastien / @bzg2 / http://bzg.fr</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Author: Bastien / @bzg2 / http://bzg.fr <ins class="diffchange diffchange-inline">(the [[Org]] maintainer for many years)</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>First make sure you read this text by Peter Norvig:</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>First make sure you read this text by Peter Norvig:</div></td></tr>
</table>
Elvince
https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&diff=46835&oldid=prev
166.200.168.217: Commented the intended lines of hello-to-bonjour ()
2014-10-28T04:49:38Z
<p>Commented the intended lines of hello-to-bonjour ()</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 04:49, 28 October 2014</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l392" >Line 392:</td>
<td colspan="2" class="diff-lineno">Line 392:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (switch-to-buffer-other-window "*test*")</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (switch-to-buffer-other-window "*test*")</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (erase-buffer)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (erase-buffer)</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> Say hello to names in `list-of-names'</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins class="diffchange diffchange-inline">;; </ins>Say hello to names in `list-of-names'</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (mapcar 'hello list-of-names)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (mapcar 'hello list-of-names)</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (goto-char (point-min))</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (goto-char (point-min))</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> Replace "Hello" by "Bonjour"</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins class="diffchange diffchange-inline">;; </ins>Replace "Hello" by "Bonjour"</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (while (search-forward "Hello" nil t)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (while (search-forward "Hello" nil t)</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (replace-match "Bonjour"))</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> (replace-match "Bonjour"))</div></td></tr>
</table>
166.200.168.217
https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&diff=46628&oldid=prev
Holocronweaver: correct spelling error
2014-04-17T02:27:40Z
<p>correct spelling error</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 02:27, 17 April 2014</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l9" >Line 9:</td>
<td colspan="2" class="diff-lineno">Line 9:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Going through this tutorial won't damage your computer unless</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Going through this tutorial won't damage your computer unless</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>you get so angry that you throw it on the floor. In that case,</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>you get so angry that you throw it on the floor. In that case,</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>I hereby decline any <del class="diffchange diffchange-inline">responsability</del>. Have fun!</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>I hereby decline any <ins class="diffchange diffchange-inline">responsibility</ins>. Have fun!</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
</table>
Holocronweaver
https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&diff=46625&oldid=prev
Elvince: cats
2014-04-16T10:54:25Z
<p>cats</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 10:54, 16 April 2014</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l465" >Line 465:</td>
<td colspan="2" class="diff-lineno">Line 465:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* LinXitoW</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* LinXitoW</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* Aaron Meurer</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* Aaron Meurer</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Customization]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Intermediate]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Emacs Lisp]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Lisp]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Programming]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Tutorial]]</ins></div></td></tr>
</table>
Elvince
https://wikemacs.org/index.php?title=Emacs_Lisp_in_15_minutes&diff=46624&oldid=prev
Elvince: elisp in 15 min
2014-04-16T10:53:42Z
<p>elisp in 15 min</p>
<p><b>New page</b></p><div><br />
This gives an introduction to Emacs Lisp in 15 minutes (v0.2d)<br />
<br />
Author: Bastien / @bzg2 / http://bzg.fr<br />
<br />
First make sure you read this text by Peter Norvig:<br />
http://norvig.com/21-days.html<br />
<br />
Going through this tutorial won't damage your computer unless<br />
you get so angry that you throw it on the floor. In that case,<br />
I hereby decline any responsability. Have fun!<br />
<br />
<br />
=== Write and execute lisp ===<br />
<br />
Fire up Emacs.<br />
<br />
Hit the '''q''' key to dismiss the welcome message.<br />
<br />
Now look at the gray line at the bottom of the window:<br />
<br />
"*scratch*" is the name of the editing space you are now in.<br />
This editing space is called a "buffer".<br />
<br />
The scratch buffer is the default buffer when opening Emacs.<br />
You are never editing files: you are editing buffers that you<br />
can save to a file.<br />
<br />
"Lisp interaction" refers to a set of commands available here.<br />
<br />
Emacs has a built-in set of commands available in every buffer,<br />
and several subsets of commands available when you activate a<br />
specific mode. Here we use the `lisp-interaction-mode', which<br />
comes with commands to evaluate and navigate within Elisp code.<br />
<br />
<br />
Semi-colons start comments anywhere on a line.<br />
<br />
Elisp programs are made of symbolic expressions ("sexps"):<br />
<br />
<source lang=lisp><br />
(+ 2 2) ;; this is a comment<br />
</source><br />
<br />
This symbolic expression reads as "Add 2 to 2".<br />
<br />
Sexps are enclosed into parentheses, possibly nested:<br />
<br />
<source lang=lisp><br />
(+ 2 (+ 1 1))<br />
</source><br />
<br />
A symbolic expression contains atoms or other symbolic<br />
expressions. In the above examples, 1 and 2 are atoms,<br />
(+ 2 (+ 1 1)) and (+ 1 1) are symbolic expressions.<br />
<br />
From `lisp-interaction-mode' you can evaluate sexps.<br />
Put the cursor right after the closing parenthesis then<br />
hold down the control and hit the j keys ("C-j" for short).<br />
<br />
<br />
<source lang=lisp><br />
(+ 3 (+ 1 2))<br />
</source><br />
{{Keys | C-j }} => 6<br />
<br />
{{Keys | C-j }} inserts the result of the evaluation in the buffer.<br />
<br />
{{Keys | C-xC-e }} displays the same result in Emacs bottom line,<br />
called the "minibuffer". We will generally use `C-xC-e',<br />
as we don't want to clutter the buffer with useless text.<br />
<br />
=== Variables ===<br />
<br />
'''setq''' stores a value into a variable:<br />
<br />
<source lang=lisp><br />
(setq my-name "Bastien")<br />
</source><br />
{{Keys | C-xC-e }} => "Bastien" (displayed in the mini-buffer)<br />
<br />
=== Insert text ===<br />
<br />
'''insert''' will insert "Hello!" where the cursor is:<br />
<br />
<source lang=lisp><br />
(insert "Hello!")<br />
</source><br />
{{Keys | C-xC-e }} => "Hello!"<br />
<br />
We used '''insert''' with only one argument "Hello!", but<br />
we can pass more arguments -- here we use two:<br />
<br />
<br />
<source lang=lisp><br />
(insert "Hello" " world!")<br />
</source><br />
{{Keys | C-xC-e }} => "Hello world!"<br />
<br />
You can use variables instead of strings:<br />
<br />
<source lang=lisp><br />
(insert "Hello, I am " my-name)<br />
</source><br />
{{Keys | C-xC-e }} => "Hello, I am Bastien"<br />
<br />
=== Functions ===<br />
<br />
You can combine sexps into functions:<br />
<br />
<source lang=lisp><br />
(defun hello () (insert "Hello, I am " my-name))<br />
</source><br />
{{Keys | C-xC-e }} => hello<br />
<br />
You can evaluate functions:<br />
<br />
<source lang=lisp><br />
(hello)<br />
</source><br />
{{Keys | C-xC-e }} => Hello, I am Bastien<br />
<br />
The empty parentheses in the function's definition means that<br />
it does not accept arguments. But always using `my-name' is<br />
boring, let's tell the function to accept one argument (here<br />
the argument is called "name"): <br />
<br />
<br />
<source lang=lisp><br />
(defun hello (name) (insert "Hello " name))<br />
</source><br />
{{Keys | C-xC-e }} => hello<br />
<br />
Now let's call the function with the string "you" as the value<br />
for its unique argument:<br />
<br />
<source lang=lisp><br />
(hello "you")<br />
</source><br />
{{Keys | C-xC-e }} => "Hello you"<br />
<br />
Yeah!<br />
<br />
Take a breath.<br />
<br />
<br />
<br />
=== Switch buffers ===<br />
<br />
Now switch to a new buffer named "*test*" in another window:<br />
<br />
<br />
<source lang=lisp><br />
(switch-to-buffer-other-window "*test*")<br />
</source><br />
{{Keys | C-xC-e }}<br />
=> [screen has two windows and cursor is in the *test* buffer]<br />
<br />
Mouse over the top window and left-click to go back. Or you can<br />
use `C-xo' (i.e. hold down control-x and hit o) to go to the other<br />
window interactively.<br />
<br />
=== Expression blocks ===<br />
<br />
You can combine several sexps with '''progn''':<br />
<br />
<source lang=lisp><br />
(progn<br />
(switch-to-buffer-other-window "*test*")<br />
(hello "you"))<br />
</source><br />
<br />
{{Keys | C-xC-e }}<br />
=> [The screen has two windows and cursor is in the *test* buffer]<br />
<br />
Now if you don't mind, I'll stop asking you to hit `C-xC-e': do it<br />
for every sexp that follows.<br />
<br />
Always go back to the *scratch* buffer with the mouse or `C-xo'.<br />
<br />
It's often useful to erase the buffer:<br />
<br />
<source lang=lisp><br />
(progn<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
(hello "there"))<br />
</source><br />
<br />
<br />
Or to go back to the other window:<br />
<br />
<source lang=lisp><br />
(progn<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
(hello "you")<br />
(other-window 1))<br />
</source><br />
<br />
<br />
=== Local variables ===<br />
<br />
You can bind a value to a local variable with '''let''':<br />
<br />
<source lang=lisp><br />
(let ((local-name "you"))<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
(hello local-name)<br />
(other-window 1))<br />
</source><br />
<br />
<br />
No need to use '''progn''' in that case, since '''let''' also combines<br />
several sexps.<br />
<br />
=== Strings handling ===<br />
<br />
Let's format a string:<br />
<br />
<source lang=lisp><br />
(format "Hello %s!\n" "visitor")<br />
</source><br />
<br />
%s is a place-holder for a string, replaced by "visitor".<br />
\n is the newline character.<br />
<br />
Let's refine our function by using format:<br />
<br />
<source lang=lisp><br />
(defun hello (name)<br />
(insert (format "Hello %s!\n" name)))<br />
<br />
(hello "you")<br />
</source><br />
<br />
Let's create another function which uses '''let''':<br />
<br />
<source lang=lisp><br />
(defun greeting (name)<br />
(let ((your-name "Bastien"))<br />
(insert (format "Hello %s!\n\nI am %s."<br />
name ; the argument of the function<br />
your-name ; the let-bound variable "Bastien"<br />
))))<br />
</source><br />
<br />
<br />
And evaluate it:<br />
<br />
<source lang=lisp><br />
(greeting "you")<br />
</source><br />
<br />
=== Interactive functions ===<br />
<br />
Some function are interactive:<br />
<br />
<source lang=lisp><br />
(read-from-minibuffer "Enter your name: ")<br />
</source><br />
<br />
Evaluating this function returns what you entered at the prompt.<br />
<br />
Let's make our '''greeting''' function prompt for your name:<br />
<br />
<source lang=lisp><br />
(defun greeting (from-name)<br />
(let ((your-name (read-from-minibuffer "Enter your name: ")))<br />
(insert (format "Hello!\n\nI am %s and you are %s."<br />
from-name ; the argument of the function<br />
your-name ; the let-bound var, entered at prompt<br />
))))<br />
<br />
(greeting "Bastien")<br />
</source><br />
<br />
Let's complete it by displaying the results in the other window:<br />
<br />
<source lang=lisp><br />
(defun greeting (from-name)<br />
(let ((your-name (read-from-minibuffer "Enter your name: ")))<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
(insert (format "Hello %s!\n\nI am %s." your-name from-name))<br />
(other-window 1)))<br />
</source><br />
<br />
Now test it:<br />
(greeting "Bastien")<br />
<br />
Take a breath.<br />
<br />
<br />
=== Lists ===<br />
<br />
Let's store a list of names:<br />
<br />
<source lang=lisp><br />
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))<br />
</source><br />
<br />
Get the first element of this list with '''car''':<br />
<br />
<source lang=lisp><br />
(car list-of-names)<br />
</source><br />
<br />
Get a list of all but the first element with '''cdr''':<br />
<br />
<source lang=lisp><br />
(cdr list-of-names)<br />
</source><br />
<br />
Add an element to the beginning of a list with '''push''':<br />
<br />
<source lang=lisp><br />
(push "Stephanie" list-of-names)<br />
</source><br />
<br />
NOTE: '''car''' and '''cdr''' don't modify the list, but '''push''' does.<br />
This is an important difference: some functions don't have any<br />
side-effects (like '''car''') while others have (like '''push''').<br />
<br />
Let's call '''hello''' for each element in `list-of-names':<br />
<br />
<source lang=lisp><br />
(mapcar 'hello list-of-names)<br />
</source><br />
<br />
Refine '''greeting''' to say hello to everyone in `list-of-names':<br />
<br />
<source lang=lisp><br />
(defun greeting ()<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
(mapcar 'hello list-of-names)<br />
(other-window 1))<br />
<br />
(greeting)<br />
</source><br />
<br />
Remember the '''hello''' function we defined above? It takes one<br />
argument, a name. '''mapcar''' calls '''hello''', successively using each<br />
element of `list-of-names' as the argument for '''hello'''.<br />
<br />
Now let's arrange a bit what we have in the displayed buffer:<br />
<br />
<br />
<source lang=lisp><br />
(defun replace-hello-by-bonjour ()<br />
(switch-to-buffer-other-window "*test*")<br />
(goto-char (point-min))<br />
(while (search-forward "Hello")<br />
(replace-match "Bonjour"))<br />
(other-window 1))<br />
</source><br />
<br />
<br />
=== Moving around ===<br />
<br />
(goto-char (point-min)) goes to the beginning of the buffer.<br />
(search-forward "Hello") searches for the string "Hello".<br />
(while x y) evaluates the y sexp(s) while x returns something.<br />
If x returns '''nil''' (nothing), we exit the while loop.<br />
<br />
<br />
<source lang=lisp><br />
(replace-hello-by-bonjour)<br />
</source><br />
<br />
You should see all occurrences of "Hello" in the *test* buffer<br />
replaced by "Bonjour".<br />
<br />
You should also get an error: "Search failed: Hello".<br />
<br />
To avoid this error, you need to tell `search-forward' whether it<br />
should stop searching at some point in the buffer, and whether it<br />
should silently fail when nothing is found:<br />
<br />
(search-forward "Hello" nil t) does the trick:<br />
<br />
The '''nil''' argument says: the search is not bound to a position.<br />
The '''t''' argument says: silently fail when nothing is found.<br />
<br />
We use this sexp in the function below, which doesn't throw an error:<br />
<br />
<br />
<source lang=lisp><br />
(defun hello-to-bonjour ()<br />
(switch-to-buffer-other-window "*test*")<br />
(erase-buffer)<br />
Say hello to names in `list-of-names'<br />
(mapcar 'hello list-of-names)<br />
(goto-char (point-min))<br />
Replace "Hello" by "Bonjour"<br />
(while (search-forward "Hello" nil t)<br />
(replace-match "Bonjour"))<br />
(other-window 1))<br />
<br />
(hello-to-bonjour)<br />
</source><br />
<br />
=== Change fonts ===<br />
<br />
Let's colorize the names:<br />
<br />
<br />
<source lang=lisp><br />
(defun boldify-names ()<br />
(switch-to-buffer-other-window "*test*")<br />
(goto-char (point-min))<br />
(while (re-search-forward "Bonjour \\(.+\\)!" nil t)<br />
(add-text-properties (match-beginning 1)<br />
(match-end 1)<br />
(list 'face 'bold)))<br />
(other-window 1))<br />
</source><br />
<br />
=== Searching regexps ===<br />
<br />
This functions introduces `re-search-forward': instead of<br />
searching for the string "Bonjour", you search for a pattern,<br />
using a "regular expression" (abbreviated in the prefix "re-").<br />
<br />
The regular expression is "Bonjour \\(.+\\)!" and it reads:<br />
the string "Bonjour ", and<br />
a group of (this is the \\( ... \\) construct)<br />
any character (this is the .)<br />
possibly repeated (this is the +)<br />
and the "!" string.<br />
<br />
Ready? Test it!<br />
<br />
<br />
<source lang=lisp><br />
(boldify-names)<br />
</source><br />
<br />
`add-text-properties' adds... text properties, like a face.<br />
<br />
OK, we are done. Happy hacking!<br />
<br />
=== How to get documentation ===<br />
<br />
If you want to know more about a variable or a function:<br />
<br />
C-h v a-variable RET<br />
C-h f a-function RET<br />
<br />
To read the Emacs Lisp manual with Emacs:<br />
<br />
C-h i m elisp RET<br />
<br />
To read an online introduction to Emacs Lisp:<br />
https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html<br />
<br />
Thanks to these people for their feedback and suggestions:<br />
* Wes Hardaker<br />
* notbob<br />
* Kevin Montuori<br />
* Arne Babenhauserheide<br />
* Alan Schmitt<br />
* LinXitoW<br />
* Aaron Meurer</div>
Elvince