]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix indentation and fontification in shell-script (Bug#26217)
authorMauro Aranda <maurooaranda@gmail.com>
Sat, 14 Oct 2023 12:05:35 +0000 (09:05 -0300)
committerStefan Monnier <monnier@iro.umontreal.ca>
Sat, 14 Oct 2023 15:00:15 +0000 (11:00 -0400)
* 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.

lisp/progmodes/sh-script.el
test/lisp/progmodes/sh-script-resources/sh-indents.erts
test/lisp/progmodes/sh-script-tests.el

index cc521cb059143a09b1be6c3b93e060be1d61e8a9..de76e175a102ec666b2523d2429de485acc9b9b8 100644 (file)
@@ -869,7 +869,7 @@ See `sh-feature'.")
   "Default expressions to highlight in Shell Script modes.  See `sh-feature'.")
 
 (defvar sh-font-lock-keywords-var-1
-  '((sh "[ \t]in\\>"))
+  '((sh "[ \t]\\(in\\|do\\)\\>"))
   "Subdued level highlighting for Shell Script modes.")
 
 (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."
   (concat "\\(?:^\\|[^\\]\\)\\(?:\\\\\\\\\\)*"
           "\\(" sh-smie--sh-operators-re "\\)"))
 
-(defun sh-smie--sh-keyword-in-p ()
-  "Assuming we're looking at \"in\", return non-nil if it's a keyword.
+(defun sh-smie--sh-keyword-in/do-p (tok)
+  "When looking at TOK (either \"in\" or \"do\"), non-nil if TOK is a keyword.
 Does not preserve point."
   (let ((forward-sexp-function nil)
         (words nil)                     ;We've seen words.
@@ -1832,7 +1832,10 @@ Does not preserve point."
        ((equal prev ";")
         (if words (setq newline t)
           (setq res 'keyword)))
-       ((member prev '("case" "for" "select")) (setq res 'keyword))
+       ((member prev (if (string= tok "in")
+                         '("case" "for" "select")
+                       '("for" "select")))
+        (setq res 'keyword))
        ((assoc prev smie-grammar) (setq res 'word))
        (t
         (if newline
@@ -1844,7 +1847,7 @@ Does not preserve point."
   "Non-nil if TOK (at which we're looking) really is a keyword."
   (cond
    ((looking-at "[[:alnum:]_]+=") nil)
-   ((equal tok "in") (sh-smie--sh-keyword-in-p))
+   ((member tok '("in" "do")) (sh-smie--sh-keyword-in/do-p tok))
    (t (sh-smie--keyword-p))))
 
 (defun sh-smie--default-forward-token ()
index 1f92610b3aa6f1571a21d29a55950f33612d6d7b..36f4e4c22abdb24bca04632458a4ebf8a6c08131 100644 (file)
@@ -38,3 +38,10 @@ if test ;then
 fi
 other
 =-=-=
+
+Name: sh-indents5
+
+=-=
+for i do echo 1; done
+for i; do echo 1; done
+=-=-=
index 52c1303c41411736f234d80f0bd21b77f42c7821..135d7afe3fe651a27ce99cc2439b168afaed5495 100644 (file)
   (should-not (test-sh-back "foo;bar"))
   (should (test-sh-back "foo#zot")))
 
+(ert-deftest sh-script-test-do-fontification ()
+  "Test that \"do\" gets fontified correctly, even with no \";\"."
+  (with-temp-buffer
+    (shell-script-mode)
+    (insert "for i do echo 1; done")
+    (font-lock-ensure)
+    (goto-char (point-min))
+    (search-forward "do")
+    (forward-char -1)
+    (should (equal (get-text-property (point) 'face) 'font-lock-keyword-face))))
+
 ;;; sh-script-tests.el ends here