Initial Commit
[packages] / xemacs-packages / prog-modes / m4-mode.el
1 ;;; m4-mode.el --- m4 code editing commands for Emacs
2
3 ;; Author: Andrew Csillag (drew@staff.prodigy.com)
4 ;; Maintainer: Andrew Csillag (drew@staff.prodigy.com)
5 ;; Keywords: languages, faces
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; A smart editing mode for m4 macro definitions.  It seems to have most of the 
27 ;; syntax right (sexp motion commands work, but function motion commands don't).
28 ;; It also sets the font-lock syntax stuff for colorization
29
30 ;; By Drew Csillag (drew@staff.prodigy.com)
31 ;; $Id: m4-mode.el,v 1.3 2003-06-07 12:14:44 adrian Exp $
32
33 ;; History:
34
35 ;; Date     Version Who Action
36 ;; -------- ------- --- ----
37 ;; 09/13/96 1.0     DC  Created
38 ;; 09/26/96 1.1     DC  added syntax table stuff so now matches `'s
39 ;;                      now can use C-c C-b to m4 on buffer,C-c C-r for region
40 ;;                      added m4-comment-region (C-c C-c)
41 ;; 09/27/96 1.2     DC  Redid header comment
42 ;;          1.3     DC  more cosmetic fixes 
43 ;;          1.4     DC  removed m4-comment-region, using comment-region instead
44 ;; 10/03/96         DC  added provide line
45 ;; 10/24/96 1.5     DC  fixed keyword regexp
46 ;; 10/29/96 1.6     DC  added m4_keywords for --prefix-builtins
47 ;;                      fixed syntax table so _define_ isn't keywordified
48 ;; 10/29/96 1.8     DC  fixed a problem where comments go fontified incorrectly
49 ;;                      if they had quotes in them
50 ;; 11/21/96 1.9     DC  collapsed 4 regexps into 1 and removed a duplicate,
51 ;;                      removed a duplicate backslash
52 ;; 05/16/97 1.10    DC  added contributed fixes and enhancements from 
53 ;;                      Martin Buchholz
54 ;;                      Can now specify options to m4 via m4-program-options
55 ;;                      Now terminates m4 processes when the buffer send
56 is 
57 ;;                      done
58 ;;                      dnl and m4_dnl recognized as comment starters
59  
60 ;; To Do's:
61
62 ;; * want to make m4-m4-(buffer|region) look sorta like M-x compile look&feel ?
63 ;; * sexp motion commands don't seem to work right
64
65 ;; to autoload m4 lisp code:  
66 ;; (autoload 'm4-mode "m4-mode" nil t)
67 ;;
68 ;; or can use (load "m4-mode") or (require 'm4-mode) to just load it 
69 ;;
70 ;; to try to "auto-detect" m4 files:
71 ;; (setq auto-mode-alist 
72 ;;       (cons '(".*\\.m4$" . m4-mode)
73 ;;             auto-mode-alist))
74
75 ;;; Thanks: 
76 ;;;         to Akim Demaille and Terry Jones for the bug reports
77 ;;;         to Simon Marshall for the regexp tip
78 ;;;         to Martin Buchholz for some general fixes
79
80 ;;; Code:
81
82 ;;path to the m4 program
83 (defvar m4-program 
84   (cond 
85    ((file-exists-p "/usr/local/bin/m4") "/usr/local/bin/m4")
86    ((file-exists-p "/usr/bin/m4") "/usr/bin/m4")
87    ((file-exists-p "/bin/m4") "/bin/m4")
88    ((file-exists-p "/usr/ccs/bin/m4") "/usr/ccs/bin/m4")
89    )
90 )
91
92 ;;options to m4
93 (defconst m4-program-options nil)
94 ;;to use --prefix-builtins, you can use
95 ;;(defconst m4-program-options '("-P"))
96 ;;or
97 ;;(defconst m4-program-options '("--prefix-builtins"))
98
99 ;;thank god for make-regexp.el!
100 (defvar m4-font-lock-keywords
101   `(
102     ("\\(\\bdnl\\b\\|\\bm4_dnl\\b\\|^\\#\\).*$" .
103 font-lock-comment-face)
104     ("\\$[*#@0-9]" . font-lock-variable-name-face)
105    
106 ("\\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"
107 . font-lock-keyword-face)
108    
109 ("\\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"
110 . font-lock-keyword-face)
111     "default font-lock-keywords")
112 )
113
114 ;;this may still need some work
115 (defvar m4-mode-syntax-table nil
116   "syntax table used in m4 mode")
117 (setq m4-mode-syntax-table (make-syntax-table))
118 (modify-syntax-entry ?` "('" m4-mode-syntax-table)
119 (modify-syntax-entry ?' ")`" m4-mode-syntax-table)
120 (modify-syntax-entry ?# "<\n" m4-mode-syntax-table)
121 (modify-syntax-entry ?\n ">#" m4-mode-syntax-table)
122 (modify-syntax-entry ?{  "_" m4-mode-syntax-table)
123 (modify-syntax-entry ?}  "_" m4-mode-syntax-table)
124 (modify-syntax-entry ?*  "." m4-mode-syntax-table)
125 (modify-syntax-entry ?_  "_" m4-mode-syntax-table)
126 (modify-syntax-entry ?\" "w" m4-mode-syntax-table)
127
128 (defvar m4-mode-map
129   (let ((map (make-sparse-keymap)))
130     (define-key map "\C-c\C-b" 'm4-m4-buffer)
131     (define-key map "\C-c\C-r" 'm4-m4-region)
132     (define-key map "\C-c\C-c" 'comment-region)
133     map))
134
135 (defun m4-end-m4 (process event)
136   (cond ((equal event "kill\n") (princ "m4 process done"))
137         (t (princ
138             (format "Process: %s had the event `%s'" process event)))))
139
140 (defun m4-start-m4 ()
141   (eval (append (append '(start-process "m4process" "*m4 output*"
142 m4-program)
143                 m4-program-options) '("-e")))
144   (set-process-sentinel (get-process "m4process") 'm4-end-m4)
145 )
146
147 (defun m4-m4-buffer ()
148   "send contents of the current buffer to m4"
149   (interactive)
150   (m4-start-m4)
151   (process-send-region "m4process" (point-min) (point-max))
152   (process-send-eof "m4process")
153   (switch-to-buffer-other-window "*m4 output*")
154   (delete-process "m4process")
155 )
156
157
158 (defun m4-m4-region ()
159   "send contents of the current region to m4"
160   (interactive)
161   (m4-start-m4)
162   (process-send-region "m4process" (point) (mark))
163   (process-send-eof "m4process")
164   (switch-to-buffer-other-window "*m4 output*")
165 )
166
167 ;;;###autoload
168 (defun m4-mode ()
169   "A major-mode to edit m4 macro files
170 \\{m4-mode-map}
171 "
172   (interactive)
173   (kill-all-local-variables)
174   (use-local-map m4-mode-map)
175
176   (make-local-variable 'comment-start)
177   (setq comment-start "#")
178   (make-local-variable 'parse-sexp-ignore-comments)
179   (setq parse-sexp-ignore-comments t)
180
181
182   (make-local-variable  'font-lock-defaults)  
183   (setq major-mode 'm4-mode
184         mode-name "m4"
185         font-lock-defaults '(m4-font-lock-keywords nil)
186         )
187   (set-syntax-table m4-mode-syntax-table)
188   (run-hooks 'm4-mode-hook))
189
190 ;; XEmacs additions
191 ;; APA: autoconf-mode is XEmacs' preferred m4 mode, see its autoload
192 ;; cookie.
193 ;; ;;;###autoload(add-to-list 'auto-mode-alist '(".*\\.m4$" . m4-mode))
194
195 (provide 'm4-mode)
196 ;;stuff to play with for debugging
197 ;(char-to-string (char-syntax ?`))
198
199 ;;;how I generate the nasty looking regexps at the top
200 ;;;(make-regexp '("builtin" "changecom" "changequote" "changeword" "debugfile" 
201 ;;;               "debugmode" "decr" "define" "defn" "divert" "divnum" "dnl" 
202 ;;;               "dumpdef" "errprint" "esyscmd" "eval" "file" "format" "gnu"
203 ;;;               "ifdef" "ifelse" "include" "incr" "index" "indir" "len" "line" 
204 ;;;               "m4exit" "m4wrap" "maketemp" "patsubst" "popdef" "pushdef" "regexp" 
205 ;;;               "shift" "sinclude" "substr" "syscmd" "sysval" "traceoff" "traceon" 
206 ;;;               "translit" "undefine" "undivert" "unix"))
207 ;;;(make-regexp '("m4_builtin" "m4_changecom" "m4_changequote" "m4_changeword" 
208 ;;;               "m4_debugfile" "m4_debugmode" "m4_decr" "m4_define" "m4_defn" 
209 ;;;               "m4_divert" "m4_divnum" "m4_dnl" "m4_dumpdef" "m4_errprint" 
210 ;;;               "m4_esyscmd" "m4_eval" "m4_file" "m4_format" "m4_ifdef" "m4_ifelse" 
211 ;;;               "m4_include" "m4_incr" "m4_index" "m4_indir" "m4_len" "m4_line" 
212 ;;;               "m4_m4exit" "m4_m4wrap" "m4_maketemp" "m4_patsubst" "m4_popdef" 
213 ;;;               "m4_pushdef" "m4_regexp" "m4_shift" "m4_sinclude" "m4_substr" 
214 ;;;               "m4_syscmd" "m4_sysval" "m4_traceoff" "m4_traceon" "m4_translit" 
215 ;;;               "m4_m4_undefine" "m4_undivert"))