From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: npostavs@users.sourceforge.net Newsgroups: gmane.emacs.bugs Subject: bug#26619: 26.0.50; Wrong indentation in emacs-lisp-mode Date: Tue, 25 Apr 2017 23:53:47 -0400 Message-ID: <87mvb3omd0.fsf@users.sourceforge.net> References: <87shkzsidm.fsf@calancha-pc> <87pofzontp.fsf@users.sourceforge.net> <87vaprlucs.fsf@drachen> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1493178800 23776 195.159.176.226 (26 Apr 2017 03:53:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 26 Apr 2017 03:53:20 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) Cc: 26619@debbugs.gnu.org, Kaushal Modi To: Michael Heerdegen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Apr 26 05:53:08 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3E0q-0005w3-2Z for geb-bug-gnu-emacs@m.gmane.org; Wed, 26 Apr 2017 05:53:08 +0200 Original-Received: from localhost ([::1]:52511 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3E0w-00009A-21 for geb-bug-gnu-emacs@m.gmane.org; Tue, 25 Apr 2017 23:53:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47600) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3E0p-00008y-08 for bug-gnu-emacs@gnu.org; Tue, 25 Apr 2017 23:53:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3E0k-0000Ul-2b for bug-gnu-emacs@gnu.org; Tue, 25 Apr 2017 23:53:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:42201) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d3E0j-0000UV-TK for bug-gnu-emacs@gnu.org; Tue, 25 Apr 2017 23:53:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1d3E0j-0000iB-Mb for bug-gnu-emacs@gnu.org; Tue, 25 Apr 2017 23:53:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: npostavs@users.sourceforge.net Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 26 Apr 2017 03:53:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26619 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 26619-submit@debbugs.gnu.org id=B26619.14931787452692 (code B ref 26619); Wed, 26 Apr 2017 03:53:01 +0000 Original-Received: (at 26619) by debbugs.gnu.org; 26 Apr 2017 03:52:25 +0000 Original-Received: from localhost ([127.0.0.1]:40400 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3E08-0000hM-Pn for submit@debbugs.gnu.org; Tue, 25 Apr 2017 23:52:25 -0400 Original-Received: from mail-io0-f194.google.com ([209.85.223.194]:35420) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3E06-0000h8-Kd for 26619@debbugs.gnu.org; Tue, 25 Apr 2017 23:52:23 -0400 Original-Received: by mail-io0-f194.google.com with SMTP id d203so56218387iof.2 for <26619@debbugs.gnu.org>; Tue, 25 Apr 2017 20:52:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=+mRsCay2FALKo+PlU93r7i+uLzlTsOXsREUf6Sgj/PI=; b=ANhi1e9+tgdnuWkn49NvRR3JHf5GOYZjyZL8FrlcpqQ8z5TYeQSpMpKr/kROfn+Nsd jrtn81kvkk8kIEHunHMJxxfpQRPezWsfScaG8GUiisVvmZC1wg6NVroWQy5H7kzw5hhY 4lRnQSUy8hbXnUgtDoTmdKUGnN9NmfTpOIgWaMdoE3aEWUoGGf6GXdIabXyiq9erntiY S5RO3ajsjjKnk1URSwaOF8RSb4pHa9S2gs2vRZsUV4G5MyMHPilgrV6pPR1P5jOHuP7o JAM/iE3uRTGrxW+7EO+97dd93EijQeQP2ujMqRz8oEI4stF9NPl/7fQ+U1/Z9JVMSgDA ijug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:references:date :in-reply-to:message-id:user-agent:mime-version; bh=+mRsCay2FALKo+PlU93r7i+uLzlTsOXsREUf6Sgj/PI=; b=rBiYwpEKLZO9IBtQD0zqVKrYB22Ulj2kl4O+7Le6lvPlW0Dg2h+F8Z9ghtbv5/JzXF 0hZvvmCeF+km+3c6KGwsUoyntsLRuxJobDbgwBqcLHH5esgbEw0J9GgIDjpY++Ie0T9X kmWSo5vIh0lXr6tC5Lwo9AsrjXY2LDasckZZHAOOpD066nWhR1TlwSv2P3y+2LsJ4ofC lcrfJpED5vgmyVWsmg6LEBDvfsHvDLfvxbP9AU8diqp2BrQz9jR5q8ySHyEFWVpuBNfi fT205Ta/QrylSrHsgETtyx5filIU441/c9Yne8lhtgyyD/mg7o53lKY1TKiDXBVD2F/c +D2g== X-Gm-Message-State: AN3rC/7nPMf5r5Ufk89PLVsHyekJ7s/+H7Ytbcibagp770b25ElVfel2 uZImfCBWr0Domg== X-Received: by 10.107.11.39 with SMTP id v39mr21414623ioi.136.1493178737080; Tue, 25 Apr 2017 20:52:17 -0700 (PDT) Original-Received: from zony ([45.2.7.65]) by smtp.googlemail.com with ESMTPSA id g73sm1101853ioi.31.2017.04.25.20.52.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Apr 2017 20:52:16 -0700 (PDT) In-Reply-To: <87vaprlucs.fsf@drachen> (Michael Heerdegen's message of "Wed, 26 Apr 2017 05:29:23 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:131990 Archived-At: --=-=-= Content-Type: text/plain Michael Heerdegen writes: > npostavs@users.sourceforge.net writes: > > Thanks for the quick response. I'll keep my eyes open and try to test > some more. Could you please send me a cumulative patch relative to > master? Sure. --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=26619.diff Content-Description: cumulative patch diff --git c/lisp/emacs-lisp/lisp-mode.el w/lisp/emacs-lisp/lisp-mode.el index fa931e76ad..12fd53140c 100644 --- c/lisp/emacs-lisp/lisp-mode.el +++ w/lisp/emacs-lisp/lisp-mode.el @@ -764,6 +764,36 @@ lisp-ppss (parse-partial-sexp (car (last (nth 9 pss))) pos) pss))) +(defun lisp-mode--indent-cache (init-depth) + "Returns a closure that computes indentation, caching by depth." + (let ((indent-stack (list nil)) + (last-depth init-depth)) + (lambda (&optional depth-or-state) + "Pass depth to update cache, or parse state for indentation." + (if (listp depth-or-state) ; It's a parse state. + (let ((val (if (car indent-stack) indent-stack + (calculate-lisp-indent depth-or-state)))) + (cond ((nth 3 depth-or-state) nil) ; Inside a string. + ((integerp val) + (setf (car indent-stack) val)) + ((consp val) ; (COLUMN CONTAINING-SEXP-START) + (car val)) + ;; This only happens if we're in a string. + (t (error "This shouldn't happen")))) + (let ((depth depth-or-state)) ; It's a depth. + (when (< depth init-depth) + (setq indent-stack (nconc indent-stack + (make-list (- init-depth depth) nil)) + last-depth (- last-depth depth) + depth init-depth)) + (let ((depth-delta (- 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 depth))))))) + (defun lisp-indent-region (start end) "Indent region as Lisp code, efficiently." (save-excursion @@ -773,31 +803,45 @@ lisp-indent-region ;; parse state, which forces each indent call to reparse from the ;; beginning. That has O(n^2) complexity. (let* ((parse-state (lisp-ppss start)) + (calc-indent (lisp-mode--indent-cache (car parse-state))) (last-syntax-point start) (pr (unless (minibufferp) (make-progress-reporter "Indenting region..." (point) end)))) (while (< (point) end) (unless (and (bolp) (eolp)) - (lisp-indent-line parse-state)) + (lisp-indent-line (funcall calc-indent parse-state))) (forward-line 1) - (let ((last-sexp (nth 2 parse-state))) - (setq parse-state (parse-partial-sexp last-syntax-point (point) - nil nil parse-state)) - ;; It's important to preserve last sexp location for - ;; `calculate-lisp-indent'. - (unless (nth 2 parse-state) - (setf (nth 2 parse-state) last-sexp)) - (setq last-syntax-point (point))) + (let ((oldstate parse-state) + (target-point (point))) + (while + (progn + (setq parse-state (parse-partial-sexp last-syntax-point target-point + nil t oldstate)) + (if (>= (point) target-point) + nil ; Done. + (when (= (nth 0 parse-state) (nth 0 oldstate)) ; Stopped before open paren. + (setq parse-state (parse-partial-sexp last-syntax-point target-point + (1+ (nth 0 parse-state)) nil parse-state))) + (setq last-syntax-point (point)) + ;; It's important to preserve last sexp location for + ;; `calculate-lisp-indent', but it's only relevant at the + ;; same depth. + (unless (or (nth 2 parse-state) (/= (nth 0 parse-state) (nth 0 oldstate))) + (setf (nth 2 parse-state) (nth 2 oldstate))) + t)) + (setq oldstate parse-state))) + ;; Update cache's depth stack. + (funcall calc-indent (car parse-state)) (and pr (progress-reporter-update pr (point)))) (and pr (progress-reporter-done pr)) (move-marker end nil)))) -(defun lisp-indent-line (&optional parse-state) +(defun lisp-indent-line (&optional indent) "Indent current line as Lisp code." (interactive) (let ((pos (- (point-max) (point))) (indent (progn (beginning-of-line) - (calculate-lisp-indent (or parse-state (lisp-ppss)))))) + (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 @@ -1117,15 +1161,12 @@ indent-sexp If optional arg ENDPOS is given, indent each line, stopping when ENDPOS is encountered." (interactive) - (let* ((indent-stack (list nil)) - ;; Use `syntax-ppss' to get initial state so we don't get + (let* (;; 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) + (calc-indent (lisp-mode--indent-cache (car state))) (last-syntax-point (point))) ;; We need a marker because we modify the buffer ;; text preceding endpos. @@ -1139,7 +1180,8 @@ indent-sexp ;; Parse this line so we can learn the state to indent the ;; next line. Preserve element 2 of the state (last sexp) for ;; `calculate-lisp-indent'. - (let ((last-sexp (nth 2 state))) + (let ((last-depth (nth 0 state)) + (last-sexp (nth 2 state))) (while (progn (setq state (parse-partial-sexp last-syntax-point (progn (end-of-line) (point)) @@ -1149,51 +1191,33 @@ indent-sexp (nth 3 state)) (setq state (parse-partial-sexp (point) (point-max) nil nil state 'syntax-table)) - (setq last-sexp (or (nth 2 state) last-sexp)) + (when (nth 2 state) + (setq last-sexp (nth 2 state)) + (setq last-depth (nth 0 state))) (setq last-syntax-point (point))) (setf (nth 2 state) last-sexp)) - (setq next-depth (car state)) + ;; Update cache's depth stack. + (funcall calc-indent (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)) ;; Now indent the next line according to what we learned from ;; parsing the previous one. (forward-line 1) (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)) ;; But not if the line is blank, or just a comment (we ;; already called `indent-for-comment' above). (skip-chars-forward " \t") (unless (or (eolp) (eq (char-syntax (char-after)) ?<)) (indent-line-to - (or (car indent-stack) - ;; The state here is actually to the end of the - ;; previous line, but that's fine for our purposes. - ;; And parsing over the newline would only destroy - ;; element 2 (last sexp position). - (let ((val (calculate-lisp-indent state))) - (cond ((integerp val) - (setf (car indent-stack) val)) - ((consp val) ; (COLUMN CONTAINING-SEXP-START) - (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!")))))))))) + ;; The state here is actually to the end of the + ;; previous line, but that's fine for our purposes. + ;; And parsing over the newline would only destroy + ;; element 2 (last sexp position). + (funcall calc-indent state)))))) (move-marker endpos nil))) (defun indent-pp-sexp (&optional arg) --=-=-=--