* lisp/progmodes/bat-mode.el: Rename from dos.el. Use "bat-" prefix.
[bpt/emacs.git] / lisp / progmodes / bat-mode.el
1 ;;; bat-mode.el --- Major mode for editing DOS/Windows scripts
2
3 ;; Copyright (C) 2003, 2008-2013 Free Software Foundation, Inc.
4
5 ;; Author: Arni Magnusson <arnima@hafro.is>
6 ;; Keywords: languages
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 ;; 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:
29 ;;
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
39 ;;
40 ;; Usage:
41 ;;
42 ;; See documentation of function `bat-mode'.
43 ;;
44 ;; Separate package `dos-indent' (Matthew Fidler) provides rudimentary
45 ;; indentation, see http://www.emacswiki.org/emacs/dos-indent.el.
46 ;;
47 ;; Acknowledgements:
48 ;;
49 ;; Inspired by `batch-mode' (Agnar Renolen) and `cmd-mode' (Tadamegu Furukawa).
50
51 ;;; Code:
52
53 ;; 1 Preamble
54
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)
59
60 ;; 2 User variables
61
62 (defface bat-label-face '((t :weight bold))
63 "Font Lock mode face used to highlight labels in batch files.")
64
65 ;; 3 Internal variables
66
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 ("^[ \t]*\\(@?rem\\_>\\|::\\).*"
84 (0 font-lock-comment-face t))
85 ("^:[^:].*"
86 . 'bat-label-face)
87 ("\\<_\\(defined\\|set\\)\\_>[ \t]*\\(\\w+\\)"
88 (2 font-lock-variable-name-face))
89 ("%\\(\\w+\\)%?"
90 (1 font-lock-variable-name-face))
91 ("!\\(\\w+\\)!?" ; delayed-expansion !variable!
92 (1 font-lock-variable-name-face))
93 ("[ =][-/]+\\(\\w+\\)"
94 (1 font-lock-type-face append))
95 (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
96 (,(concat "\\_<" (regexp-opt CONTROLFLOW) "\\_>")
97 . font-lock-keyword-face)
98 (,(concat "\\_<" (regexp-opt UNIX) "\\_>")
99 . font-lock-warning-face)))))
100
101 (defvar bat-menu
102 '("Bat"
103 ["Run" bat-run :help "Run script"]
104 ["Run with Args" bat-run-args :help "Run script with args"]
105 "--"
106 ["Imenu" imenu :help "Navigate with imenu"]
107 "--"
108 ["Template" bat-template :help "Insert template"]
109 "--"
110 ["Help (Command)" bat-cmd-help :help "Show help page for DOS command"]))
111
112 (defvar bat-mode-map
113 (let ((map (make-sparse-keymap)))
114 (easy-menu-define nil map nil bat-menu)
115 (define-key map [?\C-c ?\C-/] 'bat-cmd-help) ;FIXME: Why not C-c C-? ?
116 (define-key map [?\C-c ?\C-a] 'bat-run-args)
117 (define-key map [?\C-c ?\C-c] 'bat-run)
118 (define-key map [?\C-c ?\C-t] 'bat-template)
119 (define-key map [?\C-c ?\C-v] 'bat-run)
120 map))
121
122 (defvar bat-mode-syntax-table
123 (let ((table (make-syntax-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))
134
135 ;; 4 User functions
136
137 (defun bat-cmd-help (cmd)
138 "Show help for batch file command CMD."
139 (interactive "sHelp: ")
140 (if (string-equal cmd "net")
141 ;; FIXME: liable to quoting nightmare. Use call-process?
142 (shell-command "net /?") (shell-command (concat "help " cmd))))
143
144 (defun bat-run ()
145 "Run a batch file."
146 (interactive)
147 ;; FIXME: liable to quoting nightmare. Use call/start-process?
148 (save-buffer) (shell-command buffer-file-name))
149
150 (defun bat-run-args (args)
151 "Run a batch file with ARGS."
152 (interactive "sArgs: ")
153 ;; FIXME: Use `compile'?
154 (shell-command (concat buffer-file-name " " args)))
155
156 (defun bat-template ()
157 "Insert minimal batch file template."
158 (interactive)
159 (goto-char (point-min)) (insert "@echo off\nsetlocal\n\n"))
160
161 ;;;###autoload
162 (add-to-list 'auto-mode-alist '("\\.\\(bat\\|cmd\\)\\'" . bat-mode))
163
164 ;; 5 Main function
165
166 ;;;###autoload
167 (define-derived-mode bat-mode prog-mode "Bat"
168 "Major mode for editing DOS/Windows batch files.\n
169 Start a new script from `bat-template'. Read help pages for DOS commands
170 with `bat-cmd-help'. Navigate between sections using `imenu'.
171 Run script using `bat-run' and `bat-run-args'.\n
172 \\{bat-mode-map}"
173 (setq-local comment-start "rem ")
174 (setq-local font-lock-defaults
175 '(bat-font-lock-keywords nil t)) ; case-insensitive keywords
176 (setq-local imenu-generic-expression '((nil "^:[^:].*" 0)))
177 (setq-local outline-regexp ":[^:]"))
178
179 (provide 'bat-mode)
180
181 ;;; bat-mode.el ends here