From: Vladimir Panteleev Date: Fri, 10 Feb 2017 09:23:24 +0000 (+0200) Subject: Improve fontification in bat-mode X-Git-Tag: emacs-26.0.90~848^2~9 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2d284db5c9c5ff23269e2ec277f5348abdf1cd47;p=emacs.git 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. --- 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 @@ (2 font-lock-constant-face t)) ("^:[^:].*" . 'bat-label-face) - ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\w+\\)" + ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\(\\sw\\|\\s_\\)+\\)" (2 font-lock-variable-name-face)) - ("%\\(\\w+\\)%?" + ("%\\(\\(\\sw\\|\\s_\\)+\\)%" (1 font-lock-variable-name-face)) - ("!\\(\\w+\\)!?" ; 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) @@ -130,6 +133,7 @@ (modify-syntax-entry ?{ "_" table) (modify-syntax-entry ?} "_" table) (modify-syntax-entry ?\\ "." table) + (modify-syntax-entry ?= "." table) table)) (defconst bat--syntax-propertize @@ -175,6 +179,7 @@ with `bat-cmd-help'. Navigate between sections using `imenu'. Run script using `bat-run' and `bat-run-args'.\n \\{bat-mode-map}" (setq-local comment-start "rem ") + (setq-local comment-start-skip "rem[ \t]+") (setq-local syntax-propertize-function bat--syntax-propertize) (setq-local font-lock-defaults '(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 @@ +;;; bat-mode-tests.el --- Tests for bat-mode.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2017 Free Software Foundation, Inc. + +;; Author: Vladimir Panteleev +;; Keywords: + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'ert) +(require 'bat-mode) +(require 'htmlfontify) + +(defun bat-test-fontify (str) + "Fontify STR in `bat-mode' to a HTML string using `htmlfontify' and return it." + (with-temp-buffer + (insert str) + (bat-mode) + (let ((hfy-optimizations '(body-text-only merge-adjacent-tags))) + (with-current-buffer (htmlfontify-buffer) (buffer-string))))) + +(ert-deftest bat-test-fontification-var-decl () + "Test fontification of variable declarations." + (should + (equal + (bat-test-fontify "set a_b-c{d}e=f") + "set a_b-c{d}e=f"))) + +(ert-deftest bat-test-fontification-var-exp () + "Test fontification of variable expansions." + (should + (equal + (bat-test-fontify "echo %a_b-c{d}e%") + "echo %a_b-c{d}e%"))) + +(ert-deftest bat-test-fontification-var-delayed-exp () + "Test fontification of delayed variable expansions." + (should + (equal + (bat-test-fontify "echo !a_b-c{d}e!") + "echo !a_b-c{d}e!"))) + +(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") + "echo %%a +echo %%~dp1 +echo %%~$PATH:I"))) + +(defun bat-test-fill-paragraph (str) + "Return the result of invoking `fill-paragraph' on STR in a `bat-mode' buffer." + (with-temp-buffer + (bat-mode) + (insert str) + (goto-char 1) + (font-lock-ensure) + (fill-paragraph) + (buffer-string))) + +(ert-deftest bat-test-fill-paragraph-comment () + "Test `fill-paragraph' in a comment block." + (should (equal (bat-test-fill-paragraph "rem foo\nrem bar\n") "rem foo bar\n"))) + +(provide 'bat-tests) +;;; bat-mode-tests.el ends here