diff options
| author | Stefan Monnier | 2015-09-29 21:43:07 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2015-09-29 21:43:07 -0400 |
| commit | d6b49570f6fe1cfb5314c1b61b81ede0497a06b2 (patch) | |
| tree | 7559f63a2408a1cd39906d2a6dcda1adb3740aae | |
| parent | 90a6f8d0741eb5391c204f059c845c361e615b49 (diff) | |
| download | emacs-d6b49570f6fe1cfb5314c1b61b81ede0497a06b2.tar.gz emacs-d6b49570f6fe1cfb5314c1b61b81ede0497a06b2.zip | |
* lisp/progmodes/sh-script.el: Old "dumb" continued line indent
(sh-indent-after-continuation): Add new value `always' (bug#17620)
(sh-smie-sh-rules): Remove old handling of continued lines.
(sh-smie--indent-continuation): New function.
(sh-set-shell): Use it.
| -rw-r--r-- | etc/NEWS | 6 | ||||
| -rw-r--r-- | lisp/progmodes/sh-script.el | 79 |
2 files changed, 68 insertions, 17 deletions
| @@ -634,9 +634,13 @@ you can no longer use commas to separate regular expressions. | |||
| 634 | 634 | ||
| 635 | ** SES now supports local printer functions; see `ses-define-local-printer'. | 635 | ** SES now supports local printer functions; see `ses-define-local-printer'. |
| 636 | 636 | ||
| 637 | ** In sh-mode, you can now use `sh-shell' as a file-local variable to | 637 | ** sh-script |
| 638 | *** In sh-mode you can now use `sh-shell' as a file-local variable to | ||
| 638 | specify the type of shell in use (bash, csh, etc). | 639 | specify the type of shell in use (bash, csh, etc). |
| 639 | 640 | ||
| 641 | *** New value `always' for sh-indent-after-continuation. | ||
| 642 | This provides old-style ("dumb") indentation of continued lines. | ||
| 643 | |||
| 640 | ** TLS | 644 | ** TLS |
| 641 | --- | 645 | --- |
| 642 | *** Fatal TLS errors are now silent by default. | 646 | *** Fatal TLS errors are now silent by default. |
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 049c93dfae2..fbb4a90db40 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el | |||
| @@ -1991,9 +1991,30 @@ Does not preserve point." | |||
| 1991 | (t tok))))))) | 1991 | (t tok))))))) |
| 1992 | 1992 | ||
| 1993 | (defcustom sh-indent-after-continuation t | 1993 | (defcustom sh-indent-after-continuation t |
| 1994 | "If non-nil, try to make sure text is indented after a line continuation." | 1994 | "If non-nil, indent relative to the continued line's beginning. |
| 1995 | :version "24.3" | 1995 | Continued lines can either be indented as \"one long wrapped line\" without |
| 1996 | :type 'boolean | 1996 | paying attention to the actual syntactic structure, as in: |
| 1997 | |||
| 1998 | for f \ | ||
| 1999 | in a; do \ | ||
| 2000 | toto; \ | ||
| 2001 | done | ||
| 2002 | |||
| 2003 | or as lines that just don't have implicit semi-colons between them, as in: | ||
| 2004 | |||
| 2005 | for f \ | ||
| 2006 | in a; do \ | ||
| 2007 | toto; \ | ||
| 2008 | done | ||
| 2009 | |||
| 2010 | With `always' you get the former behavior whereas with nil you get the latter. | ||
| 2011 | With t, you get the latter as long as that would indent the continuation line | ||
| 2012 | deeper than the initial line." | ||
| 2013 | :version "25.1" | ||
| 2014 | :type '(choice | ||
| 2015 | (const nil :tag "Never") | ||
| 2016 | (const t :tag "Only if needed to make it deeper") | ||
| 2017 | (const always :tag "Always")) | ||
| 1997 | :group 'sh-indentation) | 2018 | :group 'sh-indentation) |
| 1998 | 2019 | ||
| 1999 | (defun sh-smie--continuation-start-indent () | 2020 | (defun sh-smie--continuation-start-indent () |
| @@ -2004,24 +2025,49 @@ May return nil if the line should not be treated as continued." | |||
| 2004 | (unless (sh-smie--looking-back-at-continuation-p) | 2025 | (unless (sh-smie--looking-back-at-continuation-p) |
| 2005 | (current-indentation)))) | 2026 | (current-indentation)))) |
| 2006 | 2027 | ||
| 2028 | (defun sh-smie--indent-continuation () | ||
| 2029 | (cond | ||
| 2030 | ((not (and sh-indent-after-continuation | ||
| 2031 | (save-excursion | ||
| 2032 | (ignore-errors | ||
| 2033 | (skip-chars-backward " \t") | ||
| 2034 | (sh-smie--looking-back-at-continuation-p))))) | ||
| 2035 | nil) | ||
| 2036 | ((eq sh-indent-after-continuation 'always) | ||
| 2037 | (save-excursion | ||
| 2038 | (forward-line -1) | ||
| 2039 | (if (sh-smie--looking-back-at-continuation-p) | ||
| 2040 | (current-indentation) | ||
| 2041 | (+ (current-indentation) sh-indentation)))) | ||
| 2042 | (t | ||
| 2043 | ;; Just make sure a line-continuation is indented deeper. | ||
| 2044 | (save-excursion | ||
| 2045 | (let ((indent (let ((sh-indent-after-continuation nil)) | ||
| 2046 | (smie-indent-calculate))) | ||
| 2047 | (max most-positive-fixnum)) | ||
| 2048 | (if (not (numberp indent)) indent | ||
| 2049 | (while (progn | ||
| 2050 | (forward-line -1) | ||
| 2051 | (let ((ci (current-indentation))) | ||
| 2052 | (cond | ||
| 2053 | ;; Previous line is less indented, we're good. | ||
| 2054 | ((< ci indent) nil) | ||
| 2055 | ((sh-smie--looking-back-at-continuation-p) | ||
| 2056 | (setq max (min max ci)) | ||
| 2057 | ;; Previous line is itself a continuation. | ||
| 2058 | ;; If it's indented like us, we're good, otherwise | ||
| 2059 | ;; check the line before that one. | ||
| 2060 | (> ci indent)) | ||
| 2061 | (t ;Previous line is the beginning of the continued line. | ||
| 2062 | (setq indent (min (+ ci sh-indentation) max)) | ||
| 2063 | nil))))) | ||
| 2064 | indent)))))) | ||
| 2065 | |||
| 2007 | (defun sh-smie-sh-rules (kind token) | 2066 | (defun sh-smie-sh-rules (kind token) |
| 2008 | (pcase (cons kind token) | 2067 | (pcase (cons kind token) |
| 2009 | (`(:elem . basic) sh-indentation) | 2068 | (`(:elem . basic) sh-indentation) |
| 2010 | (`(:after . "case-)") (- (sh-var-value 'sh-indent-for-case-alt) | 2069 | (`(:after . "case-)") (- (sh-var-value 'sh-indent-for-case-alt) |
| 2011 | (sh-var-value 'sh-indent-for-case-label))) | 2070 | (sh-var-value 'sh-indent-for-case-label))) |
| 2012 | ((and `(:before . ,_) | ||
| 2013 | ;; After a line-continuation, make sure the rest is indented. | ||
| 2014 | (guard sh-indent-after-continuation) | ||
| 2015 | (guard (save-excursion | ||
| 2016 | (ignore-errors | ||
| 2017 | (skip-chars-backward " \t") | ||
| 2018 | (sh-smie--looking-back-at-continuation-p)))) | ||
| 2019 | (let initial (sh-smie--continuation-start-indent)) | ||
| 2020 | (guard (let* ((sh-indent-after-continuation nil) | ||
| 2021 | (indent (smie-indent-calculate))) | ||
| 2022 | (and (numberp indent) (numberp initial) | ||
| 2023 | (<= indent initial))))) | ||
| 2024 | `(column . ,(+ initial sh-indentation))) | ||
| 2025 | (`(:before . ,(or `"(" `"{" `"[" "while" "if" "for" "case")) | 2071 | (`(:before . ,(or `"(" `"{" `"[" "while" "if" "for" "case")) |
| 2026 | (if (not (smie-rule-prev-p "&&" "||" "|")) | 2072 | (if (not (smie-rule-prev-p "&&" "||" "|")) |
| 2027 | (when (smie-rule-hanging-p) | 2073 | (when (smie-rule-hanging-p) |
| @@ -2363,6 +2409,7 @@ Calls the value of `sh-set-shell-hook' if set." | |||
| 2363 | (if (looking-at "[ \t]*\\\\\n") | 2409 | (if (looking-at "[ \t]*\\\\\n") |
| 2364 | (goto-char (match-end 0)) | 2410 | (goto-char (match-end 0)) |
| 2365 | (funcall orig)))) | 2411 | (funcall orig)))) |
| 2412 | (add-hook 'smie-indent-functions #'sh-smie--indent-continuation nil t) | ||
| 2366 | (smie-setup (symbol-value (funcall mksym "grammar")) | 2413 | (smie-setup (symbol-value (funcall mksym "grammar")) |
| 2367 | (funcall mksym "rules") | 2414 | (funcall mksym "rules") |
| 2368 | :forward-token (funcall mksym "forward-token") | 2415 | :forward-token (funcall mksym "forward-token") |