Subject: [PATCH 3/3] Improve iteration variable fontification in bat-mode

From: Vladimir Panteleev <[email-masked]>
Date: Thu, 26 Jan 2017 04:36:24 +0000

* lisp/progmodes/bat-mode.el: Improve fontification accuracy of
iteration/positional variables.
* test/lisp/progmodes/bat-mode-tests.el: Add tests.
---
 lisp/progmodes/bat-mode.el            | 7 +++++--
 test/lisp/progmodes/bat-mode-tests.el | 9 +++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/bat-mode.el b/lisp/progmodes/bat-mode.el
index a945d37f1d..1dd2e3757e 100644
--- a/lisp/progmodes/bat-mode.el
+++ b/lisp/progmodes/bat-mode.el
@@ -84,10 +84,13 @@ bat-font-lock-keywords
          . 'bat-label-face)
         ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\(\\sw\\|\\s_\\)+\\)"
          (2 font-lock-variable-name-face))
-        ("%\\(\\(\\sw\\|\\s_\\)+\\)%?"
+        ("%\\(\\(\\sw\\|\\s_\\)+\\)%"
          (1 font-lock-variable-name-face))
-        ("!\\(\\(\\sw\\|\\s_\\)+\\)!?"  ; delayed-expansion !variable!
+        ("!\\(\\(\\sw\\|\\s_\\)+\\)!"  ; delayed-expansion !variable!
          (1 font-lock-variable-name-face))
+        ("%%\\(?:~[adfnpstxz]*\\(?:\\$\\(\\(?:\\sw\\|\\s_\\)+\\):\\)?\\)?\\([]!#$&-:?-[_-{}~]\\)"
+         (1 font-lock-variable-name-face nil t) ; PATH expansion
+         (2 font-lock-variable-name-face)) ; iteration variable or positional parameter
         ("[ =][-/]+\\(\\w+\\)"
          (1 font-lock-type-face append))
         (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
diff --git a/test/lisp/progmodes/bat-mode-tests.el b/test/lisp/progmodes/bat-mode-tests.el
index 335fc16ffb..565718eea4 100644
--- a/test/lisp/progmodes/bat-mode-tests.el
+++ b/test/lisp/progmodes/bat-mode-tests.el
@@ -59,6 +59,15 @@ bat-test-fontify
     (bat-test-fontify "echo !a_b-c{d}e!")
     "<span class=\"builtin\">echo</span> !<span class=\"variable-name\">a_b-c{d}e</span>!")))
 
+(ert-deftest bat-test-fontification-iter-var-1 ()
+  "Test fontification of iteration variables."
+  (should
+   (equal
+    (bat-test-fontify "echo %%a\necho %%~dp1\necho %%~$PATH:I")
+    "<span class=\"builtin\">echo</span> %%<span class=\"variable-name\">a</span>
+<span class=\"builtin\">echo</span> %%~dp<span class=\"variable-name\">1</span>
+<span class=\"builtin\">echo</span> %%~$<span class=\"variable-name\">PATH</span>:<span class=\"variable-name\">I</span>")))
+
 (defun bat-test-fill-paragraph (str)
   "Return the result of invoking `fill-paragraph' on STR in a `bat-mode' buffer."
   (with-temp-buffer
-- 
2.11.0