Skip to content

Latest commit

 

History

History
798 lines (602 loc) · 19.1 KB

emacs-training-outline.org

File metadata and controls

798 lines (602 loc) · 19.1 KB

GNU Emacs training line-up

Emacs generalities

Emacs history overview

1976
TECMAC and TMACS, a pair of “TECO-macro realtime editors.” by Guy Steele, Dave Moon, Richard Greenblatt, Charles Frankston, et al.
1978
Multics Emacs by Bernie Greenberg. Written in MacLisp; also used Lisp as its extension language.
1981
Gosling Emacs : by James Gosling : written in C; with “Mocklisp” as its extension language.
1985
GNU Emacs 13.0? (20-mar-85) written in Spice Lisp (CMU Common Lisp) by Richard Stallman. initial public release?
1986
GNU Emacs 18.24 beta (02-oct-86).
1992
MULE 0.9.0b (4-mar-92) “Multilingual Enhancements to Emacs”: support for input methods and various languages including Japanese, Chinese, Korean, Greek, Hebrew, and Cyrillic.
1992
Lucid Emacs 19.0 (??-apr-92) by Jamie Zawinski et al.
1994
XEmacs 19.11 (13-sep-94) Lucid Emacs -> XEmacs renaming. now maintained by Chuck Thompson and Ben Wing.
1998
XEmacs 20.4 (28-feb-98) first reasonably stable release with MULE support. XEmacs “core” and “packages” now packaged separately.
2001
XEmacs 21.4.0 (16-apr-01) (stable / maintenance branch) Maintained by Stephen Turnbull. Shipped by Red Hat, Debian, Mandrake, etc.
2002
2002 GNU Emacs 21.2 (16-mar-02).
2008
Stefan Monnier and Chong Yidong take over maintenance.
2014
On November 11, 2014, development was moved to Git by ESR.
2015
John Wiegley takes over maintainance.

As of early 2014, GNU Emacs has had 579 individual commiters throughout its history.

Source: https://www.jwz.org/doc/emacs-timeline.html

Emacs directory overview

`src’
holds the C code for Emacs (the Emacs Lisp interpreter and its primitives, the redisplay code, and some basic editing functions).
`lisp’
holds the Emacs Lisp code for Emacs (almost everything else).
`leim’
holds the library of Emacs input methods, Lisp code and auxiliary data files required to type international characters which can’t be directly produced by your keyboard.
`lib-src’
holds the source code for some utility programs for use by or with Emacs, like movemail and etags.
`etc’
holds miscellaneous architecture-independent data files Emacs uses, like the tutorial text and the Zippy, the Pinhead quote database. The contents of the `lisp’, `leim’, `info’, `man’, `lispref’, and `lispintro’ subdirectories are architecture-independent too.
`info’
holds the Info documentation tree for Emacs.
`doc/emacs’
holds the source code for the Emacs Manual. If you modify the manual sources, you will need the `makeinfo’ program to produce an updated manual. `makeinfo’ is part of the GNU Texinfo package; you need version 4.6 or later of Texinfo.
`doc/lispref’
holds the source code for the Emacs Lisp reference manual.
`doc/lispintro’
holds the source code for the Introduction to Programming in Emacs Lisp manual.
`msdos’
holds configuration files for compiling Emacs under MS-DOS.
`nt’
holds various command files and documentation files that pertain to building and running Emacs on Windows 9X/ME/NT/2000/XP.
`test’
holds tests for various aspects of Emacs’s functionality.

Emacs glossary

  • active region
  • buffer
  • case folding
  • character
  • cursor
  • docstring
  • echo area
  • face
  • filling
  • fontification
  • frame
  • fringe
  • function/command
  • headline
  • indent
  • init file
  • inserting
  • kill
  • kill ring
  • line wrap
  • local/global
  • mark
  • menu-bar
  • minibuffer
  • modeline
  • narrow
  • overlay properties
  • point
  • prefix argument
  • region
  • scroll-bar
  • text properties
  • tool-bar
  • transient mark
  • variable/option
  • window
  • yank

Modifiers and keybindings

Modifiers

  • C- : hold the Control key
  • M- : hold the Meta/Alt key
  • DEL : hold the Backspace key
  • RET : hold the Return key
  • SPC : hold the Space key
  • ESC : hold the Escape key
  • TAB : hold the Tab key

General keybinding conventions

C-x [character]
mode-independent keybindings.
C-c …
mode-dependent keybindings.
C-c [character]
user-reserved keybindings.

Keybindings

  • C-y : yank
  • M-y : yank-pop
  • C-w : kill-region
  • M-w : kill-ring-save
  • C-x C-b : list buffers
  • C-x b : switch to buffer
  • C-x f : set fill column
  • C-l : recenter-top-bottom
  • C-o : open-line
  • C-h k : describe-key
  • C-h v : describe-variable
  • C-h f : describe-function
  • M-= : count-words-region
  • M-c : capitalize-word
  • C-x l : count-lines-page
  • C-x r m : bookmark-set
  • C-x r s : copy-to-register
  • C-x r SPC : point-to-register
  • C-x r w : window-configuration-to-register
  • C-x r l : bookmark-bmenu-list
  • C-x r i : insert-register
  • C-x r j : jump-to
  • C-u C-x = : what-cursor-position
  • C-x 2 : split-window-below
  • C-h i : info

Finding and saving files

  • C-x C-f : find file in the current buffer
  • C-x C-s : save current buffer, possibly in a file
  • C-x s : save all buffers
  • C-x C-b : list buffers
  • C-x b : find a buffer or create a new one
  • C-x d : dired

Moving the cursor

  • C-a/e : move to the beginning/end of line
  • C-f/b : move one character forward/backward
  • M-a/e : move to the beginning/end of sentence
  • M-f/b : move one word forward/backward
  • C-a : beginning-of-line
  • C-e : end-of-line
  • M-< : beginning-of-buffer
  • M-> : end-of-buffer
  • M-{ : backward-paragraph
  • M-} : forward-paragraph
  • C-<up/down> : move one paragraph up/down
  • C-<left/right> : move one character left/right

See sentence end definition: C-h v sentence-end TAB

Main modes and libraries

  • bookmark
  • calendar
  • calc
  • dired (and dired-x)
  • doc-view
  • electric-indent-mode
  • electric-pair-mode
  • global-font-lock-mode
  • epa-mode
  • erc
  • gnus
  • linum-mode
  • org-mode
  • register
  • info

Interactive macros

  • C-x ( : start defining a macro
  • C-x ) : stop defining a macro
  • C-x e : call the last defined macro
  • C-x C-k C-i : to create a counter and insert its value
  • C-x C-k b : bind the macro to a key
  • C-x C-k RET: edit last macro in a buffer
  • C-x C-k n: name the last macro

Example : C-x ( aaa C-x C-k C-i C-x )

Deleting, killing, yanking

  • C-d : delete-char
  • M-d : kill-word (notice kill != delete)
  • C-k : kill-line (see kill-whole-line)
  • M-k : kill-sentence

See also:

  • kill-whole-line (option)
  • kill-read-only-ok (option)
  • copy-region-as-kill (command)
  • copy-rectangle-as-kill (command)
  • copy-line (command)
  • open-line (command)

Interactive search

Go to the main Info buffer with C-h i. Hit d to make sure you are at the top-level of the Info documentation, and go at the beginning of the buffer with M-<.

Perform an incremental search for “lisp” with C-s lisp.

Hit RET to deactivate the search and the highlighting of search matches.

Hit C-s C-s to search for the last search string again.

Hit M-e to edit the search string (change it to “mode”) and RET to go back to the search mode.

Hit M-p to browse the search history and RET to search the selected string again.

Go back to the beginning of buffer with M-<.

Hit C-s to start the search.

Hit C-q C-j to search for the newline character.

Hit C-g to abort the search.

Hit C-s and search for Mail: notice only Mail will be matched, not mail.

Hit C-s and M-c to toggle case folding.

Hit C-s and M-s SPC to toggle “match spaces loosely” : when matching spaces loosely, you can search for a string like “mail client” and still match the “mail client” string.

Try searching for maii instead of mail and then hit DEL to edit the search string by deleting the last character.

Hit C-s and search for mail. Hit RET on the first occurrence and go one word backward with M-b. Now hit C-s C-w to search for the word at point. Try again with C-s C-w C-w to search for several words at point.

Hit C-s C-M-y to search for the character at point. Hit C-M-y C-M-y to add the next two characters to the search. Hit C-M-w to remove the last character from the search string.

Hit C-s to start the search. Hit M-b directly, without hitting RET. Hit C-SPC M-f M-w to copy the word at point. Go back to the beginning of the word and hit M-d to do the same: since the buffer is read-only, you can copy the word at point directly like this.

Now hit C-s again, they C-y to “yank” the text in the search area.

Hit C-s C-M-i to complete over previous searches.

In a folded org-mode buffer, search for some invisible text: the matched string is automatically unfolded when the point moves to it.

Now go back to the folded mode and hit C-s M-s i to search for visible text only. Invisible text won’t be matched.

Hit M-< and C-s search for mail. Now hit M-s o to list lines where “mail” occurs.

Hit C-s and M-s r to switch to regular expression search.

Hit C-s C-h b to browse the search options.

Text expansion and templates

  • M-TAB to run M-x completion-at-point RET
  • abbrev-mode : C-x a g and C-x a i g (see also C-x a l)
  • dabbrev-expand : M-/ (see also hippie-expand)
  • yasnippet : template expansion
  • pcomplete : custom completion when programming a mode

Emacs Lisp 1: the basics

15 minutes tutorial

http://emacs-doctor.com/learn-emacs-lisp-in-15-minutes.html explores some of the basic functions of Emacs lisp.

List of concepts seen in this tutorial:

  • symbolic expressions (sexps)
  • nesting parentheses to combine sexps
  • evaluating sexps
  • C-j and C-x C-e
  • variables and setq
  • inserting text
  • insert with more than one argument
  • combining sexps into functions
  • evaluating functions
  • switching to a new buffer
  • combining sexps with progn
  • erasing buffer’s content
  • switching to the other window
  • using format
  • using let
  • reading strings from the minibuffer
  • introducing lists
  • car and cdr
  • list mutability: using push
  • map over a list with mapcar
  • moving to a point: goto-char
  • searching with search-forward
  • replacing with replace-match
  • using regular expressions in search with re-search-forward
  • adding text properties with add-text-properties
  • C-h v and C-h f to explore variables and functions
  • C-h i m elisp to explore the Elisp manual

Types and variables

Elisp types

  • integer
  • floating-point
  • character
  • symbol
  • sequence (lists and arrays)
  • cons cell
  • array
  • string
  • vector
  • char-table
  • hash-table
  • function
  • primitive function
  • byte-code
  • autoload
  • macro
  • boolean

Defining variables and functions

  • defvar : define (“initialize”) a variable
  • defconst : define a constant
  • setq : (setq [SYM VAL]...)
  • setq-local : (setq-local VAR VAL)
  • defvar-local : (defvar-local VAR VAL &optional DOCSTRING)
  • setf : (setf (cadr x) y) <=> (setcar (cdr x) y)
  • setcar : (setcar CELL NEWCAR)
  • setcdr : (setcdr CELL NEWCDR)
  • defun : define a function
  • defstruct : define an “inline” function
  • defmacro : define a macro
  • defcustom : define an option (customizable variable)
  • boundp : check if a variable is set
  • fboundp : check if a function is set

Manipulating lists

See Manipulating lists and their elements.

Fonctions

  • cons cell
  • consp, atom, listp, nlistp, null
  • car (or first), car-safe
  • cdr, cdr-safe
  • pop
  • caar, cadr, cddr, cdar
  • nth, nthcdr
  • last
  • safe-length
  • list
  • make-list
  • append
  • add-to-list
  • push
  • memq
  • delq
  • remq
  • member
  • remove
  • remove-if
  • alist
  • plist
  • map
  • mapc
  • mapcar
  • mapconcat

Alist and plist

Functions, lambda and commands

Definitions

A function is the name of a sexp, possibly accepting arguments, returning the value of evaluating the sexp.

A command is an interactive function, using (interactive).

A lambda is an anonymous function.

Examples

(defun my-function () ; no argument
  "A docstring."      ; optional
  (+ 1 1))

;; Check whether the symbol is bound to a function
(fboundp 'my-function)

;; Calling the function
(my-function)

(defun my-function-with-argument (first-argument) ; no argument
  "A docstring."                                  ; still optional
  (message "Hello %s!" first-argument))           ; assuming a string

(my-function-with-argument "Bastien")

(defun my-command (name)
  "A docstring"
  (interactive "sYour name: ")
  (message "Hello %s!" name))

(call-interactively 'my-function)
;; => error, as the function is not a command

(call-interactively 'my-command)
;; => return the name

;; M-x my TAB does not show the function

;; A useless lambda
(lambda () (message "A simple lambda"))

;; A useless lambda command
(lambda () (interactive) (message "A simple lambda"))

;; A useless lambda command with one interactive arg
(lambda (n) (interactive "s") (message "Hello %s " n))

;; Call a lambda-command interactively
(call-interactively (lambda (n) (interactive "sName? ") (message "Hello %s " n)))

;; Bind a key to a lambda-command
(define-key global-map (kbd "C-M-?")
   (lambda (n) (interactive "sName? ") (message "Hello %s " n)))

Calling functions with programs (interactively)

(setq f 'list)
;; => list

(funcall f 'x 'y 'z)
;; => (x y z)

(funcall f 'x 'y '(z))
;; => (x y (z))
(setq f 'list)
;; => list
(apply f 'x 'y 'z)
;; => error→ Wrong type argument: listp, z
(apply '+ 1 2 '(3 4))
;; => 10
(apply '+ '(1 2 3 4))
;; => 10
(apply 'append '((a b c) nil (x y z) nil))
;; => (a b c x y z)

See also funcall-interactively.

Recursive functions

See Use a recursive function to implement “flatten”.

Dynamic binding

(defvar x -99)
;; => x

(defun getx () x)
(getx)
;; => -99

(let ((x 1)) (getx))
;; => 1

See the info manual:

When we call ‘getx’ from within a ‘let’ form in which ‘x’ is
(dynamically) bound, it retrieves the local value (i.e., 1).  But when
we call ‘getx’ outside the ‘let’ form, it retrieves the global value
(i.e., −99).

See also this info page on how to not abuse dynamic binding.

Mark, Point, Buffer, Insertions

  • set-mark
  • goto-char
  • point
  • point-at-bol
  • point-at-eol
  • beginning-of-line
  • end-of-line
  • move-beginning-of-line
  • move-end-of-line
  • insert
  • buffer-string
  • buffer-substring
  • buffer-substring-no-properties

Regular expressions

Syntax

‘.’ (Period) is a special character that matches any single character except a newline.

‘*’ is a postfix operator that means to match the preceding regular expression repetitively as many times as possible.

‘+’ is a postfix operator, similar to ‘*’ except that it must match the preceding expression at least once.

‘?’ is a postfix operator, similar to ‘*’ except that it must match the preceding expression either once or not at all.

‘*?’, ‘+?’, ‘??’ These are “non-greedy” variants of the operators ‘*’, ‘+’ and ‘?’.

‘[ … ]’ is a “character alternative”

‘[^ … ]’ This matches any character except the ones specified.

‘^’ When matching a buffer, ‘^’ matches the empty string, but only at the beginning of a line in the text being matched (or the beginning of the accessible portion of the buffer).

‘$’ is similar to ‘^’ but matches only at the end of a line.

‘\’ has two functions: it quotes the special characters (including ‘\’), and it introduces additional special constructs.

Group matching

(defun my-find-letters-numbers ()
  (interactive)
  (when (re-search-forward "\\([a-zA-Z]+\\)\\([0-9]+\\)" nil t)
    (message "String matched: %s -- Letters: %s -- Numbers: %s"
	     (match-string 0)
	     (match-string 1)
	     (match-string 2))))

(defun my-boldify ()
  (interactive)
  (while (re-search-forward "mail" nil t)
    (add-text-properties (match-beginning 0)
			 (match-end 0)
			 (list 'face 'bold))))

Examples

(defun my-new-search-function ()
  "My new search function."
  (interactive)
  (if ;; (re-search-forward "" nil t)
      (re-search-forward "\\([a-z]\\{1,2\\}\\)\\([0-9]+\\)" nil t)
      (message
       "Found two groups: \"%s\" and \"%s\""
       (match-string 1)
       (match-string 2))
    (user-error "Nothing found")))

(my-new-search-function)

;; abcde1

References

  • re-search-forward
  • match-beginning
  • match-end
  • case-fold-search

Elisp macros (defmacro)

(setq x 3)

(setq x 3 y 4)

(inc x)
;; => 4

(macroexpand '(inc x))
;; => (setq x (1+ x))

;; (inc x) <=> (setq x (1+ x))
(defmacro inc (var)
   (list 'setq var (list '1+ var)))

;; (inc2 x) <=> (inc x)
(defmacro inc2 (var)
   `(setq ,var (1+ ,var)))

;; (inc2 x) <=> (inc x)
(defmacro let-a-3 (&rest body)
   `(let ((msg "hello"))
      ,@body))

(let-a-3 ;; msg est toujours défini
 (message "blabla")
 (sit-for 2)
 (message msg))
      
(inc2 x)

(macroexpand '(inc2 x))
;; => 5

See org-preserve-lc in org-mode on how to use ,@body.

Using the debugger

Emacs Lisp 2: programming a new mode

Mode basics

  • Major and minor mode
  • derived-mode
  • syntax highlighting
  • kill-all-local-variables
  • modeline (global-mode-string)
  • keybindings (define-key)
  • menu (easy-menu-define)
  • defcustom
  • provide
  • require

Using derived-mode

Menus, modeline, headline and keybindings

Fontification

  • examining text properties with C-u C-x =
  • set-text-properties

The syntax-table

Customization

Emacs Lisp 3: advanced Elisp and mode programming

Lisp 1 and Lisp 2

Advice and hooks

Autoloading functions

Using lexical-binding

Overlay properties

Tabulated display

Major hooks

The Emacs debugger (2)

The Emacs profiler

Org-mode 1: basic functions, workflows, reports

org-mode as an outliner

Org mode syntactic elements

  • headline
  • subtree
  • paragraph / list / table / block

Org mode folding

  • initial folding state
  • #+STARTUP: showall

Org mode options

org-mode to write and publish documents

  • options
  • blocks
  • publishing projects

agendas views

  • predefined agenda views
  • custom agenda views
  • agenda blocks

Exploring workflows (GTD, etc.)

Org-mode 2: Org table and Org Babel

Main spreadsheet functionalities

Linking tables

Embed and execute code from org-mode

Get the result of embedded code

Use Org Babel to export code et results

Miscellaneous

Swap control and caps lock

!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L

Writing an Org-mode exporter

Emacs package management

magit

tramp

flycheck

semantic-refactor, xref

proced

seq.el

thing-at-point

How to contribute to GNU Emacs?

Emacs configuration file organization

  • one vs multiple .el configuration files
  • “tangling” a .el configuration file from a .org file
  • use-package