Emacs Lisp 元素

  • Book Information: Written by Protesilaos Stavrou ("Prot"), provides a big picture view of Emacs Lisp programming language. Stable version 1.0.0 released on 2025-04-12.

  • Table of Contents: 20 sections including getting started, evaluating code, side effects, buffers, text properties, symbols, macros, control flow, etc.
  • Getting started with Emacs Lisp: Purpose is to provide a big picture view. Programming your own text editor is useful and fun. Each chapter is short and has links. Text combines prose and code. Book is not a substitute for the reference manual.
  • Evaluate Emacs Lisp: Everything in Emacs calls functions. Interactive functions are commands. Can evaluate code from anywhere using C-x C-e, eval-buffer, eval-region, M-:, etc. Use the self-documenting nature of Emacs with commands like C-h v, describe-function, etc.
  • Side effect and return value: Functions take inputs and produce outputs. In Emacs Lisp, functions can also have side effects like changing the editor state. Need to account for both return value and side effects.
  • Buffers as data structures: A buffer holds data as a sequence of characters. Can use buffers for various operations like extracting file contents, presenting results, associating variables, etc. Buffers are like bundles of variables.
  • Text has its own properties: Text can have properties associated with it. Use Emacs Lisp to inspect and manipulate text properties. Functions like describe-char provide information about text properties.
  • Symbols, balanced expressions, and quoting: Emacs Lisp has many parentheses. Lists are delimited by parentheses and can be evaluated or taken as-is. Quoting is used to prevent evaluation. Symbols represent functions or variable values. The single quote and sharp quote have specific meanings.
  • Evaluate some elements inside of a list: Can partially evaluate a list or treat it as data. Use quasi-quotes and comma operators to control evaluation. Examples include using my-greeting-in-greek with message function.
  • Evaluation inside of a macro or special form: Special forms like setq, defun, and let have different evaluation rules. Macros like my-work-in-temp-buffer-from-home use quasi-quotes and splicing. Use pp-macroexpand-last-sexp to see macro expansions.
  • Mapping through a list of elements: Use functions like mapcar, mapc, dolist, etc. to map over a list of elements. Choose the appropriate function based on the task. Can use let and related forms to accumulate results.
  • The match data of the last search: Use functions like match-data, match-beginning, match-string to access the match data of the last search. Use save-excursion to inhibit side effects.
  • Switching to another buffer, window, or narrowed state: Use save-excursion, save-window-excursion, save-restriction to switch and restore states. Combine these as needed.
  • Basic control flow with if, cond, and others: Use if, when, unless, cond, etc. for control flow. Indentation helps with parentheses. progn is used for multiple function calls. pcase is a powerful macro.
  • Control flow with if-let* and friends: if-let* and when-let* are used to create bindings based on conditions. if-let* goes to ELSE if a binding is nil.
  • Run some code or fall back to some other code: Use unwind-protect to clean up intermediate states. condition-case is used to handle signals and errors.
  • When to use a named function or a lambda function: Use a named function if it is used more than once. Use a lambda for ad-hoc functions. Named functions can use lambda internally.
  • Make your interactive function also work from Lisp calls: Use interactive specification to make a function work interactively or from Lisp calls.
  • COPYING: Copyright (C) 2025 Protesilaos Stavrou. Permitted to copy, distribute, and modify under the GNU Free Documentation License.
  • GNU Free Documentation License: Details on applying the license to manuals and other works. Covers verbatim copying, copying in quantity, modifications, combining documents, collections, aggregation with independent works, translation, termination, future revisions, and relicensing.
  • ADDENDUM: How to use the license in a document. Include a copy of the license and appropriate copyright and license notices.
阅读 31
0 条评论