* lisp/progmodes/meta-mode.el: (meta-indent-line): Simplify.
[bpt/emacs.git] / lisp / progmodes / meta-mode.el
CommitLineData
e8af40ee 1;;; meta-mode.el --- major mode for editing Metafont or MetaPost sources
6b279740 2
9b026d9f
GM
3;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4;; 2009, 2010 Free Software Foundation, Inc.
6b279740
RS
5
6;; Author: Ulrik Vieth <vieth@thphy.uni-duesseldorf.de>
7;; Version: 1.0
8;; Keywords: Metafont, MetaPost, tex, languages
9
1eadb66c 10;; This file is part of GNU Emacs.
6b279740 11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
6b279740 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
6b279740 16
1eadb66c 17;; GNU Emacs is distributed in the hope that it will be useful,
6b279740
RS
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6b279740
RS
24
25;;; Commentary:
26
27;; Description:
28;;
29;; This Emacs Lisp package provides a major mode for editing Metafont
30;; or MetaPost sources. It includes all the necessary code to set up
31;; a major mode including an approriate syntax table, keymap, and a
32;; mode-specific pull-down menu. It also provides a sophisticated set
33;; of font-lock patterns, a fancy indentation function adapted from
818226ac 34;; AUCTeX's latex.el, and some basic mode-specific editing functions
6b279740
RS
35;; such as functions to move to the beginning or end of the enclosing
36;; environment, or to mark, re-indent, or comment-out environments.
37;; On the other hand, it doesn't yet provide any functionality for
38;; running Metafont or MetaPost in a shell buffer form within Emacs,
39;; but such functionality might be added later, either as part of this
40;; package or as a separate Emacs Lisp package.
41
42;; Installation:
a1506d29 43;;
6b279740
RS
44;; An interface to running Metafont or MetaPost as a shell process
45;; from within Emacs is currently under development as a separate
46;; Emacs Lisp package (meta-buf.el). In order to have that package
47;; loaded automatically when first entering Metafont or MetaPost mode,
48;; you might use the load-hook provided in this package by adding
49;; these lines to your startup file:
a1506d29
JB
50;;
51;; (add-hook 'meta-mode-load-hook
8a4c5051 52;; (lambda () (require 'meta-buf)))
6b279740
RS
53;;
54;; The add-on package loaded this way may in turn make use of the
55;; mode-hooks provided in this package to activate additional features
56;; when entering Metafont or MetaPost mode.
57
58;; Font Lock Support:
59;;
60;; If you are using global-font-lock-mode (introduced in Emacs 19.31),
61;; fontification in Metafont and/or MetaPost mode will be activated
62;; automatically. To speed up fontification for the rather complex
63;; patterns used in these modes, it may be a good idea to activate
64;; lazy-lock as a font-lock-support-mode (introduced in Emacs 19.32)
65;; by adding these lines to your startup file:
66;;
67;; (global-font-lock-mode t)
68;; (setq font-lock-support-mode 'lazy-lock-mode)
69;;
70;; If you are using an older version of Emacs, which doesn't provide
71;; global-font-lock-mode or font-lock-support-mode, you can also
72;; activate fontification in Metafont and/or MetaPost mode by adding
73;; the following lines to your startup file:
a1506d29 74;;
6b279740
RS
75;; (add-hook 'meta-common-mode-hook 'turn-on-font-lock)
76;; (add-hook 'meta-common-mode-hook 'turn-on-lazy-lock)
77
78;; Customization:
79;;
80;; Following the usual Emacs Lisp coding conventions, the major modes
81;; defined in this package provide several hook variables to allow for
82;; local customization when entering the modes. In particular, there
83;; is a `meta-common-mode-hook' which applies to both modes as well as
84;; `metafont-mode-hook' and `metapost-mode-hook' which apply to the
85;; individual modes. In addition, there are several variables and
86;; regexps controlling e.g. the behavior of the indentation function,
87;; which may be customized via `edit-options'. Please refer to the
88;; docstrings in the code below for details.
89
90;; Availability:
91;;
92;; This package is currently available via my "TeX Software" WWW page:
93;;
94;; http://www.thphy.uni-duesseldorf.de/~vieth/subjects/tex/software.html
95;;
96;; As of this version 1.0, this package will be uploaded to CTAN
97;; archives, where it shall find a permanent home, presumably in
98;; tex-archive/support/emacs-modes. It will also be submitted for
99;; integration into the GNU Emacs distribution at that time.
100;;
101;; History:
102;;
103;; v 0.0 -- 1997/02/01 UV Started writing meta-mode.el.
104;; v 0.1 -- 1997/02/02 UV Added preliminary set of font-lock patterns.
105;; v 0.2 -- 1997/02/03 UV Improved and debugged font-lock patterns.
106;; Added indent-line-function for TAB.
107;; v 0.3 -- 1997/02/17 UV Improved font-lock patterns and syntax table.
108;; Improved and debbuged indentation function.
109;; v 0.4 -- 1997/02/18 UV Added functions to indent regions for M-C-q,
110;; also added a preliminary mode-specific menu.
a1506d29 111;; v 0.5 -- 1997/02/19 UV Added functions to skip to next or previous
6b279740
RS
112;; defun and to re-indent or comment-out defuns.
113;; v 0.6 -- 1997/02/20 UV More debugging, testing and clean-up.
114;; v 0.7 -- 1997/02/22 UV Use easymenu to define mode-specific menu.
115;; v 0.8 -- 1997/02/24 UV Added completion function for M-TAB.
116;; v 0.9 -- 1997/03/08 UV Added fill-paragraph function for comments.
117;; Also fixed a few remaining font-lock problems.
118;; Added meta-mode-load-hook to load meta-buf.el.
119;; v 1.0 -- 1997/04/07 UV Cleanup for official public release.
120;;
121;; Historical Footnote:
122;;
123;; This package was begun on February 1, 1997, exactly 20 years after
124;; the genesis of TeX took place according to Don Knuth's own account
ff319790 125;; (cf. ``The Errors of TeX'', reprinted in ``Literate Programming'',
6b279740
RS
126;; Chapter 10, p. 249). What better date could there be to choose?
127;;
128
129\f
130;;; Code:
131
132(require 'easymenu)
133
28d16ed3
AS
134(defgroup meta-font nil
135 "Major mode for editing Metafont or MetaPost sources."
8ec3bce0 136 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
28d16ed3
AS
137 :group 'languages)
138
6b279740
RS
139;;; Fontification.
140
141(defvar meta-font-lock-keywords
142 (let ((input-keywords
143 "\\(input\\|generate\\)")
144 (begin-keywords
145 (concat "\\(begin\\(char\\|fig\\|graph\\|logochar\\)\\|"
146 "\\cmchar\\|dcchar\\|ecchar\\)"))
147 (end-keywords
148 "\\(end\\(char\\|fig\\|graph\\)\\)")
149 (macro-keywords-1
150 "\\(def\\|let\\|mode_def\\|vardef\\)")
151 (macro-keywords-2
152 "\\(primarydef\\|secondarydef\\|tertiarydef\\)")
153;(make-regexp
154; '("expr" "suffix" "text" "primary" "secondary" "tertiary") t)
155 (args-keywords
156 (concat "\\(expr\\|primary\\|s\\(econdary\\|uffix\\)\\|"
157 "te\\(rtiary\\|xt\\)\\)"))
158;(make-regexp
159; '("boolean" "color" "numeric" "pair" "path" "pen" "picture"
160; "string" "transform" "newinternal") t)
161 (type-keywords
162 (concat "\\(boolean\\|color\\|n\\(ewinternal\\|umeric\\)\\|"
163 "p\\(a\\(ir\\|th\\)\\|en\\|icture\\)\\|string\\|"
164 "transform\\)"))
165;(make-regexp
166; '("for" "forever" "forsuffixes" "endfor"
167; "step" "until" "upto" "downto" "thru" "within"
168; "iff" "if" "elseif" "else" "fi" "exitif" "exitunless"
169; "let" "def" "vardef" "enddef" "mode_def"
170; "true" "false" "known" "unknown" "and" "or" "not"
171; "save" "interim" "inner" "outer" "relax"
172; "begingroup" "endgroup" "expandafter" "scantokens"
173; "generate" "input" "endinput" "end" "bye"
174; "message" "errmessage" "errhelp" "special" "numspecial"
175; "readstring" "readfrom" "write") t)
176 (syntactic-keywords
177 (concat "\\(and\\|b\\(egingroup\\|ye\\)\\|"
178 "d\\(ef\\|ownto\\)\\|e\\(lse\\(\\|if\\)"
179 "\\|nd\\(\\|def\\|for\\|group\\|input\\)"
180 "\\|rr\\(help\\|message\\)"
181 "\\|x\\(it\\(if\\|unless\\)\\|pandafter\\)\\)\\|"
182 "f\\(alse\\|i\\|or\\(\\|ever\\|suffixes\\)\\)\\|"
183 "generate\\|i\\(ff?\\|n\\(ner\\|put\\|terim\\)\\)\\|"
184 "known\\|let\\|m\\(essage\\|ode_def\\)\\|"
185 "n\\(ot\\|umspecial\\)\\|o\\(r\\|uter\\)\\|"
186 "re\\(ad\\(from\\|string\\)\\|lax\\)\\|"
187 "s\\(ave\\|cantokens\\|pecial\\|tep\\)\\|"
188 "t\\(hru\\|rue\\)\\|"
189 "u\\(n\\(known\\|til\\)\\|pto\\)\\|"
190 "vardef\\|w\\(ithin\\|rite\\)\\)"))
191 )
192 (list
193 ;; embedded TeX code in btex ... etex
194 (cons (concat "\\(btex\\|verbatimtex\\)"
89c19c1e 195 "[ \t\f]+\\(.*\\)[ \t\f]+"
6b279740
RS
196 "\\(etex\\)")
197 '((1 font-lock-keyword-face)
198 (2 font-lock-string-face)
199 (3 font-lock-keyword-face)))
200 ;; unary macro definitions: def, vardef, let
201 (cons (concat "\\<" macro-keywords-1 "\\>"
89c19c1e 202 "[ \t\f]+\\(\\sw+\\|\\s_+\\|\\s.+\\)")
6b279740
RS
203 '((1 font-lock-keyword-face)
204 (2 font-lock-function-name-face)))
205 ;; binary macro defintions: <leveldef> x operator y
206 (cons (concat "\\<" macro-keywords-2 "\\>"
89c19c1e
MC
207 "[ \t\f]+\\(\\sw+\\)"
208 "[ \t\f]*\\(\\sw+\\|\\s.+\\)"
209 "[ \t\f]*\\(\\sw+\\)")
6b279740
RS
210 '((1 font-lock-keyword-face)
211 (2 font-lock-variable-name-face nil t)
212 (3 font-lock-function-name-face nil t)
213 (4 font-lock-variable-name-face nil t)))
214 ;; variable declarations: numeric, pair, color, ...
215 (cons (concat "\\<" type-keywords "\\>"
89c19c1e 216 "\\([ \t\f]+\\(\\sw+\\)\\)*")
6b279740
RS
217 '((1 font-lock-type-face)
218 (font-lock-match-meta-declaration-item-and-skip-to-next
219 (goto-char (match-end 1)) nil
220 (1 font-lock-variable-name-face nil t))))
221 ;; argument declarations: expr, suffix, text, ...
222 (cons (concat "\\<" args-keywords "\\>"
89c19c1e 223 "\\([ \t\f]+\\(\\sw+\\|\\s_+\\)\\)*")
6b279740
RS
224 '((1 font-lock-type-face)
225 (font-lock-match-meta-declaration-item-and-skip-to-next
226 (goto-char (match-end 1)) nil
227 (1 font-lock-variable-name-face nil t))))
228 ;; special case of arguments: expr x of y
89c19c1e
MC
229 (cons (concat "\\(expr\\)[ \t\f]+\\(\\sw+\\)"
230 "[ \t\f]+\\(of\\)[ \t\f]+\\(\\sw+\\)")
6b279740
RS
231 '((1 font-lock-type-face)
232 (2 font-lock-variable-name-face)
233 (3 font-lock-keyword-face nil t)
234 (4 font-lock-variable-name-face nil t)))
235 ;; syntactic keywords
236 (cons (concat "\\<" syntactic-keywords "\\>")
237 'font-lock-keyword-face)
238 ;; beginchar, beginfig
239 (cons (concat "\\<" begin-keywords "\\>")
240 'font-lock-keyword-face)
241 ;; endchar, endfig
242 (cons (concat "\\<" end-keywords "\\>")
243 'font-lock-keyword-face)
244 ;; input, generate
245 (cons (concat "\\<" input-keywords "\\>"
89c19c1e 246 "[ \t\f]+\\(\\sw+\\)")
6b279740 247 '((1 font-lock-keyword-face)
883212ce 248 (2 font-lock-constant-face)))
6b279740 249 ;; embedded Metafont/MetaPost code in comments
a1506d29 250 (cons "|\\([^|]+\\)|"
883212ce 251 '(1 font-lock-constant-face t))
6b279740
RS
252 ))
253 "Default expressions to highlight in Metafont or MetaPost mode.")
254
255
256(defun font-lock-match-meta-declaration-item-and-skip-to-next (limit)
257 ;; Match and move over Metafont/MetaPost declaration item after point.
258 ;;
259 ;; The expected syntax of an item is either "word" or "symbol",
260 ;; possibly ending with optional whitespace. Everything following
261 ;; the item (but belonging to it) is expected to by skipable by
262 ;; `forward-sexp'. The list of items is expected to be separated
263 ;; by commas and terminated by semicolons or equals signs.
264 ;;
89c19c1e 265 (if (looking-at "[ \t\f]*\\(\\sw+\\|\\s_+\\)")
6b279740
RS
266 (save-match-data
267 (condition-case nil
268 (save-restriction
269 ;; Restrict to end of line, currently guaranteed to be LIMIT.
270 (narrow-to-region (point-min) limit)
271 (goto-char (match-end 1))
272 ;; Move over any item value, etc., to the next item.
89c19c1e 273 (while (not (looking-at "[ \t\f]*\\(\\(,\\)\\|;\\|=\\|$\\)"))
6b279740
RS
274 (goto-char (or (scan-sexps (point) 1) (point-max))))
275 (goto-char (match-end 2)))
276 (error t)))))
277
278
279\f
280;;; Completion.
281
282;; The data used to prepare the following lists of primitives and
283;; standard macros available in Metafont or MetaPost was extracted
284;; from the original sources like this:
285;;
286;; grep '^primitive' texk-7.0/web2c/{mf,mp}.web |\
287;; sed 's/primitive(\("[a-zA-Z]*"\).*/\1/' > {mf,mp}_prim.list
288;;
a1506d29 289;; grep '\(let\|def\|vardef\|primarydef\|secondarydef\|tertiarydef\)'
6b279740
RS
290;; texmf/meta{font,post}/plain.{mf,mp} > {mf,mp}_plain.list
291
a1506d29 292(defconst meta-common-primitives-list
6b279740
RS
293 '("ASCII" "addto" "also" "and" "angle" "atleast" "batchmode"
294 "begingroup" "boolean" "boundarychar" "char" "charcode" "chardp"
295 "charexists" "charext" "charht" "charic" "charlist" "charwd"
296 "contour" "controls" "cosd" "curl" "cycle" "day" "decimal" "def"
297 "delimiters" "designsize" "directiontime" "doublepath" "dump" "else"
298 "elseif" "end" "enddef" "endfor" "endgroup" "endinput" "errhelp"
299 "errmessage" "errorstopmode" "everyjob" "exitif" "expandafter"
300 "expr" "extensible" "false" "fi" "floor" "fontdimen" "fontmaking"
301 "for" "forever" "forsuffixes" "headerbyte" "hex" "if" "inner"
302 "input" "interim" "intersectiontimes" "jobname" "kern" "known"
303 "length" "let" "ligtable" "makepath" "makepen" "message" "mexp"
304 "mlog" "month" "newinternal" "nonstopmode" "normaldeviate" "not"
305 "nullpen" "nullpicture" "numeric" "oct" "odd" "of" "or" "outer"
306 "pair" "path" "pausing" "pen" "pencircle" "penoffset" "picture"
307 "point" "postcontrol" "precontrol" "primary" "primarydef" "quote"
308 "randomseed" "readstring" "reverse" "rotated" "save" "scaled"
309 "scantokens" "scrollmode" "secondary" "secondarydef" "shifted"
310 "shipout" "show" "showdependencies" "showstats" "showstopping"
311 "showtoken" "showvariable" "sind" "skipto" "slanted" "special"
312 "sqrt" "step" "str" "string" "subpath" "substring" "suffix"
313 "tension" "tertiary" "tertiarydef" "text" "time" "to"
314 "tracingcapsules" "tracingchoices" "tracingcommands"
315 "tracingequations" "tracingmacros" "tracingonline" "tracingoutput"
316 "tracingrestores" "tracingspecs" "tracingstats" "tracingtitles"
317 "transform" "transformed" "true" "turningnumber" "uniformdeviate"
318 "unknown" "until" "vardef" "warningcheck" "withpen" "xpart"
319 "xscaled" "xxpart" "xypart" "year" "ypart" "yscaled" "yxpart"
a1506d29 320 "yypart" "zscaled")
6b279740
RS
321 "List of primitives common to Metafont and MetaPost.")
322
a1506d29 323(defconst metafont-primitives-list
6b279740
RS
324 '("at" "autorounding" "chardx" "chardy" "cull" "display"
325 "dropping" "fillin" "from" "granularity" "hppp" "inwindow"
326 "keeping" "numspecial" "openwindow" "proofing" "smoothing"
327 "totalweight" "tracingedges" "tracingpens" "turningcheck" "vppp"
328 "withweight" "xoffset" "yoffset")
329 "List of primitives only defined in Metafont.")
330
a1506d29 331(defconst metapost-primitives-list
6b279740
RS
332 '("arclength" "arctime" "bluepart" "bounded" "btex" "clip"
333 "clipped" "color" "dashed" "dashpart" "etex" "filled" "fontpart"
334 "fontsize" "greenpart" "infont" "linecap" "linejoin" "llcorner"
335 "lrcorner" "miterlimit" "mpxbreak" "pathpart" "penpart"
336 "prologues" "readfrom" "redpart" "setbounds" "stroked" "textpart"
337 "textual" "tracinglostchars" "truecorners" "ulcorner" "urcorner"
338 "verbatimtex" "withcolor" "within" "write")
339 "List of primitives only defined in MetaPost.")
340
a1506d29 341(defconst meta-common-plain-macros-list
6b279740
RS
342 '( "abs" "bot" "bye" "byte" "ceiling" "clear_pen_memory"
343 "clearit" "clearpen" "clearxy" "counterclockwise" "cutdraw" "decr"
344 "dir" "direction" "directionpoint" "div" "dotprod" "downto" "draw"
345 "drawdot" "erase" "exitunless" "fill" "filldraw" "flex" "gobble"
346 "hide" "incr" "interact" "interpath" "intersectionpoint" "inverse"
347 "label" "labels" "lft" "loggingall" "magstep" "makelabel" "max"
348 "min" "mod" "numtok" "penlabels" "penpos" "penstroke" "pickup"
349 "range" "reflectedabout" "relax" "rotatedabout" "rotatedaround"
350 "round" "rt" "savepen" "shipit" "softjoin" "solve" "stop"
351 "superellipse" "takepower" "tensepath" "thru" "top" "tracingall"
352 "tracingnone" "undraw" "undrawdot" "unfill" "unfilldraw"
353 "unitvector" "upto" "whatever")
354 "List of macros common to plain Metafont and MetaPost.")
355
a1506d29 356(defconst metafont-plain-macros-list
6b279740
RS
357 '("beginchar" "change_width" "culldraw" "cullit" "cutoff"
358 "define_blacker_pixels" "define_corrected_pixels"
359 "define_good_x_pixels" "define_good_y_pixels"
360 "define_horizontal_corrected_pixels" "define_pixels"
361 "define_whole_blacker_pixels" "define_whole_pixels"
362 "define_whole_vertical_blacker_pixels"
363 "define_whole_vertical_pixels" "endchar" "fix_units"
364 "font_coding_scheme" "font_extra_space" "font_identifier"
365 "font_normal_shrink" "font_normal_space" "font_normal_stretch"
366 "font_quad" "font_size" "font_slant" "font_x_height" "gfcorners"
367 "good.bot" "good.lft" "good.rt" "good.top" "good.x" "good.y"
368 "grayfont" "hround" "imagerules" "italcorr" "labelfont"
369 "lowres_fix" "makebox" "makegrid" "maketicks" "mode_lowres"
370 "mode_proof" "mode_setup" "mode_smoke" "nodisplays" "notransforms"
371 "openit" "penrazor" "pensquare" "proofoffset" "proofrule"
372 "proofrulethickness" "screenchars" "screenrule" "screenstrokes"
a1506d29 373 "showit" "slantfont" "smode" "titlefont" "vround")
6b279740
RS
374 "List of macros only defined in plain Metafont.")
375
a1506d29 376(defconst metapost-plain-macros-list
6b279740
RS
377 '("arrowhead" "bbox" "beginfig" "buildcycle" "center" "cutafter"
378 "cutbefore" "dashpattern" "dotlabel" "dotlabels" "drawarrow"
379 "drawdblarrow" "drawoptions" "endfig" "image" "label" "off" "on"
a1506d29 380 "thelabel")
6b279740
RS
381 "List of macros only defined in plain MetaPost.")
382
a1506d29
JB
383(defconst metapost-graph-macros-list
384 '("augment" "auto.x" "auto.y" "autogrid" "begingraph" "endgraph"
385 "format" "frame" "gdata" "gdotlabel" "gdraw" "gdrawarrow"
386 "gdrawdblarrow" "gfill" "glabel" "grid" "itick" "otick" "plot"
6b279740
RS
387 "setcoords" "setrange")
388 "List of macros only defined in MetaPost \"graph\" package.")
a1506d29
JB
389
390(defconst metapost-boxes-macros-list
6b279740
RS
391 '("boxit" "boxjoin" "bpath" "circleit" "drawboxed" "drawboxes"
392 "drawunboxed" "fixpos" "fixsize" "pic" "rboxit")
393 "List of macros only defined in MetaPost \"boxes\" package.")
394
395
396(defvar metafont-symbol-list
397 (append meta-common-primitives-list
398 metafont-primitives-list
399 meta-common-plain-macros-list
400 metafont-plain-macros-list)
401 "List of known symbols to complete in Metafont mode.")
402
403(defvar metapost-symbol-list
404 (append meta-common-primitives-list
405 metapost-primitives-list
406 meta-common-plain-macros-list
407 metapost-plain-macros-list
408 metapost-graph-macros-list
409 metapost-boxes-macros-list)
410 "List of known symbols to complete in MetaPost mode.")
411
412
413(defvar meta-symbol-list nil
414 "List of known symbols to complete in Metafont or MetaPost mode.")
415
a1506d29 416(defvar meta-symbol-changed nil
6b279740
RS
417 "Flag indicating whether `meta-symbol-list' has been initialized.")
418
419(defvar meta-complete-list nil
420; (list (list "\\<\\(\\sw+\\)" 1 'meta-symbol-list)
421; (list "" 'ispell-complete-word))
422 "List of ways to perform completion in Metafont or MetaPost mode.
423
424Each entry is a list with the following elements:
4251. Regexp matching the preceding text.
4262. A number indicating the subgroup in the regexp containing the text.
4273. A function returning an alist of possible completions.
4284. Text to append after a succesful completion (if any).
429
430Or alternatively:
4311. Regexp matching the preceding text.
4322. Function to do the actual completion.")
433
434
435(defun meta-add-symbols (&rest entries)
436 "Add entries to list of known symbols in Metafont or MetaPost mode."
437 (if meta-symbol-changed
438 (setq meta-symbol-list (cons entries meta-symbol-list))
439 (setq meta-symbol-changed t)
440 (setq meta-symbol-list (cons entries meta-symbol-list))))
441
442(defun meta-symbol-list ()
443 "Return value of list of known symbols in Metafont or MetaPost mode.
444If the list was changed, sort the list and remove duplicates first."
445 (if (not meta-symbol-changed)
446 ()
447 (setq meta-symbol-changed nil)
448 (message "Preparing completion list...")
449 ;; sort list of symbols
450 (setq meta-symbol-list
451 (sort (mapcar 'meta-listify (apply 'append meta-symbol-list))
452 'meta-car-string-lessp))
453 ;; remove duplicates
454 (let ((entry meta-symbol-list))
455 (while (and entry (cdr entry))
456 (let ((this (car entry))
457 (next (car (cdr entry))))
458 (if (not (string-equal (car this) (car next)))
459 (setq entry (cdr entry))
460 (if (> (length next) (length this))
461 (setcdr this (cdr next)))
462 (setcdr entry (cdr (cdr entry)))))))
463 (message "Preparing completion list... done"))
464 meta-symbol-list)
465
466(defun meta-listify (a)
467 ;; utility function used in `meta-add-symbols'
468 (if (listp a) a (list a)))
469
470(defun meta-car-string-lessp (a b)
471 ;; utility function used in `meta-add-symbols'
472 (string-lessp (car a) (car b)))
473
474
475(defun meta-complete-symbol ()
476 "Perform completion on Metafont or MetaPost symbol preceding point."
477 (interactive "*")
478 (let ((list meta-complete-list)
479 entry)
480 (while list
481 (setq entry (car list)
482 list (cdr list))
483 (if (meta-looking-at-backward (car entry) 200)
484 (setq list nil)))
485 (if (numberp (nth 1 entry))
486 (let* ((sub (nth 1 entry))
487 (close (nth 3 entry))
488 (begin (match-beginning sub))
489 (end (match-end sub))
d6b8d4e7
SM
490 (list (funcall (nth 2 entry))))
491 (completion-in-region
492 begin end
493 (if (zerop (length close)) list
494 (apply-partially 'completion-table-with-terminator
495 close list))))
6b279740
RS
496 (funcall (nth 1 entry)))))
497
498
499(defun meta-looking-at-backward (regexp &optional limit)
500 ;; utility function used in `meta-complete-symbol'
501 (let ((pos (point)))
502 (save-excursion
a1506d29 503 (and (re-search-backward
6b279740
RS
504 regexp (if limit (max (point-min) (- (point) limit))) t)
505 (eq (match-end 0) pos)))))
506
507(defun meta-match-buffer (n)
508 ;; utility function used in `meta-complete-symbol'
509 (if (match-beginning n)
510 (let ((str (buffer-substring (match-beginning n) (match-end n))))
511 (set-text-properties 0 (length str) nil str)
512 (copy-sequence str))
513 ""))
514
515
516\f
517;;; Indentation.
518
28d16ed3 519(defcustom meta-indent-level 2
3406534c 520 "Indentation of begin-end blocks in Metafont or MetaPost mode."
28d16ed3
AS
521 :type 'integer
522 :group 'meta-font)
6b279740
RS
523
524
28d16ed3 525(defcustom meta-left-comment-regexp "%%+"
3406534c 526 "Regexp matching comments that should be placed on the left margin."
28d16ed3
AS
527 :type 'regexp
528 :group 'meta-font)
6b279740 529
28d16ed3 530(defcustom meta-right-comment-regexp nil
3406534c 531 "Regexp matching comments that should be placed to the right margin."
28d16ed3
AS
532 :type '(choice regexp
533 (const :tag "None" nil))
534 :group 'meta-font)
6b279740 535
28d16ed3 536(defcustom meta-ignore-comment-regexp "%[^%]"
3406534c 537 "Regexp matching comments that whose indentation should not be touched."
28d16ed3
AS
538 :type 'regexp
539 :group 'meta-font)
6b279740
RS
540
541
28d16ed3 542(defcustom meta-begin-environment-regexp
6b279740
RS
543 (concat "\\(begin\\(char\\|fig\\|gr\\(aph\\|oup\\)\\|logochar\\)\\|"
544 "def\\|for\\(\\|ever\\|suffixes\\)\\|if\\|mode_def\\|"
545 "primarydef\\|secondarydef\\|tertiarydef\\|vardef\\)")
3406534c 546 "Regexp matching the beginning of environments to be indented."
28d16ed3
AS
547 :type 'regexp
548 :group 'meta-font)
6b279740 549
28d16ed3 550(defcustom meta-end-environment-regexp
6b279740
RS
551 (concat "\\(end\\(char\\|def\\|f\\(ig\\|or\\)\\|gr\\(aph\\|oup\\)\\)"
552 "\\|fi\\)")
3406534c 553 "Regexp matching the end of environments to be indented."
28d16ed3
AS
554 :type 'regexp
555 :group 'meta-font)
6b279740 556
28d16ed3 557(defcustom meta-within-environment-regexp
6b279740
RS
558; (concat "\\(e\\(lse\\(\\|if\\)\\|xit\\(if\\|unless\\)\\)\\)")
559 (concat "\\(else\\(\\|if\\)\\)")
3406534c 560 "Regexp matching keywords within environments not to be indented."
28d16ed3
AS
561 :type 'regexp
562 :group 'meta-font)
6b279740
RS
563
564
565(defun meta-comment-indent ()
566 "Return the indentation for a comment in Metafont or MetaPost mode."
567 (if (and meta-left-comment-regexp
568 (looking-at meta-left-comment-regexp))
569 (current-column)
89c19c1e 570 (skip-chars-backward "\t\f ")
a1506d29 571 (max (if (bolp) 0 (1+ (current-column)))
6b279740
RS
572 comment-column)))
573
574(defun meta-indent-line ()
575 "Indent the line containing point as Metafont or MetaPost source."
576 (interactive)
577 (let ((indent (meta-indent-calculate)))
e814121e
GM
578 (if (/= (current-indentation) indent)
579 (save-excursion
580 (delete-region (line-beginning-position)
581 (progn (back-to-indentation) (point)))
582 (indent-to indent)))
6b279740
RS
583 (if (< (current-column) indent)
584 (back-to-indentation))))
585
586(defun meta-indent-calculate ()
587 "Return the indentation of current line of Metafont or MetaPost source."
8a4c5051
MC
588 ;; Indentation within strings is not considered as Meta* don't allow multi
589 ;; line strings.
6b279740
RS
590 (save-excursion
591 (back-to-indentation)
a1506d29 592 (cond
8a4c5051 593 ;; Comments to the left margin.
6b279740
RS
594 ((and meta-left-comment-regexp
595 (looking-at meta-left-comment-regexp))
596 0)
8a4c5051 597 ;; Comments to the right margin.
6b279740
RS
598 ((and meta-right-comment-regexp
599 (looking-at meta-right-comment-regexp))
600 comment-column)
601 ;; Comments best left alone.
a1506d29 602 ((and meta-ignore-comment-regexp
6b279740
RS
603 (looking-at meta-ignore-comment-regexp))
604 (current-indentation))
8a4c5051
MC
605 ;; Beginning of buffer.
606 ((eq (point-at-bol) (point-min))
607 0)
6b279740 608 ;; Backindent at end of environments.
8a4c5051 609 ((meta-indent-looking-at-code
6b279740 610 (concat "\\<" meta-end-environment-regexp "\\>"))
8a4c5051 611 (- (meta-indent-current-indentation) meta-indent-level))
6b279740 612 ;; Backindent at keywords within environments.
8a4c5051 613 ((meta-indent-looking-at-code
6b279740 614 (concat "\\<" meta-within-environment-regexp "\\>"))
8a4c5051
MC
615 (- (meta-indent-current-indentation) meta-indent-level))
616 (t (meta-indent-current-indentation)))))
617
618(defun meta-indent-in-string-p ()
619 "Tell if the point is in a string."
620 (or (nth 3 (syntax-ppss))
621 (eq (get-text-property (point) 'face) font-lock-string-face)))
622
623(defun meta-indent-looking-at-code (regexp)
624 "Same as `looking-at' but checks that the point is not in a string."
625 (unless (meta-indent-in-string-p)
626 (looking-at regexp)))
627
628(defun meta-indent-previous-line ()
629 "Go to the previous line of code, skipping comments."
89c19c1e 630 (skip-chars-backward "\n\t\f ")
8a4c5051
MC
631 (move-to-column (current-indentation))
632 ;; Ignore comments.
633 (while (and (looking-at comment-start) (not (bobp)))
89c19c1e 634 (skip-chars-backward "\n\t\f ")
ebd3fa6b
MC
635 (when (not (bobp))
636 (move-to-column (current-indentation)))))
8a4c5051
MC
637
638(defun meta-indent-unfinished-line ()
639 "Tell if the current line of code ends with an unfinished expression."
640 (save-excursion
641 (end-of-line)
642 ;; Skip backward the comments.
ebd3fa6b
MC
643 (let ((point-not-in-string (point)))
644 (while (search-backward comment-start (point-at-bol) t)
645 (unless (meta-indent-in-string-p)
646 (setq point-not-in-string (point))))
647 (goto-char point-not-in-string))
8a4c5051
MC
648 ;; Search for the end of the previous expression.
649 (if (search-backward ";" (point-at-bol) t)
650 (progn (while (and (meta-indent-in-string-p)
651 (search-backward ";" (point-at-bol) t)))
652 (if (= (char-after) ?\;)
653 (forward-char)
654 (beginning-of-line)))
655 (beginning-of-line))
656 ;; See if the last statement of the line is environment-related,
657 ;; or exists at all.
658 (if (meta-indent-looking-at-code
89c19c1e 659 (concat "[ \t\f]*\\($\\|" (regexp-quote comment-start)
8a4c5051
MC
660 "\\|\\<" meta-end-environment-regexp "\\>"
661 "\\|\\<" meta-begin-environment-regexp "\\>"
662 "\\|\\<" meta-within-environment-regexp "\\>\\)"))
663 nil
664 t)))
665
666(defun meta-indent-current-indentation ()
667 "Return the indentation wanted for the current line of code."
668 (+ (meta-indent-current-nesting)
669 (if (save-excursion
670 (back-to-indentation)
671 (and (not (looking-at (concat "\\<" meta-end-environment-regexp "\\>"
672 "\\|\\<" meta-within-environment-regexp "\\>")))
673 (progn (meta-indent-previous-line)
674 (meta-indent-unfinished-line))))
675 meta-indent-level
676 0)))
677
678(defun meta-indent-current-nesting ()
679 "Return the indentation according to the nearest environment keyword."
680 (save-excursion
681 (save-restriction
682 (widen)
683 (back-to-indentation)
684 (let ((to-add 0))
685 ;; If we found some environment marker backward...
686 (if (catch 'found
687 (while (re-search-backward
688 (concat "(\\|)\\|\\<" meta-end-environment-regexp "\\>"
689 "\\|\\<" meta-begin-environment-regexp "\\>"
690 "\\|\\<" meta-within-environment-regexp "\\>")
691 nil t)
692 ;; If we aren't in a string or in a comment, we've found something.
693 (unless (or (meta-indent-in-string-p)
694 (nth 4 (syntax-ppss)))
695 (cond ((= (char-after) ?\()
696 (setq to-add (+ to-add meta-indent-level)))
697 ((= (char-after) ?\))
698 (setq to-add (- to-add meta-indent-level)))
699 (t (throw 'found t))))))
700 (progn
701 ;; ... then use it to compute the current indentation.
702 (back-to-indentation)
703 (+ to-add (current-indentation) (meta-indent-level-count)
704 ;; Compensate for backindent of end and within keywords.
705 (if (meta-indent-looking-at-code
706 (concat "\\<" meta-end-environment-regexp "\\>\\|"
707 "\\<" meta-within-environment-regexp "\\>"))
708 meta-indent-level
709 ;; Compensate for unfinished line.
710 (if (save-excursion
711 (meta-indent-previous-line)
712 (meta-indent-unfinished-line))
713 (- meta-indent-level)
714 0))))
715 0)))))
6b279740
RS
716
717(defun meta-indent-level-count ()
718 "Count indentation change for begin-end commands in the current line."
719 (save-excursion
720 (save-restriction
721 (let ((count 0))
a1506d29 722 (narrow-to-region
6b279740
RS
723 (point) (save-excursion
724 (re-search-forward "[^\\\\\"]%\\|\n\\|\\'" nil t)
725 (backward-char) (point)))
726 (while (re-search-forward "\\<\\sw+\\>\\|(\\|)" nil t)
727 (save-excursion
728 (goto-char (match-beginning 0))
729 (cond
730 ;; Count number of begin-end keywords within line.
8a4c5051 731 ((meta-indent-looking-at-code
6b279740
RS
732 (concat "\\<" meta-begin-environment-regexp "\\>"))
733 (setq count (+ count meta-indent-level)))
8a4c5051 734 ((meta-indent-looking-at-code
6b279740 735 (concat "\\<" meta-end-environment-regexp "\\>"))
8a4c5051 736 (setq count (- count meta-indent-level))))))
6b279740
RS
737 count))))
738
739
740\f
6b279740
RS
741;;; Editing commands.
742
28d16ed3 743(defcustom meta-begin-defun-regexp
6b279740
RS
744 (concat "\\(begin\\(char\\|fig\\|logochar\\)\\|def\\|mode_def\\|"
745 "primarydef\\|secondarydef\\|tertiarydef\\|vardef\\)")
3406534c 746 "Regexp matching beginning of defuns in Metafont or MetaPost mode."
28d16ed3
AS
747 :type 'regexp
748 :group 'meta-font)
6b279740 749
28d16ed3 750(defcustom meta-end-defun-regexp
6b279740 751 (concat "\\(end\\(char\\|def\\|fig\\)\\)")
3406534c 752 "Regexp matching the end of defuns in Metafont or MetaPost mode."
28d16ed3
AS
753 :type 'regexp
754 :group 'meta-font)
6b279740
RS
755
756
757(defun meta-beginning-of-defun (&optional arg)
758 "Move backward to beginnning of a defun in Metafont or MetaPost code.
a1506d29 759With numeric argument, do it that many times.
6b279740
RS
760Negative arg -N means move forward to Nth following beginning of defun.
761Returns t unless search stops due to beginning or end of buffer."
762 (interactive "p")
763 (if (or (null arg) (= 0 arg)) (setq arg 1))
764 (and arg (< arg 0) (not (eobp)) (forward-char 1))
a1506d29 765 (and (re-search-backward
6b279740
RS
766 (concat "\\<" meta-begin-defun-regexp "\\>") nil t arg)
767 (progn (goto-char (match-beginning 0))
768 (skip-chars-backward "%")
89c19c1e 769 (skip-chars-backward " \t\f") t)))
a1506d29 770
6b279740
RS
771(defun meta-end-of-defun (&optional arg)
772 "Move forward to end of a defun in Metafont or MetaPost code.
a1506d29 773With numeric argument, do it that many times.
6b279740
RS
774Negative argument -N means move back to Nth preceding end of defun.
775Returns t unless search stops due to beginning or end of buffer."
776 (interactive "p")
777 (if (or (null arg) (= 0 arg)) (setq arg 1))
778 (and (< arg 0) (not (bobp)) (forward-line -1))
779 (and (re-search-forward
780 (concat "\\<" meta-end-defun-regexp "\\>") nil t arg)
781 (progn (goto-char (match-end 0))
782 (skip-chars-forward ";")
89c19c1e 783 (skip-chars-forward " \t\f")
6b279740
RS
784 (if (looking-at "\n") (forward-line 1)) t)))
785
786
787(defun meta-comment-region (beg end &optional arg)
788 "Comment out active region as Metafont or MetaPost source."
789 (interactive "r")
790 (comment-region beg end arg))
791
792(defun meta-uncomment-region (beg end)
793 "Uncomment active region as Metafont or MetaPost source."
794 (interactive "r")
795 (comment-region beg end -1))
796
797(defun meta-comment-defun (&optional arg)
798 "Comment out current environment as Metafont or MetaPost source.
799With prefix argument, uncomment the environment.
800The environment used is the one that contains point or follows point."
801 (interactive "P")
802 (save-excursion
803 (let* ((end (if (meta-end-of-defun) (point) (point-max)))
804 (beg (if (meta-beginning-of-defun) (point) (point-min))))
805 (comment-region beg end arg))))
806
807(defun meta-uncomment-defun ()
808 "Uncomment current environment as Metafont or MetaPost source."
809 (interactive)
810 (meta-comment-defun -1))
811
812
813(defun meta-indent-region (beg end)
814 "Indent the active region as Metafont or MetaPost source."
815 (interactive "r")
816 (indent-region beg end nil))
817
818(defun meta-indent-buffer ()
819 "Indent the whole buffer contents as Metafont or MetaPost source."
820 (interactive)
821 (save-excursion
822 (indent-region (point-min) (point-max) nil)))
823
824(defun meta-indent-defun ()
825 "Indent the current environment as Metafont or MetaPost source.
826The environment indented is the one that contains point or follows point."
827 (interactive)
828 (save-excursion
829 (let* ((end (if (meta-end-of-defun) (point) (point-max)))
830 (beg (if (meta-beginning-of-defun) (point) (point-min))))
831 (indent-region beg end nil))))
832
833
834(defun meta-mark-defun ()
835 "Put mark at end of the environment, point at the beginning.
836The environment marked is the one that contains point or follows point."
837 (interactive)
838 (push-mark (point))
839 (meta-end-of-defun)
840 (push-mark (point) nil t)
841 (meta-beginning-of-defun))
842
843
844\f
845;;; Syntax table, keymap and menu.
846
847(defvar meta-mode-abbrev-table nil
848 "Abbrev table used in Metafont or MetaPost mode.")
849(define-abbrev-table 'meta-mode-abbrev-table ())
850
ff319790
SM
851(defvar meta-mode-syntax-table
852 (let ((st (make-syntax-table)))
853 ;; underscores are word constituents
854 (modify-syntax-entry ?_ "w" st)
855 ;; miscellaneous non-word symbols
856 (modify-syntax-entry ?# "_" st)
857 (modify-syntax-entry ?@ "_" st)
858 (modify-syntax-entry ?$ "_" st)
859 (modify-syntax-entry ?? "_" st)
860 (modify-syntax-entry ?! "_" st)
861 ;; binary operators
862 (modify-syntax-entry ?& "." st)
863 (modify-syntax-entry ?+ "." st)
864 (modify-syntax-entry ?- "." st)
865 (modify-syntax-entry ?/ "." st)
866 (modify-syntax-entry ?* "." st)
867 (modify-syntax-entry ?. "." st)
868 (modify-syntax-entry ?: "." st)
869 (modify-syntax-entry ?= "." st)
870 (modify-syntax-entry ?< "." st)
871 (modify-syntax-entry ?> "." st)
872 (modify-syntax-entry ?| "." st)
873 ;; opening and closing delimiters
874 (modify-syntax-entry ?\( "()" st)
875 (modify-syntax-entry ?\) ")(" st)
876 (modify-syntax-entry ?\[ "(]" st)
877 (modify-syntax-entry ?\] ")[" st)
878 (modify-syntax-entry ?\{ "(}" st)
879 (modify-syntax-entry ?\} "){" st)
880 ;; comment character
881 (modify-syntax-entry ?% "<" st)
882 (modify-syntax-entry ?\n ">" st)
883 ;; escape character, needed for embedded TeX code
884 (modify-syntax-entry ?\\ "\\" st)
885 st)
6b279740 886 "Syntax table used in Metafont or MetaPost mode.")
6b279740 887
ff319790
SM
888(defvar meta-mode-map
889 (let ((map (make-sparse-keymap)))
890 (define-key map "\C-m" 'reindent-then-newline-and-indent)
891 ;; Comment Paragraphs:
892 ;; (define-key map "\M-a" 'backward-sentence)
893 ;; (define-key map "\M-e" 'forward-sentence)
894 ;; (define-key map "\M-h" 'mark-paragraph)
895 ;; (define-key map "\M-q" 'fill-paragraph)
896 ;; Navigation:
897 (define-key map "\M-\C-a" 'meta-beginning-of-defun)
898 (define-key map "\M-\C-e" 'meta-end-of-defun)
899 (define-key map "\M-\C-h" 'meta-mark-defun)
900 ;; Indentation:
901 (define-key map "\M-\C-q" 'meta-indent-defun)
902 (define-key map "\C-c\C-qe" 'meta-indent-defun)
903 (define-key map "\C-c\C-qr" 'meta-indent-region)
904 (define-key map "\C-c\C-qb" 'meta-indent-buffer)
905 ;; Commenting Out:
906 (define-key map "\C-c%" 'meta-comment-defun)
907 ;; (define-key map "\C-uC-c%" 'meta-uncomment-defun)
908 (define-key map "\C-c;" 'meta-comment-region)
909 (define-key map "\C-c:" 'meta-uncomment-region)
910 ;; Symbol Completion:
911 (define-key map "\M-\t" 'meta-complete-symbol)
912 ;; Shell Commands:
913 ;; (define-key map "\C-c\C-c" 'meta-command-file)
914 ;; (define-key map "\C-c\C-k" 'meta-kill-job)
915 ;; (define-key map "\C-c\C-l" 'meta-recenter-output)
916 map)
6b279740 917 "Keymap used in Metafont or MetaPost mode.")
ff319790 918
6b279740 919
a1506d29 920(easy-menu-define
6b279740
RS
921 meta-mode-menu meta-mode-map
922 "Menu used in Metafont or MetaPost mode."
923 (list "Meta"
924 ["Forward Environment" meta-beginning-of-defun t]
925 ["Backward Environment" meta-end-of-defun t]
926 "--"
927 ["Indent Line" meta-indent-line t]
928 ["Indent Environment" meta-indent-defun t]
a1506d29 929 ["Indent Region" meta-indent-region
6b279740
RS
930 :active (meta-mark-active)]
931 ["Indent Buffer" meta-indent-buffer t]
932 "--"
933 ["Comment Out Environment" meta-comment-defun t]
934 ["Uncomment Environment" meta-uncomment-defun t]
a1506d29 935 ["Comment Out Region" meta-comment-region
6b279740 936 :active (meta-mark-active)]
a1506d29 937 ["Uncomment Region" meta-uncomment-region
6b279740
RS
938 :active (meta-mark-active)]
939 "--"
940 ["Complete Symbol" meta-complete-symbol t]
941; "--"
942; ["Command on Buffer" meta-command-file t]
943; ["Kill Job" meta-kill-job t]
944; ["Recenter Output Buffer" meta-recenter-output-buffer t]
945 ))
946
947;; Compatibility: XEmacs doesn't have the `mark-active' variable.
948(defun meta-mark-active ()
949 "Return whether the mark and region are currently active in this buffer."
3a51abf1 950 (if (boundp 'mark-active) mark-active (mark)))
6b279740
RS
951
952
953\f
954;;; Hook variables.
955
28d16ed3 956(defcustom meta-mode-load-hook nil
3406534c 957 "Hook evaluated when first loading Metafont or MetaPost mode."
28d16ed3
AS
958 :type 'hook
959 :group 'meta-font)
960
961(defcustom meta-common-mode-hook nil
3406534c 962 "Hook evaluated by both `metafont-mode' and `metapost-mode'."
28d16ed3
AS
963 :type 'hook
964 :group 'meta-font)
965
966(defcustom metafont-mode-hook nil
3406534c 967 "Hook evaluated by `metafont-mode' after `meta-common-mode-hook'."
28d16ed3
AS
968 :type 'hook
969 :group 'meta-font)
970(defcustom metapost-mode-hook nil
3406534c 971 "Hook evaluated by `metapost-mode' after `meta-common-mode-hook'."
28d16ed3
AS
972 :type 'hook
973 :group 'meta-font)
6b279740
RS
974
975
976\f
977;;; Initialization.
978
979(defun meta-common-initialization ()
980 "Common initialization for Metafont or MetaPost mode."
981 (kill-all-local-variables)
982
983 (make-local-variable 'paragraph-start)
984 (make-local-variable 'paragraph-separate)
a1506d29 985 (setq paragraph-start
6b279740 986 (concat page-delimiter "\\|$"))
a1506d29 987 (setq paragraph-separate
6b279740
RS
988 (concat page-delimiter "\\|$"))
989
990 (make-local-variable 'paragraph-ignore-fill-prefix)
991 (setq paragraph-ignore-fill-prefix t)
992
993 (make-local-variable 'comment-start-skip)
994 (make-local-variable 'comment-start)
995 (make-local-variable 'comment-end)
996 (make-local-variable 'comment-multi-line)
89c19c1e 997 (setq comment-start-skip "%+[ \t\f]*")
6b279740
RS
998 (setq comment-start "%")
999 (setq comment-end "")
1000 (setq comment-multi-line nil)
1001
89c19c1e
MC
1002 ;; We use `back-to-indentation' but \f is no indentation sign.
1003 (modify-syntax-entry ?\f "_ ")
1004
6b279740
RS
1005 (make-local-variable 'parse-sexp-ignore-comments)
1006 (setq parse-sexp-ignore-comments t)
1007
1008 (make-local-variable 'comment-indent-function)
1009 (setq comment-indent-function 'meta-comment-indent)
6b279740
RS
1010 (make-local-variable 'indent-line-function)
1011 (setq indent-line-function 'meta-indent-line)
1012 ;; No need to define a mode-specific 'indent-region-function.
1013 ;; Simply use the generic 'indent-region and 'comment-region.
1014
1015 ;; Set defaults for font-lock mode.
1016 (make-local-variable 'font-lock-defaults)
1017 (setq font-lock-defaults
1018 '(meta-font-lock-keywords
1019 nil nil ((?_ . "w")) nil
1020 (font-lock-comment-start-regexp . "%")))
1021
1022 ;; Activate syntax table, keymap and menu.
1023 (setq local-abbrev-table meta-mode-abbrev-table)
1024 (set-syntax-table meta-mode-syntax-table)
1025 (use-local-map meta-mode-map)
1026 (easy-menu-add meta-mode-menu)
1027 )
1028
1029
1eadb66c 1030;;;###autoload
6b279740
RS
1031(defun metafont-mode ()
1032 "Major mode for editing Metafont sources.
1033Special commands:
1034\\{meta-mode-map}
1035
1036Turning on Metafont mode calls the value of the variables
1037`meta-common-mode-hook' and `metafont-mode-hook'."
1038 (interactive)
1039 (meta-common-initialization)
1040 (setq mode-name "Metafont")
1041 (setq major-mode 'metafont-mode)
1042
1043 ;; Set defaults for completion function.
1044 (make-local-variable 'meta-symbol-list)
1045 (make-local-variable 'meta-symbol-changed)
1046 (make-local-variable 'meta-complete-list)
1047 (setq meta-symbol-list nil)
1048 (setq meta-symbol-changed nil)
1049 (apply 'meta-add-symbols metafont-symbol-list)
1050 (setq meta-complete-list
1051 (list (list "\\<\\(\\sw+\\)" 1 'meta-symbol-list)
1052 (list "" 'ispell-complete-word)))
9a969196 1053 (run-mode-hooks 'meta-common-mode-hook 'metafont-mode-hook))
6b279740 1054
1eadb66c 1055;;;###autoload
6b279740
RS
1056(defun metapost-mode ()
1057 "Major mode for editing MetaPost sources.
1058Special commands:
1059\\{meta-mode-map}
1060
1061Turning on MetaPost mode calls the value of the variable
1062`meta-common-mode-hook' and `metafont-mode-hook'."
1063 (interactive)
1064 (meta-common-initialization)
1065 (setq mode-name "MetaPost")
1066 (setq major-mode 'metapost-mode)
1067
1068 ;; Set defaults for completion function.
1069 (make-local-variable 'meta-symbol-list)
1070 (make-local-variable 'meta-symbol-changed)
1071 (make-local-variable 'meta-complete-list)
1072 (setq meta-symbol-list nil)
1073 (setq meta-symbol-changed nil)
1074 (apply 'meta-add-symbols metapost-symbol-list)
1075 (setq meta-complete-list
1076 (list (list "\\<\\(\\sw+\\)" 1 'meta-symbol-list)
1077 (list "" 'ispell-complete-word)))
9a969196 1078 (run-mode-hooks 'meta-common-mode-hook 'metapost-mode-hook))
6b279740
RS
1079
1080
1081;;; Just in case ...
1082
1083(provide 'meta-mode)
1084(run-hooks 'meta-mode-load-hook)
1085
1086;;; meta-mode.el ends here