aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVladimir Panteleev2017-02-10 11:23:24 +0200
committerEli Zaretskii2017-02-10 11:23:24 +0200
commit2d284db5c9c5ff23269e2ec277f5348abdf1cd47 (patch)
tree6f135fd8d70b0e0d18391915295b521aa2635a6f
parent937bf04804246c86a4b1bd55b506169f5a894e3b (diff)
downloademacs-2d284db5c9c5ff23269e2ec277f5348abdf1cd47.tar.gz
emacs-2d284db5c9c5ff23269e2ec277f5348abdf1cd47.zip
Improve fontification in bat-mode
* lisp/progmodes/bat-mode.el (bat-font-lock-keywords): Match word and symbol constituents when looking for variable names to fontify; also, correct the syntax table and mark the equal sign (=) character as punctuation. Improve fontification accuracy of iteration/positional variables. (bat-mode): Set comment-start-skip. (Bug#25541) * test/lisp/progmodes/bat-mode-tests.el: New file, tests for bat-mode.el.
-rw-r--r--lisp/progmodes/bat-mode.el11
-rw-r--r--test/lisp/progmodes/bat-mode-tests.el86
2 files changed, 94 insertions, 3 deletions
diff --git a/lisp/progmodes/bat-mode.el b/lisp/progmodes/bat-mode.el
index 156331cf86d..1dd2e3757ed 100644
--- a/lisp/progmodes/bat-mode.el
+++ b/lisp/progmodes/bat-mode.el
@@ -82,12 +82,15 @@
82 (2 font-lock-constant-face t)) 82 (2 font-lock-constant-face t))
83 ("^:[^:].*" 83 ("^:[^:].*"
84 . 'bat-label-face) 84 . 'bat-label-face)
85 ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\w+\\)" 85 ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\(\\sw\\|\\s_\\)+\\)"
86 (2 font-lock-variable-name-face)) 86 (2 font-lock-variable-name-face))
87 ("%\\(\\w+\\)%?" 87 ("%\\(\\(\\sw\\|\\s_\\)+\\)%"
88 (1 font-lock-variable-name-face)) 88 (1 font-lock-variable-name-face))
89 ("!\\(\\w+\\)!?" ; delayed-expansion !variable! 89 ("!\\(\\(\\sw\\|\\s_\\)+\\)!" ; delayed-expansion !variable!
90 (1 font-lock-variable-name-face)) 90 (1 font-lock-variable-name-face))
91 ("%%\\(?:~[adfnpstxz]*\\(?:\\$\\(\\(?:\\sw\\|\\s_\\)+\\):\\)?\\)?\\([]!#$&-:?-[_-{}~]\\)"
92 (1 font-lock-variable-name-face nil t) ; PATH expansion
93 (2 font-lock-variable-name-face)) ; iteration variable or positional parameter
91 ("[ =][-/]+\\(\\w+\\)" 94 ("[ =][-/]+\\(\\w+\\)"
92 (1 font-lock-type-face append)) 95 (1 font-lock-type-face append))
93 (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face) 96 (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
@@ -130,6 +133,7 @@
130 (modify-syntax-entry ?{ "_" table) 133 (modify-syntax-entry ?{ "_" table)
131 (modify-syntax-entry ?} "_" table) 134 (modify-syntax-entry ?} "_" table)
132 (modify-syntax-entry ?\\ "." table) 135 (modify-syntax-entry ?\\ "." table)
136 (modify-syntax-entry ?= "." table)
133 table)) 137 table))
134 138
135(defconst bat--syntax-propertize 139(defconst bat--syntax-propertize
@@ -175,6 +179,7 @@ with `bat-cmd-help'. Navigate between sections using `imenu'.
175Run script using `bat-run' and `bat-run-args'.\n 179Run script using `bat-run' and `bat-run-args'.\n
176\\{bat-mode-map}" 180\\{bat-mode-map}"
177 (setq-local comment-start "rem ") 181 (setq-local comment-start "rem ")
182 (setq-local comment-start-skip "rem[ \t]+")
178 (setq-local syntax-propertize-function bat--syntax-propertize) 183 (setq-local syntax-propertize-function bat--syntax-propertize)
179 (setq-local font-lock-defaults 184 (setq-local font-lock-defaults
180 '(bat-font-lock-keywords nil t)) ; case-insensitive keywords 185 '(bat-font-lock-keywords nil t)) ; case-insensitive keywords
diff --git a/test/lisp/progmodes/bat-mode-tests.el b/test/lisp/progmodes/bat-mode-tests.el
new file mode 100644
index 00000000000..565718eea41
--- /dev/null
+++ b/test/lisp/progmodes/bat-mode-tests.el
@@ -0,0 +1,86 @@
1;;; bat-mode-tests.el --- Tests for bat-mode.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; Author: Vladimir Panteleev <vladimir@thecybershadow.net>
6;; Keywords:
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;;
26
27;;; Code:
28
29(require 'ert)
30(require 'bat-mode)
31(require 'htmlfontify)
32
33(defun bat-test-fontify (str)
34 "Fontify STR in `bat-mode' to a HTML string using `htmlfontify' and return it."
35 (with-temp-buffer
36 (insert str)
37 (bat-mode)
38 (let ((hfy-optimizations '(body-text-only merge-adjacent-tags)))
39 (with-current-buffer (htmlfontify-buffer) (buffer-string)))))
40
41(ert-deftest bat-test-fontification-var-decl ()
42 "Test fontification of variable declarations."
43 (should
44 (equal
45 (bat-test-fontify "set a_b-c{d}e=f")
46 "<span class=\"builtin\">set</span> <span class=\"variable-name\">a_b-c{d}e</span>=f")))
47
48(ert-deftest bat-test-fontification-var-exp ()
49 "Test fontification of variable expansions."
50 (should
51 (equal
52 (bat-test-fontify "echo %a_b-c{d}e%")
53 "<span class=\"builtin\">echo</span> %<span class=\"variable-name\">a_b-c{d}e</span>%")))
54
55(ert-deftest bat-test-fontification-var-delayed-exp ()
56 "Test fontification of delayed variable expansions."
57 (should
58 (equal
59 (bat-test-fontify "echo !a_b-c{d}e!")
60 "<span class=\"builtin\">echo</span> !<span class=\"variable-name\">a_b-c{d}e</span>!")))
61
62(ert-deftest bat-test-fontification-iter-var-1 ()
63 "Test fontification of iteration variables."
64 (should
65 (equal
66 (bat-test-fontify "echo %%a\necho %%~dp1\necho %%~$PATH:I")
67 "<span class=\"builtin\">echo</span> %%<span class=\"variable-name\">a</span>
68<span class=\"builtin\">echo</span> %%~dp<span class=\"variable-name\">1</span>
69<span class=\"builtin\">echo</span> %%~$<span class=\"variable-name\">PATH</span>:<span class=\"variable-name\">I</span>")))
70
71(defun bat-test-fill-paragraph (str)
72 "Return the result of invoking `fill-paragraph' on STR in a `bat-mode' buffer."
73 (with-temp-buffer
74 (bat-mode)
75 (insert str)
76 (goto-char 1)
77 (font-lock-ensure)
78 (fill-paragraph)
79 (buffer-string)))
80
81(ert-deftest bat-test-fill-paragraph-comment ()
82 "Test `fill-paragraph' in a comment block."
83 (should (equal (bat-test-fill-paragraph "rem foo\nrem bar\n") "rem foo bar\n")))
84
85(provide 'bat-tests)
86;;; bat-mode-tests.el ends here