summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/lisp-mode.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/emacs-lisp/lisp-mode.el')
-rw-r--r--lisp/emacs-lisp/lisp-mode.el237
1 files changed, 142 insertions, 95 deletions
diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el
index 2e6e13f1dd1..6287f27b139 100644
--- a/lisp/emacs-lisp/lisp-mode.el
+++ b/lisp/emacs-lisp/lisp-mode.el
@@ -164,6 +164,9 @@
(put 'defalias 'doc-string-elt 3)
(put 'defvaralias 'doc-string-elt 3)
(put 'define-category 'doc-string-elt 2)
+;; CL
+(put 'defconstant 'doc-string-elt 3)
+(put 'defparameter 'doc-string-elt 3)
(defvar lisp-doc-string-elt-property 'doc-string-elt
"The symbol property that holds the docstring position info.")
@@ -594,6 +597,7 @@ font-lock keywords will not be case sensitive."
;; I believe that newcomment's auto-fill code properly deals with it -stef
;;(set (make-local-variable 'adaptive-fill-mode) nil)
(setq-local indent-line-function 'lisp-indent-line)
+ (setq-local indent-region-function 'lisp-indent-region)
(setq-local outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
(setq-local outline-level 'lisp-outline-level)
(setq-local add-log-current-defun-function #'lisp-current-defun-name)
@@ -748,14 +752,110 @@ function is `common-lisp-indent-function'."
:type 'function
:group 'lisp)
-(defun lisp-indent-line (&optional _whole-exp)
- "Indent current line as Lisp code.
-With argument, indent any additional lines of the same expression
-rigidly along with this one."
- (interactive "P")
- (let ((indent (calculate-lisp-indent)) shift-amt
- (pos (- (point-max) (point)))
- (beg (progn (beginning-of-line) (point))))
+(defun lisp-ppss (&optional pos)
+ "Return Parse-Partial-Sexp State at POS, defaulting to point.
+Like `syntax-ppss' but includes the character address of the last
+complete sexp in the innermost containing list at position
+2 (counting from 0). This is important for lisp indentation."
+ (unless pos (setq pos (point)))
+ (let ((pss (syntax-ppss pos)))
+ (if (nth 9 pss)
+ (let ((sexp-start (car (last (nth 9 pss)))))
+ (parse-partial-sexp sexp-start pos nil nil (syntax-ppss sexp-start)))
+ pss)))
+
+(cl-defstruct (lisp-indent-state
+ (:constructor nil)
+ (:constructor lisp-indent-initial-state
+ (&aux (ppss (lisp-ppss))
+ (ppss-point (point))
+ (depth (car ppss))
+ (stack (make-list (1+ depth) nil)))))
+ stack ;; Cached indentation, per depth.
+ ppss
+ depth
+ ppss-point)
+
+(defun lisp-indent-calc-next (state)
+ "Move to next line and return calculated indent for it.
+STATE is updated by side effect, the first state should be
+created by `lisp-indent-initial-state'. This function may move
+by more than one line to cross a string literal."
+ (pcase-let (((cl-struct lisp-indent-state
+ (stack indent-stack) ppss depth ppss-point)
+ state))
+ ;; Parse this line so we can learn the state to indent the
+ ;; next line.
+ (while (let ((last-sexp (nth 2 ppss)))
+ (setq ppss (parse-partial-sexp
+ ppss-point (progn (end-of-line) (point))
+ nil nil ppss))
+ ;; Preserve last sexp of state (position 2) for
+ ;; `calculate-lisp-indent', if we're at the same depth.
+ (if (and (not (nth 2 ppss)) (= depth (car ppss)))
+ (setf (nth 2 ppss) last-sexp)
+ (setq last-sexp (nth 2 ppss)))
+ ;; Skip over newlines within strings.
+ (nth 3 ppss))
+ (let ((string-start (nth 8 ppss)))
+ (setq ppss (parse-partial-sexp (point) (point-max)
+ nil nil ppss 'syntax-table))
+ (setf (nth 2 ppss) string-start)) ; Finished a complete string.
+ (setq ppss-point (point)))
+ (setq ppss-point (point))
+ (let* ((next-depth (car ppss))
+ (depth-delta (- next-depth depth)))
+ (cond ((< depth-delta 0)
+ (setq indent-stack (nthcdr (- depth-delta) indent-stack)))
+ ((> depth-delta 0)
+ (setq indent-stack (nconc (make-list depth-delta nil)
+ indent-stack))))
+ (setq depth next-depth))
+ (prog1
+ (let (indent)
+ (cond ((= (forward-line 1) 1) nil)
+ ((car indent-stack))
+ ((integerp (setq indent (calculate-lisp-indent ppss)))
+ (setf (car indent-stack) indent))
+ ((consp indent) ; (COLUMN CONTAINING-SEXP-START)
+ (car indent))
+ ;; This only happens if we're in a string.
+ (t (error "This shouldn't happen"))))
+ (setf (lisp-indent-state-stack state) indent-stack)
+ (setf (lisp-indent-state-depth state) depth)
+ (setf (lisp-indent-state-ppss-point state) ppss-point)
+ (setf (lisp-indent-state-ppss state) ppss))))
+
+(defun lisp-indent-region (start end)
+ "Indent region as Lisp code, efficiently."
+ (save-excursion
+ (setq end (copy-marker end))
+ (goto-char start)
+ (beginning-of-line)
+ ;; The default `indent-region-line-by-line' doesn't hold a running
+ ;; parse state, which forces each indent call to reparse from the
+ ;; beginning. That has O(n^2) complexity.
+ (let* ((parse-state (lisp-indent-initial-state))
+ (pr (unless (minibufferp)
+ (make-progress-reporter "Indenting region..." (point) end))))
+ (let ((ppss (lisp-indent-state-ppss parse-state)))
+ (unless (or (and (bolp) (eolp)) (nth 3 ppss))
+ (lisp-indent-line (calculate-lisp-indent ppss))))
+ (let ((indent nil))
+ (while (progn (setq indent (lisp-indent-calc-next parse-state))
+ (< (point) end))
+ (unless (or (and (bolp) (eolp)) (not indent))
+ (lisp-indent-line indent))
+ (and pr (progress-reporter-update pr (point)))))
+ (and pr (progress-reporter-done pr))
+ (move-marker end nil))))
+
+(defun lisp-indent-line (&optional indent)
+ "Indent current line as Lisp code."
+ (interactive)
+ (let ((pos (- (point-max) (point)))
+ (indent (progn (beginning-of-line)
+ (or indent (calculate-lisp-indent (lisp-ppss))))))
(skip-chars-forward " \t")
(if (or (null indent) (looking-at "\\s<\\s<\\s<"))
;; Don't alter indentation of a ;;; comment line
@@ -767,11 +867,7 @@ rigidly along with this one."
;; as comment lines, not as code.
(progn (indent-for-comment) (forward-char -1))
(if (listp indent) (setq indent (car indent)))
- (setq shift-amt (- indent (current-column)))
- (if (zerop shift-amt)
- nil
- (delete-region beg (point))
- (indent-to indent)))
+ (indent-line-to indent))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
@@ -785,6 +881,10 @@ In usual case returns an integer: the column to indent to.
If the value is nil, that means don't change the indentation
because the line starts inside a string.
+PARSE-START may be a buffer position to start parsing from, or a
+parse state as returned by calling `parse-partial-sexp' up to the
+beginning of the current line.
+
The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
This means that following lines at the same level of indentation
should not necessarily be indented the same as this line.
@@ -798,12 +898,14 @@ is the buffer position of the start of the containing expression."
(desired-indent nil)
(retry t)
calculate-lisp-indent-last-sexp containing-sexp)
- (if parse-start
- (goto-char parse-start)
- (beginning-of-defun))
- ;; Find outermost containing sexp
- (while (< (point) indent-point)
- (setq state (parse-partial-sexp (point) indent-point 0)))
+ (cond ((or (markerp parse-start) (integerp parse-start))
+ (goto-char parse-start))
+ ((null parse-start) (beginning-of-defun))
+ (t (setq state parse-start)))
+ (unless state
+ ;; Find outermost containing sexp
+ (while (< (point) indent-point)
+ (setq state (parse-partial-sexp (point) indent-point 0))))
;; Find innermost containing sexp
(while (and retry
state
@@ -1073,86 +1175,31 @@ Lisp function does not specify a special indentation."
If optional arg ENDPOS is given, indent each line, stopping when
ENDPOS is encountered."
(interactive)
- (let* ((indent-stack (list nil))
- ;; If ENDPOS is non-nil, use beginning of defun as STARTING-POINT.
- ;; If ENDPOS is nil, it is safe not to scan before point
- ;; since every line we indent is more deeply nested than point is.
- (starting-point (save-excursion (if endpos (beginning-of-defun))
- (point)))
- ;; Use `syntax-ppss' to get initial state so we don't get
- ;; confused by starting inside a string. We don't use
- ;; `syntax-ppss' in the loop, because this is measurably
- ;; slower when we're called on a long list.
- (state (syntax-ppss))
- (init-depth (car state))
- (next-depth init-depth)
- (last-depth init-depth)
- (last-syntax-point (point))
- (real-endpos endpos))
- (unless endpos
- ;; Get error now if we don't have a complete sexp after point.
- (save-excursion (forward-sexp 1)
- ;; We need a marker because we modify the buffer
- ;; text preceding endpos.
- (setq endpos (point-marker))))
+ (let* ((parse-state (lisp-indent-initial-state)))
+ ;; We need a marker because we modify the buffer
+ ;; text preceding endpos.
+ (setq endpos (copy-marker
+ (if endpos endpos
+ ;; Get error now if we don't have a complete sexp
+ ;; after point.
+ (save-excursion (forward-sexp 1) (point)))))
(save-excursion
(while (< (point) endpos)
- ;; Parse this line so we can learn the state to indent the
- ;; next line.
- (while (progn
- (setq state (parse-partial-sexp
- last-syntax-point (progn (end-of-line) (point))
- nil nil state))
- ;; Skip over newlines within strings.
- (nth 3 state))
- (setq state (parse-partial-sexp (point) (point-max)
- nil nil state 'syntax-table))
- (setq last-syntax-point (point)))
- (setq next-depth (car state))
- ;; If the line contains a comment indent it now with
- ;; `indent-for-comment'.
- (when (nth 4 state)
- (indent-for-comment)
- (end-of-line))
- (setq last-syntax-point (point))
- (when (< next-depth init-depth)
- (setq indent-stack (nconc indent-stack
- (make-list (- init-depth next-depth) nil))
- last-depth (- last-depth next-depth)
- next-depth init-depth))
- (forward-line 1)
- (when (and (not real-endpos) (<= next-depth init-depth))
- (goto-char endpos))
- (when (< (point) endpos)
- (let ((depth-delta (- next-depth last-depth)))
- (cond ((< depth-delta 0)
- (setq indent-stack (nthcdr (- depth-delta) indent-stack)))
- ((> depth-delta 0)
- (setq indent-stack (nconc (make-list depth-delta nil)
- indent-stack))))
- (setq last-depth next-depth))
- ;; Now indent the next line according
- ;; to what we learned from parsing the previous one.
- (skip-chars-forward " \t")
+ (let ((indent (lisp-indent-calc-next parse-state)))
+ ;; If the line contains a comment indent it now with
+ ;; `indent-for-comment'.
+ (when (nth 4 (lisp-indent-state-ppss parse-state))
+ (save-excursion
+ (goto-char (lisp-indent-state-ppss-point parse-state))
+ (indent-for-comment)
+ (setf (lisp-indent-state-ppss-point parse-state)
+ (line-end-position))))
;; But not if the line is blank, or just a comment (we
;; already called `indent-for-comment' above).
- (unless (or (eolp) (eq (char-syntax (char-after)) ?<))
- (let ((this-indent (car indent-stack)))
- (when (listp this-indent)
- (let ((val (calculate-lisp-indent
- (or (car this-indent) starting-point))))
- (setq
- this-indent
- (cond ((integerp val)
- (setf (car indent-stack) val))
- ((consp val) ; (COLUMN CONTAINING-SEXP-START)
- (setf (car indent-stack) (cdr val))
- (car val))
- ;; `calculate-lisp-indent' only returns nil
- ;; when we're in a string, but this won't
- ;; happen because we skip strings above.
- (t (error "This shouldn't happen!"))))))
- (indent-line-to this-indent))))))))
+ (skip-chars-forward " \t")
+ (unless (or (eolp) (eq (char-syntax (char-after)) ?<) (not indent))
+ (indent-line-to indent)))))
+ (move-marker endpos nil)))
(defun indent-pp-sexp (&optional arg)
"Indent each line of the list starting just after point, or prettyprint it.