* lisp/vc/vc-git.el (vc-git-region-history): Handle local changes
[emacs.git] / lisp / progmodes / bat-mode.el
blob2c5f192bf87853d415835773fd7c6ae3096121bf
1 ;;; bat-mode.el --- Major mode for editing DOS/Windows scripts
3 ;; Copyright (C) 2003, 2008-2015 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]*\\(\\w+\\)"
86 (2 font-lock-variable-name-face))
87 ("%\\(\\w+\\)%?"
88 (1 font-lock-variable-name-face))
89 ("!\\(\\w+\\)!?" ; delayed-expansion !variable!
90 (1 font-lock-variable-name-face))
91 ("[ =][-/]+\\(\\w+\\)"
92 (1 font-lock-type-face append))
93 (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
94 (,(concat "\\_<" (regexp-opt CONTROLFLOW) "\\_>")
95 . font-lock-keyword-face)
96 (,(concat "\\_<" (regexp-opt UNIX) "\\_>")
97 . font-lock-warning-face)))))
99 (defvar bat-menu
100 '("Bat"
101 ["Run" bat-run :help "Run script"]
102 ["Run with Args" bat-run-args :help "Run script with args"]
103 "--"
104 ["Imenu" imenu :help "Navigate with imenu"]
105 "--"
106 ["Template" bat-template :help "Insert template"]
107 "--"
108 ["Help (Command)" bat-cmd-help :help "Show help page for DOS command"]))
110 (defvar bat-mode-map
111 (let ((map (make-sparse-keymap)))
112 (easy-menu-define nil map nil bat-menu)
113 (define-key map [?\C-c ?\C-/] 'bat-cmd-help) ;FIXME: Why not C-c C-? ?
114 (define-key map [?\C-c ?\C-a] 'bat-run-args)
115 (define-key map [?\C-c ?\C-c] 'bat-run)
116 (define-key map [?\C-c ?\C-t] 'bat-template)
117 (define-key map [?\C-c ?\C-v] 'bat-run)
118 map))
120 (defvar bat-mode-syntax-table
121 (let ((table (make-syntax-table)))
122 (modify-syntax-entry ?\n ">" table)
123 (modify-syntax-entry ?\" "\"" table)
124 ;; Beware: `w' should not be used for non-alphabetic chars.
125 (modify-syntax-entry ?~ "_" table)
126 (modify-syntax-entry ?% "." table)
127 (modify-syntax-entry ?- "_" table)
128 (modify-syntax-entry ?_ "_" table)
129 ;; FIXME: { and } can appear in identifiers? Really?
130 (modify-syntax-entry ?{ "_" table)
131 (modify-syntax-entry ?} "_" table)
132 (modify-syntax-entry ?\\ "." table)
133 table))
135 (defconst bat--syntax-propertize
136 (syntax-propertize-rules
137 ("^[ \t]*\\(?:\\(@?r\\)em\\_>\\|\\(?1::\\):\\).*" (1 "<"))))
139 ;; 4 User functions
141 (defun bat-cmd-help (cmd)
142 "Show help for batch file command CMD."
143 (interactive "sHelp: ")
144 (if (string-equal cmd "net")
145 ;; FIXME: liable to quoting nightmare. Use call-process?
146 (shell-command "net /?") (shell-command (concat "help " cmd))))
148 (defun bat-run ()
149 "Run a batch file."
150 (interactive)
151 ;; FIXME: liable to quoting nightmare. Use call/start-process?
152 (save-buffer) (shell-command buffer-file-name))
154 (defun bat-run-args (args)
155 "Run a batch file with ARGS."
156 (interactive "sArgs: ")
157 ;; FIXME: Use `compile'?
158 (shell-command (concat buffer-file-name " " args)))
160 (defun bat-template ()
161 "Insert minimal batch file template."
162 (interactive)
163 (goto-char (point-min)) (insert "@echo off\nsetlocal\n\n"))
165 ;;;###autoload
166 (add-to-list 'auto-mode-alist '("\\.\\(bat\\|cmd\\)\\'" . bat-mode))
168 ;; 5 Main function
170 ;;;###autoload
171 (define-derived-mode bat-mode prog-mode "Bat"
172 "Major mode for editing DOS/Windows batch files.\n
173 Start a new script from `bat-template'. Read help pages for DOS commands
174 with `bat-cmd-help'. Navigate between sections using `imenu'.
175 Run script using `bat-run' and `bat-run-args'.\n
176 \\{bat-mode-map}"
177 (setq-local comment-start "rem ")
178 (setq-local syntax-propertize-function bat--syntax-propertize)
179 (setq-local font-lock-defaults
180 '(bat-font-lock-keywords nil t)) ; case-insensitive keywords
181 (setq-local imenu-generic-expression '((nil "^:[^:].*" 0)))
182 (setq-local outline-regexp ":[^:]"))
184 (provide 'bat-mode)
186 ;;; bat-mode.el ends here