lisp.el 29 KB

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