-
Notifications
You must be signed in to change notification settings - Fork 139
/
emacs-tutor3.html
4811 lines (4057 loc) · 251 KB
/
emacs-tutor3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-09-14 Wed 11:37 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Emacs Mini Manual (PART 3) - CUSTOMIZING AND EXTENDING EMACS</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Tu Do" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" href="./static/worg.css">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble" class="status">
<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project=9874755;
var sc_invisible=1;
var sc_security="c2028bb7";
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" + scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="hit counter"
href="http://statcounter.com/free-hit-counter/" target="_blank"><img
class="statcounter" src="http://c.statcounter.com/9874755/0/c2028bb7/1/"
alt="hit counter"></a></div></noscript>
<!-- End of StatCounter Code for Default Guide -->
<h2><a href="index.html">Back to Table of Contents</a></h2>
</div>
<div id="content">
<h1 class="title">Emacs Mini Manual (PART 3) - CUSTOMIZING AND EXTENDING EMACS</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgheadline1">Why customize and extend Emacs?</a></li>
<li><a href="#orgheadline2">Just enough Emacs Lisp</a>
<ul>
<li><a href="#orgheadline3">Function: <b>(setq [ <i>SYM</i> <i>VAL</i> ]…)</b></a></li>
<li><a href="#orgheadline4">Function: <b>(load <i>FILE</i> &optional <i>NOERROR</i> <i>NOMESSAGE</i> <i>NOSUFFIX</i> <i>MUST-SUFFIX</i>)</b></a></li>
<li><a href="#orgheadline5">Function: <b>(require <i>FEATURE</i> &optional <i>FILENAME</i> <i>NOERROR</i>)</b></a></li>
<li><a href="#orgheadline6">Function: <b>(provide <i>FEATURE</i> &optional <i>SUBFEATURES</i>)</b></a></li>
<li><a href="#orgheadline7">Function: <b>(add-to-list <i>LIST-VAR</i> <i>ELEMENT</i> &optional <i>APPEND</i> <i>COMPARE-FN</i>)</b></a></li>
<li><a href="#orgheadline8">Function: <b>(add-hook <i>HOOK</i> <i>FUNCTION</i> &optional <i>APPEND</i> <i>LOCAL</i>)</b></a></li>
<li><a href="#orgheadline9">Function: <b>(global-set-key <i>KEY</i> <i>COMMAND</i>)</b></a></li>
<li><a href="#orgheadline10">Function: <b>(define-key <i>KEYMAP</i> <i>KEY</i> <i>DEF</i>)</b></a></li>
<li><a href="#orgheadline11">Function: <b>(defalias <i>SYMBOL</i> <i>DEFINITION</i> &optional <i>DOCSTRING</i>)</b></a></li>
<li><a href="#orgheadline12">Function: <b>(mapc <i>FUNCTION</i> <i>SEQUENCE</i>)</b></a></li>
<li><a href="#orgheadline13">Macro: <b>(defun <i>NAME</i> <i>ARGLIST</i> &optional <i>DOCSTRING</i> <i>DECL</i> &rest <i>BODY</i>)</b></a></li>
</ul>
</li>
<li><a href="#orgheadline14">Useful built-in key bindings for navigating pairs</a></li>
<li><a href="#orgheadline15">Let's supercharge Emacs</a>
<ul>
<li><a href="#orgheadline16">How to use Emacs package manager</a></li>
<li><a href="#orgheadline17">Customize Emacs</a>
<ul>
<li><a href="#orgheadline18">Using GUI</a></li>
<li><a href="#orgheadline19">Using Emacs Lisp</a></li>
</ul>
</li>
<li><a href="#orgheadline20">init.el</a>
<ul>
<li><a href="#orgheadline21">Package: <code>workgroups2</code></a></li>
</ul>
</li>
<li><a href="#orgheadline22">setup-editing.el</a>
<ul>
<li><a href="#orgheadline23">Customize built-in functions</a></li>
<li><a href="#orgheadline24">Package: <code>duplicate-thing</code></a></li>
<li><a href="#orgheadline25">Package: <code>volatile-highlights</code></a></li>
<li><a href="#orgheadline26">Package: <code>smartparens</code></a></li>
<li><a href="#orgheadline27">Package: <code>clean-aindent-mode</code></a></li>
<li><a href="#orgheadline28">Package: <code>undo-tree</code></a></li>
<li><a href="#orgheadline29">Package: <code>yasnippet</code></a></li>
</ul>
</li>
<li><a href="#orgheadline30">setup-convenience.el</a>
<ul>
<li><a href="#orgheadline31">Package: <code>company</code></a></li>
<li><a href="#orgheadline32">Package: <code>expand-region</code></a></li>
<li><a href="#orgheadline33">Package: <code>ibuffer-vc</code></a></li>
<li><a href="#orgheadline34">Package: <code>projectile</code></a></li>
</ul>
</li>
<li><a href="#orgheadline35">Package in a league of its own: <code>helm</code></a></li>
<li><a href="#orgheadline36">Alternative to Helm: <code>ido</code> + <code>ido-ubiquitous</code> + <code>flx-ido</code> + <code>smex</code></a>
<ul>
<li><a href="#orgheadline37">Package: <code>ido-ubiquitous</code></a></li>
<li><a href="#orgheadline38">Package: <code>flx-ido</code></a></li>
<li><a href="#orgheadline39">Package: <code>smex</code></a></li>
</ul>
</li>
<li><a href="#orgheadline40">setup-files.el</a>
<ul>
<li><a href="#orgheadline41">Package: <code>dired+</code></a></li>
<li><a href="#orgheadline42">Package: <code>recentf-ext</code></a></li>
<li><a href="#orgheadline43">Package: <code>ztree</code></a></li>
<li><a href="#orgheadline44">Package: <code>vlf</code></a></li>
</ul>
</li>
<li><a href="#orgheadline45">setup-text.el</a></li>
<li><a href="#orgheadline46">setup-data.el</a></li>
<li><a href="#orgheadline47">setup-external.el</a></li>
<li><a href="#orgheadline48">setup-communication.el</a></li>
<li><a href="#orgheadline49">setup-programming.el</a>
<ul>
<li><a href="#orgheadline50">Package: <code>diff-hl</code></a></li>
<li><a href="#orgheadline51">Package: <code>magit</code></a></li>
<li><a href="#orgheadline52">Package: <code>flycheck</code></a></li>
<li><a href="#orgheadline53">Package: <code>flycheck-tip</code></a></li>
</ul>
</li>
<li><a href="#orgheadline54">setup-applications.el</a></li>
<li><a href="#orgheadline55">setup-development.el</a></li>
<li><a href="#orgheadline56">setup-environment.el</a>
<ul>
<li><a href="#orgheadline57">Package: <code>nyan-mode</code></a></li>
<li><a href="#orgheadline58">Package: <code>golden-ratio</code></a></li>
</ul>
</li>
<li><a href="#orgheadline59">setup-faces-and-ui.el</a>
<ul>
<li><a href="#orgheadline60">Package: <code>highlight-numbers</code></a></li>
<li><a href="#orgheadline61">Package: <code>highlight-symbol</code></a></li>
<li><a href="#orgheadline62">Change Emacs appearance by using color theme</a></li>
</ul>
</li>
<li><a href="#orgheadline63">setup-help.el</a>
<ul>
<li><a href="#orgheadline64">Package: <code>info+</code></a></li>
<li><a href="#orgheadline65">Package: <code>discover-my-major</code></a></li>
<li><a href="#orgheadline66">Package: <code>rainbow-mode</code></a></li>
<li><a href="#orgheadline67">Package: <code>help+</code></a></li>
<li><a href="#orgheadline68">Package: <code>help-fns+</code></a></li>
<li><a href="#orgheadline69">Package: <code>help-mode+</code></a></li>
</ul>
</li>
<li><a href="#orgheadline70">setup-local.el</a></li>
</ul>
</li>
<li><a href="#orgheadline71">More Emacs Lisp resources</a></li>
</ul>
</div>
</div>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1"><a id="ID-bbed4318-de81-421d-a6c4-04b40b60b591"></a>Why customize and extend Emacs?</h2>
<div class="outline-text-2" id="text-orgheadline1">
<p>
Emacs comes with default settings. Some interesting features are
disabled and hidden, i.e. ibuffer, Semantic, electric
modes… Probably, to make it more user friendly to new users and
make it behave more like "normal" editors, i.e. Normal users do not
expect automatic pairing of punctuation marks like parentheses,
brackets, curly brackets…
</p>
<p>
Because the nature of Emacs is an extensible system, people write
extensions to improve Emacs and share with others. The extensions
improve various aspects of Emacs: Improve existing and add new editing
features, integrate 3rd party tools, add programming languages
supports, change Emacs appearance… Without the ability to extend,
Emacs will just be another obscure editor with some useful features
but cannot meet the demands of people, because different people have
different needs, and Emacs maintainers cannot provide them all and
integrate all into Emacs. With the ability to extend, people can bend
Emacs the way they want, much like Lisp.
</p>
<p>
Unlike other editors which encourage users to stay with the default as
much as they can, Emacs encourages users to customize and extend Emacs
as much as they can.
</p>
<p>
In the old days, when Emacs did not have a package manager and did
not have many learning resources, it's really uneasy to customize
Emacs, because to customize Emacs properly you have to read a decent
chunk of the Emacs Lisp manual. Many of us, including me, do not have
that luxury of time. The way I learned to customize Emacs was copying
little code snippets that solve specific problems - when I encountered
them - and paste everything in my <code>~/.emacs</code> file. I also had to
download Emacs packages manually and load it manually in <code>~/.emacs</code>. I
hope that this guide helps you to be at least as good as me in
customizing Emacs without having to spend a long time in collecting
configurations, or reading the whole Emacs Lisp manual. After you
finish this chapter, you can dive in the Emacs Lisp manually if you
want, and easier.
</p>
</div>
</div>
<div id="outline-container-orgheadline2" class="outline-2">
<h2 id="orgheadline2"><a id="ID-bd3c88d9-3a94-483f-bc35-3b5c215d0e79"></a>Just enough Emacs Lisp</h2>
<div class="outline-text-2" id="text-orgheadline2">
<p>
In this section, you only need to read and understand. I will list the
commonly used function used for customizing Emacs that we will use in
later sections. It's important that you understand the core
ideas. After you finish the sub-sections of this section, we will play
with Emacs Lisp code for customizing Emacs, and it's really fun to see
your Emacs "evolves" gradually.
</p>
<p>
It's worth to review again. To read most of Lisp code, it is easy. You
only need to these rules:
</p>
<ul class="org-ul">
<li>Code that get executed is a pair of parentheses, with things in it:
<code>(...)</code>. The first element always gets evaluated; your Lisp
environment (in our case, Emacs) determines which of the following 3
forms the symbol in the first slot is, then acts accordingly:</li>
<li><p>
Function form: if the first element points to a function definition,
then the following elements get evaluated from left to right and
finally passed into the function as arguments.
</p>
<p>
For example:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>message <span style="color: #8b2252;">"Hello word %d"</span> <span style="color: #7388d6;">(</span>+ <span style="color: #008b8b;">10</span> <span style="color: #008b8b;">10</span><span style="color: #7388d6;">)</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
<code>message</code> is a function that prints to echo area and <code>*Message*</code>
buffer (can be opened with <b>C-h e</b>). Emacs detects that <code>message</code> is a
function, then it evaluates the string "Hello world %d" and another
nested form <code>(+ 10 10)</code> sequentially. When Emacs gets to <code>(+ 10
10)</code>, it evaluates the form and return <code>20</code>. Then the string "Hello
world %d" and the number <code>20</code> are passed into <code>message</code>, and finally
output "Hello world 20" in the echo area and <code>*Message*</code> buffer.
</p></li>
<li><p>
Special form: if the first element is one of predefined functions in
Emacs Lisp that need special rules and/or syntax, then the remaining
elements are evaluated depend on the rule of the predefined
functions. Special form is just function form, but handle the
remaining in its own way rather than using the default in function
form. That's why we call it special.
</p>
<p>
For example, <code>if</code> is a special form as we learned in previous part:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">if</span> condition
true-case
false-case<span style="color: #707183;">)</span>
</pre>
</div>
<p>
<code>condition</code> is a valid Lisp form; if it is evaluated to true (which
is anything not <code>NIL</code>, or the empty list <code>()</code>), then the true case -
also a valid Lisp form - gets evaluated; otherwise the false case -
also a valid Lisp form - gets evaluated.
</p>
<p>
Another special form is <code>and</code>. After Emacs determines the first
element is <code>and</code> special form, then it keeps evaluating the
remaining elements from left to right until one of the element
return false. If all elements get evaluated, <code>and</code> returns true.
</p></li>
<li>Macro form: if the first element is created by the function
<code>defmacro</code> (which you do not need to know at this stage), then Emacs
do not evaluate any remaining elements, but passes them in as
data. Macro is a way for programmers to create their own
mini-language that do not follow the evaluation rules predefined in
Emacs (function form and special form). If you read a form that is
not a function form or any predefined special form, don't panic!
It's a macro form. Programmers always provide documentations on how
to use their mini-languages.</li>
</ul>
<p>
After all, those different forms are just under one category:
function. Function form is a regular function; special form is a
function with its own rule; macro form is a function that generate
code.
</p>
<ul class="org-ul">
<li>Data: has two types:</li>
<li><b>Atom</b>: primitives such as number, string, symbol and NIL.</li>
<li><p>
<b>Non-atom</b>: if you put a <code>’</code> just before a form, it becomes
a list. Emacs treats these <b>quoted</b> forms as data.
</p>
<p>
Emacs also has other data types like array, hash-table… but list
is the most popular, used when performance is not needed.
</p></li>
</ul>
<p>
In this section, I only explained the frequently used functions. In
the later sections, there are more functions used. I can't explain
them all. You should really work it out on your own gradually with
<b>C-h f</b> and <b>C-h v</b>.
</p>
</div>
<div id="outline-container-orgheadline3" class="outline-3">
<h3 id="orgheadline3"><a id="ID-efdf75c7-0278-46ab-910c-fd557d0584e9"></a>Function: <b>(setq [ <i>SYM</i> <i>VAL</i> ]…)</b></h3>
<div class="outline-text-3" id="text-orgheadline3">
<p>
<span class="underline">Comment</span>: A really fundamental function for customizing Emacs
settings. An Emacs setting is really just a variable. Emacs has GUI
for changing setting, but <code>setq</code> a variable is also equivalent.
</p>
<p>
<span class="underline">Example</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">setq</span> global-mark-ring-max <span style="color: #008b8b;">50000</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Set each <i>SYM</i> to the value of its <i>VAL</i>.
The symbols <i>SYM</i> are variables; they are literal (not evaluated).
The values <i>VAL</i> are expressions; they are evaluated.
Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
The second <i>VAL</i> is not computed until after the first <i>SYM</i> is set, and so on;
each VAL can use the new value of variables set earlier in the `<sub>setq</sub>_'.
The return value of the `<sub>setq</sub>_' form is the value of the last <i>VAL</i>.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-3">
<h3 id="orgheadline4"><a id="ID-554a5d52-623c-487d-9e9b-81c5f8f67746"></a>Function: <b>(load <i>FILE</i> &optional <i>NOERROR</i> <i>NOMESSAGE</i> <i>NOSUFFIX</i> <i>MUST-SUFFIX</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline4">
<p>
<span class="underline">Comment</span>: This function allows you to load a file. <i>FILE</i> is the
a filename that is looked up in the variable <code>load-path</code>. Do you
notice <code>&optional</code> keyword? When you see this keyword, it means
everything after <code>&optional</code> is, optional. The parameters <i>NOERROR</i>,
<i>NOMESSAGE</i>, <i>NOSUFFIX</i>, <i>MUST-SUFFIX</i> are not required to be passed
into the function if you don't need. However, if you want to pass an
argument into the place of one a parameter, you must also pass
arguments to all the parameters to the left of your chosen
parameter. For example, if you want to pass an argument into
<i>NOSUFFIX</i>, you are <b>required</b> to pass arguments into <i>NOERROR</i>,
<i>NOMESSAGE</i> first. You are safe to ignore everything after your
chosen parameter, and in our example, it's <i>MUST-SUFFIX</i>.
</p>
<p>
<span class="underline">Example</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>load <span style="color: #7388d6;">(</span>substitute-in-file-name <span style="color: #8b2252;">"$HOME/.emacs.d/module"</span><span style="color: #7388d6;">)</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">first try to load module.elc; if not found, try to load module.el</span>
<span style="color: #707183;">(</span>load <span style="color: #7388d6;">(</span>substitute-in-file-name <span style="color: #8b2252;">"$HOME/.emacs.d/module.el"</span><span style="color: #7388d6;">)</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">only load module.el</span>
<span style="color: #707183;">(</span>load <span style="color: #7388d6;">(</span>substitute-in-file-name <span style="color: #8b2252;">"$HOME/.emacs.d/module.elc"</span><span style="color: #7388d6;">)</span><span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">only load module.elc</span>
<span style="color: #707183;">(</span>load <span style="color: #8b2252;">"module"</span><span style="color: #707183;">)</span> <span style="color: #b22222;">; </span><span style="color: #b22222;">search for the file module.el or module.elc in variable load-path</span>
</pre>
</div>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Execute a file of Lisp code named FILE.
First try FILE with `.elc' appended, then try with `.el', then try
FILE unmodified (the exact suffixes in the exact order are determined
by `load-suffixes'). Environment variable references in FILE are
replaced with their values by calling `substitute-in-file-name'. This
function searches the directories in `load-path'.
</p>
<p>
….(<b>C-h f</b> for more information)…
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline5" class="outline-3">
<h3 id="orgheadline5"><a id="ID-6b51529d-424f-4735-84e6-c13a0818376b"></a>Function: <b>(require <i>FEATURE</i> &optional <i>FILENAME</i> <i>NOERROR</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline5">
<p>
<span class="underline">Comment</span>: If you install a package from <code>M-x list-packages</code>, to
load that package, use <code>(require 'installed-package)</code>.
</p>
<p>
<span class="underline">Example</span>: For example, you installed package <code>volatile-highlights</code>,
to load it:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">require</span> '<span style="color: #008b8b;">volatile-highlights</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Later in the guide, you will create your own custom modules that uses
<code>provide</code> function to provide new feature to Emacs. To use a feature,
the module must be activated with <code>require</code>.
</p>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
If feature <i>FEATURE</i> is not loaded, load it from <i>FILENAME</i>.
If <i>FEATURE</i> is not a member of the list `features', then the feature
is not loaded; so load the file <i>FILENAME</i>.
If <i>FILENAME</i> is omitted, the printname of <i>FEATURE</i> is used as the file name,
and `load' will try to load this name appended with the suffix `.elc' or
`.el', in that order. The name without appended suffix will not be used.
See `get-load-suffixes' for the complete list of suffixes.
If the optional third argument <i>NOERROR</i> is non-nil,
then return nil if the file is not found instead of signaling an error.
Normally the return value is <i>FEATURE</i>.
The normal messages at start and end of loading <i>FILENAME</i> are suppressed.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-3">
<h3 id="orgheadline6"><a id="ID-8adc5418-4ddc-4755-9ed6-f10cc96cfdb2"></a>Function: <b>(provide <i>FEATURE</i> &optional <i>SUBFEATURES</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline6">
<p>
<span class="underline">Comment</span>: You can use this function at the end of a file to turn the
file into a loadable module that is only loaded when called with
<code>require</code>.
</p>
<p>
<span class="underline">Example</span>:
</p>
<p>
Suppose that in a file <code>setup-editing.el</code>, you put this line at the
bottom:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span><span style="color: #a020f0;">provide</span> '<span style="color: #008b8b;">setup-editing</span><span style="color: #707183;">)</span>
</pre>
</div>
<p>
Then, even if you load it with <code>load</code> function, it won't be
activated. To activate, you have to execute <code>(require
'setup-editing)</code>.
</p>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Announce that <i>FEATURE</i> is a feature of the current Emacs. The optional
argument <i>SUBFEATURES</i> should be a list of symbols listing particular
subfeatures supported in this version of <i>FEATURE</i>.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline7" class="outline-3">
<h3 id="orgheadline7"><a id="ID-6d573c2c-768b-4233-b8c0-5796f0b23304"></a>Function: <b>(add-to-list <i>LIST-VAR</i> <i>ELEMENT</i> &optional <i>APPEND</i> <i>COMPARE-FN</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline7">
<p>
<span class="underline">Comment</span>: Add an element <code>ELEMENT</code> to a list named <code>LIST-VAR</code>.
</p>
<p>
<span class="underline">Example</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>add-to-list 'load-path <span style="color: #8b2252;">"~/.emacs.d/personal"</span><span style="color: #707183;">)</span> <span style="color: #b22222;">; </span><span style="color: #b22222;">add personal to load-path,</span>
<span style="color: #b22222;">; </span><span style="color: #b22222;">so "load" function can search for files in it</span>
</pre>
</div>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Add <i>ELEMENT</i> to the value of <i>LIST-VAR</i> if it isn't there yet.
The test for presence of <i>ELEMENT</i> is done with `equal', or with
<i>COMPARE-FN</i> if that's non-nil.
If <i>ELEMENT</i> is added, it is added at the beginning of the list,
unless the optional argument <i>APPEND</i> is non-nil, in which case
<i>ELEMENT</i> is added at the end.
</p>
<p>
The return value is the new value of <i>LIST-VAR</i>.
</p>
<p>
This is handy to add some elements to configuration variables,
but please do not abuse it in Elisp code, where you are usually
better off using `push' or `cl-pushnew'.
</p>
<p>
If you want to use `add-to-list' on a variable that is not
defined until a certain package is loaded, you should put the
call to `add-to-list' into a hook function that will be run only
after loading the package. `eval-after-load' provides one way to
do this. In some cases other hooks, such as major mode hooks,
can do the job.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline8" class="outline-3">
<h3 id="orgheadline8"><a id="ID-acb677cd-cad6-43c7-9bdb-b31578337fe0"></a>Function: <b>(add-hook <i>HOOK</i> <i>FUNCTION</i> &optional <i>APPEND</i> <i>LOCAL</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline8">
<p>
<span class="underline">Comment</span>: A <b>hook</b> is a Lisp variable which holds a list of
functions, to be called on some well-defined occasion. (This is called
running the hook. You can search for hook using <b>C-h v</b> and enter
<code>-hook</code> suffix then <b>TAB</b>. Or you can find hooks in <b>Customization Groups</b>.
</p>
<p>
<span class="underline">Example</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>add-hook 'prog-mode-hook 'linum-mode<span style="color: #707183;">)</span>
</pre>
</div>
<p>
After you add the function <code>linum-mode</code> - which activates line number
on the left margin of your Emacs - then every time you enter a
<code>prog-mode</code>, which is the root all programming major modes derive
from. A programming mode can be <code>c-mode</code>, <code>asm-mode</code>,
<code>emacs-lisp-mode</code>, <code>java-mode</code>…
</p>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Add to the value of HOOK the function <i>FUNCTION</i>.
<i>FUNCTION</i> is not added if already present.
<i>FUNCTION</i> is added (if necessary) at the beginning of the hook list
unless the optional argument <i>APPEND</i> is non-nil, in which case
<i>FUNCTION</i> is added at the end.
</p>
<p>
The optional fourth argument, <i>LOCAL</i>, if non-nil, says to modify
the hook's buffer-local value rather than its global value.
This makes the hook buffer-local, and it makes t a member of the
buffer-local value. That acts as a flag to run the hook
functions of the global value as well as in the local value.
</p>
<p>
<i>HOOK</i> should be a symbol, and <i>FUNCTION</i> may be any valid function. If
<i>HOOK</i> is void, it is first set to nil. If <i>HOOK</i>'s value is a single
function, it is changed to a list of functions.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline9" class="outline-3">
<h3 id="orgheadline9"><a id="ID-5c06ee7d-fb65-48bb-8419-c5660d4d4fd2"></a>Function: <b>(global-set-key <i>KEY</i> <i>COMMAND</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline9">
<p>
<span class="underline">Comment</span>: This function binds a command to a key, as you can see in
the function interface.
</p>
<p>
<span class="underline">Example</span>:
</p>
<p>
You can bind in one of the following ways:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>global-set-key <span style="color: #7388d6;">(</span>kbd <span style="color: #8b2252;">"C-x C-b"</span><span style="color: #7388d6;">)</span> 'ibuffer<span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">bind "C-x C-b" to ibuffer command</span>
<span style="color: #707183;">(</span>global-set-key <span style="color: #8b2252;">"\C-x\C-b"</span> 'ibuffer<span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">bind "C-x C-b to ibuffer command, but modifier </span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">keys must be escaped with the backslash</span>
<span style="color: #707183;">(</span>global-set-key <span style="color: #7388d6;">[</span>?\C-x?\C-b<span style="color: #7388d6;">]</span> 'ibuffer<span style="color: #707183;">)</span> <span style="color: #b22222;">;; </span><span style="color: #b22222;">use vector instead of a string</span>
</pre>
</div>
<p>
I recommend you to use <code>(kbd ...)</code> function because we can write key
bindings using our familiar key notations without adding unnecessary
characters. Vector is array in other languages. Vector was used for
mapping function keys, such as <code>[left]</code>, <code>[right]</code>, <code>[up]</code>, <code>[down]</code>,
<code>[f1]...[f12]</code>. But now, you can also map function keys in <code>(kbd
...)</code> function using angle brackets:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #707183;">(</span>global-set-key <span style="color: #7388d6;">(</span>kbd <span style="color: #8b2252;">"<f3>"</span><span style="color: #7388d6;">)</span> 'kmacro-start-macro-or-insert-counter<span style="color: #707183;">)</span>
</pre>
</div>
<p>
Here are common function keys (remember to wrap them in a pair of
angle bracket):
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Key</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>left, up, right, down</code></td>
<td class="org-left">Cursor arrow keys</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>begin, end, home, next, prior</code></td>
<td class="org-left">Other cursor re-positioning keys</td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left"><code>prior</code> means <code>PageUp</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left"><code>next</code> means <code>PageDOwn</code></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>select, print, execute, backtab</code></td>
<td class="org-left">Miscellaneous keys</td>
</tr>
<tr>
<td class="org-left"><code>insert, undo, redo, clearline</code></td>
<td class="org-left"><code>backtab</code> means <code>S-TAB</code> or <code>C-iso-tab</code></td>
</tr>
<tr>
<td class="org-left"><code>insertline, deleteline, insertchar, deletechar</code></td>
<td class="org-left"> </td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>f1, f2, ... F35</code></td>
<td class="org-left">Numbered function keys on top of your keyboard</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>kp-add, kp-subtract, kp-multiply, kp-divide</code></td>
<td class="org-left">Keypad keys (to the right of the regular keyboard)</td>
</tr>
<tr>
<td class="org-left"><code>kp-backtab, kp-space, kp-tab, kp-enter</code></td>
<td class="org-left">, with names or punctuation.</td>
</tr>
<tr>
<td class="org-left"><code>kp-separator, kp-decimal, kp-equal</code></td>
<td class="org-left"> </td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left"> </td>
</tr>
<tr>
<td class="org-left"><code>kp-0, kp-1, ... kp-9</code></td>
<td class="org-left">Keypad keys with digits.</td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left"> </td>
</tr>
<tr>
<td class="org-left"><code>kp-f1, kp-f2, kp-f3, kp-f4</code></td>
<td class="org-left">Keypad PF keys.</td>
</tr>
</tbody>
</table>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
Give <i>KEY</i> a global binding as <i>COMMAND</i>.
<i>COMMAND</i> is the command definition to use; usually it is
a symbol naming an interactively-callable function.
<i>KEY</i> is a key sequence; noninteractively, it is a string or vector
of characters or event types, and non-ASCII characters with codes
above 127 (such as ISO Latin-1) can be included if you use a vector.
</p>
<p>
Note that if <i>KEY</i> has a local binding in the current buffer,
that local binding will continue to shadow any global binding
that you make with this function.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-orgheadline10" class="outline-3">
<h3 id="orgheadline10"><a id="ID-879a92aa-5c2b-4623-913a-45dddb84ed7b"></a>Function: <b>(define-key <i>KEYMAP</i> <i>KEY</i> <i>DEF</i>)</b></h3>
<div class="outline-text-3" id="text-orgheadline10">
<p>
<span class="underline">Comment</span>:
</p>
<p>
This function binds a definition <code>DEF</code>, usually a command, to a key
sequence <code>KEY</code>. A definition can be other things that you can find in
the built-in documentation.
</p>
<p>
A key sequence (key, for short) is a sequence of input events that
have a meaning as a unit. Input events include characters, function
keys and mouse buttons—all the inputs that you can send to the
computer. A key sequence gets its meaning from its binding, which says
what command it runs.
</p>
<p>
When a key sequence <code>KEY</code> is pressed, Emacs runs the associated
function. A keymap <code>KEYMAP</code> stores a list of bindings between <code>KEY</code>
and definition <code>DEF</code>. Major mode or minor mode uses keymap to provide
its own key bindings. A keymap usually has <code>-mode-map</code> suffix,
i.e. dired-mode-map; if you want to change or add a key binding in a
major mode or minor mode, you use <code>define-key</code> function like this:
</p>
<p>
<span class="underline">Example</span>:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #b22222;">;; </span><span style="color: #b22222;">Dired uses "e", "f" or RET to open a file</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">you can reuse one of these keys for different purpose</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">for example, you can bind it to wdired-change-to-wdired-mode</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">wdired-change-to-wdired-mode allows you to edit your Dired buffer</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">like a normal text buffer, such as edit file/directory names,</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">permission bits.. and then commit the changes to disk.</span>
<span style="color: #b22222;">;;</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">"e" is short for "edit"</span>
<span style="color: #b22222;">;; </span><span style="color: #b22222;">After finish your editing, "C-c C-c" to commit, "C-c C-k" to abort</span>
<span style="color: #707183;">(</span>define-key dired-mode-map <span style="color: #7388d6;">(</span>kbd <span style="color: #8b2252;">"e"</span><span style="color: #7388d6;">)</span> 'wdired-change-to-wdired-mode<span style="color: #707183;">)</span>
</pre>
</div>
<p>
<span class="underline">Built-in Documentation</span>:
</p>
<blockquote>
<p>
In <i>KEYMAP</i>, define key sequence <i>KEY</i> as <i>DEF</i>.
<i>KEYMAP</i> is a keymap.
</p>
<p>
<i>KEY</i> is a string or a vector of symbols and characters, representing a
sequence of keystrokes and events. Non-ASCII characters with codes
above 127 (such as ISO Latin-1) can be represented by vectors.
Two types of vector have special meanings:
[remap COMMAND] remaps any key binding for <i>COMMAND</i>.
[t] creates a default definition, which applies to any event with no
other definition in <i>KEYMAP</i>.
</p>
<p>
<i>DEF</i> is anything that can be a key's definition:
nil (means key is undefined in this keymap),
a command (a Lisp function suitable for interactive calling),
a string (treated as a keyboard macro),
a keymap (to define a prefix key),
a symbol (when the key is looked up, the symbol will stand for its
function definition, which should at that time be one of the above,
or another symbol whose function definition is used, etc.),
a cons (STRING . DEFN), meaning that DEFN is the definition
(DEFN should be a valid definition in its own right),
or a cons (MAP . CHAR), meaning use definition of CHAR in keymap MAP,
or an extended menu item definition.
(See info node `(elisp)Extended Menu Items'.)
</p>
<p>
If <i>KEYMAP</i> is a sparse keymap with a binding for <i>KEY</i>, the existing
binding is altered. If there is no binding for <i>KEY</i>, the new pair
binding <i>KEY</i> to <i>DEF</i> is added at the front of <i>KEYMAP</i>.
</p>
</blockquote>
</div>
</div>