bat-mode.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ;;; bat-mode.el --- Major mode for editing DOS/Windows scripts
  2. ;; Copyright (C) 2003, 2008-2015 Free Software Foundation, Inc.
  3. ;; Author: Arni Magnusson <arnima@hafro.is>
  4. ;; Keywords: languages
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Major mode for editing DOS/Windows scripts (batch files). Provides syntax
  19. ;; highlighting, a basic template, access to DOS help pages, imenu/outline
  20. ;; navigation, and the ability to run scripts from within Emacs. The syntax
  21. ;; groups for highlighting are:
  22. ;;
  23. ;; Face Example
  24. ;; bat-label-face :LABEL
  25. ;; font-lock-comment-face rem
  26. ;; font-lock-builtin-face copy
  27. ;; font-lock-keyword-face goto
  28. ;; font-lock-warning-face cp
  29. ;; font-lock-constant-face [call] prog
  30. ;; font-lock-variable-name-face %var%
  31. ;; font-lock-type-face -option
  32. ;;
  33. ;; Usage:
  34. ;;
  35. ;; See documentation of function `bat-mode'.
  36. ;;
  37. ;; Separate package `dos-indent' (Matthew Fidler) provides rudimentary
  38. ;; indentation, see http://www.emacswiki.org/emacs/dos-indent.el.
  39. ;;
  40. ;; Acknowledgements:
  41. ;;
  42. ;; Inspired by `batch-mode' (Agnar Renolen) and `cmd-mode' (Tadamegu Furukawa).
  43. ;;; Code:
  44. ;; 1 Preamble
  45. (defgroup bat-mode nil
  46. "Major mode for editing DOS/Windows batch files."
  47. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  48. :group 'languages)
  49. ;; 2 User variables
  50. (defface bat-label-face '((t :weight bold))
  51. "Font Lock mode face used to highlight labels in batch files.")
  52. ;; 3 Internal variables
  53. (defvar bat-font-lock-keywords
  54. (eval-when-compile
  55. (let ((COMMANDS
  56. '("assoc" "at" "attrib" "cd" "cls" "color" "copy" "date" "del" "dir"
  57. "doskey" "echo" "endlocal" "erase" "fc" "find" "findstr" "format"
  58. "ftype" "label" "md" "mkdir" "more" "move" "net" "path" "pause"
  59. "popd" "prompt" "pushd" "rd" "ren" "rename" "replace" "rmdir" "set"
  60. "setlocal" "shift" "sort" "subst" "time" "title" "tree" "type"
  61. "ver" "vol" "xcopy"))
  62. (CONTROLFLOW
  63. '("call" "cmd" "defined" "do" "else" "equ" "exist" "exit" "for" "geq"
  64. "goto" "gtr" "if" "in" "leq" "lss" "neq" "not" "start"))
  65. (UNIX
  66. '("bash" "cat" "cp" "fgrep" "grep" "ls" "sed" "sh" "mv" "rm")))
  67. `(("\\_<\\(call\\|goto\\)\\_>[ \t]+%?\\([A-Za-z0-9-_\\:.]+\\)%?"
  68. (2 font-lock-constant-face t))
  69. ("^:[^:].*"
  70. . 'bat-label-face)
  71. ("\\_<\\(defined\\|set\\)\\_>[ \t]*\\(\\w+\\)"
  72. (2 font-lock-variable-name-face))
  73. ("%\\(\\w+\\)%?"
  74. (1 font-lock-variable-name-face))
  75. ("!\\(\\w+\\)!?" ; delayed-expansion !variable!
  76. (1 font-lock-variable-name-face))
  77. ("[ =][-/]+\\(\\w+\\)"
  78. (1 font-lock-type-face append))
  79. (,(concat "\\_<" (regexp-opt COMMANDS) "\\_>") . font-lock-builtin-face)
  80. (,(concat "\\_<" (regexp-opt CONTROLFLOW) "\\_>")
  81. . font-lock-keyword-face)
  82. (,(concat "\\_<" (regexp-opt UNIX) "\\_>")
  83. . font-lock-warning-face)))))
  84. (defvar bat-menu
  85. '("Bat"
  86. ["Run" bat-run :help "Run script"]
  87. ["Run with Args" bat-run-args :help "Run script with args"]
  88. "--"
  89. ["Imenu" imenu :help "Navigate with imenu"]
  90. "--"
  91. ["Template" bat-template :help "Insert template"]
  92. "--"
  93. ["Help (Command)" bat-cmd-help :help "Show help page for DOS command"]))
  94. (defvar bat-mode-map
  95. (let ((map (make-sparse-keymap)))
  96. (easy-menu-define nil map nil bat-menu)
  97. (define-key map [?\C-c ?\C-/] 'bat-cmd-help) ;FIXME: Why not C-c C-? ?
  98. (define-key map [?\C-c ?\C-a] 'bat-run-args)
  99. (define-key map [?\C-c ?\C-c] 'bat-run)
  100. (define-key map [?\C-c ?\C-t] 'bat-template)
  101. (define-key map [?\C-c ?\C-v] 'bat-run)
  102. map))
  103. (defvar bat-mode-syntax-table
  104. (let ((table (make-syntax-table)))
  105. (modify-syntax-entry ?\n ">" table)
  106. (modify-syntax-entry ?\" "\"" table)
  107. ;; Beware: `w' should not be used for non-alphabetic chars.
  108. (modify-syntax-entry ?~ "_" table)
  109. (modify-syntax-entry ?% "." table)
  110. (modify-syntax-entry ?- "_" table)
  111. (modify-syntax-entry ?_ "_" table)
  112. ;; FIXME: { and } can appear in identifiers? Really?
  113. (modify-syntax-entry ?{ "_" table)
  114. (modify-syntax-entry ?} "_" table)
  115. (modify-syntax-entry ?\\ "." table)
  116. table))
  117. (defconst bat--syntax-propertize
  118. (syntax-propertize-rules
  119. ("^[ \t]*\\(?:\\(@?r\\)em\\_>\\|\\(?1::\\):\\).*" (1 "<"))))
  120. ;; 4 User functions
  121. (defun bat-cmd-help (cmd)
  122. "Show help for batch file command CMD."
  123. (interactive "sHelp: ")
  124. (if (string-equal cmd "net")
  125. ;; FIXME: liable to quoting nightmare. Use call-process?
  126. (shell-command "net /?") (shell-command (concat "help " cmd))))
  127. (defun bat-run ()
  128. "Run a batch file."
  129. (interactive)
  130. ;; FIXME: liable to quoting nightmare. Use call/start-process?
  131. (save-buffer) (shell-command buffer-file-name))
  132. (defun bat-run-args (args)
  133. "Run a batch file with ARGS."
  134. (interactive "sArgs: ")
  135. ;; FIXME: Use `compile'?
  136. (shell-command (concat buffer-file-name " " args)))
  137. (defun bat-template ()
  138. "Insert minimal batch file template."
  139. (interactive)
  140. (goto-char (point-min)) (insert "@echo off\nsetlocal\n\n"))
  141. ;;;###autoload
  142. (add-to-list 'auto-mode-alist '("\\.\\(bat\\|cmd\\)\\'" . bat-mode))
  143. ;; 5 Main function
  144. ;;;###autoload
  145. (define-derived-mode bat-mode prog-mode "Bat"
  146. "Major mode for editing DOS/Windows batch files.\n
  147. Start a new script from `bat-template'. Read help pages for DOS commands
  148. with `bat-cmd-help'. Navigate between sections using `imenu'.
  149. Run script using `bat-run' and `bat-run-args'.\n
  150. \\{bat-mode-map}"
  151. (setq-local comment-start "rem ")
  152. (setq-local syntax-propertize-function bat--syntax-propertize)
  153. (setq-local font-lock-defaults
  154. '(bat-font-lock-keywords nil t)) ; case-insensitive keywords
  155. (setq-local imenu-generic-expression '((nil "^:[^:].*" 0)))
  156. (setq-local outline-regexp ":[^:]"))
  157. (provide 'bat-mode)
  158. ;;; bat-mode.el ends here