lisp-mode.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. ;; Lisp mode, and its idiosyncratic commands.
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defvar lisp-mode-syntax-table nil "")
  18. (defvar lisp-mode-abbrev-table nil "")
  19. (if (not lisp-mode-syntax-table)
  20. (let ((i 0))
  21. (setq lisp-mode-syntax-table (make-syntax-table))
  22. (while (< i ?0)
  23. (modify-syntax-entry i "_ " lisp-mode-syntax-table)
  24. (setq i (1+ i)))
  25. (setq i (1+ ?9))
  26. (while (< i ?A)
  27. (modify-syntax-entry i "_ " lisp-mode-syntax-table)
  28. (setq i (1+ i)))
  29. (setq i (1+ ?Z))
  30. (while (< i ?a)
  31. (modify-syntax-entry i "_ " lisp-mode-syntax-table)
  32. (setq i (1+ i)))
  33. (setq i (1+ ?z))
  34. (while (< i 128)
  35. (modify-syntax-entry i "_ " lisp-mode-syntax-table)
  36. (setq i (1+ i)))
  37. (modify-syntax-entry ? " " lisp-mode-syntax-table)
  38. (modify-syntax-entry ?\t " " lisp-mode-syntax-table)
  39. (modify-syntax-entry ?\n "> " lisp-mode-syntax-table)
  40. (modify-syntax-entry ?\f "> " lisp-mode-syntax-table)
  41. (modify-syntax-entry ?\; "< " lisp-mode-syntax-table)
  42. (modify-syntax-entry ?` "' " lisp-mode-syntax-table)
  43. (modify-syntax-entry ?' "' " lisp-mode-syntax-table)
  44. (modify-syntax-entry ?, "' " lisp-mode-syntax-table)
  45. (modify-syntax-entry ?. "' " lisp-mode-syntax-table)
  46. (modify-syntax-entry ?# "' " lisp-mode-syntax-table)
  47. (modify-syntax-entry ?\" "\" " lisp-mode-syntax-table)
  48. (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table)
  49. (modify-syntax-entry ?\\ "\\ " lisp-mode-syntax-table)
  50. (modify-syntax-entry ?\( "() " lisp-mode-syntax-table)
  51. (modify-syntax-entry ?\) ")( " lisp-mode-syntax-table)))
  52. (define-abbrev-table 'lisp-mode-abbrev-table ())
  53. (defun lisp-mode-variables ()
  54. (set-syntax-table lisp-mode-syntax-table)
  55. (setq local-abbrev-table lisp-mode-abbrev-table)
  56. (make-local-variable 'paragraph-start)
  57. (setq paragraph-start (concat "^$\\|" page-delimiter))
  58. (make-local-variable 'paragraph-separate)
  59. (setq paragraph-separate paragraph-start)
  60. (make-local-variable 'indent-line-function)
  61. (setq indent-line-function 'lisp-indent-line)
  62. (make-local-variable 'comment-start)
  63. (setq comment-start ";")
  64. (make-local-variable 'comment-start-skip)
  65. (setq comment-start-skip ";+ *")
  66. (make-local-variable 'comment-column)
  67. (setq comment-column 40)
  68. (make-local-variable 'comment-indent-hook)
  69. (setq comment-indent-hook 'lisp-comment-indent))
  70. (defun lisp-mode-commands (map)
  71. (define-key map "\e\C-q" 'indent-sexp)
  72. (define-key map "\177" 'backward-delete-char-untabify)
  73. (define-key map "\t" 'lisp-indent-line))
  74. (defvar emacs-lisp-mode-map () "")
  75. (if emacs-lisp-mode-map
  76. ()
  77. (setq emacs-lisp-mode-map (make-sparse-keymap))
  78. (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
  79. (lisp-mode-commands emacs-lisp-mode-map))
  80. (defun emacs-lisp-mode ()
  81. "Major mode for editing Lisp code to run in Emacs.
  82. Commands:
  83. Delete converts tabs to spaces as it moves back.
  84. Blank lines separate paragraphs. Semicolons start comments.
  85. \\{emacs-lisp-mode-map}
  86. Entry to this mode calls the value of emacs-lisp-mode-hook
  87. if that value is non-nil."
  88. (interactive)
  89. (kill-all-local-variables)
  90. (use-local-map emacs-lisp-mode-map)
  91. (setq major-mode 'emacs-lisp-mode)
  92. (setq mode-name "Emacs-Lisp")
  93. (lisp-mode-variables)
  94. (run-hooks 'emacs-lisp-mode-hook))
  95. (defvar lisp-mode-map ())
  96. (if lisp-mode-map
  97. ()
  98. (setq lisp-mode-map (make-sparse-keymap))
  99. (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
  100. (lisp-mode-commands lisp-mode-map))
  101. (defun lisp-mode ()
  102. "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
  103. Commands:
  104. Delete converts tabs to spaces as it moves back.
  105. Blank lines separate paragraphs. Semicolons start comments.
  106. \\{lisp-mode-map}
  107. Entry to this mode calls the value of lisp-mode-hook
  108. if that value is non-nil."
  109. (interactive)
  110. (kill-all-local-variables)
  111. (use-local-map lisp-mode-map)
  112. (setq major-mode 'lisp-mode)
  113. (setq mode-name "Lisp")
  114. (lisp-mode-variables)
  115. (run-hooks 'lisp-mode-hook))
  116. ;; This will do unless shell.el is loaded.
  117. (defun lisp-send-defun nil
  118. "Send the current defun to the Lisp process made by M-x run-lisp."
  119. (interactive)
  120. (error "Process lisp does not exist"))
  121. (defvar lisp-interaction-mode-map ())
  122. (if lisp-interaction-mode-map
  123. ()
  124. (setq lisp-interaction-mode-map (make-sparse-keymap))
  125. (lisp-mode-commands lisp-interaction-mode-map)
  126. (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
  127. (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
  128. (defun lisp-interaction-mode ()
  129. "Major mode for typing and evaluating Lisp forms.
  130. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
  131. before point, and prints its value into the buffer, advancing point.
  132. Commands:
  133. Delete converts tabs to spaces as it moves back.
  134. Paragraphs are separated only by blank lines. Semicolons start comments.
  135. \\{lisp-interaction-mode-map}
  136. Entry to this mode calls the value of lisp-interaction-mode-hook
  137. if that value is non-nil."
  138. (interactive)
  139. (kill-all-local-variables)
  140. (use-local-map lisp-interaction-mode-map)
  141. (setq major-mode 'lisp-interaction-mode)
  142. (setq mode-name "Lisp Interaction")
  143. (lisp-mode-variables)
  144. (run-hooks 'lisp-interaction-mode-hook))
  145. (defun eval-print-last-sexp (arg)
  146. "Evaluate sexp before point; print value into current buffer."
  147. (interactive "P")
  148. (eval-region
  149. (let ((stab (syntax-table)))
  150. (unwind-protect
  151. (save-excursion
  152. (set-syntax-table lisp-mode-syntax-table)
  153. (forward-sexp -1)
  154. (point))
  155. (set-syntax-table stab)))
  156. (point)
  157. (current-buffer)))
  158. (defun eval-last-sexp (arg)
  159. "Evaluate sexp before point; print value in minibuffer.
  160. With argument, print output into current buffer."
  161. (interactive "P")
  162. (eval-region
  163. (let ((stab (syntax-table)))
  164. (unwind-protect
  165. (save-excursion
  166. (set-syntax-table lisp-mode-syntax-table)
  167. (forward-sexp -1)
  168. (point))
  169. (set-syntax-table stab)))
  170. (point)
  171. (if arg (current-buffer) t)))
  172. (defun eval-defun (arg)
  173. "Evaluate defun that point is in or before.
  174. Print value in minibuffer.
  175. With argument, insert value in current buffer after the defun."
  176. (interactive "P")
  177. (save-excursion
  178. (end-of-defun)
  179. (let ((end (point)))
  180. (beginning-of-defun)
  181. (eval-region (point) end
  182. (if arg (current-buffer) t)))))
  183. (defun lisp-comment-indent ()
  184. (if (looking-at ";;;")
  185. (current-column)
  186. (if (looking-at ";;")
  187. (let ((tem (calculate-lisp-indent)))
  188. (if (listp tem) (car tem) tem))
  189. (skip-chars-backward " \t")
  190. (max (if (bolp) 0 (1+ (current-column)))
  191. comment-column))))
  192. (defconst lisp-indent-offset nil "")
  193. (defconst lisp-indent-hook 'lisp-indent-hook "")
  194. (defun lisp-indent-line (&optional whole-exp)
  195. "Indent current line as Lisp code.
  196. With argument, indent any additional lines of the same expression
  197. rigidly along with this one."
  198. (interactive "P")
  199. (let ((indent (calculate-lisp-indent)) shift-amt beg end
  200. (pos (- (point-max) (point))))
  201. (beginning-of-line)
  202. (setq beg (point))
  203. (skip-chars-forward " \t")
  204. (if (looking-at "[ \t]*;;;")
  205. ;; Don't alter indentation of a ;;; comment line.
  206. nil
  207. (if (listp indent) (setq indent (car indent)))
  208. (setq shift-amt (- indent (current-column)))
  209. (if (zerop shift-amt)
  210. nil
  211. (delete-region beg (point))
  212. (indent-to indent))
  213. ;; If initial point was within line's indentation,
  214. ;; position after the indentation. Else stay at same point in text.
  215. (if (> (- (point-max) pos) (point))
  216. (goto-char (- (point-max) pos)))
  217. ;; If desired, shift remaining lines of expression the same amount.
  218. (and whole-exp (not (zerop shift-amt))
  219. (save-excursion
  220. (goto-char beg)
  221. (forward-sexp 1)
  222. (setq end (point))
  223. (goto-char beg)
  224. (forward-line 1)
  225. (setq beg (point))
  226. (> end beg))
  227. (indent-code-rigidly beg end shift-amt)))))
  228. (defun calculate-lisp-indent (&optional parse-start)
  229. "Return appropriate indentation for current line as Lisp code.
  230. In usual case returns an integer: the column to indent to.
  231. Can instead return a list, whose car is the column to indent to.
  232. This means that following lines at the same level of indentation
  233. should not necessarily be indented the same way.
  234. The second element of the list is the buffer position
  235. of the start of the containing expression."
  236. (save-excursion
  237. (beginning-of-line)
  238. (let ((indent-point (point)) state paren-depth desired-indent (retry t)
  239. last-sexp containing-sexp)
  240. (if parse-start
  241. (goto-char parse-start)
  242. (beginning-of-defun))
  243. ;; Find outermost containing sexp
  244. (while (< (point) indent-point)
  245. (setq state (parse-partial-sexp (point) indent-point 0)))
  246. ;; Find innermost containing sexp
  247. (while (and retry (setq paren-depth (car state)) (> paren-depth 0))
  248. (setq retry nil)
  249. (setq last-sexp (nth 2 state))
  250. (setq containing-sexp (car (cdr state)))
  251. ;; Position following last unclosed open.
  252. (goto-char (1+ containing-sexp))
  253. ;; Is there a complete sexp since then?
  254. (if (and last-sexp (> last-sexp (point)))
  255. ;; Yes, but is there a containing sexp after that?
  256. (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  257. (if (setq retry (car (cdr peek))) (setq state peek))))
  258. (if (not retry)
  259. ;; Innermost containing sexp found
  260. (progn
  261. (goto-char (1+ containing-sexp))
  262. (if (not last-sexp)
  263. ;; indent-point immediately follows open paren.
  264. ;; Don't call hook.
  265. (setq desired-indent (current-column))
  266. ;; Move to first sexp after containing open paren
  267. (parse-partial-sexp (point) last-sexp 0 t)
  268. (cond
  269. ((looking-at "\\s(")
  270. ;; Looking at a list. Don't call hook.
  271. (if (not (> (save-excursion (forward-line 1) (point))
  272. last-sexp))
  273. (progn (goto-char last-sexp)
  274. (beginning-of-line)
  275. (parse-partial-sexp (point) last-sexp 0 t)))
  276. ;; Indent under the list or under the first sexp on the
  277. ;; same line as last-sexp. Note that first thing on that
  278. ;; line has to be complete sexp since we are inside the
  279. ;; innermost containing sexp.
  280. (backward-prefix-chars)
  281. (setq desired-indent (current-column)))
  282. ((> (save-excursion (forward-line 1) (point))
  283. last-sexp)
  284. ;; Last sexp is on same line as containing sexp.
  285. ;; It's almost certainly a function call.
  286. (parse-partial-sexp (point) last-sexp 0 t)
  287. (if (/= (point) last-sexp)
  288. ;; Indent beneath first argument or, if only one sexp
  289. ;; on line, indent beneath that.
  290. (progn (forward-sexp 1)
  291. (parse-partial-sexp (point) last-sexp 0 t)))
  292. (backward-prefix-chars))
  293. (t
  294. ;; Indent beneath first sexp on same line as last-sexp.
  295. ;; Again, it's almost certainly a function call.
  296. (goto-char last-sexp)
  297. (beginning-of-line)
  298. (parse-partial-sexp (point) last-sexp 0 t)
  299. (backward-prefix-chars)))))))
  300. ;; Point is at the point to indent under unless we are inside a string.
  301. ;; Call indentation hook except when overriden by lisp-indent-offset
  302. ;; or if the desired indentation has already been computed.
  303. (cond ((car (nthcdr 3 state))
  304. ;; Inside a string, don't change indentation.
  305. (goto-char indent-point)
  306. (skip-chars-forward " \t")
  307. (setq desired-indent (current-column)))
  308. ((and (integerp lisp-indent-offset) containing-sexp)
  309. ;; Indent by constant offset
  310. (goto-char containing-sexp)
  311. (setq desired-indent (+ lisp-indent-offset (current-column))))
  312. ((not (or desired-indent
  313. (and (boundp 'lisp-indent-hook)
  314. lisp-indent-hook
  315. (not retry)
  316. (setq desired-indent
  317. (funcall lisp-indent-hook indent-point state)))))
  318. ;; Use default indentation if not computed yet
  319. (setq desired-indent (current-column))))
  320. desired-indent)))
  321. (defun lisp-indent-hook (indent-point state)
  322. (let ((normal-indent (current-column)))
  323. (save-excursion
  324. (goto-char (1+ (car (cdr state))))
  325. (re-search-forward "\\sw\\|\\s_")
  326. (if (/= (point) (car (cdr state)))
  327. (let ((function (buffer-substring (progn (forward-char -1) (point))
  328. (progn (forward-sexp 1) (point))))
  329. method)
  330. (setq method (get (intern-soft function) 'lisp-indent-hook))
  331. (if (or (eq method 'defun)
  332. (and (null method)
  333. (> (length function) 3)
  334. (string-equal (substring function 0 3) "def")))
  335. (lisp-indent-defform state indent-point)
  336. (if (integerp method)
  337. (lisp-indent-specform method state indent-point)
  338. (if method
  339. (funcall method state indent-point)))))))))
  340. (defconst lisp-body-indent 2 "")
  341. (defun lisp-indent-specform (count state indent-point)
  342. (let ((containing-form-start (car (cdr state))) (i count)
  343. body-indent containing-form-column)
  344. ;; Move to the start of containing form, calculate indentation
  345. ;; to use for non-distinguished forms (> count), and move past the
  346. ;; function symbol. lisp-indent-hook guarantees that there is at
  347. ;; least one word or symbol character following open paren of containing
  348. ;; form.
  349. (goto-char containing-form-start)
  350. (setq containing-form-column (current-column))
  351. (setq body-indent (+ lisp-body-indent containing-form-column))
  352. (forward-char 1)
  353. (forward-sexp 1)
  354. ;; Now find the start of the last form.
  355. (parse-partial-sexp (point) indent-point 1 t)
  356. (while (and (< (point) indent-point)
  357. (condition-case nil
  358. (progn
  359. (setq count (1- count))
  360. (forward-sexp 1)
  361. (parse-partial-sexp (point) indent-point 1 t))
  362. (error nil))))
  363. ;; Point is sitting on first character of last (or count) sexp.
  364. (if (> count 0)
  365. ;; A distinguished form. If it is the first or second form use double
  366. ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
  367. ;; to 2 (the default), this just happens to work the same with if as
  368. ;; the older code, but it makes unwind-protect, condition-case,
  369. ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
  370. ;; less hacked, behavior can be obtained by replacing below with
  371. ;; (list normal-indent containing-form-start).
  372. (if (<= (- i count) 1)
  373. (list (+ containing-form-column (* 2 lisp-body-indent))
  374. containing-form-start)
  375. (list normal-indent containing-form-start))
  376. ;; A non-distinguished form. Use body-indent if there are no distinguished
  377. ;; forms and this is the first undistinguished form, or if this is the
  378. ;; first undistinguished form and the preceding distinguished form has
  379. ;; indentation at least as great as body-indent.
  380. (if (or (and (= i 0) (= count 0))
  381. (and (= count 0) (<= body-indent normal-indent)))
  382. body-indent
  383. normal-indent))))
  384. (defun lisp-indent-defform (state indent-point)
  385. (goto-char (car (cdr state)))
  386. (forward-line 1)
  387. (if (> (point) (car (cdr (cdr state))))
  388. (progn
  389. (goto-char (car (cdr state)))
  390. (+ lisp-body-indent (current-column)))))
  391. ;; (put 'progn 'lisp-indent-hook 0), say, causes progn to be indented
  392. ;; like defun if the first form is placed on the next line, otherwise
  393. ;; it is indented like any other form (i.e. forms line up under first).
  394. (put 'lambda 'lisp-indent-hook 'defun)
  395. (put 'progn 'lisp-indent-hook 0)
  396. (put 'prog1 'lisp-indent-hook 1)
  397. (put 'save-excursion 'lisp-indent-hook 0)
  398. (put 'save-window-excursion 'lisp-indent-hook 0)
  399. (put 'save-restriction 'lisp-indent-hook 0)
  400. (put 'let 'lisp-indent-hook 1)
  401. (put 'let* 'lisp-indent-hook 1)
  402. (put 'while 'lisp-indent-hook 1)
  403. (put 'if 'lisp-indent-hook 2)
  404. (put 'catch 'lisp-indent-hook 1)
  405. (put 'condition-case 'lisp-indent-hook 2)
  406. (put 'unwind-protect 'lisp-indent-hook 1)
  407. (put 'with-output-to-temp-buffer 'lisp-indent-hook 1)
  408. (defun indent-sexp ()
  409. "Indent each line of the list starting just after point."
  410. (interactive)
  411. (let ((indent-stack (list nil)) (next-depth 0) bol
  412. outer-loop-done inner-loop-done state this-indent)
  413. (save-excursion (forward-sexp 1))
  414. (save-excursion
  415. (setq outer-loop-done nil)
  416. (while (not outer-loop-done)
  417. (setq last-depth next-depth
  418. innerloop-done nil)
  419. (while (and (not innerloop-done)
  420. (not (setq outer-loop-done (eobp))))
  421. (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  422. nil nil state))
  423. (setq next-depth (car state))
  424. (if (car (nthcdr 4 state))
  425. (progn (indent-for-comment)
  426. (end-of-line)
  427. (setcar (nthcdr 4 state) nil)))
  428. (if (car (nthcdr 3 state))
  429. (progn
  430. (forward-line 1)
  431. (setcar (nthcdr 5 state) nil))
  432. (setq innerloop-done t)))
  433. (if (setq outer-loop-done (<= next-depth 0))
  434. nil
  435. (while (> last-depth next-depth)
  436. (setq indent-stack (cdr indent-stack)
  437. last-depth (1- last-depth)))
  438. (while (< last-depth next-depth)
  439. (setq indent-stack (cons nil indent-stack)
  440. last-depth (1+ last-depth)))
  441. (forward-line 1)
  442. (setq bol (point))
  443. (skip-chars-forward " \t")
  444. (if (or (eobp) (looking-at "[;\n]"))
  445. nil
  446. (if (and (car indent-stack)
  447. (>= (car indent-stack) 0))
  448. (setq this-indent (car indent-stack))
  449. (let ((val (calculate-lisp-indent
  450. (if (car indent-stack) (- (car indent-stack))))))
  451. (if (integerp val)
  452. (setcar indent-stack
  453. (setq this-indent val))
  454. (setcar indent-stack (- (car (cdr val))))
  455. (setq this-indent (car val)))))
  456. (if (/= (current-column) this-indent)
  457. (progn (delete-region bol (point))
  458. (indent-to this-indent)))))))))
  459. (defun indent-code-rigidly (start end arg &optional nochange-regexp)
  460. "Indent all lines of code, starting in the region, sideways by ARG columns.
  461. Does not affect lines starting inside comments or strings,
  462. assuming that the start of the region is not inside them.
  463. Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
  464. The last is a regexp which, if matched at the beginning of a line,
  465. means don't indent that line."
  466. (interactive "r\np")
  467. (let (state)
  468. (save-excursion
  469. (goto-char end)
  470. (setq end (point-marker))
  471. (goto-char start)
  472. (or (bolp)
  473. (setq state (parse-partial-sexp (point)
  474. (progn
  475. (forward-line 1) (point))
  476. nil nil state)))
  477. (while (< (point) end)
  478. (or (car (nthcdr 3 state))
  479. (and nochange-regexp
  480. (looking-at nochange-regexp))
  481. ;; If line does not start in string, indent it
  482. (let ((indent (current-indentation)))
  483. (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  484. (or (eolp)
  485. (indent-to (max 0 (+ indent arg)) 0))))
  486. (setq state (parse-partial-sexp (point)
  487. (progn
  488. (forward-line 1) (point))
  489. nil nil state))))))