m4-mode.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;;; m4-mode.el --- m4 code editing commands for Emacs
  2. ;; Copyright (C) 1996-1997, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Andrew Csillag <drew@thecsillags.com>
  4. ;; Keywords: languages, faces
  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. ;; A smart editing mode for m4 macro definitions. It seems to have most of the
  18. ;; syntax right (sexp motion commands work, but function motion commands don't).
  19. ;; It also sets the font-lock syntax stuff for colorization
  20. ;; To Do's:
  21. ;; * want to make m4-m4-(buffer|region) look sorta like M-x compile look&feel ?
  22. ;; * sexp motion commands don't seem to work right
  23. ;;; Thanks:
  24. ;;; to Akim Demaille and Terry Jones for the bug reports
  25. ;;; to Simon Marshall for the regexp tip
  26. ;;; to Martin Buchholz for some general fixes
  27. ;;; Code:
  28. (defgroup m4 nil
  29. "m4 code editing commands for Emacs."
  30. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  31. :prefix "m4-"
  32. :group 'languages)
  33. (defcustom m4-program "m4"
  34. "File name of the m4 executable.
  35. If m4 is not in your PATH, set this to an absolute file name."
  36. :version "24.4"
  37. :type 'file
  38. :group 'm4)
  39. ;;options to m4
  40. (defcustom m4-program-options nil
  41. "Options to pass to `m4-program'."
  42. :type '(repeat string)
  43. :group 'm4)
  44. ;;to use --prefix-builtins, you can use
  45. ;;(defconst m4-program-options '("-P"))
  46. ;;or
  47. ;;(defconst m4-program-options '("--prefix-builtins"))
  48. (defvar m4-font-lock-keywords
  49. `(
  50. ("\\(\\_<\\(m4_\\)?dnl\\_>\\).*$" . font-lock-comment-face)
  51. ("\\$[*#@0-9]" . font-lock-variable-name-face)
  52. ("\\$\\@" . font-lock-variable-name-face)
  53. ("\\$\\*" . font-lock-variable-name-face)
  54. ("\\_<\\(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\\)\\|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\\)\\)\\_>" . font-lock-keyword-face))
  55. "Default `font-lock-keywords' for M4 mode.")
  56. (defcustom m4-mode-hook nil
  57. "Hook called by `m4-mode'."
  58. :type 'hook
  59. :group 'm4)
  60. ;;this may still need some work
  61. (defvar m4-mode-syntax-table
  62. (let ((table (make-syntax-table)))
  63. (modify-syntax-entry ?` "('" table)
  64. (modify-syntax-entry ?' ")`" table)
  65. (modify-syntax-entry ?# "<\n" table)
  66. (modify-syntax-entry ?\n ">#" table)
  67. (modify-syntax-entry ?{ "." table)
  68. (modify-syntax-entry ?} "." table)
  69. (modify-syntax-entry ?_ "_" table)
  70. (modify-syntax-entry ?* "." table)
  71. (modify-syntax-entry ?\" "." table)
  72. table)
  73. "Syntax table used while in `m4-mode'.")
  74. (defun m4--quoted-p (pos)
  75. "Return non-nil if POS is inside a quoted string."
  76. (let ((quoted nil))
  77. (dolist (o (nth 9 (save-excursion (syntax-ppss pos))))
  78. (if (eq (char-after o) ?\`) (setq quoted t)))
  79. quoted))
  80. (defconst m4-syntax-propertize
  81. (syntax-propertize-rules
  82. ("#" (0 (when (m4--quoted-p (match-beginning 0))
  83. (string-to-syntax "."))))))
  84. (defvar m4-mode-map
  85. (let ((map (make-sparse-keymap))
  86. (menu-map (make-sparse-keymap)))
  87. (define-key map "\C-c\C-b" 'm4-m4-buffer)
  88. (define-key map "\C-c\C-r" 'm4-m4-region)
  89. (define-key map "\C-c\C-c" 'comment-region)
  90. (define-key map [menu-bar m4-mode] (cons "M4" menu-map))
  91. (define-key menu-map [m4c]
  92. '(menu-item "Comment Region" comment-region
  93. :help "Comment Region"))
  94. (define-key menu-map [m4b]
  95. '(menu-item "M4 Buffer" m4-m4-buffer
  96. :help "Send contents of the current buffer to m4"))
  97. (define-key menu-map [m4r]
  98. '(menu-item "M4 Region" m4-m4-region
  99. :help "Send contents of the current region to m4"))
  100. map))
  101. (defun m4-m4-buffer ()
  102. "Send contents of the current buffer to m4."
  103. (interactive)
  104. (shell-command-on-region
  105. (point-min) (point-max)
  106. (mapconcat 'identity (cons m4-program m4-program-options) "\s")
  107. "*m4-output*" nil)
  108. (switch-to-buffer-other-window "*m4-output*"))
  109. (defun m4-m4-region ()
  110. "Send contents of the current region to m4."
  111. (interactive)
  112. (shell-command-on-region
  113. (point) (mark)
  114. (mapconcat 'identity (cons m4-program m4-program-options) "\s")
  115. "*m4-output*" nil)
  116. (switch-to-buffer-other-window "*m4-output*"))
  117. (defun m4-current-defun-name ()
  118. "Return the name of the M4 function at point, or nil."
  119. (save-excursion
  120. (if (re-search-backward
  121. "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
  122. (match-string-no-properties 3))))
  123. ;;;###autoload
  124. (define-derived-mode m4-mode prog-mode "m4"
  125. "A major mode to edit m4 macro files."
  126. (setq-local comment-start "#")
  127. (setq-local parse-sexp-ignore-comments t)
  128. (setq-local add-log-current-defun-function #'m4-current-defun-name)
  129. (setq-local syntax-propertize-function m4-syntax-propertize)
  130. (setq-local font-lock-defaults '(m4-font-lock-keywords nil)))
  131. (provide 'm4-mode)
  132. ;;stuff to play with for debugging
  133. ;(char-to-string (char-syntax ?`))
  134. ;;;how I generate the nasty looking regexps at the top
  135. ;;;(make-regexp '("builtin" "changecom" "changequote" "changeword" "debugfile"
  136. ;;; "debugmode" "decr" "define" "defn" "divert" "divnum" "dnl"
  137. ;;; "dumpdef" "errprint" "esyscmd" "eval" "file" "format" "gnu"
  138. ;;; "ifdef" "ifelse" "include" "incr" "index" "indir" "len" "line"
  139. ;;; "m4exit" "m4wrap" "maketemp" "patsubst" "popdef" "pushdef" "regexp"
  140. ;;; "shift" "sinclude" "substr" "syscmd" "sysval" "traceoff" "traceon"
  141. ;;; "translit" "undefine" "undivert" "unix"))
  142. ;;;(make-regexp '("m4_builtin" "m4_changecom" "m4_changequote" "m4_changeword"
  143. ;;; "m4_debugfile" "m4_debugmode" "m4_decr" "m4_define" "m4_defn"
  144. ;;; "m4_divert" "m4_divnum" "m4_dnl" "m4_dumpdef" "m4_errprint"
  145. ;;; "m4_esyscmd" "m4_eval" "m4_file" "m4_format" "m4_ifdef" "m4_ifelse"
  146. ;;; "m4_include" "m4_incr" "m4_index" "m4_indir" "m4_len" "m4_line"
  147. ;;; "m4_m4exit" "m4_m4wrap" "m4_maketemp" "m4_patsubst" "m4_popdef"
  148. ;;; "m4_pushdef" "m4_regexp" "m4_shift" "m4_sinclude" "m4_substr"
  149. ;;; "m4_syscmd" "m4_sysval" "m4_traceoff" "m4_traceon" "m4_translit"
  150. ;;; "m4_m4_undefine" "m4_undivert"))
  151. ;;; m4-mode.el ends here