lisp.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. ;;; lisp.el --- Lisp editing commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1994, 2000-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. ;; Lisp editing commands to go with Lisp major mode. More-or-less
  19. ;; applicable in other modes too.
  20. ;;; Code:
  21. ;; Note that this variable is used by non-lisp modes too.
  22. (defcustom defun-prompt-regexp nil
  23. "If non-nil, a regexp to ignore before a defun.
  24. This is only necessary if the opening paren or brace is not in column 0.
  25. See function `beginning-of-defun'."
  26. :type '(choice (const nil)
  27. regexp)
  28. :group 'lisp)
  29. (make-variable-buffer-local 'defun-prompt-regexp)
  30. (defcustom parens-require-spaces t
  31. "If non-nil, add whitespace as needed when inserting parentheses.
  32. This affects `insert-parentheses' and `insert-pair'."
  33. :type 'boolean
  34. :group 'lisp)
  35. (defvar forward-sexp-function nil
  36. "If non-nil, `forward-sexp' delegates to this function.
  37. Should take the same arguments and behave similarly to `forward-sexp'.")
  38. (defun forward-sexp (&optional arg)
  39. "Move forward across one balanced expression (sexp).
  40. With ARG, do it that many times. Negative arg -N means
  41. move backward across N balanced expressions.
  42. This command assumes point is not in a string or comment."
  43. (interactive "^p")
  44. (or arg (setq arg 1))
  45. (if forward-sexp-function
  46. (funcall forward-sexp-function arg)
  47. (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
  48. (if (< arg 0) (backward-prefix-chars))))
  49. (defun backward-sexp (&optional arg)
  50. "Move backward across one balanced expression (sexp).
  51. With ARG, do it that many times. Negative arg -N means
  52. move forward across N balanced expressions.
  53. This command assumes point is not in a string or comment."
  54. (interactive "^p")
  55. (or arg (setq arg 1))
  56. (forward-sexp (- arg)))
  57. (defun mark-sexp (&optional arg allow-extend)
  58. "Set mark ARG sexps from point.
  59. The place mark goes is the same place \\[forward-sexp] would
  60. move to with the same argument.
  61. Interactively, if this command is repeated
  62. or (in Transient Mark mode) if the mark is active,
  63. it marks the next ARG sexps after the ones already marked.
  64. This command assumes point is not in a string or comment."
  65. (interactive "P\np")
  66. (cond ((and allow-extend
  67. (or (and (eq last-command this-command) (mark t))
  68. (and transient-mark-mode mark-active)))
  69. (setq arg (if arg (prefix-numeric-value arg)
  70. (if (< (mark) (point)) -1 1)))
  71. (set-mark
  72. (save-excursion
  73. (goto-char (mark))
  74. (forward-sexp arg)
  75. (point))))
  76. (t
  77. (push-mark
  78. (save-excursion
  79. (forward-sexp (prefix-numeric-value arg))
  80. (point))
  81. nil t))))
  82. (defun forward-list (&optional arg)
  83. "Move forward across one balanced group of parentheses.
  84. With ARG, do it that many times.
  85. Negative arg -N means move backward across N groups of parentheses.
  86. This command assumes point is not in a string or comment."
  87. (interactive "^p")
  88. (or arg (setq arg 1))
  89. (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
  90. (defun backward-list (&optional arg)
  91. "Move backward across one balanced group of parentheses.
  92. With ARG, do it that many times.
  93. Negative arg -N means move forward across N groups of parentheses.
  94. This command assumes point is not in a string or comment."
  95. (interactive "^p")
  96. (or arg (setq arg 1))
  97. (forward-list (- arg)))
  98. (defun down-list (&optional arg)
  99. "Move forward down one level of parentheses.
  100. With ARG, do this that many times.
  101. A negative argument means move backward but still go down a level.
  102. This command assumes point is not in a string or comment."
  103. (interactive "^p")
  104. (or arg (setq arg 1))
  105. (let ((inc (if (> arg 0) 1 -1)))
  106. (while (/= arg 0)
  107. (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
  108. (setq arg (- arg inc)))))
  109. (defun backward-up-list (&optional arg)
  110. "Move backward out of one level of parentheses.
  111. With ARG, do this that many times.
  112. A negative argument means move forward but still to a less deep spot.
  113. This command assumes point is not in a string or comment."
  114. (interactive "^p")
  115. (up-list (- (or arg 1))))
  116. (defun up-list (&optional arg)
  117. "Move forward out of one level of parentheses.
  118. With ARG, do this that many times.
  119. A negative argument means move backward but still to a less deep spot.
  120. This command assumes point is not in a string or comment."
  121. (interactive "^p")
  122. (or arg (setq arg 1))
  123. (let ((inc (if (> arg 0) 1 -1))
  124. pos)
  125. (while (/= arg 0)
  126. (if (null forward-sexp-function)
  127. (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
  128. (condition-case err
  129. (while (progn (setq pos (point))
  130. (forward-sexp inc)
  131. (/= (point) pos)))
  132. (scan-error (goto-char (nth (if (> arg 0) 3 2) err))))
  133. (if (= (point) pos)
  134. (signal 'scan-error
  135. (list "Unbalanced parentheses" (point) (point)))))
  136. (setq arg (- arg inc)))))
  137. (defun kill-sexp (&optional arg)
  138. "Kill the sexp (balanced expression) following point.
  139. With ARG, kill that many sexps after point.
  140. Negative arg -N means kill N sexps before point.
  141. This command assumes point is not in a string or comment."
  142. (interactive "p")
  143. (let ((opoint (point)))
  144. (forward-sexp (or arg 1))
  145. (kill-region opoint (point))))
  146. (defun backward-kill-sexp (&optional arg)
  147. "Kill the sexp (balanced expression) preceding point.
  148. With ARG, kill that many sexps before point.
  149. Negative arg -N means kill N sexps after point.
  150. This command assumes point is not in a string or comment."
  151. (interactive "p")
  152. (kill-sexp (- (or arg 1))))
  153. ;; After Zmacs:
  154. (defun kill-backward-up-list (&optional arg)
  155. "Kill the form containing the current sexp, leaving the sexp itself.
  156. A prefix argument ARG causes the relevant number of surrounding
  157. forms to be removed.
  158. This command assumes point is not in a string or comment."
  159. (interactive "*p")
  160. (let ((current-sexp (thing-at-point 'sexp)))
  161. (if current-sexp
  162. (save-excursion
  163. (backward-up-list arg)
  164. (kill-sexp)
  165. (insert current-sexp))
  166. (error "Not at a sexp"))))
  167. (defvar beginning-of-defun-function nil
  168. "If non-nil, function for `beginning-of-defun-raw' to call.
  169. This is used to find the beginning of the defun instead of using the
  170. normal recipe (see `beginning-of-defun'). Major modes can define this
  171. if defining `defun-prompt-regexp' is not sufficient to handle the mode's
  172. needs.
  173. The function takes the same argument as `beginning-of-defun' and should
  174. behave similarly, returning non-nil if it found the beginning of a defun.
  175. Ideally it should move to a point right before an open-paren which encloses
  176. the body of the defun.")
  177. (defun beginning-of-defun (&optional arg)
  178. "Move backward to the beginning of a defun.
  179. With ARG, do it that many times. Negative ARG means move forward
  180. to the ARGth following beginning of defun.
  181. If search is successful, return t; point ends up at the beginning
  182. of the line where the search succeeded. Otherwise, return nil.
  183. When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
  184. is assumed to start where there is a char with open-parenthesis
  185. syntax at the beginning of a line. If `defun-prompt-regexp' is
  186. non-nil, then a string which matches that regexp may also precede
  187. the open-parenthesis. If `defun-prompt-regexp' and
  188. `open-paren-in-column-0-is-defun-start' are both nil, this
  189. function instead finds an open-paren at the outermost level.
  190. If the variable `beginning-of-defun-function' is non-nil, its
  191. value is called as a function, with argument ARG, to find the
  192. defun's beginning.
  193. Regardless of the values of `defun-prompt-regexp' and
  194. `beginning-of-defun-function', point always moves to the
  195. beginning of the line whenever the search is successful."
  196. (interactive "^p")
  197. (or (not (eq this-command 'beginning-of-defun))
  198. (eq last-command 'beginning-of-defun)
  199. (and transient-mark-mode mark-active)
  200. (push-mark))
  201. (and (beginning-of-defun-raw arg)
  202. (progn (beginning-of-line) t)))
  203. (defun beginning-of-defun-raw (&optional arg)
  204. "Move point to the character that starts a defun.
  205. This is identical to function `beginning-of-defun', except that point
  206. does not move to the beginning of the line when `defun-prompt-regexp'
  207. is non-nil.
  208. If variable `beginning-of-defun-function' is non-nil, its value
  209. is called as a function to find the defun's beginning."
  210. (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
  211. ; to beginning-of-defun-function.
  212. (unless arg (setq arg 1))
  213. (cond
  214. (beginning-of-defun-function
  215. (condition-case nil
  216. (funcall beginning-of-defun-function arg)
  217. ;; We used to define beginning-of-defun-function as taking no argument
  218. ;; but that makes it impossible to implement correct forward motion:
  219. ;; we used to use end-of-defun for that, but it's not supposed to do
  220. ;; the same thing (it moves to the end of a defun not to the beginning
  221. ;; of the next).
  222. ;; In case the beginning-of-defun-function uses the old calling
  223. ;; convention, fallback on the old implementation.
  224. (wrong-number-of-arguments
  225. (if (> arg 0)
  226. (dotimes (i arg)
  227. (funcall beginning-of-defun-function))
  228. (dotimes (i (- arg))
  229. (funcall end-of-defun-function))))))
  230. ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start)
  231. (and (< arg 0) (not (eobp)) (forward-char 1))
  232. (and (re-search-backward (if defun-prompt-regexp
  233. (concat (if open-paren-in-column-0-is-defun-start
  234. "^\\s(\\|" "")
  235. "\\(?:" defun-prompt-regexp "\\)\\s(")
  236. "^\\s(")
  237. nil 'move arg)
  238. (progn (goto-char (1- (match-end 0)))
  239. t)))
  240. ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
  241. ;; are both nil, column 0 has no significance - so scan forward
  242. ;; from BOB to see how nested point is, then carry on from there.
  243. ;;
  244. ;; It is generally not a good idea to land up here, because the
  245. ;; call to scan-lists below can be extremely slow. This is because
  246. ;; back_comment in syntax.c may have to scan from bob to find the
  247. ;; beginning of each comment. Fixing this is not trivial -- cyd.
  248. ((eq arg 0))
  249. (t
  250. (let ((floor (point-min))
  251. (ceiling (point-max))
  252. (arg-+ve (> arg 0)))
  253. (save-restriction
  254. (widen)
  255. (let ((ppss (let (syntax-begin-function
  256. font-lock-beginning-of-syntax-function)
  257. (syntax-ppss)))
  258. ;; position of least enclosing paren, or nil.
  259. encl-pos)
  260. ;; Back out of any comment/string, so that encl-pos will always
  261. ;; become nil if we're at top-level.
  262. (when (nth 8 ppss)
  263. (goto-char (nth 8 ppss))
  264. (setq ppss (syntax-ppss))) ; should be fast, due to cache.
  265. (setq encl-pos (syntax-ppss-toplevel-pos ppss))
  266. (if encl-pos (goto-char encl-pos))
  267. (and encl-pos arg-+ve (setq arg (1- arg)))
  268. (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
  269. (setq arg (1+ arg)))
  270. (condition-case nil ; to catch crazy parens.
  271. (progn
  272. (goto-char (scan-lists (point) (- arg) 0))
  273. (if arg-+ve
  274. (if (>= (point) floor)
  275. t
  276. (goto-char floor)
  277. nil)
  278. ;; forward to next (, or trigger the c-c
  279. (goto-char (1- (scan-lists (point) 1 -1)))
  280. (if (<= (point) ceiling)
  281. t
  282. (goto-char ceiling)
  283. nil)))
  284. (error
  285. (goto-char (if arg-+ve floor ceiling))
  286. nil))))))))
  287. (defvar end-of-defun-function
  288. (lambda () (forward-sexp 1))
  289. "Function for `end-of-defun' to call.
  290. This is used to find the end of the defun at point.
  291. It is called with no argument, right after calling `beginning-of-defun-raw'.
  292. So the function can assume that point is at the beginning of the defun body.
  293. It should move point to the first position after the defun.")
  294. (defun buffer-end (arg)
  295. "Return the \"far end\" position of the buffer, in direction ARG.
  296. If ARG is positive, that's the end of the buffer.
  297. Otherwise, that's the beginning of the buffer."
  298. (if (> arg 0) (point-max) (point-min)))
  299. (defun end-of-defun (&optional arg)
  300. "Move forward to next end of defun.
  301. With argument, do it that many times.
  302. Negative argument -N means move back to Nth preceding end of defun.
  303. An end of a defun occurs right after the close-parenthesis that
  304. matches the open-parenthesis that starts a defun; see function
  305. `beginning-of-defun'.
  306. If variable `end-of-defun-function' is non-nil, its value
  307. is called as a function to find the defun's end."
  308. (interactive "^p")
  309. (or (not (eq this-command 'end-of-defun))
  310. (eq last-command 'end-of-defun)
  311. (and transient-mark-mode mark-active)
  312. (push-mark))
  313. (if (or (null arg) (= arg 0)) (setq arg 1))
  314. (let ((pos (point))
  315. (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
  316. (funcall end-of-defun-function)
  317. ;; When comparing point against pos, we want to consider that if
  318. ;; point was right after the end of the function, it's still
  319. ;; considered as "in that function".
  320. ;; E.g. `eval-defun' from right after the last close-paren.
  321. (unless (bolp)
  322. (skip-chars-forward " \t")
  323. (if (looking-at "\\s<\\|\n")
  324. (forward-line 1)))
  325. (cond
  326. ((> arg 0)
  327. ;; Moving forward.
  328. (if (> (point) pos)
  329. ;; We already moved forward by one because we started from
  330. ;; within a function.
  331. (setq arg (1- arg))
  332. ;; We started from after the end of the previous function.
  333. (goto-char pos))
  334. (unless (zerop arg)
  335. (beginning-of-defun-raw (- arg))
  336. (funcall end-of-defun-function)))
  337. ((< arg 0)
  338. ;; Moving backward.
  339. (if (< (point) pos)
  340. ;; We already moved backward because we started from between
  341. ;; two functions.
  342. (setq arg (1+ arg))
  343. ;; We started from inside a function.
  344. (goto-char beg))
  345. (unless (zerop arg)
  346. (beginning-of-defun-raw (- arg))
  347. (funcall end-of-defun-function))))
  348. (unless (bolp)
  349. (skip-chars-forward " \t")
  350. (if (looking-at "\\s<\\|\n")
  351. (forward-line 1)))))
  352. (defun mark-defun (&optional allow-extend)
  353. "Put mark at end of this defun, point at beginning.
  354. The defun marked is the one that contains point or follows point.
  355. Interactively, if this command is repeated
  356. or (in Transient Mark mode) if the mark is active,
  357. it marks the next defun after the ones already marked."
  358. (interactive "p")
  359. (cond ((and allow-extend
  360. (or (and (eq last-command this-command) (mark t))
  361. (and transient-mark-mode mark-active)))
  362. (set-mark
  363. (save-excursion
  364. (goto-char (mark))
  365. (end-of-defun)
  366. (point))))
  367. (t
  368. (let ((opoint (point))
  369. beg end)
  370. (push-mark opoint)
  371. ;; Try first in this order for the sake of languages with nested
  372. ;; functions where several can end at the same place as with
  373. ;; the offside rule, e.g. Python.
  374. (beginning-of-defun)
  375. (setq beg (point))
  376. (end-of-defun)
  377. (setq end (point))
  378. (while (looking-at "^\n")
  379. (forward-line 1))
  380. (if (> (point) opoint)
  381. (progn
  382. ;; We got the right defun.
  383. (push-mark beg nil t)
  384. (goto-char end)
  385. (exchange-point-and-mark))
  386. ;; beginning-of-defun moved back one defun
  387. ;; so we got the wrong one.
  388. (goto-char opoint)
  389. (end-of-defun)
  390. (push-mark (point) nil t)
  391. (beginning-of-defun))
  392. (re-search-backward "^\n" (- (point) 1) t)))))
  393. (defun narrow-to-defun (&optional arg)
  394. "Make text outside current defun invisible.
  395. The defun visible is the one that contains point or follows point.
  396. Optional ARG is ignored."
  397. (interactive)
  398. (save-excursion
  399. (widen)
  400. (let ((opoint (point))
  401. beg end)
  402. ;; Try first in this order for the sake of languages with nested
  403. ;; functions where several can end at the same place as with
  404. ;; the offside rule, e.g. Python.
  405. (beginning-of-defun)
  406. (setq beg (point))
  407. (end-of-defun)
  408. (setq end (point))
  409. (while (looking-at "^\n")
  410. (forward-line 1))
  411. (unless (> (point) opoint)
  412. ;; beginning-of-defun moved back one defun
  413. ;; so we got the wrong one.
  414. (goto-char opoint)
  415. (end-of-defun)
  416. (setq end (point))
  417. (beginning-of-defun)
  418. (setq beg (point)))
  419. (goto-char end)
  420. (re-search-backward "^\n" (- (point) 1) t)
  421. (narrow-to-region beg end))))
  422. (defvar insert-pair-alist
  423. '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
  424. "Alist of paired characters inserted by `insert-pair'.
  425. Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
  426. OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
  427. of the pair whose key is equal to the last input character with
  428. or without modifiers, are inserted by `insert-pair'.")
  429. (defun insert-pair (&optional arg open close)
  430. "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
  431. Leave point after the first character.
  432. A negative ARG encloses the preceding ARG sexps instead.
  433. No argument is equivalent to zero: just insert characters
  434. and leave point between.
  435. If `parens-require-spaces' is non-nil, this command also inserts a space
  436. before and after, depending on the surrounding characters.
  437. If region is active, insert enclosing characters at region boundaries.
  438. If arguments OPEN and CLOSE are nil, the character pair is found
  439. from the variable `insert-pair-alist' according to the last input
  440. character with or without modifiers. If no character pair is
  441. found in the variable `insert-pair-alist', then the last input
  442. character is inserted ARG times.
  443. This command assumes point is not in a string or comment."
  444. (interactive "P")
  445. (if (not (and open close))
  446. (let ((pair (or (assq last-command-event insert-pair-alist)
  447. (assq (event-basic-type last-command-event)
  448. insert-pair-alist))))
  449. (if pair
  450. (if (nth 2 pair)
  451. (setq open (nth 1 pair) close (nth 2 pair))
  452. (setq open (nth 0 pair) close (nth 1 pair))))))
  453. (if (and open close)
  454. (if (and transient-mark-mode mark-active)
  455. (progn
  456. (save-excursion (goto-char (region-end)) (insert close))
  457. (save-excursion (goto-char (region-beginning)) (insert open)))
  458. (if arg (setq arg (prefix-numeric-value arg))
  459. (setq arg 0))
  460. (cond ((> arg 0) (skip-chars-forward " \t"))
  461. ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
  462. (and parens-require-spaces
  463. (not (bobp))
  464. (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
  465. (insert " "))
  466. (insert open)
  467. (save-excursion
  468. (or (eq arg 0) (forward-sexp arg))
  469. (insert close)
  470. (and parens-require-spaces
  471. (not (eobp))
  472. (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
  473. (insert " "))))
  474. (insert-char (event-basic-type last-command-event)
  475. (prefix-numeric-value arg))))
  476. (defun insert-parentheses (&optional arg)
  477. "Enclose following ARG sexps in parentheses.
  478. Leave point after open-paren.
  479. A negative ARG encloses the preceding ARG sexps instead.
  480. No argument is equivalent to zero: just insert `()' and leave point between.
  481. If `parens-require-spaces' is non-nil, this command also inserts a space
  482. before and after, depending on the surrounding characters.
  483. If region is active, insert enclosing characters at region boundaries.
  484. This command assumes point is not in a string or comment."
  485. (interactive "P")
  486. (insert-pair arg ?\( ?\)))
  487. (defun delete-pair ()
  488. "Delete a pair of characters enclosing the sexp that follows point."
  489. (interactive)
  490. (save-excursion (forward-sexp 1) (delete-char -1))
  491. (delete-char 1))
  492. (defun raise-sexp (&optional arg)
  493. "Raise ARG sexps higher up the tree."
  494. (interactive "p")
  495. (let ((s (if (and transient-mark-mode mark-active)
  496. (buffer-substring (region-beginning) (region-end))
  497. (buffer-substring
  498. (point)
  499. (save-excursion (forward-sexp arg) (point))))))
  500. (backward-up-list 1)
  501. (delete-region (point) (save-excursion (forward-sexp 1) (point)))
  502. (save-excursion (insert s))))
  503. (defun move-past-close-and-reindent ()
  504. "Move past next `)', delete indentation before it, then indent after it."
  505. (interactive)
  506. (up-list 1)
  507. (forward-char -1)
  508. (while (save-excursion ; this is my contribution
  509. (let ((before-paren (point)))
  510. (back-to-indentation)
  511. (and (= (point) before-paren)
  512. (progn
  513. ;; Move to end of previous line.
  514. (beginning-of-line)
  515. (forward-char -1)
  516. ;; Verify it doesn't end within a string or comment.
  517. (let ((end (point))
  518. state)
  519. (beginning-of-line)
  520. ;; Get state at start of line.
  521. (setq state (list 0 nil nil
  522. (null (calculate-lisp-indent))
  523. nil nil nil nil
  524. nil))
  525. ;; Parse state across the line to get state at end.
  526. (setq state (parse-partial-sexp (point) end nil nil
  527. state))
  528. ;; Check not in string or comment.
  529. (and (not (elt state 3)) (not (elt state 4))))))))
  530. (delete-indentation))
  531. (forward-char 1)
  532. (newline-and-indent))
  533. (defun check-parens () ; lame name?
  534. "Check for unbalanced parentheses in the current buffer.
  535. More accurately, check the narrowed part of the buffer for unbalanced
  536. expressions (\"sexps\") in general. This is done according to the
  537. current syntax table and will find unbalanced brackets or quotes as
  538. appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
  539. found, an error is signaled and point is left at the first unbalanced
  540. character."
  541. (interactive)
  542. (condition-case data
  543. ;; Buffer can't have more than (point-max) sexps.
  544. (scan-sexps (point-min) (point-max))
  545. (scan-error (goto-char (nth 2 data))
  546. ;; Could print (nth 1 data), which is either
  547. ;; "Containing expression ends prematurely" or
  548. ;; "Unbalanced parentheses", but those may not be so
  549. ;; accurate/helpful, e.g. quotes may actually be
  550. ;; mismatched.
  551. (error "Unmatched bracket or quote"))))
  552. (defun field-complete (table &optional predicate)
  553. (let ((minibuffer-completion-table table)
  554. (minibuffer-completion-predicate predicate)
  555. ;; This made sense for lisp-complete-symbol, but for
  556. ;; field-complete, this is out of place. --Stef
  557. ;; (completion-annotate-function
  558. ;; (unless (eq predicate 'fboundp)
  559. ;; (lambda (str)
  560. ;; (if (fboundp (intern-soft str)) " <f>"))))
  561. )
  562. (call-interactively 'minibuffer-complete)))
  563. (defun lisp-complete-symbol (&optional predicate)
  564. "Perform completion on Lisp symbol preceding point.
  565. Compare that symbol against the known Lisp symbols.
  566. If no characters can be completed, display a list of possible completions.
  567. Repeating the command at that point scrolls the list.
  568. When called from a program, optional arg PREDICATE is a predicate
  569. determining which symbols are considered, e.g. `commandp'.
  570. If PREDICATE is nil, the context determines which symbols are
  571. considered. If the symbol starts just after an open-parenthesis, only
  572. symbols with function definitions are considered. Otherwise, all
  573. symbols with function definitions, values or properties are
  574. considered."
  575. (interactive)
  576. (let* ((data (lisp-completion-at-point predicate))
  577. (plist (nthcdr 3 data)))
  578. (if (null data)
  579. (minibuffer-message "Nothing to complete")
  580. (let ((completion-extra-properties plist))
  581. (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
  582. (plist-get plist :predicate))))))
  583. (defun lisp-completion-at-point (&optional predicate)
  584. "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
  585. ;; FIXME: the `end' could be after point?
  586. (with-syntax-table emacs-lisp-mode-syntax-table
  587. (let* ((pos (point))
  588. (beg (condition-case nil
  589. (save-excursion
  590. (backward-sexp 1)
  591. (skip-syntax-forward "'")
  592. (point))
  593. (scan-error pos)))
  594. (predicate
  595. (or predicate
  596. (save-excursion
  597. (goto-char beg)
  598. (if (not (eq (char-before) ?\())
  599. (lambda (sym) ;why not just nil ? -sm
  600. (or (boundp sym) (fboundp sym)
  601. (symbol-plist sym)))
  602. ;; Looks like a funcall position. Let's double check.
  603. (if (condition-case nil
  604. (progn (up-list -2) (forward-char 1)
  605. (eq (char-after) ?\())
  606. (error nil))
  607. ;; If the first element of the parent list is an open
  608. ;; paren we are probably not in a funcall position.
  609. ;; Maybe a `let' varlist or something.
  610. nil
  611. ;; Else, we assume that a function name is expected.
  612. 'fboundp)))))
  613. (end
  614. (unless (or (eq beg (point-max))
  615. (member (char-syntax (char-after beg)) '(?\" ?\( ?\))))
  616. (condition-case nil
  617. (save-excursion
  618. (goto-char beg)
  619. (forward-sexp 1)
  620. (when (>= (point) pos)
  621. (point)))
  622. (scan-error pos)))))
  623. (when end
  624. (list beg end obarray
  625. :predicate predicate
  626. :annotation-function
  627. (unless (eq predicate 'fboundp)
  628. (lambda (str) (if (fboundp (intern-soft str)) " <f>"))))))))
  629. ;;; lisp.el ends here