From da0916b591a983c9b4ee9c8201600a77ba506b92 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Sat, 17 Mar 2018 21:14:11 -0400 Subject: [PATCH v1] Handle indentation of malformed Lisp * lisp/emacs-lisp/lisp-mode.el (lisp-ppss-open-paren-in-column-0-is-defun-start): New function. * lisp/emacs-lisp/lisp-mode.el (lisp-indent-calc-next): If we run out of indent stack, reset the parse state. --- lisp/emacs-lisp/lisp-mode.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index f082983d48..58710e9876 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -794,6 +794,21 @@ lisp-ppss (parse-partial-sexp sexp-start pos nil nil (syntax-ppss sexp-start))) pss))) +(defun lisp-ppss-open-paren-in-column-0-is-defun-start (&optional pos) + (save-excursion + (if pos + (goto-char pos) + (setq pos (point))) + (or (looking-at-p "^(") + (re-search-backward "^(" nil t) + (goto-char (point-min))) + (let ((ppss (parse-partial-sexp (point) pos))) + (if (< (car ppss) 0) + ;; Too many close parens, probably syntax error. Give a + ;; fresh state. + (parse-partial-sexp (point) (point)) + ppss)))) + (cl-defstruct (lisp-indent-state (:constructor nil) (:constructor lisp-indent-initial-state @@ -844,6 +859,10 @@ lisp-indent-calc-next (prog1 (let (indent) (cond ((= (forward-line 1) 1) nil) + ;; Negative depth, probably some kind of syntax error. + ((null indent-stack) + ;; Reset state. + (setq ppss (parse-partial-sexp (point) (point)))) ((car indent-stack)) ((integerp (setq indent (calculate-lisp-indent ppss))) (setf (car indent-stack) indent)) -- 2.11.0