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: Wed, 26 Apr 2017 18:31:53 -0400 Message-ID: <87h91aol5y.fsf@users.sourceforge.net> References: <87shkzsidm.fsf@calancha-pc> <87pofzontp.fsf@users.sourceforge.net> <87vaprlucs.fsf@drachen> <87mvb3omd0.fsf@users.sourceforge.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1493245885 11215 195.159.176.226 (26 Apr 2017 22:31:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 26 Apr 2017 22:31:25 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) Cc: Michael Heerdegen , 26619@debbugs.gnu.org To: Kaushal Modi Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Apr 27 00:31:21 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 1d3VSy-0002nj-DY for geb-bug-gnu-emacs@m.gmane.org; Thu, 27 Apr 2017 00:31:20 +0200 Original-Received: from localhost ([::1]:57395 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3VT4-00059a-8M for geb-bug-gnu-emacs@m.gmane.org; Wed, 26 Apr 2017 18:31:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3VSk-00053W-NB for bug-gnu-emacs@gnu.org; Wed, 26 Apr 2017 18:31:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3VSg-0003ic-Er for bug-gnu-emacs@gnu.org; Wed, 26 Apr 2017 18:31:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:43921) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d3VSg-0003iW-9F for bug-gnu-emacs@gnu.org; Wed, 26 Apr 2017 18:31:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1d3VSg-0000YI-3p for bug-gnu-emacs@gnu.org; Wed, 26 Apr 2017 18:31:02 -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 22:31:02 +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.14932458292082 (code B ref 26619); Wed, 26 Apr 2017 22:31:02 +0000 Original-Received: (at 26619) by debbugs.gnu.org; 26 Apr 2017 22:30:29 +0000 Original-Received: from localhost ([127.0.0.1]:42120 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3VS9-0000XW-BC for submit@debbugs.gnu.org; Wed, 26 Apr 2017 18:30:29 -0400 Original-Received: from mail-it0-f67.google.com ([209.85.214.67]:33011) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3VS7-0000XH-KW for 26619@debbugs.gnu.org; Wed, 26 Apr 2017 18:30:28 -0400 Original-Received: by mail-it0-f67.google.com with SMTP id z67so331838itb.0 for <26619@debbugs.gnu.org>; Wed, 26 Apr 2017 15:30:27 -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=cjbJjutfxNfTaK2vSZsPSETluZbbvLXNnsGoYsLfx0w=; b=JqVsWTh60gw+uybwS2UHY/hD3Asm2e53dzqN6vJ8TssDGKQtOnpcCldEl0lMYWss9c E1t3ZSNedS7zfMMRY9h5e8DEp+aoZQ0CL1eYRZtIbJvas+zeGofzULtlA99mjS9nh+Ms 22/MN/xw7GIU9wZoTk/Ne66uVnBodNCknRT3NqQd/RHEZM7wgzdbHkS32XhslT5eZF83 3nBgH8vde+LUHv4DlvSUJ1zsJQOibcmG/huj4EWdhUWcXs/sZMy10dojLtCopB07W8GJ pMcUO6cMuW+6icfdfikEPC53jrE2g5I2LKHiN+Z3E6SKxsv0byDmoAjyvUPSnPNk9ep4 g71Q== 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=cjbJjutfxNfTaK2vSZsPSETluZbbvLXNnsGoYsLfx0w=; b=Qz2yO3GI2dxbOyXDiD2AKkB4Qj5BimVGdtPQP28m4dVO4kEiEG9hnYKCXT5r5erAuA N5FkUm05ZJ3dZ2J6FYvgm3IUnYaGsSYJ2NM9Qh6NAy1UyfE55lwmAdAWLuCt0kbkromL YP0F524FDvGoE/Aq/iwMlSTEeCp0B2IgxCg7WH4huwugRFL6JfSw4DYBpb0a+dhJuu1o UsprTrAFevPxDV66g3Ebz6nRumCKMB1NOm38ELsIipLkLB+1bS7HelRzmfoSrkji4cFl jGgSKNRsSpIB8lTiwqmvWVisFPg9Rc2gqQkLI8sIbYtOX26aQZ82ZovhpDtz9t+qBU46 iPcg== X-Gm-Message-State: AN3rC/44Kucb4jzrmLvGQPdNnxp2q5vE/xRtIt+6TqPS/xg1+dPPVcXc oeWtKdWqZVaDGA== X-Received: by 10.36.173.19 with SMTP id c19mr450975itf.76.1493245822079; Wed, 26 Apr 2017 15:30:22 -0700 (PDT) Original-Received: from zony ([45.2.7.65]) by smtp.googlemail.com with ESMTPSA id v66sm434778ith.18.2017.04.26.15.30.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 26 Apr 2017 15:30:21 -0700 (PDT) In-Reply-To: (Kaushal Modi's message of "Wed, 26 Apr 2017 18:27:57 +0000") 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:132039 Archived-At: --=-=-= Content-Type: text/plain Kaushal Modi writes: > > I tried that patch, but now the indentation is all compressed! > > Earlier, the default indentation was 2 spaces, now it is 1. Um, right, I didn't test that patch properly, it doesn't work at all. Here's a fix for it, cumulative diff also attached. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=26619-fixup.diff Content-Description: patch --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -816,20 +816,15 @@ lisp-indent-region (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))) + (nth 0 oldstate) nil oldstate)) + (setq last-syntax-point (point)) + (< (point) target-point)) + (setq oldstate parse-state)) + ;; 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)))) ;; Update cache's depth stack. (funcall calc-indent (car parse-state)) (and pr (progress-reporter-update pr (point)))) --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=26619-cumulative.diff Content-Description: cumulative patch --- a/lisp/emacs-lisp/lisp-mode.el +++ b/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,40 @@ 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)) + (let ((oldstate parse-state) + (target-point (point))) + (while + (progn + (setq parse-state (parse-partial-sexp last-syntax-point target-point + (nth 0 oldstate) nil oldstate)) + (setq last-syntax-point (point)) + (< (point) target-point)) + (setq oldstate 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))) + ;; `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)))) + ;; 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 +1156,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 +1175,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 +1186,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) --=-=-=--