m4-mode.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; m4-mode.el --- m4 code editing commands for Emacs
  2. ;; Copyright (C) 1996-1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Andrew Csillag <drew_csillag@geocities.com>
  4. ;; Maintainer: Andrew Csillag <drew_csillag@geocities.com>
  5. ;; Keywords: languages, faces
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; A smart editing mode for m4 macro definitions. It seems to have most of the
  19. ;; syntax right (sexp motion commands work, but function motion commands don't).
  20. ;; It also sets the font-lock syntax stuff for colorization
  21. ;; To Do's:
  22. ;; * want to make m4-m4-(buffer|region) look sorta like M-x compile look&feel ?
  23. ;; * sexp motion commands don't seem to work right
  24. ;;; Thanks:
  25. ;;; to Akim Demaille and Terry Jones for the bug reports
  26. ;;; to Simon Marshall for the regexp tip
  27. ;;; to Martin Buchholz for some general fixes
  28. ;;; Code:
  29. (defgroup m4 nil
  30. "m4 code editing commands for Emacs."
  31. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  32. :prefix "m4-"
  33. :group 'languages)
  34. (defcustom m4-program
  35. (cond
  36. ((file-exists-p "/usr/local/bin/m4") "/usr/local/bin/m4")
  37. ((file-exists-p "/usr/bin/m4") "/usr/bin/m4")
  38. ((file-exists-p "/bin/m4") "/bin/m4")
  39. ((file-exists-p "/usr/ccs/bin/m4") "/usr/ccs/bin/m4")
  40. ( t "m4")
  41. )
  42. "File name of the m4 executable."
  43. :type 'file
  44. :group 'm4)
  45. ;;options to m4
  46. (defcustom m4-program-options nil
  47. "Options to pass to `m4-program'."
  48. :type '(repeat string)
  49. :group 'm4)
  50. ;;to use --prefix-builtins, you can use
  51. ;;(defconst m4-program-options '("-P"))
  52. ;;or
  53. ;;(defconst m4-program-options '("--prefix-builtins"))
  54. (defvar m4-font-lock-keywords
  55. `(
  56. ("\\(\\b\\(m4_\\)?dnl\\b\\|^\\#\\).*$" . font-lock-comment-face)
  57. ; ("\\(\\bdnl\\b\\|\\bm4_dnl\\b\\|^\\#\\).*$" . font-lock-comment-face)
  58. ("\\$[*#@0-9]" . font-lock-variable-name-face)
  59. ("\\\$\\\@" . font-lock-variable-name-face)
  60. ("\\\$\\\*" . font-lock-variable-name-face)
  61. ("\\b\\(builtin\\|change\\(com\\|quote\\|word\\)\\|d\\(e\\(bug\\(file\\|mode\\)\\|cr\\|f\\(ine\\|n\\)\\)\\|iv\\(ert\\|num\\)\\|nl\\|umpdef\\)\\|e\\(rrprint\\|syscmd\\|val\\)\\|f\\(ile\\|ormat\\)\\|gnu\\|i\\(f\\(def\\|else\\)\\|n\\(c\\(lude\\|r\\)\\|d\\(ex\\|ir\\)\\)\\)\\|l\\(en\\|ine\\)\\|m\\(4\\(exit\\|wrap\\)\\|aketemp\\)\\|p\\(atsubst\\|opdef\\|ushdef\\)\\|regexp\\|s\\(hift\\|include\\|ubstr\\|ys\\(cmd\\|val\\)\\)\\|tra\\(ceo\\(ff\\|n\\)\\|nslit\\)\\|un\\(d\\(efine\\|ivert\\)\\|ix\\)\\)\\b" . font-lock-keyword-face)
  62. ("\\b\\(m4_\\(builtin\\|change\\(com\\|quote\\|word\\)\\|d\\(e\\(bug\\(file\\|mode\\)\\|cr\\|f\\(ine\\|n\\)\\)\\|iv\\(ert\\|num\\)\\|nl\\|umpdef\\)\\|e\\(rrprint\\|syscmd\\|val\\)\\|f\\(ile\\|ormat\\)\\|i\\(f\\(def\\|else\\)\\|n\\(c\\(lude\\|r\\)\\|d\\(ex\\|ir\\)\\)\\)\\|l\\(en\\|ine\\)\\|m\\(4\\(_undefine\\|exit\\|wrap\\)\\|aketemp\\)\\|p\\(atsubst\\|opdef\\|ushdef\\)\\|regexp\\|s\\(hift\\|include\\|ubstr\\|ys\\(cmd\\|val\\)\\)\\|tra\\(ceo\\(ff\\|n\\)\\|nslit\\)\\|undivert\\)\\)\\b" . font-lock-keyword-face))
  63. "Default font-lock-keywords for `m4 mode'.")
  64. (defcustom m4-mode-hook nil
  65. "*Hook called by `m4-mode'."
  66. :type 'hook
  67. :group 'm4)
  68. ;;this may still need some work
  69. (defvar m4-mode-syntax-table nil
  70. "Syntax table used while in `m4-mode'.")
  71. (setq m4-mode-syntax-table (make-syntax-table))
  72. (modify-syntax-entry ?` "('" m4-mode-syntax-table)
  73. (modify-syntax-entry ?' ")`" m4-mode-syntax-table)
  74. (modify-syntax-entry ?# "<\n" m4-mode-syntax-table)
  75. (modify-syntax-entry ?\n ">#" m4-mode-syntax-table)
  76. (modify-syntax-entry ?{ "_" m4-mode-syntax-table)
  77. (modify-syntax-entry ?} "_" m4-mode-syntax-table)
  78. (modify-syntax-entry ?* "w" m4-mode-syntax-table)
  79. (modify-syntax-entry ?_ "w" m4-mode-syntax-table)
  80. (modify-syntax-entry ?\" "w" m4-mode-syntax-table)
  81. (modify-syntax-entry ?\" "w" m4-mode-syntax-table)
  82. (defvar m4-mode-map
  83. (let ((map (make-sparse-keymap))
  84. (menu-map (make-sparse-keymap)))
  85. (define-key map "\C-c\C-b" 'm4-m4-buffer)
  86. (define-key map "\C-c\C-r" 'm4-m4-region)
  87. (define-key map "\C-c\C-c" 'comment-region)
  88. (define-key map [menu-bar m4-mode] (cons "M4" menu-map))
  89. (define-key menu-map [m4c]
  90. '(menu-item "Comment Region" comment-region
  91. :help "Comment Region"))
  92. (define-key menu-map [m4b]
  93. '(menu-item "M4 Buffer" m4-m4-buffer
  94. :help "Send contents of the current buffer to m4"))
  95. (define-key menu-map [m4r]
  96. '(menu-item "M4 Region" m4-m4-region
  97. :help "Send contents of the current region to m4"))
  98. map))
  99. (defvar m4-mode-abbrev-table nil
  100. "Abbrev table used while in `m4-mode'.")
  101. (unless m4-mode-abbrev-table
  102. (define-abbrev-table 'm4-mode-abbrev-table ()))
  103. (defun m4-m4-buffer ()
  104. "Send contents of the current buffer to m4."
  105. (interactive)
  106. (shell-command-on-region
  107. (point-min) (point-max)
  108. (mapconcat 'identity (cons m4-program m4-program-options) "\s")
  109. "*m4-output*" nil)
  110. (switch-to-buffer-other-window "*m4-output*"))
  111. (defun m4-m4-region ()
  112. "Send contents of the current region to m4."
  113. (interactive)
  114. (shell-command-on-region
  115. (point) (mark)
  116. (mapconcat 'identity (cons m4-program m4-program-options) "\s")
  117. "*m4-output*" nil)
  118. (switch-to-buffer-other-window "*m4-output*"))
  119. ;;;###autoload
  120. (define-derived-mode m4-mode prog-mode "m4"
  121. "A major mode to edit m4 macro files."
  122. :abbrev-table m4-mode-abbrev-table
  123. (set (make-local-variable 'comment-start) "#")
  124. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  125. (set (make-local-variable 'font-lock-defaults) '(m4-font-lock-keywords nil)))
  126. (provide 'm4-mode)
  127. ;;stuff to play with for debugging
  128. ;(char-to-string (char-syntax ?`))
  129. ;;;how I generate the nasty looking regexps at the top
  130. ;;;(make-regexp '("builtin" "changecom" "changequote" "changeword" "debugfile"
  131. ;;; "debugmode" "decr" "define" "defn" "divert" "divnum" "dnl"
  132. ;;; "dumpdef" "errprint" "esyscmd" "eval" "file" "format" "gnu"
  133. ;;; "ifdef" "ifelse" "include" "incr" "index" "indir" "len" "line"
  134. ;;; "m4exit" "m4wrap" "maketemp" "patsubst" "popdef" "pushdef" "regexp"
  135. ;;; "shift" "sinclude" "substr" "syscmd" "sysval" "traceoff" "traceon"
  136. ;;; "translit" "undefine" "undivert" "unix"))
  137. ;;;(make-regexp '("m4_builtin" "m4_changecom" "m4_changequote" "m4_changeword"
  138. ;;; "m4_debugfile" "m4_debugmode" "m4_decr" "m4_define" "m4_defn"
  139. ;;; "m4_divert" "m4_divnum" "m4_dnl" "m4_dumpdef" "m4_errprint"
  140. ;;; "m4_esyscmd" "m4_eval" "m4_file" "m4_format" "m4_ifdef" "m4_ifelse"
  141. ;;; "m4_include" "m4_incr" "m4_index" "m4_indir" "m4_len" "m4_line"
  142. ;;; "m4_m4exit" "m4_m4wrap" "m4_maketemp" "m4_patsubst" "m4_popdef"
  143. ;;; "m4_pushdef" "m4_regexp" "m4_shift" "m4_sinclude" "m4_substr"
  144. ;;; "m4_syscmd" "m4_sysval" "m4_traceoff" "m4_traceon" "m4_translit"
  145. ;;; "m4_m4_undefine" "m4_undivert"))
  146. ;;; m4-mode.el ends here