Difference between revisions of "Emacs Lisp"
(add ELSA static analyzer) |
|||
(52 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Infobox major-mode | ||
+ | |title = Emacs Lisp | ||
+ | |library = lisp-mode | ||
+ | |command = emacs-lisp-mode | ||
+ | |builtin = yes | ||
+ | |auto activates = *.el, .emacs, _emacs | ||
+ | }} | ||
+ | '''Emacs Lisp''' is a dialect of the [[Lisp]] programming language used by GNU Emacs. Most of the editing functionality built into Emacs is written in Emacs Lisp, with the remainder being written in C (as is the Lisp interpreter itself). Users of Emacs commonly write Emacs Lisp code to customize and extend Emacs. | ||
+ | |||
+ | Emacs Lisp is also commonly referred to as "elisp" or "Elisp". Files containing Emacs Lisp code use the <tt>.el</tt> filename suffix; when [[byte-compile]]d, the same filename prefix is used but with the <tt>.elc</tt> filename suffix. | ||
+ | |||
+ | Emacs Lisp is a [https://hornbeck.wordpress.com/2009/07/05/lisp-1-vs-lisp-2/ Lisp-2], which means that a single identifier (in Lisp terminology, "symbol") can simultaneously exist as ("be bound to") both a function and a variable. | ||
+ | |||
+ | == Basic setup == | ||
+ | |||
+ | You can customize the way Emacs edits and displays this and all other [[:Category:Lisp|lisp languages]] with {{CustomizeGroup|lisp}}. | ||
+ | |||
+ | == Helpful keybindings == | ||
+ | |||
+ | ; {{Keys|M-TAB}} | ||
+ | : Complete at point | ||
+ | |||
+ | ; {{Keys|C-M-q}} | ||
+ | : Indent the [[S-expression]] following [[point and mark|point]] | ||
+ | |||
+ | ; {{Keys|C-M-x}} | ||
+ | : Evaluate the <code>defun</code> at [[point and mark|point]] | ||
+ | |||
+ | == Common customizations == | ||
+ | |||
+ | === Outlining === | ||
+ | |||
+ | For [[Org]]-style outlining, add the following snippet to your {{EmacsConfigFile}}. | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | ;; Turn on outline minor mode | ||
+ | (add-hook 'emacs-lisp-mode-hook 'outline-minor-mode) | ||
+ | |||
+ | ;; Add key bindings for Org-style outline cycling | ||
+ | (add-hook 'outline-minor-mode-hook | ||
+ | (lambda () | ||
+ | (define-key outline-minor-mode-map [(control tab)] 'org-cycle) | ||
+ | (define-key outline-minor-mode-map [(shift tab)] 'org-global-cycle))) | ||
+ | </source> | ||
+ | |||
+ | Now visit any elisp file (say {{Command|find-library RET outline}}) and keep pressing {{Keys|S-TAB}} and see what happens. Experiment similarly with {{Keys|C-TAB}}. | ||
+ | |||
+ | === Indentation === | ||
+ | |||
+ | Add the following snippet to your {{EmacsConfigFile}}, so that you don't have to indent deliberately. See {{Command|reindent-then-newline-and-indent}}. | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (add-hook 'emacs-lisp-mode-hook | ||
+ | (lambda nil | ||
+ | (local-set-key [(return)] 'reindent-then-newline-and-indent))) | ||
+ | </source> | ||
+ | |||
+ | See also [[Lisp_editing]] and specially Parinfer for modes that help you keep the indentation (and the parens) automatically balanced. | ||
+ | |||
+ | === Always keep parentheses balanced === | ||
+ | |||
+ | See [[Skeleton#Keep some chars always balanced]] | ||
+ | |||
+ | === Scope === | ||
+ | By default elisp uses [[dynamic scope]]. Since Emacs 24 [[lexical scope]] has been added. <br/> | ||
+ | To use lexical binding, an Emacs-lisp source file must set a file-variable {{Variable|lexical-binding}} to <tt>t</tt> in the file header, e.g., by using a first line like: <br/> | ||
+ | |||
+ | ;;; -*- lexical-binding: t -*- | ||
+ | |||
+ | == Static Analysis == | ||
+ | |||
+ | [https://github.com/emacs-elsa/Elsa Elsa] is the Emacs Lisp Static Analyzer. It analyses your code without loading or running it. It can track types and provide helpful hints when things don't match up before you even try to run the code. | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | === elisp-refs - intelligent code search for Emacs Lisp === | ||
+ | |||
+ | [https://github.com/Wilfred/elisp-refs elisp-refs] (in MELPA) can find references to functions, macros or variables. Unlike a dumb text search, elisp-refs actually parses the code, so it's never confused by comments or variables with the same name as functions. This is particularly useful for finding all the places a function is used, or finding examples of usage. Interested readers may enjoy the author's blog post: [http://www.wilfred.me.uk/blog/2016/09/30/searching-a-million-lines-of-lisp/ Searching A Million Lines Of Lisp]. | ||
+ | |||
+ | === Convenient UI to edit lists === | ||
+ | |||
+ | [https://github.com/Wilfred/refine refine] provides a convenient UI for editing variables. Refine is not for editing files, but for changing elisp variables, particularly big lists (such as hooks). | ||
+ | |||
+ | === Lisp editing === | ||
+ | [[lisp editing]] here on wikemacs. | ||
+ | |||
+ | == External links == | ||
+ | |||
+ | * [http://www.gnu.org/software/emacs/manual/html_node/elisp/index.html Emacs Lisp manual] | ||
+ | * [http://libreplanet.org/wiki/Programming_in_elisp Programming in elisp], libreplanet.org wiki | ||
+ | * [http://www.nongnu.org/emacs-tiny-tools/elisp-coding/ Rules on Elisp coding], on nongnu.org | ||
+ | * [https://github.com/bbatsov/emacs-lisp-style-guide Emacs Lisp Style Guide], on Github | ||
+ | |||
[[Category:Lisp]] | [[Category:Lisp]] | ||
+ | [[Category:Emacs Lisp]] | ||
+ | [[Category:Programming]] | ||
+ | [[Category:Programming languages]] |
Latest revision as of 15:12, 25 October 2018
Library | lisp-mode |
---|---|
Command | emacs-lisp-mode |
Builtin | yes |
Auto-activates for | *.el, .emacs, _emacs |
Emacs Lisp is a dialect of the Lisp programming language used by GNU Emacs. Most of the editing functionality built into Emacs is written in Emacs Lisp, with the remainder being written in C (as is the Lisp interpreter itself). Users of Emacs commonly write Emacs Lisp code to customize and extend Emacs.
Emacs Lisp is also commonly referred to as "elisp" or "Elisp". Files containing Emacs Lisp code use the .el filename suffix; when byte-compiled, the same filename prefix is used but with the .elc filename suffix.
Emacs Lisp is a Lisp-2, which means that a single identifier (in Lisp terminology, "symbol") can simultaneously exist as ("be bound to") both a function and a variable.
Basic setup
You can customize the way Emacs edits and displays this and all other lisp languages with M-x customize-group RET lisp RET.
Helpful keybindings
- [M-TAB]
- Complete at point
- [C-M-q]
- Indent the S-expression following point
- [C-M-x]
- Evaluate the
defun
at point
Common customizations
Outlining
For Org-style outlining, add the following snippet to your Emacs configuration file.
;; Turn on outline minor mode
(add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)
;; Add key bindings for Org-style outline cycling
(add-hook 'outline-minor-mode-hook
(lambda ()
(define-key outline-minor-mode-map [(control tab)] 'org-cycle)
(define-key outline-minor-mode-map [(shift tab)] 'org-global-cycle)))
Now visit any elisp file (say M-x find-library RET outline) and keep pressing [S-TAB] and see what happens. Experiment similarly with [C-TAB].
Indentation
Add the following snippet to your Emacs configuration file, so that you don't have to indent deliberately. See M-x reindent-then-newline-and-indent.
(add-hook 'emacs-lisp-mode-hook
(lambda nil
(local-set-key [(return)] 'reindent-then-newline-and-indent)))
See also Lisp_editing and specially Parinfer for modes that help you keep the indentation (and the parens) automatically balanced.
Always keep parentheses balanced
See Skeleton#Keep some chars always balanced
Scope
By default elisp uses dynamic scope. Since Emacs 24 lexical scope has been added.
To use lexical binding, an Emacs-lisp source file must set a file-variable lexical-binding to t in the file header, e.g., by using a first line like:
;;; -*- lexical-binding: t -*-
Static Analysis
Elsa is the Emacs Lisp Static Analyzer. It analyses your code without loading or running it. It can track types and provide helpful hints when things don't match up before you even try to run the code.
See also
elisp-refs - intelligent code search for Emacs Lisp
elisp-refs (in MELPA) can find references to functions, macros or variables. Unlike a dumb text search, elisp-refs actually parses the code, so it's never confused by comments or variables with the same name as functions. This is particularly useful for finding all the places a function is used, or finding examples of usage. Interested readers may enjoy the author's blog post: Searching A Million Lines Of Lisp.
Convenient UI to edit lists
refine provides a convenient UI for editing variables. Refine is not for editing files, but for changing elisp variables, particularly big lists (such as hooks).
Lisp editing
lisp editing here on wikemacs.
External links
- Emacs Lisp manual
- Programming in elisp, libreplanet.org wiki
- Rules on Elisp coding, on nongnu.org
- Emacs Lisp Style Guide, on Github