aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauro Aranda2023-10-14 09:05:35 -0300
committerStefan Monnier2023-10-14 11:00:15 -0400
commitad02fc212b5a88d2ea793858d538809d9976b154 (patch)
tree7552c560f37894fc2c03b8301fb6eddb63443018
parent1f95f91d855b8e6fd4221650b7878008f7665454 (diff)
downloademacs-ad02fc212b5a88d2ea793858d538809d9976b154.tar.gz
emacs-ad02fc212b5a88d2ea793858d538809d9976b154.zip
Fix indentation and fontification in shell-script (Bug#26217)
* lisp/progmodes/sh-script.el (sh-smie--sh-keyword-p): Treat "do" as special, like we treat "in". (sh-smie--sh-keyword-in-p): Change signature. Take the token to decide correctly if it's a keyword. (sh-font-lock-keywords-var-1): Add do. * test/lisp/progmodes/sh-script-resources/sh-indents.erts: New test. * test/lisp/progmodes/sh-script-tests.el (sh-script-test-do-fontification): New test.
-rw-r--r--lisp/progmodes/sh-script.el13
-rw-r--r--test/lisp/progmodes/sh-script-resources/sh-indents.erts7
-rw-r--r--test/lisp/progmodes/sh-script-tests.el11
3 files changed, 26 insertions, 5 deletions
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index cc521cb0591..de76e175a10 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -869,7 +869,7 @@ See `sh-feature'.")
869 "Default expressions to highlight in Shell Script modes. See `sh-feature'.") 869 "Default expressions to highlight in Shell Script modes. See `sh-feature'.")
870 870
871(defvar sh-font-lock-keywords-var-1 871(defvar sh-font-lock-keywords-var-1
872 '((sh "[ \t]in\\>")) 872 '((sh "[ \t]\\(in\\|do\\)\\>"))
873 "Subdued level highlighting for Shell Script modes.") 873 "Subdued level highlighting for Shell Script modes.")
874 874
875(defvar sh-font-lock-keywords-var-2 () 875(defvar sh-font-lock-keywords-var-2 ()
@@ -1809,8 +1809,8 @@ before the newline and in that case point should be just before the token."
1809 (concat "\\(?:^\\|[^\\]\\)\\(?:\\\\\\\\\\)*" 1809 (concat "\\(?:^\\|[^\\]\\)\\(?:\\\\\\\\\\)*"
1810 "\\(" sh-smie--sh-operators-re "\\)")) 1810 "\\(" sh-smie--sh-operators-re "\\)"))
1811 1811
1812(defun sh-smie--sh-keyword-in-p () 1812(defun sh-smie--sh-keyword-in/do-p (tok)
1813 "Assuming we're looking at \"in\", return non-nil if it's a keyword. 1813 "When looking at TOK (either \"in\" or \"do\"), non-nil if TOK is a keyword.
1814Does not preserve point." 1814Does not preserve point."
1815 (let ((forward-sexp-function nil) 1815 (let ((forward-sexp-function nil)
1816 (words nil) ;We've seen words. 1816 (words nil) ;We've seen words.
@@ -1832,7 +1832,10 @@ Does not preserve point."
1832 ((equal prev ";") 1832 ((equal prev ";")
1833 (if words (setq newline t) 1833 (if words (setq newline t)
1834 (setq res 'keyword))) 1834 (setq res 'keyword)))
1835 ((member prev '("case" "for" "select")) (setq res 'keyword)) 1835 ((member prev (if (string= tok "in")
1836 '("case" "for" "select")
1837 '("for" "select")))
1838 (setq res 'keyword))
1836 ((assoc prev smie-grammar) (setq res 'word)) 1839 ((assoc prev smie-grammar) (setq res 'word))
1837 (t 1840 (t
1838 (if newline 1841 (if newline
@@ -1844,7 +1847,7 @@ Does not preserve point."
1844 "Non-nil if TOK (at which we're looking) really is a keyword." 1847 "Non-nil if TOK (at which we're looking) really is a keyword."
1845 (cond 1848 (cond
1846 ((looking-at "[[:alnum:]_]+=") nil) 1849 ((looking-at "[[:alnum:]_]+=") nil)
1847 ((equal tok "in") (sh-smie--sh-keyword-in-p)) 1850 ((member tok '("in" "do")) (sh-smie--sh-keyword-in/do-p tok))
1848 (t (sh-smie--keyword-p)))) 1851 (t (sh-smie--keyword-p))))
1849 1852
1850(defun sh-smie--default-forward-token () 1853(defun sh-smie--default-forward-token ()
diff --git a/test/lisp/progmodes/sh-script-resources/sh-indents.erts b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
index 1f92610b3aa..36f4e4c22ab 100644
--- a/test/lisp/progmodes/sh-script-resources/sh-indents.erts
+++ b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
@@ -38,3 +38,10 @@ if test ;then
38fi 38fi
39other 39other
40=-=-= 40=-=-=
41
42Name: sh-indents5
43
44=-=
45for i do echo 1; done
46for i; do echo 1; done
47=-=-=
diff --git a/test/lisp/progmodes/sh-script-tests.el b/test/lisp/progmodes/sh-script-tests.el
index 52c1303c414..135d7afe3fe 100644
--- a/test/lisp/progmodes/sh-script-tests.el
+++ b/test/lisp/progmodes/sh-script-tests.el
@@ -87,4 +87,15 @@
87 (should-not (test-sh-back "foo;bar")) 87 (should-not (test-sh-back "foo;bar"))
88 (should (test-sh-back "foo#zot"))) 88 (should (test-sh-back "foo#zot")))
89 89
90(ert-deftest sh-script-test-do-fontification ()
91 "Test that \"do\" gets fontified correctly, even with no \";\"."
92 (with-temp-buffer
93 (shell-script-mode)
94 (insert "for i do echo 1; done")
95 (font-lock-ensure)
96 (goto-char (point-min))
97 (search-forward "do")
98 (forward-char -1)
99 (should (equal (get-text-property (point) 'face) 'font-lock-keyword-face))))
100
90;;; sh-script-tests.el ends here 101;;; sh-script-tests.el ends here