lisp-mode.el 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
  2. ;; Copyright (C) 1985-1986, 1999-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: lisp, languages
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
  19. ;; This mode is documented in the Emacs manual.
  20. ;;; Code:
  21. (defvar font-lock-comment-face)
  22. (defvar font-lock-doc-face)
  23. (defvar font-lock-keywords-case-fold-search)
  24. (defvar font-lock-string-face)
  25. (defvar lisp-mode-abbrev-table nil)
  26. (define-abbrev-table 'lisp-mode-abbrev-table ()
  27. "Abbrev table for Lisp mode.")
  28. (defvar emacs-lisp-mode-abbrev-table nil)
  29. (define-abbrev-table 'emacs-lisp-mode-abbrev-table ()
  30. "Abbrev table for Emacs Lisp mode.
  31. It has `lisp-mode-abbrev-table' as its parent."
  32. :parents (list lisp-mode-abbrev-table))
  33. (defvar emacs-lisp-mode-syntax-table
  34. (let ((table (make-syntax-table))
  35. (i 0))
  36. (while (< i ?0)
  37. (modify-syntax-entry i "_ " table)
  38. (setq i (1+ i)))
  39. (setq i (1+ ?9))
  40. (while (< i ?A)
  41. (modify-syntax-entry i "_ " table)
  42. (setq i (1+ i)))
  43. (setq i (1+ ?Z))
  44. (while (< i ?a)
  45. (modify-syntax-entry i "_ " table)
  46. (setq i (1+ i)))
  47. (setq i (1+ ?z))
  48. (while (< i 128)
  49. (modify-syntax-entry i "_ " table)
  50. (setq i (1+ i)))
  51. (modify-syntax-entry ?\s " " table)
  52. ;; Non-break space acts as whitespace.
  53. (modify-syntax-entry ?\x8a0 " " table)
  54. (modify-syntax-entry ?\t " " table)
  55. (modify-syntax-entry ?\f " " table)
  56. (modify-syntax-entry ?\n "> " table)
  57. ;; This is probably obsolete since nowadays such features use overlays.
  58. ;; ;; Give CR the same syntax as newline, for selective-display.
  59. ;; (modify-syntax-entry ?\^m "> " table)
  60. (modify-syntax-entry ?\; "< " table)
  61. (modify-syntax-entry ?` "' " table)
  62. (modify-syntax-entry ?' "' " table)
  63. (modify-syntax-entry ?, "' " table)
  64. (modify-syntax-entry ?@ "' " table)
  65. ;; Used to be singlequote; changed for flonums.
  66. (modify-syntax-entry ?. "_ " table)
  67. (modify-syntax-entry ?# "' " table)
  68. (modify-syntax-entry ?\" "\" " table)
  69. (modify-syntax-entry ?\\ "\\ " table)
  70. (modify-syntax-entry ?\( "() " table)
  71. (modify-syntax-entry ?\) ")( " table)
  72. (modify-syntax-entry ?\[ "(] " table)
  73. (modify-syntax-entry ?\] ")[ " table)
  74. table)
  75. "Syntax table used in `emacs-lisp-mode'.")
  76. (defvar lisp-mode-syntax-table
  77. (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
  78. (modify-syntax-entry ?\[ "_ " table)
  79. (modify-syntax-entry ?\] "_ " table)
  80. (modify-syntax-entry ?# "' 14" table)
  81. (modify-syntax-entry ?| "\" 23bn" table)
  82. table)
  83. "Syntax table used in `lisp-mode'.")
  84. (defvar lisp-imenu-generic-expression
  85. (list
  86. (list nil
  87. (purecopy (concat "^\\s-*("
  88. (eval-when-compile
  89. (regexp-opt
  90. '("defun" "defun*" "defsubst" "defmacro"
  91. "defadvice" "define-skeleton"
  92. "define-minor-mode" "define-global-minor-mode"
  93. "define-globalized-minor-mode"
  94. "define-derived-mode" "define-generic-mode"
  95. "define-compiler-macro" "define-modify-macro"
  96. "defsetf" "define-setf-expander"
  97. "define-method-combination"
  98. "defgeneric" "defmethod") t))
  99. "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
  100. 2)
  101. (list (purecopy "Variables")
  102. (purecopy (concat "^\\s-*("
  103. (eval-when-compile
  104. (regexp-opt
  105. '("defvar" "defconst" "defconstant" "defcustom"
  106. "defparameter" "define-symbol-macro") t))
  107. "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
  108. 2)
  109. (list (purecopy "Types")
  110. (purecopy (concat "^\\s-*("
  111. (eval-when-compile
  112. (regexp-opt
  113. '("defgroup" "deftheme" "deftype" "defstruct"
  114. "defclass" "define-condition" "define-widget"
  115. "defface" "defpackage") t))
  116. "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
  117. 2))
  118. "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
  119. ;; This was originally in autoload.el and is still used there.
  120. (put 'autoload 'doc-string-elt 3)
  121. (put 'defun 'doc-string-elt 3)
  122. (put 'defun* 'doc-string-elt 3)
  123. (put 'defmethod 'doc-string-elt 3)
  124. (put 'defvar 'doc-string-elt 3)
  125. (put 'defcustom 'doc-string-elt 3)
  126. (put 'deftheme 'doc-string-elt 2)
  127. (put 'deftype 'doc-string-elt 3)
  128. (put 'defconst 'doc-string-elt 3)
  129. (put 'defmacro 'doc-string-elt 3)
  130. (put 'defmacro* 'doc-string-elt 3)
  131. (put 'defsubst 'doc-string-elt 3)
  132. (put 'defstruct 'doc-string-elt 2)
  133. (put 'define-skeleton 'doc-string-elt 2)
  134. (put 'define-derived-mode 'doc-string-elt 4)
  135. (put 'define-compilation-mode 'doc-string-elt 3)
  136. (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
  137. (put 'define-minor-mode 'doc-string-elt 2)
  138. (put 'easy-mmode-define-global-mode 'doc-string-elt 2)
  139. (put 'define-global-minor-mode 'doc-string-elt 2)
  140. (put 'define-globalized-minor-mode 'doc-string-elt 2)
  141. (put 'define-generic-mode 'doc-string-elt 7)
  142. (put 'define-ibuffer-filter 'doc-string-elt 2)
  143. (put 'define-ibuffer-op 'doc-string-elt 3)
  144. (put 'define-ibuffer-sorter 'doc-string-elt 2)
  145. (put 'lambda 'doc-string-elt 2)
  146. (put 'defalias 'doc-string-elt 3)
  147. (put 'defvaralias 'doc-string-elt 3)
  148. (put 'define-category 'doc-string-elt 2)
  149. (put 'define-overloadable-function 'doc-string-elt 3)
  150. (defvar lisp-doc-string-elt-property 'doc-string-elt
  151. "The symbol property that holds the docstring position info.")
  152. (defun lisp-font-lock-syntactic-face-function (state)
  153. (if (nth 3 state)
  154. ;; This might be a (doc)string or a |...| symbol.
  155. (let ((startpos (nth 8 state)))
  156. (if (eq (char-after startpos) ?|)
  157. ;; This is not a string, but a |...| symbol.
  158. nil
  159. (let* ((listbeg (nth 1 state))
  160. (firstsym (and listbeg
  161. (save-excursion
  162. (goto-char listbeg)
  163. (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
  164. (match-string 1)))))
  165. (docelt (and firstsym (get (intern-soft firstsym)
  166. lisp-doc-string-elt-property))))
  167. (if (and docelt
  168. ;; It's a string in a form that can have a docstring.
  169. ;; Check whether it's in docstring position.
  170. (save-excursion
  171. (when (functionp docelt)
  172. (goto-char (match-end 1))
  173. (setq docelt (funcall docelt)))
  174. (goto-char listbeg)
  175. (forward-char 1)
  176. (condition-case nil
  177. (while (and (> docelt 0) (< (point) startpos)
  178. (progn (forward-sexp 1) t))
  179. (setq docelt (1- docelt)))
  180. (error nil))
  181. (and (zerop docelt) (<= (point) startpos)
  182. (progn (forward-comment (point-max)) t)
  183. (= (point) (nth 8 state)))))
  184. font-lock-doc-face
  185. font-lock-string-face))))
  186. font-lock-comment-face))
  187. (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive)
  188. "Common initialization routine for lisp modes.
  189. The LISP-SYNTAX argument is used by code in inf-lisp.el and is
  190. \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
  191. score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
  192. font-lock keywords will not be case sensitive."
  193. (when lisp-syntax
  194. (set-syntax-table lisp-mode-syntax-table))
  195. (make-local-variable 'paragraph-ignore-fill-prefix)
  196. (setq paragraph-ignore-fill-prefix t)
  197. (make-local-variable 'fill-paragraph-function)
  198. (setq fill-paragraph-function 'lisp-fill-paragraph)
  199. ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
  200. ;; a single docstring. Let's fix it here.
  201. (set (make-local-variable 'adaptive-fill-function)
  202. (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
  203. ;; Adaptive fill mode gets in the way of auto-fill,
  204. ;; and should make no difference for explicit fill
  205. ;; because lisp-fill-paragraph should do the job.
  206. ;; I believe that newcomment's auto-fill code properly deals with it -stef
  207. ;;(set (make-local-variable 'adaptive-fill-mode) nil)
  208. (make-local-variable 'indent-line-function)
  209. (setq indent-line-function 'lisp-indent-line)
  210. (make-local-variable 'outline-regexp)
  211. (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
  212. (make-local-variable 'outline-level)
  213. (setq outline-level 'lisp-outline-level)
  214. (make-local-variable 'comment-start)
  215. (setq comment-start ";")
  216. (make-local-variable 'comment-start-skip)
  217. ;; Look within the line for a ; following an even number of backslashes
  218. ;; after either a non-backslash or the line beginning.
  219. (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
  220. (make-local-variable 'font-lock-comment-start-skip)
  221. ;; Font lock mode uses this only when it KNOWS a comment is starting.
  222. (setq font-lock-comment-start-skip ";+ *")
  223. (make-local-variable 'comment-add)
  224. (setq comment-add 1) ;default to `;;' in comment-region
  225. (make-local-variable 'comment-column)
  226. (setq comment-column 40)
  227. ;; Don't get confused by `;' in doc strings when paragraph-filling.
  228. (set (make-local-variable 'comment-use-global-state) t)
  229. (make-local-variable 'imenu-generic-expression)
  230. (setq imenu-generic-expression lisp-imenu-generic-expression)
  231. (make-local-variable 'multibyte-syntax-as-symbol)
  232. (setq multibyte-syntax-as-symbol t)
  233. (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
  234. (setq font-lock-defaults
  235. `((lisp-font-lock-keywords
  236. lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
  237. nil ,keywords-case-insensitive (("+-*/.<>=!?$%_&~^:@" . "w")) nil
  238. (font-lock-mark-block-function . mark-defun)
  239. (font-lock-syntactic-face-function
  240. . lisp-font-lock-syntactic-face-function))))
  241. (defun lisp-outline-level ()
  242. "Lisp mode `outline-level' function."
  243. (let ((len (- (match-end 0) (match-beginning 0))))
  244. (if (looking-at "(\\|;;;###autoload")
  245. 1000
  246. len)))
  247. (defvar lisp-mode-shared-map
  248. (let ((map (make-sparse-keymap)))
  249. (define-key map "\e\C-q" 'indent-sexp)
  250. (define-key map "\177" 'backward-delete-char-untabify)
  251. ;; This gets in the way when viewing a Lisp file in view-mode. As
  252. ;; long as [backspace] is mapped into DEL via the
  253. ;; function-key-map, this should remain disabled!!
  254. ;;;(define-key map [backspace] 'backward-delete-char-untabify)
  255. map)
  256. "Keymap for commands shared by all sorts of Lisp modes.")
  257. (defvar emacs-lisp-mode-map
  258. (let ((map (make-sparse-keymap "Emacs-Lisp"))
  259. (menu-map (make-sparse-keymap "Emacs-Lisp"))
  260. (lint-map (make-sparse-keymap))
  261. (prof-map (make-sparse-keymap))
  262. (tracing-map (make-sparse-keymap)))
  263. (set-keymap-parent map lisp-mode-shared-map)
  264. (define-key map "\e\t" 'completion-at-point)
  265. (define-key map "\e\C-x" 'eval-defun)
  266. (define-key map "\e\C-q" 'indent-pp-sexp)
  267. (define-key map [menu-bar emacs-lisp] (cons (purecopy "Emacs-Lisp") menu-map))
  268. (define-key menu-map [eldoc]
  269. `(menu-item ,(purecopy "Auto-Display Documentation Strings") eldoc-mode
  270. :button (:toggle . (bound-and-true-p eldoc-mode))
  271. :help ,(purecopy "Display the documentation string for the item under cursor")))
  272. (define-key menu-map [checkdoc]
  273. `(menu-item ,(purecopy "Check Documentation Strings") checkdoc
  274. :help ,(purecopy "Check documentation strings for style requirements")))
  275. (define-key menu-map [re-builder]
  276. `(menu-item ,(purecopy "Construct Regexp") re-builder
  277. :help ,(purecopy "Construct a regexp interactively")))
  278. (define-key menu-map [tracing] (cons (purecopy "Tracing") tracing-map))
  279. (define-key tracing-map [tr-a]
  280. `(menu-item ,(purecopy "Untrace All") untrace-all
  281. :help ,(purecopy "Untrace all currently traced functions")))
  282. (define-key tracing-map [tr-uf]
  283. `(menu-item ,(purecopy "Untrace Function...") untrace-function
  284. :help ,(purecopy "Untrace function, and possibly activate all remaining advice")))
  285. (define-key tracing-map [tr-sep] menu-bar-separator)
  286. (define-key tracing-map [tr-q]
  287. `(menu-item ,(purecopy "Trace Function Quietly...") trace-function-background
  288. :help ,(purecopy "Trace the function with trace output going quietly to a buffer")))
  289. (define-key tracing-map [tr-f]
  290. `(menu-item ,(purecopy "Trace Function...") trace-function
  291. :help ,(purecopy "Trace the function given as an argument")))
  292. (define-key menu-map [profiling] (cons (purecopy "Profiling") prof-map))
  293. (define-key prof-map [prof-restall]
  294. `(menu-item ,(purecopy "Remove Instrumentation for All Functions") elp-restore-all
  295. :help ,(purecopy "Restore the original definitions of all functions being profiled")))
  296. (define-key prof-map [prof-restfunc]
  297. `(menu-item ,(purecopy "Remove Instrumentation for Function...") elp-restore-function
  298. :help ,(purecopy "Restore an instrumented function to its original definition")))
  299. (define-key prof-map [sep-rem] menu-bar-separator)
  300. (define-key prof-map [prof-resall]
  301. `(menu-item ,(purecopy "Reset Counters for All Functions") elp-reset-all
  302. :help ,(purecopy "Reset the profiling information for all functions being profiled")))
  303. (define-key prof-map [prof-resfunc]
  304. `(menu-item ,(purecopy "Reset Counters for Function...") elp-reset-function
  305. :help ,(purecopy "Reset the profiling information for a function")))
  306. (define-key prof-map [prof-res]
  307. `(menu-item ,(purecopy "Show Profiling Results") elp-results
  308. :help ,(purecopy "Display current profiling results")))
  309. (define-key prof-map [prof-pack]
  310. `(menu-item ,(purecopy "Instrument Package...") elp-instrument-package
  311. :help ,(purecopy "Instrument for profiling all function that start with a prefix")))
  312. (define-key prof-map [prof-func]
  313. `(menu-item ,(purecopy "Instrument Function...") elp-instrument-function
  314. :help ,(purecopy "Instrument a function for profiling")))
  315. (define-key menu-map [lint] (cons (purecopy "Linting") lint-map))
  316. (define-key lint-map [lint-di]
  317. `(menu-item ,(purecopy "Lint Directory...") elint-directory
  318. :help ,(purecopy "Lint a directory")))
  319. (define-key lint-map [lint-f]
  320. `(menu-item ,(purecopy "Lint File...") elint-file
  321. :help ,(purecopy "Lint a file")))
  322. (define-key lint-map [lint-b]
  323. `(menu-item ,(purecopy "Lint Buffer") elint-current-buffer
  324. :help ,(purecopy "Lint the current buffer")))
  325. (define-key lint-map [lint-d]
  326. `(menu-item ,(purecopy "Lint Defun") elint-defun
  327. :help ,(purecopy "Lint the function at point")))
  328. (define-key menu-map [edebug-defun]
  329. `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
  330. :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
  331. :keys ,(purecopy "C-u C-M-x")))
  332. (define-key menu-map [separator-byte] menu-bar-separator)
  333. (define-key menu-map [disas]
  334. `(menu-item ,(purecopy "Disassemble Byte Compiled Object...") disassemble
  335. :help ,(purecopy "Print disassembled code for OBJECT in a buffer")))
  336. (define-key menu-map [byte-recompile]
  337. `(menu-item ,(purecopy "Byte-recompile Directory...") byte-recompile-directory
  338. :help ,(purecopy "Recompile every `.el' file in DIRECTORY that needs recompilation")))
  339. (define-key menu-map [emacs-byte-compile-and-load]
  340. `(menu-item ,(purecopy "Byte-compile and Load") emacs-lisp-byte-compile-and-load
  341. :help ,(purecopy "Byte-compile the current file (if it has changed), then load compiled code")))
  342. (define-key menu-map [byte-compile]
  343. `(menu-item ,(purecopy "Byte-compile This File") emacs-lisp-byte-compile
  344. :help ,(purecopy "Byte compile the file containing the current buffer")))
  345. (define-key menu-map [separator-eval] menu-bar-separator)
  346. (define-key menu-map [ielm]
  347. `(menu-item ,(purecopy "Interactive Expression Evaluation") ielm
  348. :help ,(purecopy "Interactively evaluate Emacs Lisp expressions")))
  349. (define-key menu-map [eval-buffer]
  350. `(menu-item ,(purecopy "Evaluate Buffer") eval-buffer
  351. :help ,(purecopy "Execute the current buffer as Lisp code")))
  352. (define-key menu-map [eval-region]
  353. `(menu-item ,(purecopy "Evaluate Region") eval-region
  354. :help ,(purecopy "Execute the region as Lisp code")
  355. :enable mark-active))
  356. (define-key menu-map [eval-sexp]
  357. `(menu-item ,(purecopy "Evaluate Last S-expression") eval-last-sexp
  358. :help ,(purecopy "Evaluate sexp before point; print value in minibuffer")))
  359. (define-key menu-map [separator-format] menu-bar-separator)
  360. (define-key menu-map [comment-region]
  361. `(menu-item ,(purecopy "Comment Out Region") comment-region
  362. :help ,(purecopy "Comment or uncomment each line in the region")
  363. :enable mark-active))
  364. (define-key menu-map [indent-region]
  365. `(menu-item ,(purecopy "Indent Region") indent-region
  366. :help ,(purecopy "Indent each nonblank line in the region")
  367. :enable mark-active))
  368. (define-key menu-map [indent-line]
  369. `(menu-item ,(purecopy "Indent Line") lisp-indent-line))
  370. map)
  371. "Keymap for Emacs Lisp mode.
  372. All commands in `lisp-mode-shared-map' are inherited by this map.")
  373. (defun emacs-lisp-byte-compile ()
  374. "Byte compile the file containing the current buffer."
  375. (interactive)
  376. (if buffer-file-name
  377. (byte-compile-file buffer-file-name)
  378. (error "The buffer must be saved in a file first")))
  379. (defun emacs-lisp-byte-compile-and-load ()
  380. "Byte-compile the current file (if it has changed), then load compiled code."
  381. (interactive)
  382. (or buffer-file-name
  383. (error "The buffer must be saved in a file first"))
  384. (require 'bytecomp)
  385. ;; Recompile if file or buffer has changed since last compilation.
  386. (if (and (buffer-modified-p)
  387. (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
  388. (save-buffer))
  389. (byte-recompile-file buffer-file-name nil 0 t))
  390. (defcustom emacs-lisp-mode-hook nil
  391. "Hook run when entering Emacs Lisp mode."
  392. :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
  393. :type 'hook
  394. :group 'lisp)
  395. (defcustom lisp-mode-hook nil
  396. "Hook run when entering Lisp mode."
  397. :options '(imenu-add-menubar-index)
  398. :type 'hook
  399. :group 'lisp)
  400. (defcustom lisp-interaction-mode-hook nil
  401. "Hook run when entering Lisp Interaction mode."
  402. :options '(turn-on-eldoc-mode)
  403. :type 'hook
  404. :group 'lisp)
  405. (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
  406. "Major mode for editing Lisp code to run in Emacs.
  407. Commands:
  408. Delete converts tabs to spaces as it moves back.
  409. Blank lines separate paragraphs. Semicolons start comments.
  410. \\{emacs-lisp-mode-map}
  411. Entry to this mode calls the value of `emacs-lisp-mode-hook'
  412. if that value is non-nil."
  413. :group 'lisp
  414. (lisp-mode-variables)
  415. (setq imenu-case-fold-search nil)
  416. (add-hook 'completion-at-point-functions
  417. 'lisp-completion-at-point nil 'local))
  418. (defvar lisp-mode-map
  419. (let ((map (make-sparse-keymap))
  420. (menu-map (make-sparse-keymap "Lisp")))
  421. (set-keymap-parent map lisp-mode-shared-map)
  422. (define-key map "\e\C-x" 'lisp-eval-defun)
  423. (define-key map "\C-c\C-z" 'run-lisp)
  424. (define-key map [menu-bar lisp] (cons (purecopy "Lisp") menu-map))
  425. (define-key menu-map [run-lisp]
  426. `(menu-item ,(purecopy "Run inferior Lisp") run-lisp
  427. :help ,(purecopy "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'")))
  428. (define-key menu-map [ev-def]
  429. `(menu-item ,(purecopy "Eval defun") lisp-eval-defun
  430. :help ,(purecopy "Send the current defun to the Lisp process made by M-x run-lisp")))
  431. (define-key menu-map [ind-sexp]
  432. `(menu-item ,(purecopy "Indent sexp") indent-sexp
  433. :help ,(purecopy "Indent each line of the list starting just after point")))
  434. map)
  435. "Keymap for ordinary Lisp mode.
  436. All commands in `lisp-mode-shared-map' are inherited by this map.")
  437. (define-derived-mode lisp-mode prog-mode "Lisp"
  438. "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
  439. Commands:
  440. Delete converts tabs to spaces as it moves back.
  441. Blank lines separate paragraphs. Semicolons start comments.
  442. \\{lisp-mode-map}
  443. Note that `run-lisp' may be used either to start an inferior Lisp job
  444. or to switch back to an existing one.
  445. Entry to this mode calls the value of `lisp-mode-hook'
  446. if that value is non-nil."
  447. (lisp-mode-variables nil t)
  448. (set (make-local-variable 'find-tag-default-function) 'lisp-find-tag-default)
  449. (make-local-variable 'comment-start-skip)
  450. (setq comment-start-skip
  451. "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
  452. (setq imenu-case-fold-search t))
  453. (defun lisp-find-tag-default ()
  454. (let ((default (find-tag-default)))
  455. (when (stringp default)
  456. (if (string-match ":+" default)
  457. (substring default (match-end 0))
  458. default))))
  459. ;; Used in old LispM code.
  460. (defalias 'common-lisp-mode 'lisp-mode)
  461. ;; This will do unless inf-lisp.el is loaded.
  462. (defun lisp-eval-defun (&optional and-go)
  463. "Send the current defun to the Lisp process made by \\[run-lisp]."
  464. (interactive)
  465. (error "Process lisp does not exist"))
  466. (defvar lisp-interaction-mode-map
  467. (let ((map (make-sparse-keymap))
  468. (menu-map (make-sparse-keymap "Lisp-Interaction")))
  469. (set-keymap-parent map lisp-mode-shared-map)
  470. (define-key map "\e\C-x" 'eval-defun)
  471. (define-key map "\e\C-q" 'indent-pp-sexp)
  472. (define-key map "\e\t" 'completion-at-point)
  473. (define-key map "\n" 'eval-print-last-sexp)
  474. (define-key map [menu-bar lisp-interaction] (cons (purecopy "Lisp-Interaction") menu-map))
  475. (define-key menu-map [eval-defun]
  476. `(menu-item ,(purecopy "Evaluate Defun") eval-defun
  477. :help ,(purecopy "Evaluate the top-level form containing point, or after point")))
  478. (define-key menu-map [eval-print-last-sexp]
  479. `(menu-item ,(purecopy "Evaluate and Print") eval-print-last-sexp
  480. :help ,(purecopy "Evaluate sexp before point; print value into current buffer")))
  481. (define-key menu-map [edebug-defun-lisp-interaction]
  482. `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
  483. :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
  484. :keys ,(purecopy "C-u C-M-x")))
  485. (define-key menu-map [indent-pp-sexp]
  486. `(menu-item ,(purecopy "Indent or Pretty-Print") indent-pp-sexp
  487. :help ,(purecopy "Indent each line of the list starting just after point, or prettyprint it")))
  488. (define-key menu-map [complete-symbol]
  489. `(menu-item ,(purecopy "Complete Lisp Symbol") completion-at-point
  490. :help ,(purecopy "Perform completion on Lisp symbol preceding point")))
  491. map)
  492. "Keymap for Lisp Interaction mode.
  493. All commands in `lisp-mode-shared-map' are inherited by this map.")
  494. (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
  495. "Major mode for typing and evaluating Lisp forms.
  496. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
  497. before point, and prints its value into the buffer, advancing point.
  498. Note that printing is controlled by `eval-expression-print-length'
  499. and `eval-expression-print-level'.
  500. Commands:
  501. Delete converts tabs to spaces as it moves back.
  502. Paragraphs are separated only by blank lines.
  503. Semicolons start comments.
  504. \\{lisp-interaction-mode-map}
  505. Entry to this mode calls the value of `lisp-interaction-mode-hook'
  506. if that value is non-nil."
  507. :abbrev-table nil)
  508. (defun eval-print-last-sexp ()
  509. "Evaluate sexp before point; print value into current buffer.
  510. If `eval-expression-debug-on-error' is non-nil, which is the default,
  511. this command arranges for all errors to enter the debugger.
  512. Note that printing the result is controlled by the variables
  513. `eval-expression-print-length' and `eval-expression-print-level',
  514. which see."
  515. (interactive)
  516. (let ((standard-output (current-buffer)))
  517. (terpri)
  518. (eval-last-sexp t)
  519. (terpri)))
  520. (defun last-sexp-setup-props (beg end value alt1 alt2)
  521. "Set up text properties for the output of `eval-last-sexp-1'.
  522. BEG and END are the start and end of the output in current-buffer.
  523. VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
  524. alternative printed representations that can be displayed."
  525. (let ((map (make-sparse-keymap)))
  526. (define-key map "\C-m" 'last-sexp-toggle-display)
  527. (define-key map [down-mouse-2] 'mouse-set-point)
  528. (define-key map [mouse-2] 'last-sexp-toggle-display)
  529. (add-text-properties
  530. beg end
  531. `(printed-value (,value ,alt1 ,alt2)
  532. mouse-face highlight
  533. keymap ,map
  534. help-echo "RET, mouse-2: toggle abbreviated display"
  535. rear-nonsticky (mouse-face keymap help-echo
  536. printed-value)))))
  537. (defun last-sexp-toggle-display (&optional arg)
  538. "Toggle between abbreviated and unabbreviated printed representations."
  539. (interactive "P")
  540. (save-restriction
  541. (widen)
  542. (let ((value (get-text-property (point) 'printed-value)))
  543. (when value
  544. (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
  545. 'printed-value)
  546. (point)))
  547. (end (or (next-single-char-property-change (point) 'printed-value) (point)))
  548. (standard-output (current-buffer))
  549. (point (point)))
  550. (delete-region beg end)
  551. (insert (nth 1 value))
  552. (or (= beg point)
  553. (setq point (1- (point))))
  554. (last-sexp-setup-props beg (point)
  555. (nth 0 value)
  556. (nth 2 value)
  557. (nth 1 value))
  558. (goto-char (min (point-max) point)))))))
  559. (defun prin1-char (char)
  560. "Return a string representing CHAR as a character rather than as an integer.
  561. If CHAR is not a character, return nil."
  562. (and (integerp char)
  563. (eventp char)
  564. (let ((c (event-basic-type char))
  565. (mods (event-modifiers char))
  566. string)
  567. ;; Prevent ?A from turning into ?\S-a.
  568. (if (and (memq 'shift mods)
  569. (zerop (logand char ?\S-\^@))
  570. (not (let ((case-fold-search nil))
  571. (char-equal c (upcase c)))))
  572. (setq c (upcase c) mods nil))
  573. ;; What string are we considering using?
  574. (condition-case nil
  575. (setq string
  576. (concat
  577. "?"
  578. (mapconcat
  579. (lambda (modif)
  580. (cond ((eq modif 'super) "\\s-")
  581. (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
  582. mods "")
  583. (cond
  584. ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
  585. ((eq c 127) "\\C-?")
  586. (t
  587. (string c)))))
  588. (error nil))
  589. ;; Verify the string reads a CHAR, not to some other character.
  590. ;; If it doesn't, return nil instead.
  591. (and string
  592. (= (car (read-from-string string)) char)
  593. string))))
  594. (defun preceding-sexp ()
  595. "Return sexp before the point."
  596. (let ((opoint (point))
  597. ignore-quotes
  598. expr)
  599. (save-excursion
  600. (with-syntax-table emacs-lisp-mode-syntax-table
  601. ;; If this sexp appears to be enclosed in `...'
  602. ;; then ignore the surrounding quotes.
  603. (setq ignore-quotes
  604. (or (eq (following-char) ?\')
  605. (eq (preceding-char) ?\')))
  606. (forward-sexp -1)
  607. ;; If we were after `?\e' (or similar case),
  608. ;; use the whole thing, not just the `e'.
  609. (when (eq (preceding-char) ?\\)
  610. (forward-char -1)
  611. (when (eq (preceding-char) ??)
  612. (forward-char -1)))
  613. ;; Skip over hash table read syntax.
  614. (and (> (point) (1+ (point-min)))
  615. (looking-back "#s" (- (point) 2))
  616. (forward-char -2))
  617. ;; Skip over `#N='s.
  618. (when (eq (preceding-char) ?=)
  619. (let (labeled-p)
  620. (save-excursion
  621. (skip-chars-backward "0-9#=")
  622. (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
  623. (when labeled-p
  624. (forward-sexp -1))))
  625. (save-restriction
  626. ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
  627. ;; `variable' so that the value is returned, not the
  628. ;; name
  629. (if (and ignore-quotes
  630. (eq (following-char) ?`))
  631. (forward-char))
  632. (narrow-to-region (point-min) opoint)
  633. (setq expr (read (current-buffer)))
  634. ;; If it's an (interactive ...) form, it's more
  635. ;; useful to show how an interactive call would
  636. ;; use it.
  637. (and (consp expr)
  638. (eq (car expr) 'interactive)
  639. (setq expr
  640. (list 'call-interactively
  641. (list 'quote
  642. (list 'lambda
  643. '(&rest args)
  644. expr
  645. 'args)))))
  646. expr)))))
  647. (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
  648. "Evaluate sexp before point; print value in minibuffer.
  649. With argument, print output into current buffer."
  650. (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
  651. ;; Setup the lexical environment if lexical-binding is enabled.
  652. (eval-last-sexp-print-value
  653. (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding))))
  654. (defun eval-last-sexp-print-value (value)
  655. (let ((unabbreviated (let ((print-length nil) (print-level nil))
  656. (prin1-to-string value)))
  657. (print-length eval-expression-print-length)
  658. (print-level eval-expression-print-level)
  659. (beg (point))
  660. end)
  661. (prog1
  662. (prin1 value)
  663. (let ((str (eval-expression-print-format value)))
  664. (if str (princ str)))
  665. (setq end (point))
  666. (when (and (bufferp standard-output)
  667. (or (not (null print-length))
  668. (not (null print-level)))
  669. (not (string= unabbreviated
  670. (buffer-substring-no-properties beg end))))
  671. (last-sexp-setup-props beg end value
  672. unabbreviated
  673. (buffer-substring-no-properties beg end))
  674. ))))
  675. (defvar eval-last-sexp-fake-value (make-symbol "t"))
  676. (defun eval-sexp-add-defvars (exp &optional pos)
  677. "Prepend EXP with all the `defvar's that precede it in the buffer.
  678. POS specifies the starting position where EXP was found and defaults to point."
  679. (if (not lexical-binding)
  680. exp
  681. (save-excursion
  682. (unless pos (setq pos (point)))
  683. (let ((vars ()))
  684. (goto-char (point-min))
  685. (while (re-search-forward
  686. "^(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
  687. pos t)
  688. (let ((var (intern (match-string 1))))
  689. (unless (special-variable-p var)
  690. (push var vars))))
  691. `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
  692. (defun eval-last-sexp (eval-last-sexp-arg-internal)
  693. "Evaluate sexp before point; print value in minibuffer.
  694. Interactively, with prefix argument, print output into current buffer.
  695. Truncates long output according to the value of the variables
  696. `eval-expression-print-length' and `eval-expression-print-level'.
  697. If `eval-expression-debug-on-error' is non-nil, which is the default,
  698. this command arranges for all errors to enter the debugger."
  699. (interactive "P")
  700. (if (null eval-expression-debug-on-error)
  701. (eval-last-sexp-1 eval-last-sexp-arg-internal)
  702. (let ((value
  703. (let ((debug-on-error eval-last-sexp-fake-value))
  704. (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
  705. debug-on-error))))
  706. (unless (eq (cdr value) eval-last-sexp-fake-value)
  707. (setq debug-on-error (cdr value)))
  708. (car value))))
  709. (defun eval-defun-1 (form)
  710. "Treat some expressions specially.
  711. Reset the `defvar' and `defcustom' variables to the initial value.
  712. Reinitialize the face according to the `defface' specification."
  713. ;; The code in edebug-defun should be consistent with this, but not
  714. ;; the same, since this gets a macroexpanded form.
  715. (cond ((not (listp form))
  716. form)
  717. ((and (eq (car form) 'defvar)
  718. (cdr-safe (cdr-safe form))
  719. (boundp (cadr form)))
  720. ;; Force variable to be re-set.
  721. `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
  722. (setq-default ,(nth 1 form) ,(nth 2 form))))
  723. ;; `defcustom' is now macroexpanded to
  724. ;; `custom-declare-variable' with a quoted value arg.
  725. ((and (eq (car form) 'custom-declare-variable)
  726. (default-boundp (eval (nth 1 form) lexical-binding)))
  727. ;; Force variable to be bound.
  728. (set-default (eval (nth 1 form) lexical-binding)
  729. (eval (nth 1 (nth 2 form)) lexical-binding))
  730. form)
  731. ;; `defface' is macroexpanded to `custom-declare-face'.
  732. ((eq (car form) 'custom-declare-face)
  733. ;; Reset the face.
  734. (let ((face-symbol (eval (nth 1 form) lexical-binding)))
  735. (setq face-new-frame-defaults
  736. (assq-delete-all face-symbol face-new-frame-defaults))
  737. (put face-symbol 'face-defface-spec nil)
  738. (put face-symbol 'face-documentation (nth 3 form))
  739. ;; Setting `customized-face' to the new spec after calling
  740. ;; the form, but preserving the old saved spec in `saved-face',
  741. ;; imitates the situation when the new face spec is set
  742. ;; temporarily for the current session in the customize
  743. ;; buffer, thus allowing `face-user-default-spec' to use the
  744. ;; new customized spec instead of the saved spec.
  745. ;; Resetting `saved-face' temporarily to nil is needed to let
  746. ;; `defface' change the spec, regardless of a saved spec.
  747. (prog1 `(prog1 ,form
  748. (put ,(nth 1 form) 'saved-face
  749. ',(get face-symbol 'saved-face))
  750. (put ,(nth 1 form) 'customized-face
  751. ,(nth 2 form)))
  752. (put face-symbol 'saved-face nil))))
  753. ((eq (car form) 'progn)
  754. (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
  755. (t form)))
  756. (defun eval-defun-2 ()
  757. "Evaluate defun that point is in or before.
  758. The value is displayed in the minibuffer.
  759. If the current defun is actually a call to `defvar',
  760. then reset the variable using the initial value expression
  761. even if the variable already has some other value.
  762. \(Normally `defvar' does not change the variable's value
  763. if it already has a value.\)
  764. With argument, insert value in current buffer after the defun.
  765. Return the result of evaluation."
  766. (interactive "P")
  767. ;; FIXME: the print-length/level bindings should only be applied while
  768. ;; printing, not while evaluating.
  769. (let ((debug-on-error eval-expression-debug-on-error)
  770. (print-length eval-expression-print-length)
  771. (print-level eval-expression-print-level))
  772. (save-excursion
  773. ;; Arrange for eval-region to "read" the (possibly) altered form.
  774. ;; eval-region handles recording which file defines a function or
  775. ;; variable. Re-written using `apply' to avoid capturing
  776. ;; variables like `end'.
  777. (apply
  778. #'eval-region
  779. (let ((standard-output t)
  780. beg end form)
  781. ;; Read the form from the buffer, and record where it ends.
  782. (save-excursion
  783. (end-of-defun)
  784. (beginning-of-defun)
  785. (setq beg (point))
  786. (setq form (eval-sexp-add-defvars (read (current-buffer))))
  787. (setq end (point)))
  788. ;; Alter the form if necessary.
  789. (setq form (eval-defun-1 (macroexpand form)))
  790. (list beg end standard-output
  791. `(lambda (ignore)
  792. ;; Skipping to the end of the specified region
  793. ;; will make eval-region return.
  794. (goto-char ,end)
  795. ',form))))))
  796. ;; The result of evaluation has been put onto VALUES. So return it.
  797. (car values))
  798. (defun eval-defun (edebug-it)
  799. "Evaluate the top-level form containing point, or after point.
  800. If the current defun is actually a call to `defvar' or `defcustom',
  801. evaluating it this way resets the variable using its initial value
  802. expression even if the variable already has some other value.
  803. \(Normally `defvar' and `defcustom' do not alter the value if there
  804. already is one.) In an analogous way, evaluating a `defface'
  805. overrides any customizations of the face, so that it becomes
  806. defined exactly as the `defface' expression says.
  807. If `eval-expression-debug-on-error' is non-nil, which is the default,
  808. this command arranges for all errors to enter the debugger.
  809. With a prefix argument, instrument the code for Edebug.
  810. If acting on a `defun' for FUNCTION, and the function was
  811. instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
  812. instrumented, just FUNCTION is printed.
  813. If not acting on a `defun', the result of evaluation is displayed in
  814. the minibuffer. This display is controlled by the variables
  815. `eval-expression-print-length' and `eval-expression-print-level',
  816. which see."
  817. (interactive "P")
  818. (cond (edebug-it
  819. (require 'edebug)
  820. (eval-defun (not edebug-all-defs)))
  821. (t
  822. (if (null eval-expression-debug-on-error)
  823. (eval-defun-2)
  824. (let ((old-value (make-symbol "t")) new-value value)
  825. (let ((debug-on-error old-value))
  826. (setq value (eval-defun-2))
  827. (setq new-value debug-on-error))
  828. (unless (eq old-value new-value)
  829. (setq debug-on-error new-value))
  830. value)))))
  831. ;; May still be used by some external Lisp-mode variant.
  832. (define-obsolete-function-alias 'lisp-comment-indent
  833. 'comment-indent-default "22.1")
  834. (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
  835. (defcustom lisp-indent-offset nil
  836. "If non-nil, indent second line of expressions that many more columns."
  837. :group 'lisp
  838. :type '(choice (const nil) integer))
  839. (put 'lisp-indent-offset 'safe-local-variable
  840. (lambda (x) (or (null x) (integerp x))))
  841. (defcustom lisp-indent-function 'lisp-indent-function
  842. "A function to be called by `calculate-lisp-indent'.
  843. It indents the arguments of a Lisp function call. This function
  844. should accept two arguments: the indent-point, and the
  845. `parse-partial-sexp' state at that position. One option for this
  846. function is `common-lisp-indent-function'."
  847. :type 'function
  848. :group 'lisp)
  849. (defun lisp-indent-line (&optional whole-exp)
  850. "Indent current line as Lisp code.
  851. With argument, indent any additional lines of the same expression
  852. rigidly along with this one."
  853. (interactive "P")
  854. (let ((indent (calculate-lisp-indent)) shift-amt end
  855. (pos (- (point-max) (point)))
  856. (beg (progn (beginning-of-line) (point))))
  857. (skip-chars-forward " \t")
  858. (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
  859. ;; Don't alter indentation of a ;;; comment line
  860. ;; or a line that starts in a string.
  861. (goto-char (- (point-max) pos))
  862. (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
  863. ;; Single-semicolon comment lines should be indented
  864. ;; as comment lines, not as code.
  865. (progn (indent-for-comment) (forward-char -1))
  866. (if (listp indent) (setq indent (car indent)))
  867. (setq shift-amt (- indent (current-column)))
  868. (if (zerop shift-amt)
  869. nil
  870. (delete-region beg (point))
  871. (indent-to indent)))
  872. ;; If initial point was within line's indentation,
  873. ;; position after the indentation. Else stay at same point in text.
  874. (if (> (- (point-max) pos) (point))
  875. (goto-char (- (point-max) pos)))
  876. ;; If desired, shift remaining lines of expression the same amount.
  877. (and whole-exp (not (zerop shift-amt))
  878. (save-excursion
  879. (goto-char beg)
  880. (forward-sexp 1)
  881. (setq end (point))
  882. (goto-char beg)
  883. (forward-line 1)
  884. (setq beg (point))
  885. (> end beg))
  886. (indent-code-rigidly beg end shift-amt)))))
  887. (defvar calculate-lisp-indent-last-sexp)
  888. (defun calculate-lisp-indent (&optional parse-start)
  889. "Return appropriate indentation for current line as Lisp code.
  890. In usual case returns an integer: the column to indent to.
  891. If the value is nil, that means don't change the indentation
  892. because the line starts inside a string.
  893. The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
  894. This means that following lines at the same level of indentation
  895. should not necessarily be indented the same as this line.
  896. Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
  897. is the buffer position of the start of the containing expression."
  898. (save-excursion
  899. (beginning-of-line)
  900. (let ((indent-point (point))
  901. state paren-depth
  902. ;; setting this to a number inhibits calling hook
  903. (desired-indent nil)
  904. (retry t)
  905. calculate-lisp-indent-last-sexp containing-sexp)
  906. (if parse-start
  907. (goto-char parse-start)
  908. (beginning-of-defun))
  909. ;; Find outermost containing sexp
  910. (while (< (point) indent-point)
  911. (setq state (parse-partial-sexp (point) indent-point 0)))
  912. ;; Find innermost containing sexp
  913. (while (and retry
  914. state
  915. (> (setq paren-depth (elt state 0)) 0))
  916. (setq retry nil)
  917. (setq calculate-lisp-indent-last-sexp (elt state 2))
  918. (setq containing-sexp (elt state 1))
  919. ;; Position following last unclosed open.
  920. (goto-char (1+ containing-sexp))
  921. ;; Is there a complete sexp since then?
  922. (if (and calculate-lisp-indent-last-sexp
  923. (> calculate-lisp-indent-last-sexp (point)))
  924. ;; Yes, but is there a containing sexp after that?
  925. (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
  926. indent-point 0)))
  927. (if (setq retry (car (cdr peek))) (setq state peek)))))
  928. (if retry
  929. nil
  930. ;; Innermost containing sexp found
  931. (goto-char (1+ containing-sexp))
  932. (if (not calculate-lisp-indent-last-sexp)
  933. ;; indent-point immediately follows open paren.
  934. ;; Don't call hook.
  935. (setq desired-indent (current-column))
  936. ;; Find the start of first element of containing sexp.
  937. (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  938. (cond ((looking-at "\\s(")
  939. ;; First element of containing sexp is a list.
  940. ;; Indent under that list.
  941. )
  942. ((> (save-excursion (forward-line 1) (point))
  943. calculate-lisp-indent-last-sexp)
  944. ;; This is the first line to start within the containing sexp.
  945. ;; It's almost certainly a function call.
  946. (if (= (point) calculate-lisp-indent-last-sexp)
  947. ;; Containing sexp has nothing before this line
  948. ;; except the first element. Indent under that element.
  949. nil
  950. ;; Skip the first element, find start of second (the first
  951. ;; argument of the function call) and indent under.
  952. (progn (forward-sexp 1)
  953. (parse-partial-sexp (point)
  954. calculate-lisp-indent-last-sexp
  955. 0 t)))
  956. (backward-prefix-chars))
  957. (t
  958. ;; Indent beneath first sexp on same line as
  959. ;; `calculate-lisp-indent-last-sexp'. Again, it's
  960. ;; almost certainly a function call.
  961. (goto-char calculate-lisp-indent-last-sexp)
  962. (beginning-of-line)
  963. (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
  964. 0 t)
  965. (backward-prefix-chars)))))
  966. ;; Point is at the point to indent under unless we are inside a string.
  967. ;; Call indentation hook except when overridden by lisp-indent-offset
  968. ;; or if the desired indentation has already been computed.
  969. (let ((normal-indent (current-column)))
  970. (cond ((elt state 3)
  971. ;; Inside a string, don't change indentation.
  972. nil)
  973. ((and (integerp lisp-indent-offset) containing-sexp)
  974. ;; Indent by constant offset
  975. (goto-char containing-sexp)
  976. (+ (current-column) lisp-indent-offset))
  977. ;; in this case calculate-lisp-indent-last-sexp is not nil
  978. (calculate-lisp-indent-last-sexp
  979. (or
  980. ;; try to align the parameters of a known function
  981. (and lisp-indent-function
  982. (not retry)
  983. (funcall lisp-indent-function indent-point state))
  984. ;; If the function has no special alignment
  985. ;; or it does not apply to this argument,
  986. ;; try to align a constant-symbol under the last
  987. ;; preceding constant symbol, if there is such one of
  988. ;; the last 2 preceding symbols, in the previous
  989. ;; uncommented line.
  990. (and (save-excursion
  991. (goto-char indent-point)
  992. (skip-chars-forward " \t")
  993. (looking-at ":"))
  994. ;; The last sexp may not be at the indentation
  995. ;; where it begins, so find that one, instead.
  996. (save-excursion
  997. (goto-char calculate-lisp-indent-last-sexp)
  998. ;; Handle prefix characters and whitespace
  999. ;; following an open paren. (Bug#1012)
  1000. (backward-prefix-chars)
  1001. (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
  1002. (or (not containing-sexp)
  1003. (< (1+ containing-sexp) (point))))
  1004. (forward-sexp -1)
  1005. (backward-prefix-chars))
  1006. (setq calculate-lisp-indent-last-sexp (point)))
  1007. (> calculate-lisp-indent-last-sexp
  1008. (save-excursion
  1009. (goto-char (1+ containing-sexp))
  1010. (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  1011. (point)))
  1012. (let ((parse-sexp-ignore-comments t)
  1013. indent)
  1014. (goto-char calculate-lisp-indent-last-sexp)
  1015. (or (and (looking-at ":")
  1016. (setq indent (current-column)))
  1017. (and (< (line-beginning-position)
  1018. (prog2 (backward-sexp) (point)))
  1019. (looking-at ":")
  1020. (setq indent (current-column))))
  1021. indent))
  1022. ;; another symbols or constants not preceded by a constant
  1023. ;; as defined above.
  1024. normal-indent))
  1025. ;; in this case calculate-lisp-indent-last-sexp is nil
  1026. (desired-indent)
  1027. (t
  1028. normal-indent))))))
  1029. (defun lisp-indent-function (indent-point state)
  1030. "This function is the normal value of the variable `lisp-indent-function'.
  1031. The function `calculate-lisp-indent' calls this to determine
  1032. if the arguments of a Lisp function call should be indented specially.
  1033. INDENT-POINT is the position where the user typed TAB, or equivalent.
  1034. Point is located at the point to indent under (for default indentation);
  1035. STATE is the `parse-partial-sexp' state for that position.
  1036. If the current line is in a call to a Lisp function that has a non-nil
  1037. property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
  1038. it specifies how to indent. The property value can be:
  1039. * `defun', meaning indent `defun'-style
  1040. \(this is also the case if there is no property and the function
  1041. has a name that begins with \"def\", and three or more arguments);
  1042. * an integer N, meaning indent the first N arguments specially
  1043. (like ordinary function arguments), and then indent any further
  1044. arguments like a body;
  1045. * a function to call that returns the indentation (or nil).
  1046. `lisp-indent-function' calls this function with the same two arguments
  1047. that it itself received.
  1048. This function returns either the indentation to use, or nil if the
  1049. Lisp function does not specify a special indentation."
  1050. (let ((normal-indent (current-column)))
  1051. (goto-char (1+ (elt state 1)))
  1052. (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  1053. (if (and (elt state 2)
  1054. (not (looking-at "\\sw\\|\\s_")))
  1055. ;; car of form doesn't seem to be a symbol
  1056. (progn
  1057. (if (not (> (save-excursion (forward-line 1) (point))
  1058. calculate-lisp-indent-last-sexp))
  1059. (progn (goto-char calculate-lisp-indent-last-sexp)
  1060. (beginning-of-line)
  1061. (parse-partial-sexp (point)
  1062. calculate-lisp-indent-last-sexp 0 t)))
  1063. ;; Indent under the list or under the first sexp on the same
  1064. ;; line as calculate-lisp-indent-last-sexp. Note that first
  1065. ;; thing on that line has to be complete sexp since we are
  1066. ;; inside the innermost containing sexp.
  1067. (backward-prefix-chars)
  1068. (current-column))
  1069. (let ((function (buffer-substring (point)
  1070. (progn (forward-sexp 1) (point))))
  1071. method)
  1072. (setq method (or (get (intern-soft function) 'lisp-indent-function)
  1073. (get (intern-soft function) 'lisp-indent-hook)))
  1074. (cond ((or (eq method 'defun)
  1075. (and (null method)
  1076. (> (length function) 3)
  1077. (string-match "\\`def" function)))
  1078. (lisp-indent-defform state indent-point))
  1079. ((integerp method)
  1080. (lisp-indent-specform method state
  1081. indent-point normal-indent))
  1082. (method
  1083. (funcall method indent-point state)))))))
  1084. (defcustom lisp-body-indent 2
  1085. "Number of columns to indent the second line of a `(def...)' form."
  1086. :group 'lisp
  1087. :type 'integer)
  1088. (put 'lisp-body-indent 'safe-local-variable 'integerp)
  1089. (defun lisp-indent-specform (count state indent-point normal-indent)
  1090. (let ((containing-form-start (elt state 1))
  1091. (i count)
  1092. body-indent containing-form-column)
  1093. ;; Move to the start of containing form, calculate indentation
  1094. ;; to use for non-distinguished forms (> count), and move past the
  1095. ;; function symbol. lisp-indent-function guarantees that there is at
  1096. ;; least one word or symbol character following open paren of containing
  1097. ;; form.
  1098. (goto-char containing-form-start)
  1099. (setq containing-form-column (current-column))
  1100. (setq body-indent (+ lisp-body-indent containing-form-column))
  1101. (forward-char 1)
  1102. (forward-sexp 1)
  1103. ;; Now find the start of the last form.
  1104. (parse-partial-sexp (point) indent-point 1 t)
  1105. (while (and (< (point) indent-point)
  1106. (condition-case ()
  1107. (progn
  1108. (setq count (1- count))
  1109. (forward-sexp 1)
  1110. (parse-partial-sexp (point) indent-point 1 t))
  1111. (error nil))))
  1112. ;; Point is sitting on first character of last (or count) sexp.
  1113. (if (> count 0)
  1114. ;; A distinguished form. If it is the first or second form use double
  1115. ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
  1116. ;; to 2 (the default), this just happens to work the same with if as
  1117. ;; the older code, but it makes unwind-protect, condition-case,
  1118. ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
  1119. ;; less hacked, behavior can be obtained by replacing below with
  1120. ;; (list normal-indent containing-form-start).
  1121. (if (<= (- i count) 1)
  1122. (list (+ containing-form-column (* 2 lisp-body-indent))
  1123. containing-form-start)
  1124. (list normal-indent containing-form-start))
  1125. ;; A non-distinguished form. Use body-indent if there are no
  1126. ;; distinguished forms and this is the first undistinguished form,
  1127. ;; or if this is the first undistinguished form and the preceding
  1128. ;; distinguished form has indentation at least as great as body-indent.
  1129. (if (or (and (= i 0) (= count 0))
  1130. (and (= count 0) (<= body-indent normal-indent)))
  1131. body-indent
  1132. normal-indent))))
  1133. (defun lisp-indent-defform (state indent-point)
  1134. (goto-char (car (cdr state)))
  1135. (forward-line 1)
  1136. (if (> (point) (car (cdr (cdr state))))
  1137. (progn
  1138. (goto-char (car (cdr state)))
  1139. (+ lisp-body-indent (current-column)))))
  1140. ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
  1141. ;; like defun if the first form is placed on the next line, otherwise
  1142. ;; it is indented like any other form (i.e. forms line up under first).
  1143. (put 'lambda 'lisp-indent-function 'defun)
  1144. (put 'autoload 'lisp-indent-function 'defun)
  1145. (put 'progn 'lisp-indent-function 0)
  1146. (put 'prog1 'lisp-indent-function 1)
  1147. (put 'prog2 'lisp-indent-function 2)
  1148. (put 'save-excursion 'lisp-indent-function 0)
  1149. (put 'save-restriction 'lisp-indent-function 0)
  1150. (put 'save-match-data 'lisp-indent-function 0)
  1151. (put 'save-current-buffer 'lisp-indent-function 0)
  1152. (put 'let 'lisp-indent-function 1)
  1153. (put 'let* 'lisp-indent-function 1)
  1154. (put 'while 'lisp-indent-function 1)
  1155. (put 'if 'lisp-indent-function 2)
  1156. (put 'catch 'lisp-indent-function 1)
  1157. (put 'condition-case 'lisp-indent-function 2)
  1158. (put 'unwind-protect 'lisp-indent-function 1)
  1159. (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
  1160. (defun indent-sexp (&optional endpos)
  1161. "Indent each line of the list starting just after point.
  1162. If optional arg ENDPOS is given, indent each line, stopping when
  1163. ENDPOS is encountered."
  1164. (interactive)
  1165. (let ((indent-stack (list nil))
  1166. (next-depth 0)
  1167. ;; If ENDPOS is non-nil, use nil as STARTING-POINT
  1168. ;; so that calculate-lisp-indent will find the beginning of
  1169. ;; the defun we are in.
  1170. ;; If ENDPOS is nil, it is safe not to scan before point
  1171. ;; since every line we indent is more deeply nested than point is.
  1172. (starting-point (if endpos nil (point)))
  1173. (last-point (point))
  1174. last-depth bol outer-loop-done inner-loop-done state this-indent)
  1175. (or endpos
  1176. ;; Get error now if we don't have a complete sexp after point.
  1177. (save-excursion (forward-sexp 1)))
  1178. (save-excursion
  1179. (setq outer-loop-done nil)
  1180. (while (if endpos (< (point) endpos)
  1181. (not outer-loop-done))
  1182. (setq last-depth next-depth
  1183. inner-loop-done nil)
  1184. ;; Parse this line so we can learn the state
  1185. ;; to indent the next line.
  1186. ;; This inner loop goes through only once
  1187. ;; unless a line ends inside a string.
  1188. (while (and (not inner-loop-done)
  1189. (not (setq outer-loop-done (eobp))))
  1190. (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  1191. nil nil state))
  1192. (setq next-depth (car state))
  1193. ;; If the line contains a comment other than the sort
  1194. ;; that is indented like code,
  1195. ;; indent it now with indent-for-comment.
  1196. ;; Comments indented like code are right already.
  1197. ;; In any case clear the in-comment flag in the state
  1198. ;; because parse-partial-sexp never sees the newlines.
  1199. (if (car (nthcdr 4 state))
  1200. (progn (indent-for-comment)
  1201. (end-of-line)
  1202. (setcar (nthcdr 4 state) nil)))
  1203. ;; If this line ends inside a string,
  1204. ;; go straight to next line, remaining within the inner loop,
  1205. ;; and turn off the \-flag.
  1206. (if (car (nthcdr 3 state))
  1207. (progn
  1208. (forward-line 1)
  1209. (setcar (nthcdr 5 state) nil))
  1210. (setq inner-loop-done t)))
  1211. (and endpos
  1212. (<= next-depth 0)
  1213. (progn
  1214. (setq indent-stack (nconc indent-stack
  1215. (make-list (- next-depth) nil))
  1216. last-depth (- last-depth next-depth)
  1217. next-depth 0)))
  1218. (forward-line 1)
  1219. ;; Decide whether to exit.
  1220. (if endpos
  1221. ;; If we have already reached the specified end,
  1222. ;; give up and do not reindent this line.
  1223. (if (<= endpos (point))
  1224. (setq outer-loop-done t))
  1225. ;; If no specified end, we are done if we have finished one sexp.
  1226. (if (<= next-depth 0)
  1227. (setq outer-loop-done t)))
  1228. (unless outer-loop-done
  1229. (while (> last-depth next-depth)
  1230. (setq indent-stack (cdr indent-stack)
  1231. last-depth (1- last-depth)))
  1232. (while (< last-depth next-depth)
  1233. (setq indent-stack (cons nil indent-stack)
  1234. last-depth (1+ last-depth)))
  1235. ;; Now indent the next line according
  1236. ;; to what we learned from parsing the previous one.
  1237. (setq bol (point))
  1238. (skip-chars-forward " \t")
  1239. ;; But not if the line is blank, or just a comment
  1240. ;; (except for double-semi comments; indent them as usual).
  1241. (if (or (eobp) (looking-at "\\s<\\|\n"))
  1242. nil
  1243. (if (and (car indent-stack)
  1244. (>= (car indent-stack) 0))
  1245. (setq this-indent (car indent-stack))
  1246. (let ((val (calculate-lisp-indent
  1247. (if (car indent-stack) (- (car indent-stack))
  1248. starting-point))))
  1249. (if (null val)
  1250. (setq this-indent val)
  1251. (if (integerp val)
  1252. (setcar indent-stack
  1253. (setq this-indent val))
  1254. (setcar indent-stack (- (car (cdr val))))
  1255. (setq this-indent (car val))))))
  1256. (if (and this-indent (/= (current-column) this-indent))
  1257. (progn (delete-region bol (point))
  1258. (indent-to this-indent)))))
  1259. (or outer-loop-done
  1260. (setq outer-loop-done (= (point) last-point))
  1261. (setq last-point (point)))))))
  1262. (defun indent-pp-sexp (&optional arg)
  1263. "Indent each line of the list starting just after point, or prettyprint it.
  1264. A prefix argument specifies pretty-printing."
  1265. (interactive "P")
  1266. (if arg
  1267. (save-excursion
  1268. (save-restriction
  1269. (narrow-to-region (point) (progn (forward-sexp 1) (point)))
  1270. (pp-buffer)
  1271. (goto-char (point-max))
  1272. (if (eq (char-before) ?\n)
  1273. (delete-char -1)))))
  1274. (indent-sexp))
  1275. ;;;; Lisp paragraph filling commands.
  1276. (defcustom emacs-lisp-docstring-fill-column 65
  1277. "Value of `fill-column' to use when filling a docstring.
  1278. Any non-integer value means do not use a different value of
  1279. `fill-column' when filling docstrings."
  1280. :type '(choice (integer)
  1281. (const :tag "Use the current `fill-column'" t))
  1282. :group 'lisp)
  1283. (defun lisp-fill-paragraph (&optional justify)
  1284. "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
  1285. If any of the current line is a comment, fill the comment or the
  1286. paragraph of it that point is in, preserving the comment's indentation
  1287. and initial semicolons."
  1288. (interactive "P")
  1289. (or (fill-comment-paragraph justify)
  1290. ;; Since fill-comment-paragraph returned nil, that means we're not in
  1291. ;; a comment: Point is on a program line; we are interested
  1292. ;; particularly in docstring lines.
  1293. ;;
  1294. ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
  1295. ;; are buffer-local, but we avoid changing them so that they can be set
  1296. ;; to make `forward-paragraph' and friends do something the user wants.
  1297. ;;
  1298. ;; `paragraph-start': The `(' in the character alternative and the
  1299. ;; left-singlequote plus `(' sequence after the \\| alternative prevent
  1300. ;; sexps and backquoted sexps that follow a docstring from being filled
  1301. ;; with the docstring. This setting has the consequence of inhibiting
  1302. ;; filling many program lines that are not docstrings, which is sensible,
  1303. ;; because the user probably asked to fill program lines by accident, or
  1304. ;; expecting indentation (perhaps we should try to do indenting in that
  1305. ;; case). The `;' and `:' stop the paragraph being filled at following
  1306. ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
  1307. ;; escaped to keep font-locking, filling, & paren matching in the source
  1308. ;; file happy.
  1309. ;;
  1310. ;; `paragraph-separate': A clever regexp distinguishes the first line of
  1311. ;; a docstring and identifies it as a paragraph separator, so that it
  1312. ;; won't be filled. (Since the first line of documentation stands alone
  1313. ;; in some contexts, filling should not alter the contents the author has
  1314. ;; chosen.) Only the first line of a docstring begins with whitespace
  1315. ;; and a quotation mark and ends with a period or (rarely) a comma.
  1316. ;;
  1317. ;; The `fill-column' is temporarily bound to
  1318. ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
  1319. (let ((paragraph-start (concat paragraph-start
  1320. "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
  1321. (paragraph-separate
  1322. (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
  1323. (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
  1324. (derived-mode-p 'emacs-lisp-mode))
  1325. emacs-lisp-docstring-fill-column
  1326. fill-column)))
  1327. (fill-paragraph justify))
  1328. ;; Never return nil.
  1329. t))
  1330. (defun indent-code-rigidly (start end arg &optional nochange-regexp)
  1331. "Indent all lines of code, starting in the region, sideways by ARG columns.
  1332. Does not affect lines starting inside comments or strings, assuming that
  1333. the start of the region is not inside them.
  1334. Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
  1335. The last is a regexp which, if matched at the beginning of a line,
  1336. means don't indent that line."
  1337. (interactive "r\np")
  1338. (let (state)
  1339. (save-excursion
  1340. (goto-char end)
  1341. (setq end (point-marker))
  1342. (goto-char start)
  1343. (or (bolp)
  1344. (setq state (parse-partial-sexp (point)
  1345. (progn
  1346. (forward-line 1) (point))
  1347. nil nil state)))
  1348. (while (< (point) end)
  1349. (or (car (nthcdr 3 state))
  1350. (and nochange-regexp
  1351. (looking-at nochange-regexp))
  1352. ;; If line does not start in string, indent it
  1353. (let ((indent (current-indentation)))
  1354. (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  1355. (or (eolp)
  1356. (indent-to (max 0 (+ indent arg)) 0))))
  1357. (setq state (parse-partial-sexp (point)
  1358. (progn
  1359. (forward-line 1) (point))
  1360. nil nil state))))))
  1361. (provide 'lisp-mode)
  1362. ;;; lisp-mode.el ends here