From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#17620: sh-mode indentation of continued do loop lists Date: Tue, 29 Sep 2015 21:46:37 -0400 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1443650375 21219 80.91.229.3 (30 Sep 2015 21:59:35 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Sep 2015 21:59:35 +0000 (UTC) Cc: 17620-done@debbugs.gnu.org To: Glenn Morris Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Sep 30 23:59:22 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZhPPF-0007wt-PH for geb-bug-gnu-emacs@m.gmane.org; Wed, 30 Sep 2015 23:59:22 +0200 Original-Received: from localhost ([::1]:34188 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZhPPF-0005JG-43 for geb-bug-gnu-emacs@m.gmane.org; Wed, 30 Sep 2015 17:59:21 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58841) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zh6k3-0001r1-6C for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2015 22:03:45 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zh6ju-0006y7-KM for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2015 22:03:35 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:59392) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zh6U3-0004Oi-3C for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2015 21:47:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1Zh6U2-0000Sw-Uz for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2015 21:47:03 -0400 Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-To: bug-gnu-emacs@gnu.org Resent-Date: Wed, 30 Sep 2015 01:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: cc-closed 17620 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Mail-Followup-To: 17620@debbugs.gnu.org, monnier@iro.umontreal.ca, rgm@gnu.org Original-Received: via spool by 17620-done@debbugs.gnu.org id=D17620.14435776011748 (code D ref 17620); Wed, 30 Sep 2015 01:47:02 +0000 Original-Received: (at 17620-done) by debbugs.gnu.org; 30 Sep 2015 01:46:41 +0000 Original-Received: from localhost ([127.0.0.1]:48360 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Zh6Th-0000S8-2u for submit@debbugs.gnu.org; Tue, 29 Sep 2015 21:46:41 -0400 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181]:40198) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Zh6Tf-0000Rz-1f for 17620-done@debbugs.gnu.org; Tue, 29 Sep 2015 21:46:39 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0A2FgA731xV/wihxEVcgxCEAoVVu0CEfoJNBAICgTw6EwEBAQEBAQGBCkEFg10BAQMBViMQCw4mEhQYDSSINwjPIwEBAQEBAQQBAQEBHos6hQUHhC0FhQmWW5dbgUUjgWaCLiKCeAEBAQ X-IPAS-Result: A0A2FgA731xV/wihxEVcgxCEAoVVu0CEfoJNBAICgTw6EwEBAQEBAQGBCkEFg10BAQMBViMQCw4mEhQYDSSINwjPIwEBAQEBAQQBAQEBHos6hQUHhC0FhQmWW5dbgUUjgWaCLiKCeAEBAQ X-IronPort-AV: E=Sophos;i="5.13,465,1427774400"; d="scan'208";a="166437498" Original-Received: from 69-196-161-8.dsl.teksavvy.com (HELO pastel.home) ([69.196.161.8]) by ironport2-out.teksavvy.com with ESMTP; 29 Sep 2015 21:46:37 -0400 Original-Received: by pastel.home (Postfix, from userid 20848) id 7B20C627CC; Tue, 29 Sep 2015 21:46:37 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Wed, 28 May 2014 22:23:50 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x 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-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:107074 Archived-At: >> In 24.3.91, the continued line gets way too much indentation: >> for foo in a \ >> b; do >> echo $foo >> done > I think this requires a config variable: You can now set sh-indent-after-continuation to `always' to get the "dumb" behavior. Stefan diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 049c93d..89d36bc 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -1991,9 +1991,30 @@ Does not preserve point." (t tok))))))) (defcustom sh-indent-after-continuation t - "If non-nil, try to make sure text is indented after a line continuation." - :version "24.3" - :type 'boolean + "If non-nil, indent relative to the continued line's beginning. +Continued lines can either be indented as \"one long wrapped line\" without +paying attention to the actual syntactic structure, as in: + + for f \ + in a; do \ + toto; \ + done + +or as lines that just don't have implicit semi-colons between them, as in: + + for f \ + in a; do \ + toto; \ + done + +With `always' you get the former behavior whereas with nil you get the latter. +With t, you get the latter except if that would not indent the continuation line +deeper than the initial line." + :version "25.1" + :type '(choice + (const nil :tag "Never") + (const t :tag "Only if needed to make it deeper") + (const always :tag "Always")) :group 'sh-indentation) (defun sh-smie--continuation-start-indent () @@ -2004,24 +2025,49 @@ May return nil if the line should not be treated as continued." (unless (sh-smie--looking-back-at-continuation-p) (current-indentation)))) +(defun sh-smie--indent-continuation () + (cond + ((not (and sh-indent-after-continuation + (save-excursion + (ignore-errors + (skip-chars-backward " \t") + (sh-smie--looking-back-at-continuation-p))))) + nil) + ((eq sh-indent-after-continuation 'always) + (save-excursion + (forward-line -1) + (if (sh-smie--looking-back-at-continuation-p) + (current-indentation) + (+ (current-indentation) sh-indentation)))) + (t + ;; Just make sure a line-continuation is indented deeper. + (save-excursion + (let ((indent (let ((sh-indent-after-continuation nil)) + (smie-indent-calculate))) + (max most-positive-fixnum)) + (if (not (numberp indent)) indent + (while (progn + (forward-line -1) + (let ((ci (current-indentation))) + (cond + ;; Previous line is less indented, we're good. + ((< ci indent) nil) + ((sh-smie--looking-back-at-continuation-p) + (setq max (min max ci)) + ;; Previous line is itself a continuation. + ;; If it's indented like us, we're good, otherwise + ;; check the line before that one. + (> ci indent)) + (t ;Previous line is the beginning of the continued line. + (setq indent (min (+ ci sh-indentation) max)) + nil))))) + indent)))))) + (defun sh-smie-sh-rules (kind token) (pcase (cons kind token) (`(:elem . basic) sh-indentation) (`(:after . "case-)") (- (sh-var-value 'sh-indent-for-case-alt) (sh-var-value 'sh-indent-for-case-label))) - ((and `(:before . ,_) - ;; After a line-continuation, make sure the rest is indented. - (guard sh-indent-after-continuation) - (guard (save-excursion - (ignore-errors - (skip-chars-backward " \t") - (sh-smie--looking-back-at-continuation-p)))) - (let initial (sh-smie--continuation-start-indent)) - (guard (let* ((sh-indent-after-continuation nil) - (indent (smie-indent-calculate))) - (and (numberp indent) (numberp initial) - (<= indent initial))))) - `(column . ,(+ initial sh-indentation))) (`(:before . ,(or `"(" `"{" `"[" "while" "if" "for" "case")) (if (not (smie-rule-prev-p "&&" "||" "|")) (when (smie-rule-hanging-p) @@ -2363,6 +2409,7 @@ Calls the value of `sh-set-shell-hook' if set." (if (looking-at "[ \t]*\\\\\n") (goto-char (match-end 0)) (funcall orig)))) + (add-hook 'smie-indent-functions #'sh-smie--indent-continuation nil t) (smie-setup (symbol-value (funcall mksym "grammar")) (funcall mksym "rules") :forward-token (funcall mksym "forward-token")