Minor fixes in lisp/progmodes/dos.el.
[emacs.git] / lisp / progmodes / dos.el
blob593b5e9fc92c140c45be75426460615e85f63349
1 ;;; dos.el --- Major mode for editing Dos scripts
3 ;; Copyright (C) 2003, 2008-2013 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 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 ;; dos-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 `dos-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 dos 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 dos-label-face '((t :weight bold))
63 "Font Lock mode face used to highlight labels in batch files."
64 :group 'dos)
66 ;; 3 Internal variables
68 (defvar dos-font-lock-keywords
69 (eval-when-compile
70 (let ((COMMANDS
71 '("assoc" "at" "attrib" "cd" "cls" "color" "copy" "date" "del" "dir"
72 "doskey" "echo" "endlocal" "erase" "fc" "find" "findstr" "format"
73 "ftype" "label" "md" "mkdir" "more" "move" "net" "path" "pause"
74 "popd" "prompt" "pushd" "rd" "ren" "rename" "replace" "rmdir" "set"
75 "setlocal" "shift" "sort" "subst" "time" "title" "tree" "type"
76 "ver" "vol" "xcopy"))
77 (CONTROLFLOW
78 '("call" "cmd" "defined" "do" "else" "equ" "exist" "exit" "for" "geq"
79 "goto" "gtr" "if" "in" "leq" "lss" "neq" "not" "start"))
80 (UNIX
81 '("bash" "cat" "cp" "fgrep" "grep" "ls" "sed" "sh" "mv" "rm")))
82 `(("\\<_\\(call\\|goto\\)\\_>[ \t]+%?\\([A-Za-z0-9-_\\:.]+\\)%?"
83 (2 font-lock-constant-face t))
84 ("^[ \t]*\\(@?rem\\_>\\|::\\).*"
85 (0 font-lock-comment-face t))
86 ("^:[^:].*"
87 . 'dos-label-face)
88 ("\\<_\\(defined\\|set\\)\\_>[ \t]*\\(\\w+\\)"
89 (2 font-lock-variable-name-face))
90 ("%\\(\\w+\\)%?"
91 (1 font-lock-variable-name-face))
92 ("!\\(\\w+\\)!?" ; delayed-expansion !variable!
93 (1 font-lock-variable-name-face))
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 dos-menu
103 '("Dos"
104 ["Run" dos-run :help "Run script"]
105 ["Run with Args" dos-run-args :help "Run script with args"]
106 "--"
107 ["Imenu" imenu :help "Navigate with imenu"]
108 "--"
109 ["Template" dos-template :help "Insert template"]
110 "--"
111 ["Help (Command)" dos-cmd-help :help "Show help page for Dos command"]
112 ["Help (Mode)" dos-mode-help :help "Show help page for Emacs Dos Mode"]))
114 (defvar dos-mode-map
115 (let ((map (make-sparse-keymap)))
116 (easy-menu-define nil map nil dos-menu)
117 (define-key map [?\C-c ?\C-.] 'dos-mode-help)
118 (define-key map [?\C-c ?\C-/] 'dos-cmd-help) ;FIXME: Why not C-c C-? ?
119 (define-key map [?\C-c ?\C-a] 'dos-run-args)
120 (define-key map [?\C-c ?\C-c] 'dos-run)
121 (define-key map [?\C-c ?\C-t] 'dos-template)
122 (define-key map [?\C-c ?\C-v] 'dos-run)
123 map))
125 (defvar dos-mode-syntax-table
126 (let ((table (make-syntax-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 table))
138 ;; 4 User functions
140 (defun dos-cmd-help (cmd)
141 "Show help for batch file command CMD."
142 (interactive "sHelp: ")
143 (if (string-equal cmd "net")
144 ;; FIXME: liable to quoting nightmare. Use call-process?
145 (shell-command "net /?") (shell-command (concat "help " cmd))))
147 (defun dos-mode-help ()
148 "Show help page for `dos-mode'."
149 (interactive)
150 (describe-function 'dos-mode)
151 (switch-to-buffer "*Help*") (delete-other-windows) (message nil))
153 (defun dos-run ()
154 "Run a batch file."
155 (interactive)
156 ;; FIXME: liable to quoting nightmare. Use call/start-process?
157 (save-buffer) (shell-command buffer-file-name))
159 (defun dos-run-args (args)
160 "Run a batch file with ARGS."
161 (interactive "sArgs: ")
162 ;; FIXME: Use `compile'?
163 (shell-command (concat buffer-file-name " " args)))
165 (defun dos-template ()
166 "Insert minimal batch file template."
167 (interactive)
168 (goto-char (point-min)) (insert "@echo off\nsetlocal\n\n"))
170 ;;;###autoload
171 (add-to-list 'auto-mode-alist '("\\.\\(bat\\|cmd\\)\\'" . dos-mode))
173 ;; 5 Main function
175 ;;;###autoload
176 (define-derived-mode dos-mode prog-mode "Dos"
177 "Major mode for editing DOS/Windows batch files.\n
178 The `dos-mode-help' command shows this page.\n
179 Start a new script from `dos-template'. Read help pages for Dos commands
180 with `dos-cmd-help'. Navigate between sections using `imenu'.
181 Run script using `dos-run' and `dos-run-args'.\n
182 \\{dos-mode-map}"
183 (setq-local comment-start "rem ")
184 (setq-local font-lock-defaults
185 '(dos-font-lock-keywords nil t)) ; case-insensitive keywords
186 (setq-local imenu-generic-expression '((nil "^:[^:].*" 0)))
187 (setq-local outline-regexp ":[^:]"))
189 (provide 'dos)
191 ;;; dos.el ends here