Fix a comment whitespace typo.
[emacs.git] / lisp / progmodes / bat-mode.el
blob1dd2e3757edafee73b515573137fe6329629af3c
1 ;;; bat-mode.el --- Major mode for editing DOS/Windows scripts
3 ;; Copyright (C) 2003, 2008-2017 Free Software Foundation, Inc.
5 ;; Author: Arni Magnusson <arnima@hafro.is>
6 ;; Keywords: languages
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Major mode for editing DOS/Windows scripts (batch files). Provides syntax
26 ;; highlighting, a basic template, access to DOS help pages, imenu/outline
27 ;; navigation, and the ability to run scripts from within Emacs. The syntax
28 ;; groups for highlighting are:
30 ;; Face Example
31 ;; bat-label-face :LABEL
32 ;; font-lock-comment-face rem
33 ;; font-lock-builtin-face copy
34 ;; font-lock-keyword-face goto
35 ;; font-lock-warning-face cp
36 ;; font-lock-constant-face [call] prog
37 ;; font-lock-variable-name-face %var%
38 ;; font-lock-type-face -option
40 ;; Usage:
42 ;; See documentation of function `bat-mode'.
44 ;; Separate package `dos-indent' (Matthew Fidler) provides rudimentary
45 ;; indentation, see http://www.emacswiki.org/emacs/dos-indent.el.
47 ;; Acknowledgements:
49 ;; Inspired by `batch-mode' (Agnar Renolen) and `cmd-mode' (Tadamegu Furukawa).
51 ;;; Code:
53 ;; 1 Preamble
55 (defgroup bat-mode nil
56 "Major mode for editing DOS/Windows batch files."
57 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
58 :group 'languages)
60 ;; 2 User variables
62 (defface bat-label-face '((t :weight bold))
63 "Font Lock mode face used to highlight labels in batch files.")
65 ;; 3 Internal variables
67 (defvar bat-font-lock-keywords
68 (eval-when-compile
69 (let ((COMMANDS
70 '("assoc" "at" "attrib" "cd" "cls" "color" "copy" "date" "del" "dir"
71 "doskey" "echo" "endlocal" "erase" "fc" "find" "findstr" "format"
72 "ftype" "label" "md" "mkdir" "more" "move" "net" "path" "pause"
73 "popd" "prompt" "pushd" "rd" "ren" "rename" "replace" "rmdir" "set"
74 "setlocal" "shift" "sort" "subst" "time" "title" "tree" "type"
75 "ver" "vol" "xcopy"))
76 (CONTROLFLOW
77 '("call" "cmd" "defined" "do" "else" "equ" "exist" "exit" "for" "geq"
78 "goto" "gtr" "if" "in" "leq" "lss" "neq" "not" "start"))
79 (UNIX
80 '("bash" "cat" "cp" "fgrep" "grep" "ls" "sed" "sh" "mv" "rm")))
81 `(("\\_<\\(call\\|goto\\)\\_>[ \t]+%?\\([A-Za-z0-9-_\\:.]+\\)%?"
82 (2 font-lock-constant-face t))
83 ("^:[^:].*"
84 . 'bat-label-face)
85 ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\(\\sw\\|\\s_\\)+\\)"
86 (2 font-lock-variable-name-face))
87 ("%\\(\\(\\sw\\|\\s_\\)+\\)%"
88 (1 font-lock-variable-name-face))
89 ("!\\(\\(\\sw\\|\\s_\\)+\\)!" ; delayed-expansion !variable!
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
94 ("[ =][-/]+\\(\\w+\\)"
95 (1 font-lock-type-face append))
96 (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
97 (,(concat "\\_<" (regexp-opt CONTROLFLOW) "\\_>")
98 . font-lock-keyword-face)
99 (,(concat "\\_<" (regexp-opt UNIX) "\\_>")
100 . font-lock-warning-face)))))
102 (defvar bat-menu
103 '("Bat"
104 ["Run" bat-run :help "Run script"]
105 ["Run with Args" bat-run-args :help "Run script with args"]
106 "--"
107 ["Imenu" imenu :help "Navigate with imenu"]
108 "--"
109 ["Template" bat-template :help "Insert template"]
110 "--"
111 ["Help (Command)" bat-cmd-help :help "Show help page for DOS command"]))
113 (defvar bat-mode-map
114 (let ((map (make-sparse-keymap)))
115 (easy-menu-define nil map nil bat-menu)
116 (define-key map [?\C-c ?\C-/] 'bat-cmd-help) ;FIXME: Why not C-c C-? ?
117 (define-key map [?\C-c ?\C-a] 'bat-run-args)
118 (define-key map [?\C-c ?\C-c] 'bat-run)
119 (define-key map [?\C-c ?\C-t] 'bat-template)
120 (define-key map [?\C-c ?\C-v] 'bat-run)
121 map))
123 (defvar bat-mode-syntax-table
124 (let ((table (make-syntax-table)))
125 (modify-syntax-entry ?\n ">" table)
126 (modify-syntax-entry ?\" "\"" table)
127 ;; Beware: `w' should not be used for non-alphabetic chars.
128 (modify-syntax-entry ?~ "_" table)
129 (modify-syntax-entry ?% "." table)
130 (modify-syntax-entry ?- "_" table)
131 (modify-syntax-entry ?_ "_" table)
132 ;; FIXME: { and } can appear in identifiers? Really?
133 (modify-syntax-entry ?{ "_" table)
134 (modify-syntax-entry ?} "_" table)
135 (modify-syntax-entry ?\\ "." table)
136 (modify-syntax-entry ?= "." table)
137 table))
139 (defconst bat--syntax-propertize
140 (syntax-propertize-rules
141 ("^[ \t]*\\(?:\\(@?r\\)em\\_>\\|\\(?1::\\):\\).*" (1 "<"))))
143 ;; 4 User functions
145 (defun bat-cmd-help (cmd)
146 "Show help for batch file command CMD."
147 (interactive "sHelp: ")
148 (if (string-equal cmd "net")
149 ;; FIXME: liable to quoting nightmare. Use call-process?
150 (shell-command "net /?") (shell-command (concat "help " cmd))))
152 (defun bat-run ()
153 "Run a batch file."
154 (interactive)
155 ;; FIXME: liable to quoting nightmare. Use call/start-process?
156 (save-buffer) (shell-command buffer-file-name))
158 (defun bat-run-args (args)
159 "Run a batch file with ARGS."
160 (interactive "sArgs: ")
161 ;; FIXME: Use `compile'?
162 (shell-command (concat buffer-file-name " " args)))
164 (defun bat-template ()
165 "Insert minimal batch file template."
166 (interactive)
167 (goto-char (point-min)) (insert "@echo off\nsetlocal\n\n"))
169 ;;;###autoload
170 (add-to-list 'auto-mode-alist '("\\.\\(bat\\|cmd\\)\\'" . bat-mode))
172 ;; 5 Main function
174 ;;;###autoload
175 (define-derived-mode bat-mode prog-mode "Bat"
176 "Major mode for editing DOS/Windows batch files.\n
177 Start a new script from `bat-template'. Read help pages for DOS commands
178 with `bat-cmd-help'. Navigate between sections using `imenu'.
179 Run script using `bat-run' and `bat-run-args'.\n
180 \\{bat-mode-map}"
181 (setq-local comment-start "rem ")
182 (setq-local comment-start-skip "rem[ \t]+")
183 (setq-local syntax-propertize-function bat--syntax-propertize)
184 (setq-local font-lock-defaults
185 '(bat-font-lock-keywords nil t)) ; case-insensitive keywords
186 (setq-local imenu-generic-expression '((nil "^:[^:].*" 0)))
187 (setq-local outline-regexp ":[^:]"))
189 (provide 'bat-mode)
191 ;;; bat-mode.el ends here