]> code.delx.au - gnu-emacs-elpa/blob - packages/auctex/context.el
* auctex: Shorten copyright year ranges.
[gnu-emacs-elpa] / packages / auctex / context.el
1 ;;; context.el --- Support for ConTeXt documents.
2
3 ;; Copyright (C) 2003-2006, 2008, 2010, 2012
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: Berend de Boer <berend@pobox.com>
7 ;; Keywords: tex
8
9 ;; This file is part of AUCTeX.
10
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 ;; 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This is in progress ConTeXt support for AUCTeX. Please report
29 ;; anomalies or things you believe should be added.
30
31 ;; AUCTeX is closely interwoven with LaTeX. We have to split up
32 ;; things without breaking 'em.
33
34 ;; some parts are stolen from latex.el and adapted to ConTeXt.
35
36 ;; TODO
37 ;; 1. indentation still bad.
38 ;; 2. paragraph refilling doesn't work 100%, and is very slow.
39 ;; 4. Remove dependency on LaTeX by moving LaTeX commands to TeX.
40 ;; 5. Most ConTeXt macro's don't currently have lisp code to query for
41 ;; arguments. As ConTeXt arguments are quite complex, the LaTeX way
42 ;; of querying for arguments just doesn't cut it.
43 ;; 6. Check auto-parsing: does it detect % interface=nl for example?
44 ;; 7. Complete adding ConTeXt macro's. Perhaps parse cont-en.xml and
45 ;; generate the interfaces?
46 ;; 8. Add to menu: make TeX hash (mktexlsr), context format and metapost format.
47
48 ;; TODO Documentation
49 ;; 1. multifile done differently with ConTeXt
50
51 ;;; Code:
52
53 (require 'tex-buf)
54 (require 'tex)
55 (require 'latex) ; for functions like `TeX-look-at' and `LaTeX-split-long-menu'
56 (require 'plain-tex) ; for `plain-TeX-common-initialization'
57
58 (defgroup ConTeXt-macro nil
59 "Special support for ConTeXt macros in AUCTeX."
60 :prefix "TeX-"
61 :group 'ConTeXt
62 :group 'TeX-macro)
63
64
65 ;;; variables
66
67 ;; globals used in certain macro's.
68 (defvar done-mark nil
69 "Position of point afterwards, default nil (meaning end).")
70
71 (defvar reference nil
72 "Set by `ConTeXt-section-ref', used by `ConTeXt-section-section'.")
73
74 (defvar title nil
75 "Set by `ConTeXt-section-title', used by `ConTeXt-section-section'.")
76
77
78 ;; others
79
80 (defvar ConTeXt-known-interfaces '("cz" "de" "en" "it" "nl" "ro" "uk"))
81
82 (defcustom ConTeXt-default-interface "en"
83 "Default interface to be used when running ConTeXt."
84 :group 'ConTeXt
85 :type 'string)
86
87 (defvar ConTeXt-current-interface "en"
88 "Interface to be used for inserting macros and ConTeXt run.")
89 (make-variable-buffer-local 'ConTeXt-current-interface)
90
91 (defvar ConTeXt-menu-changed nil)
92 ;; Need to update ConTeXt menu.
93 (make-variable-buffer-local 'ConTeXt-menu-changed)
94
95 (defvar ConTeXt-largest-level nil
96 "Largest sectioning level within current document.")
97 (make-variable-buffer-local 'ConTeXt-largest-level)
98
99 (defun ConTeXt-largest-level ()
100 (TeX-update-style)
101 ConTeXt-largest-level)
102
103
104 ;;; Syntax
105
106 (defvar ConTeXt-optop "["
107 "The ConTeXt optional argument opening character.")
108
109 (defvar ConTeXt-optcl "]"
110 "The ConTeXt optional argument closing character.")
111
112
113 ;; Define a ConTeXt macro
114
115 (defvar ConTeXt-define-list ()
116 "Calls ConTeXt-XX-define-list where XX is the current interface.")
117
118 (defun ConTeXt-define-command (what)
119 "The ConTeXt macro to define WHAT."
120 (funcall
121 (intern (concat "ConTeXt-define-command-" ConTeXt-current-interface)) what))
122
123 (defun ConTeXt-insert-define (define)
124 "Insert the ConTeXt define macro DEFINE."
125 (insert TeX-esc (ConTeXt-define-command define))
126 (newline)
127 (indent-according-to-mode)
128 (ConTeXt-arg-setup nil))
129
130
131 ;; Setup a ConTeXt macro
132
133 (defvar ConTeXt-setup-list ()
134 "Calls ConTeXt-XX-setup-list where XX is the current interface.")
135
136 (defun ConTeXt-setup-command (what)
137 "The ConTeXt macro to setup WHAT."
138 (funcall
139 (intern (concat "ConTeXt-setup-command-" ConTeXt-current-interface)) what))
140
141 (defun ConTeXt-insert-setup (setup)
142 "Insert the ConTeXt setup macro SETUP."
143 (insert TeX-esc (ConTeXt-setup-command setup))
144 (newline)
145 (indent-according-to-mode)
146 (ConTeXt-arg-setup nil))
147
148
149 ;; Referencing ConTeXt macro's
150
151 (defvar ConTeXt-referencing-list ()
152 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
153
154 (defun ConTeXt-referencing-command (what)
155 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
156 what)
157
158 (defun ConTeXt-insert-referencing (what)
159 "Insert the ConTeXt referencing SETUP."
160 (insert TeX-esc (ConTeXt-referencing-command what))
161 (newline)
162 (indent-according-to-mode)
163 (ConTeXt-arg-setup nil))
164
165
166 ;; Other ConTeXt macro's
167
168 (defvar ConTeXt-other-macro-list ()
169 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
170
171 (defun ConTeXt-other-macro-command (what)
172 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
173 what)
174
175 (defun ConTeXt-insert-other-macro (other-macro)
176 "Insert the ConTeXt other macro's macro SETUP."
177 (insert TeX-esc (ConTeXt-other-macro-command other-macro))
178 (newline)
179 (indent-according-to-mode)
180 (ConTeXt-arg-setup nil))
181
182
183 ;;; Project structure
184
185 (defvar ConTeXt-project-structure-list ()
186 "Calls ConTeXt-XX-project-structure where XX is the current interface.")
187
188 (defun ConTeXt-project-structure (N)
189 "Insert a ConTeXt project structure where N is in index into `ConTeXt-project-structure-list'."
190 (funcall (intern(concat
191 "ConTeXt-project-"
192 (nth N ConTeXt-project-structure-list)
193 "-insert"))))
194
195 (defun ConTeXt-project-project-insert ()
196 "Insert a basic template for a new ConTeXt project."
197 (save-excursion
198 (insert "% The following names are examples only\n")
199 (insert TeX-esc (ConTeXt-environment-start-name) (nth 0 ConTeXt-project-structure-list) " myproject")
200 (newline 2)
201 (insert TeX-esc (nth 1 ConTeXt-project-structure-list) " myenvironment")
202 (newline 2)
203 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
204 (newline 2)
205 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct2")
206 (newline 2)
207 (insert TeX-esc (ConTeXt-environment-stop-name) (nth 0 ConTeXt-project-structure-list))))
208
209 (defun ConTeXt-project-environment-insert ()
210 "Insert a basic template for the environment of a ConTeXt project."
211 (save-excursion
212 (insert "% The name 'myenvironment' is an example only.\n"
213 "% It must match the name in your project file.\n")
214 (insert TeX-esc (ConTeXt-environment-start-name)
215 (nth 1 ConTeXt-project-structure-list) " myenvironment\n\n")
216 (insert "% Put environment charateristics that must be defined at the "
217 "highest level here\n\n")
218 (insert TeX-esc (ConTeXt-environment-stop-name)
219 (nth 1 ConTeXt-project-structure-list))))
220
221 (defun ConTeXt-project-product-insert ()
222 "Insert a basic template for a product of a ConTeXt project."
223 (save-excursion
224 (insert "% The following names are examples only\n")
225 (insert TeX-esc (ConTeXt-environment-start-name)
226 (nth 2 ConTeXt-project-structure-list) " myproduct1")
227 (newline 2)
228 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject")
229 (newline 2)
230 (insert "% Components are optional. "
231 "You can also just start your document here.\n")
232 (insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent1")
233 (newline 2)
234 (insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent2")
235 (newline 2)
236 (insert TeX-esc (ConTeXt-environment-stop-name)
237 (nth 2 ConTeXt-project-structure-list))))
238
239 (defun ConTeXt-project-component-insert ()
240 "Insert a basic template for a component of a ConTeXt project."
241 (save-excursion
242 (insert "% The following names are examples only\n")
243 (insert TeX-esc (ConTeXt-environment-start-name)
244 (nth 3 ConTeXt-project-structure-list) " mycomponent1")
245 (newline 2)
246 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject\n")
247 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
248 (newline 2)
249 (insert "% ... text here ...")
250 (newline 2)
251 (insert TeX-esc (ConTeXt-environment-stop-name)
252 (nth 3 ConTeXt-project-structure-list))))
253
254
255 ;;; Section blocks
256
257 (defvar ConTeXt-section-block-list ()
258 "Calls ConTeXt-XX-section-list where XX is the current interface.")
259
260 (defun ConTeXt-section-block (section-block)
261 "Insert the ConTeXt section block SECTION-BLOCK."
262 (ConTeXt-environment-menu section-block))
263
264
265 ;;; Sections
266
267 (defun ConTeXt-section (arg)
268 "Insert a template for a ConTeXt section.
269 Determinate the type of section to be inserted, by the argument ARG.
270
271 If ARG is nil or missing, use the current level.
272 If ARG is a list (selected by \\[universal-argument]), go downward one level.
273 If ARG is negative, go up that many levels.
274 If ARG is positive or zero, use absolute level:
275
276 0 : part
277 1 : chapter
278 2 : section
279 3 : subsection
280 4 : subsubsection
281 5 : subsubsubsection
282
283 Or:
284
285 0 : title
286 1 : subject
287 2 : subsubject
288 3 : subsubsubject
289
290 The following variables can be set to customize:
291
292 `ConTeXt-section-hook' Hooks to run when inserting a section.
293 `ConTeXt-section-ref' Prefix to all section references."
294
295 (interactive "*P")
296 (let* ((val (prefix-numeric-value arg))
297 (level (cond ((null arg)
298 (ConTeXt-current-section))
299 ((listp arg)
300 (ConTeXt-down-section))
301 ((< val 0)
302 (ConTeXt-up-section (- val)))
303 (t val)))
304 (name (ConTeXt-section-name level))
305 (toc nil)
306 (title "")
307 (done-mark (make-marker)))
308 (newline)
309 (run-hooks 'ConTeXt-section-hook)
310 (newline)
311 (if (marker-position done-mark)
312 (goto-char (marker-position done-mark)))
313 (set-marker done-mark nil)))
314
315 ;; LaTeX has a max function here, which makes no sense.
316 ;; I think you want to insert a section that is max ConTeXt-largest-level
317 (defun ConTeXt-current-section ()
318 "Return the level of the section that contain point.
319 See also `ConTeXt-section' for description of levels."
320 (save-excursion
321 (min (ConTeXt-largest-level)
322 (if (re-search-backward outline-regexp nil t)
323 (+ 1 (- (ConTeXt-outline-level) (ConTeXt-outline-offset)))
324 (ConTeXt-largest-level)))))
325
326 (defun ConTeXt-down-section ()
327 "Return the value of a section one level under the current.
328 Tries to find what kind of section that have been used earlier in the
329 text, if this fail, it will just return one less than the current
330 section."
331 (save-excursion
332 (let ((current (ConTeXt-current-section))
333 (next nil)
334 (regexp outline-regexp))
335 (if (not (re-search-backward regexp nil t))
336 (1+ current)
337 (while (not next)
338 (cond
339 ((eq (ConTeXt-current-section) current)
340 (if (re-search-forward regexp nil t)
341 (if (<= (setq next (ConTeXt-current-section)) current) ;Wow!
342 (setq next (1+ current)))
343 (setq next (1+ current))))
344 ((not (re-search-backward regexp nil t))
345 (setq next (1+ current)))))
346 next))))
347
348 (defun ConTeXt-up-section (arg)
349 "Return the value of the section ARG levels above this one."
350 (save-excursion
351 (if (zerop arg)
352 (ConTeXt-current-section)
353 (let ((current (ConTeXt-current-section)))
354 (while (and (>= (ConTeXt-current-section) current)
355 (re-search-backward outline-regexp
356 nil t)))
357 (ConTeXt-up-section (1- arg))))))
358
359 (defvar ConTeXt-section-list ()
360 "ConTeXt-XX-section-list where XX is the current interface.")
361
362 (defun ConTeXt-section-name (level)
363 "Return the name of the section corresponding to LEVEL."
364 (let ((entry (TeX-member level ConTeXt-section-list
365 (function (lambda (a b) (equal a (nth 1 b)))))))
366 (if entry
367 (nth 0 entry)
368 nil)))
369
370 (defun ConTeXt-section-level (name)
371 "Return the level of the section NAME."
372 (let ((entry (TeX-member name ConTeXt-section-list
373 (function (lambda (a b) (equal a (nth 0 b)))))))
374
375 (if entry
376 (nth 1 entry)
377 nil)))
378
379
380 ;;; Section Hooks.
381
382 (defcustom ConTeXt-section-hook
383 '(ConTeXt-section-heading
384 ConTeXt-section-title
385 ConTeXt-section-ref
386 ConTeXt-section-section)
387 "List of hooks to run when a new section is inserted.
388
389 The following variables are set before the hooks are run
390
391 level - numeric section level, see the documentation of `ConTeXt-section'.
392 name - name of the sectioning command, derived from `level'.
393 title - The title of the section, default to an empty string.
394 `done-mark' - Position of point afterwards, default nil (meaning end).
395
396 The following standard hook exist -
397
398 ConTeXt-section-heading: Query the user about the name of the
399 sectioning command. Modifies `level' and `name'.
400
401 ConTeXt-section-title: Query the user about the title of the
402 section. Modifies `title'.
403
404 ConTeXt-section-section: Insert ConTeXt section command according to
405 `name', `title', and `reference'. If `title' is an empty string,
406 `done-mark' will be placed at the point they should be inserted.
407
408 ConTeXt-section-ref: Insert a reference for this section command.
409
410 To get a full featured `ConTeXt-section' command, insert
411
412 (setq ConTeXt-section-hook
413 '(ConTeXt-section-heading
414 ConTeXt-section-title
415 ConTeXt-section-section
416 ConTeXt-section-ref))
417
418 in your .emacs file."
419 :group 'ConTeXt-macro
420 :type 'hook
421 :options
422 '(ConTeXt-section-heading
423 ConTeXt-section-title
424 ConTeXt-section-ref
425 ConTeXt-section-section))
426
427 (defun ConTeXt-section-heading ()
428 "Hook to prompt for ConTeXt section name.
429 Insert this hook into `ConTeXt-section-hook' to allow the user to change
430 the name of the sectioning command inserted with `\\[ConTeXt-section]'."
431 (let ((string (completing-read
432 (concat "Select level: (default " name ") ")
433 ConTeXt-section-list
434 nil nil nil)))
435 ;; Update name
436 (if (not (zerop (length string)))
437 (setq name string))))
438
439 (defun ConTeXt-section-title ()
440 "Hook to prompt for ConTeXt section title.
441 Insert this hook into `ConTeXt-section-hook' to allow the user to change
442 the title of the section inserted with `\\[ConTeXt-section]."
443 (setq title (read-string "What title: ")))
444
445 (defun ConTeXt-section-section ()
446 "Hook to insert ConTeXt section command into the file.
447 Insert this hook into `ConTeXt-section-hook' after those hooks which sets
448 the `name', `title', and `reference' variables, but before those hooks which
449 assumes the section already is inserted."
450 (insert TeX-esc name)
451 (cond ((null reference))
452 ((zerop (length reference))
453 (insert ConTeXt-optop)
454 (set-marker done-mark (point))
455 (insert ConTeXt-optcl))
456 (t
457 (insert ConTeXt-optop reference ConTeXt-optcl)))
458 (insert TeX-grop)
459 (if (zerop (length title))
460 (set-marker done-mark (point)))
461 (insert title TeX-grcl)
462 (newline)
463 ;; If RefTeX is available, tell it that we've just made a new section
464 (and (fboundp 'reftex-notice-new-section)
465 (funcall (symbol-function 'reftex-notice-new-section))))
466
467 (defun ConTeXt-section-ref ()
468 "Hook to insert a reference after the sectioning command.
469 Insert this hook into `ConTeXt-section-hook' to prompt for a label to be
470 inserted after the sectioning command."
471
472 (setq reference (completing-read
473 (TeX-argument-prompt t nil
474 "Comma separated list of references")
475 (LaTeX-label-list) nil nil))
476 ;; No reference or empty string entered?
477 (if (string-equal "" reference)
478 (setq reference nil)))
479
480
481 ;; Various
482 (defun TeX-ConTeXt-sentinel (process name)
483 "Cleanup TeX output buffer after running ConTeXt."
484 (cond ((TeX-TeX-sentinel-check process name))
485 ((save-excursion
486 ;; in a full ConTeXt run there will multiple texutil
487 ;; outputs. Just looking for "another run needed" would
488 ;; find the first occurence
489 (goto-char (point-max))
490 (re-search-backward "TeXUtil " nil t)
491 (re-search-forward "another run needed" nil t))
492 (message (concat "You should run ConTeXt again "
493 "to get references right, "
494 (TeX-current-pages)))
495 (setq TeX-command-next TeX-command-default))
496 ((re-search-forward "removed files :" nil t)
497 (message "sucessfully cleaned up"))
498 ((re-search-forward "^ ?TeX\\(Exec\\|Util\\)" nil t) ;; strange regexp --pg
499 (message (concat name ": successfully formatted "
500 (TeX-current-pages)))
501 (setq TeX-command-next TeX-command-Show))
502 (t
503 (message (concat name ": problems after "
504 (TeX-current-pages)))
505 (setq TeX-command-next TeX-command-default))))
506
507
508 ;;; Environments
509
510 (defgroup ConTeXt-environment nil
511 "Environments in AUCTeX."
512 :group 'ConTeXt-macro)
513
514 ;; TODO: interface awareness
515 (defcustom ConTeXt-default-environment "itemize"
516 "*The default environment when creating new ones with `ConTeXt-environment'."
517 :group 'ConTeXt-environment
518 :type 'string)
519 (make-variable-buffer-local 'ConTeXt-default-environment)
520
521 (TeX-auto-add-type "environment" "ConTeXt")
522
523 (fset 'ConTeXt-add-environments-auto
524 (symbol-function 'ConTeXt-add-environments))
525 (defun ConTeXt-add-environments (&rest environments)
526 "Add ENVIRONMENTS to the list of known environments."
527 (apply 'ConTeXt-add-environments-auto environments)
528 (setq ConTeXt-menu-changed t))
529
530 ;; (defvar ConTeXt-environment-list ()
531 ;; "ConTeXt-environment-list-XX where XX is the current interface.")
532
533 (defvar ConTeXt-environment-history nil)
534
535 (defun ConTeXt-environment-start-name ()
536 "Return the \\start translated to the language in current interface."
537 ;; it is "inizia", others are "start"
538 (cond ((equal ConTeXt-current-interface "it")
539 "inizia")
540 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
541 "start")
542 (t
543 ;; this should not happen
544 (error "Unknown interface: %s" ConTeXt-current-interface))))
545
546 (defun ConTeXt-environment-stop-name ()
547 "Return the \\stop translated to the language in current interface."
548 ;; it is "termina", others are "stop"
549 (cond ((equal ConTeXt-current-interface "it")
550 "termina")
551 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
552 "stop")
553 (t
554 ;; this should not happen
555 (error "Unknown interface: %s" ConTeXt-current-interface))))
556
557
558 (defun ConTeXt-environment (arg)
559 "Make ConTeXt environment (\\start...-\\stop... pair).
560 With optional ARG, modify current environment."
561 (interactive "*P")
562 (let ((environment (
563 completing-read (concat "Environment type: (default "
564 (if (TeX-near-bobp)
565 "text"
566 ConTeXt-default-environment)
567 ") ")
568 ConTeXt-environment-list
569 nil nil nil
570 'ConTeXt-environment-history)
571 ))
572 ;; Get default
573 (cond ((and (zerop (length environment))
574 (TeX-near-bobp))
575 (setq environment "text"))
576 ((zerop (length environment))
577 (setq environment ConTeXt-default-environment))
578 (t
579 (setq ConTeXt-default-environment environment)))
580
581 (let ((entry (assoc environment ConTeXt-environment-list)))
582 (when (null entry)
583 (ConTeXt-add-environments (list environment)))
584 (if arg
585 (ConTeXt-modify-environment environment)
586 (ConTeXt-environment-menu environment)))))
587
588 (defun ConTeXt-modify-environment (environment)
589 "Modify current environment."
590 (save-excursion
591 (ConTeXt-find-matching-stop)
592 (re-search-backward (concat (regexp-quote TeX-esc)
593 (ConTeXt-environment-stop-name)
594 " *\\([a-zA-Z]*\\)")
595 (save-excursion (beginning-of-line 1) (point)))
596 (replace-match
597 (concat TeX-esc (ConTeXt-environment-stop-name) environment) t t)
598 (beginning-of-line 1)
599 (ConTeXt-find-matching-start)
600 (re-search-forward (concat (regexp-quote TeX-esc)
601 (ConTeXt-environment-start-name)
602 " *\\([a-zA-Z]*\\)")
603 (save-excursion (end-of-line 1) (point)))
604 (replace-match
605 (concat TeX-esc (ConTeXt-environment-start-name) environment) t t)))
606
607
608 (defun ConTeXt-environment-menu (environment)
609 "Insert ENVIRONMENT around point or region."
610 (let ((entry (assoc environment ConTeXt-environment-list)))
611 (cond ((not (and entry (nth 1 entry)))
612 (ConTeXt-insert-environment environment))
613 ((numberp (nth 1 entry))
614 (let ((count (nth 1 entry))
615 (args ""))
616 (while (> count 0)
617 (setq args (concat args TeX-grop TeX-grcl))
618 (setq count (- count 1)))
619 (ConTeXt-insert-environment environment args)))
620 ((stringp (nth 1 entry))
621 (let ((prompts (cdr entry))
622 (args ""))
623 (while prompts
624 (setq args (concat args
625 TeX-grop
626 (read-from-minibuffer
627 (concat (car prompts) ": "))
628 TeX-grcl))
629 (setq prompts (cdr prompts)))
630 (ConTeXt-insert-environment environment args)))
631 (t
632 (apply (nth 1 entry) environment (nthcdr 2 entry))))))
633
634 (defun ConTeXt-close-environment ()
635 "Insert \\stop... to match the current environment."
636 (interactive "*")
637 (beginning-of-line)
638 (let ((empty-line (looking-at "[ \t]*$")))
639 (end-of-line)
640 (if (not empty-line)
641 (newline)))
642 (insert TeX-esc (ConTeXt-environment-stop-name)
643 (ConTeXt-current-environment))
644 ;; indent broken, so don't do it.
645 ;;(indent-according-to-mode)
646 (end-of-line)
647 (newline))
648
649 (defun ConTeXt-insert-environment (environment &optional extra)
650 "Insert ENVIRONMENT, with optional argument EXTRA."
651 (if (and (TeX-active-mark)
652 (not (eq (mark) (point))))
653 (save-excursion
654 (if (< (mark) (point))
655 (exchange-point-and-mark))
656 (insert TeX-esc (ConTeXt-environment-start-name) environment)
657 (newline)
658 (forward-line -1)
659 (indent-according-to-mode)
660 (if extra (insert extra))
661 (goto-char (mark))
662 (or (TeX-looking-at-backward "^[ \t]*")
663 (newline))
664 (insert TeX-esc (ConTeXt-environment-stop-name) environment)
665 (newline)
666 (forward-line -1)
667 (indent-according-to-mode)
668 ;;(goto-char (point))
669 )
670 (or (TeX-looking-at-backward "^[ \t]*")
671 (newline))
672 (insert TeX-esc (ConTeXt-environment-start-name) environment)
673 (indent-according-to-mode)
674 (if extra (insert extra))
675 (end-of-line)
676 (newline-and-indent)
677 (newline)
678 (insert TeX-esc (ConTeXt-environment-stop-name) environment)
679 (or (looking-at "[ \t]*$")
680 (save-excursion (newline-and-indent)))
681 (indent-according-to-mode)
682 (end-of-line 0)))
683
684 \f
685 ;; with the following we can call a function on an environment. Say
686 ;; you have metapost stuff within your TeX file, go to the environment
687 ;; and run ConTeXt-work-on-environment (suggested Key: C-c !). AUCTeX
688 ;; sees that you are inside e.g. \startMPpage....\stopMPpage and
689 ;; looks in ConTeXt-environment-helper for a function to be called.
690
691 ;; % so pressing C-c ! inside the following ...
692 ;;\startuseMPgraphic{Logo}{Scale}
693 ;; % Top rectangle
694 ;; filldraw (0,0)--(2cm,0)--(2cm,1cm)--(0,1cm)--cycle withcolor blue ;
695 ;; % Bottom black rectangle
696 ;; drawfill (0,0)--(2cm,0)--(2cm,-1cm)--(0,-1cm)--cycle withcolor black;
697 ;; % White Text
698 ;; draw btex \bf AB etex withcolor white ;
699 ;; % resize to size
700 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
701 ;; \stopuseMPgraphic
702
703 ;; % ...should give you a "new buffer" (currently narrowed to region
704 ;; % and switched to metapost-mode and recursive-edit)
705
706 ;; % Top rectangle
707 ;; filldraw (0,0)--(2cm,0)--(2cm,1cm)--(0,1cm)--cycle withcolor blue ;
708 ;; % Bottom black rectangle
709 ;; drawfill (0,0)--(2cm,0)--(2cm,-1cm)--(0,-1cm)--cycle withcolor black;
710 ;; % White Text
711 ;; draw btex \bf AB etex withcolor white ;
712 ;; % resize to size
713 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
714
715
716 (defvar ConTeXt-environment-helper
717 '(("useMPgraphic" . ConTeXt-mp-region)
718 ("MPpage" . ConTeXt-mp-region))
719 "Alist that holds functions to call for working on regions.
720 An entry looks like: (\"environment\" . function)")
721
722 (defun ConTeXt-mp-region ()
723 "Edit region in `metapost-mode'."
724 (ConTeXt-mark-environment t)
725 (narrow-to-region (mark) (point))
726 (metapost-mode)
727 (message "Type `M-x exit-recursive-edit' to get back")
728 (recursive-edit)
729 (context-mode)
730 (widen))
731
732 ;; find smarter name. Suggestions welcome
733 (defun ConTeXt-work-on-environment ()
734 "Takes current environment and does something on it (todo: documentation)."
735 (interactive)
736 (let ((fun (cdr (assoc (ConTeXt-current-environment)
737 ConTeXt-environment-helper))))
738 (when (functionp fun)
739 (funcall fun))))
740
741 (defun ConTeXt-current-environment ()
742 "Return the name of the current environment."
743 ;; don't make this interactive.
744 (let ((beg))
745 (save-excursion
746 (ConTeXt-last-unended-start)
747 (setq beg (+ (point) (length (ConTeXt-environment-start-name)) 1))
748 (goto-char (match-end 0))
749 (skip-chars-forward "a-zA-Z")
750 (buffer-substring beg (point)))))
751
752 (defun ConTeXt-last-unended-start ()
753 "Leave point at the beginning of the last `\\start...' that is unstopped looking from the current cursor."
754 (while (and (re-search-backward "\\\\start[a-zA-Z]*\\|\\\\stop[a-zA-Z]*")
755 (looking-at "\\\\stop[a-zA-Z]*"))
756 (ConTeXt-last-unended-start)))
757
758 (defun ConTeXt-mark-environment (&optional inner)
759 "Set mark to end of current environment (\\start...-\\stop...) and
760 point to the matching begin.
761 If optional INNER is not nil, include \\start... and \\stop, otherwise only
762 the contents."
763 (interactive)
764 (let ((cur (point)))
765 (ConTeXt-find-matching-stop inner)
766 (set-mark (point))
767 (goto-char cur)
768 (ConTeXt-find-matching-start inner)
769 (TeX-activate-region)))
770
771 (defun ConTeXt-find-matching-stop (&optional inner)
772 "Find end of current \\start...\\stop-Pair.
773 If INNER is non-nil, go to the point just past before
774 \\stop... macro. Otherwise goto the point just past \\stop..."
775 (interactive)
776 (let ((regexp (concat (regexp-quote TeX-esc)
777 "\\("
778 (ConTeXt-environment-start-name)
779 "\\|"
780 (ConTeXt-environment-stop-name)
781 "\\)"
782 ))
783 (level 1)
784 (pos))
785 ;;jump over the \start... when at the beginning of it.
786 (when (looking-at (concat (regexp-quote TeX-esc)
787 (ConTeXt-environment-start-name)))
788 (re-search-forward regexp nil t))
789 (while (and (> level 0)
790 (re-search-forward regexp nil t)
791 (goto-char (1- (match-beginning 1)))
792 (cond ((looking-at (concat (regexp-quote TeX-esc)
793 (ConTeXt-environment-start-name)))
794 (re-search-forward regexp nil t)
795 (setq level (1+ level)))
796 ((looking-at (concat (regexp-quote TeX-esc)
797 (ConTeXt-environment-stop-name)))
798 (re-search-forward regexp nil t)
799 (setq level (1- level))))))
800 ;; now we have to look if we want to start behind the \start... macro
801 (if inner
802 (beginning-of-line)
803 (skip-chars-forward "a-zA-Z"))))
804
805 (defun ConTeXt-find-matching-start (&optional inner)
806 "Find beginning of current \\start...\\stop-Pair.
807 If INNER is non-nil, go to the point just past the \\start... macro."
808 (interactive)
809 (let ((regexp (concat (regexp-quote TeX-esc)
810 "\\("
811 (ConTeXt-environment-start-name)
812 "\\|"
813 (ConTeXt-environment-stop-name)
814 "\\)"
815 ))
816 (level 1)
817 (pos))
818 (while (and (> level 0)
819 (re-search-backward regexp nil t)
820 (cond ((looking-at (concat (regexp-quote TeX-esc)
821 (ConTeXt-environment-stop-name)))
822 (setq level (1+ level)))
823 ((looking-at (concat (regexp-quote TeX-esc)
824 (ConTeXt-environment-start-name)))
825 (setq level (1- level))))))
826 ;; now we have to look if we want to start behind the \start... macro
827 (when inner
828 ;; \startfoo can have 0 or more {} and [] pairs. I assume that
829 ;; skipping all those parens will be smart enough. It fails when
830 ;; the first part in the \start-\stop-environment is { or [, like
831 ;; in \startquotation {\em important} \stopquotation. There is
832 ;; yet another pitfall: \startsetups SomeSetup foo bar
833 ;; \stopsetups will use SomeSetup as the argument and the
834 ;; environment
835 (skip-chars-forward "\\\\a-zA-Z")
836 (save-excursion
837 (while (progn
838 (skip-chars-forward "\t\n ")
839 (forward-comment 1)
840 (skip-chars-forward "\t\n ")
841 (looking-at "\\s\("))
842 (forward-list 1)
843 (setq pos (point))))
844 (when pos
845 (goto-char pos))
846 (unless (bolp)
847 (forward-line)))))
848
849 ;;; items
850
851 (defun ConTeXt-insert-item ()
852 "Insert a new item."
853 (interactive "*")
854 (or (TeX-looking-at-backward "^[ \t]*")
855 (newline))
856 (TeX-insert-macro "item")
857 (indent-according-to-mode))
858
859
860 ;;; Macro Argument Hooks
861
862 (defun ConTeXt-optional-argument-insert (arg &optional prefix)
863 "Insert ARG surrounded by square brackets."
864 (insert ConTeXt-optop)
865 (insert arg)
866 (insert ConTeXt-optcl))
867
868 (defun ConTeXt-required-argument-insert (arg &optional prefix)
869 "Insert ARG surrounded by curly braces."
870 (insert TeX-grop)
871 (insert arg)
872 (insert TeX-grcl))
873
874 (defun ConTeXt-argument-insert (arg optional &optional prefix)
875 "Insert ARG surrounded by curly braces.
876
877 If OPTIONAL, only insert it if not empty, and then use square brackets."
878 (if optional
879 (if
880 (not (string-equal arg ""))
881 (ConTeXt-optional-argument-insert arg prefix))
882 (ConTeXt-required-argument-insert arg prefix)))
883
884 (defun ConTeXt-arg-ref (optional &optional prompt definition)
885 "Prompt for a reference completing with known references."
886 (let ((ref (completing-read (TeX-argument-prompt optional prompt "ref")
887 (LaTeX-label-list))))
888 (if (and definition (not (string-equal "" ref)))
889 (LaTeX-add-labels ref))
890 (ConTeXt-argument-insert ref optional)))
891
892 (defun ConTeXt-arg-define-ref (&optional prompt)
893 "Prompt for an optional reference completing with known references."
894 (ConTeXt-arg-ref t prompt t))
895
896 (defun ConTeXt-arg-setup (optional &optional prompt)
897 "Prompt for setup arguments."
898 (let ((setup (read-from-minibuffer
899 (TeX-argument-prompt optional prompt "Setup"))))
900 (ConTeXt-argument-insert setup t)))
901
902
903 ;; paragraph (re)-formatting
904
905 (defvar ConTeXt-item-list ()
906 "List of macro's considered items.")
907
908 (defun ConTeXt-paragraph-commands-regexp ()
909 "Return a regexp matching macros that should have their own line."
910 (concat
911 (regexp-quote TeX-esc) "\\("
912 "[][]\\|" ; display math delimitors (is this applicable to ConTeXt??)
913 (ConTeXt-environment-start-name) "\\|"
914 (ConTeXt-environment-stop-name) "\\|"
915 (mapconcat 'car ConTeXt-section-list "\\b\\|") "\\b\\|"
916 (mapconcat 'identity ConTeXt-extra-paragraph-commands "\\b\\|")
917 "\\b\\|"
918 (mapconcat 'identity ConTeXt-item-list "\\b\\|") "\\b\\)"))
919
920
921 ;; Outline support
922
923 (defun ConTeXt-environment-full-start-name (environment)
924 "Return the ConTeXt macro name that starts ENVIRONMENT.
925 It is interface aware"
926 (concat (ConTeXt-environment-start-name) environment))
927
928 (defun ConTeXt-outline-regexp (&optional anywhere)
929 "Return regexp for ConTeXt section blocks and sections.
930
931 If optional argument ANYWHERE is not nil, do not require that the
932 header is at the start of a line."
933 (concat
934 (if anywhere "" "^")
935 "[ \t]*"
936 (regexp-quote TeX-esc)
937 "\\("
938 (mapconcat 'ConTeXt-environment-full-start-name ConTeXt-section-block-list "\\|") "\\|"
939 (mapconcat 'car ConTeXt-section-list "\\|")
940 "\\)\\b"
941 (if TeX-outline-extra
942 "\\|"
943 "")
944 (mapconcat 'car TeX-outline-extra "\\|")
945 "\\|" (ConTeXt-header-end) "\\b"
946 "\\|" (ConTeXt-trailer-start) "\\b"))
947
948 (defvar ConTeXt-text "Name of ConTeXt macro that begins the text body.")
949
950 (defun ConTeXt-header-end ()
951 "Default end of header marker for ConTeXt documents."
952 (concat
953 (regexp-quote TeX-esc)
954 (ConTeXt-environment-start-name)
955 ConTeXt-text))
956
957 (defun ConTeXt-trailer-start ()
958 "Default start of trailer marker for ConTeXt documents."
959 (concat
960 (regexp-quote TeX-esc)
961 (ConTeXt-environment-stop-name)
962 ConTeXt-text))
963
964 (defun ConTeXt-outline-offset ()
965 "Offset to add to `ConTeXt-section-list' levels to get outline level."
966 (- 4 (ConTeXt-largest-level)))
967
968 (defun ConTeXt-start-environment-regexp (list)
969 "Regular expression that matches a start of all environments mentioned in LIST."
970 (concat
971 "start\\("
972 (mapconcat 'identity list "\\|")
973 "\\)\\b"))
974
975 ;; The top headings are \starttext, \startfrontmatter, \startbodymatter etc.
976 ;; \part, \chapter etc. are children of that.
977 (defun ConTeXt-outline-level ()
978 "Find the level of current outline heading in an ConTeXt document."
979 (cond ((looking-at (concat (ConTeXt-header-end) "\\b")) 1)
980 ((looking-at (concat (ConTeXt-trailer-start) "\\b")) 1)
981 ((TeX-look-at TeX-outline-extra)
982 (max 1 (+ (TeX-look-at TeX-outline-extra)
983 (ConTeXt-outline-offset))))
984 (t
985 (save-excursion
986 (skip-chars-forward " \t")
987 (forward-char 1)
988 (cond ((looking-at (ConTeXt-start-environment-regexp
989 ConTeXt-section-block-list)) 1)
990 ((TeX-look-at ConTeXt-section-list)
991 (max 1 (+ (TeX-look-at ConTeXt-section-list)
992 (ConTeXt-outline-offset))))
993 (t
994 (error "Unrecognized header")))))))
995
996
997 ;;; Fonts
998
999 (defcustom ConTeXt-font-list '((?\C-b "{\\bf " "}")
1000 (?\C-c "{\\sc " "}")
1001 (?\C-e "{\\em " "}")
1002 (?\C-i "{\\it " "}")
1003 (?\C-r "{\\rm " "}")
1004 (?\C-s "{\\sl " "}")
1005 (?\C-t "{\\tt " "}")
1006 (?\C-d "" "" t))
1007 "List of fonts used by `TeX-font'.
1008
1009 Each entry is a list.
1010 The first element is the key to activate the font.
1011 The second element is the string to insert before point, and the third
1012 element is the string to insert after point.
1013 If the fourth and fifth element are strings, they specify the prefix and
1014 suffix to be used in math mode.
1015 An optional fourth (or sixth) element means always replace if t."
1016 :group 'TeX-macro
1017 :type '(repeat
1018 (group
1019 :value (?\C-a "" "")
1020 (character :tag "Key")
1021 (string :tag "Prefix")
1022 (string :tag "Suffix")
1023 (option (group
1024 :inline t
1025 (string :tag "Math Prefix")
1026 (string :tag "Math Suffix")))
1027 (option (sexp :format "Replace\n" :value t)))))
1028
1029
1030 ;; Imenu support
1031
1032 (defun ConTeXt-outline-name ()
1033 "Guess a name for the current header line."
1034 (save-excursion
1035 (if (re-search-forward "{\\([^\}]*\\)}" (point-at-eol) t)
1036 (match-string 1)
1037 (buffer-substring-no-properties (point) (point-at-eol)))))
1038
1039 ;; This imenu also includes commented out chapters. Perhaps a feature
1040 ;; for LaTeX, not sure we want or need that for ConTeXt.
1041
1042 (defun ConTeXt-imenu-create-index-function ()
1043 "Imenu support function for ConTeXt."
1044 (TeX-update-style)
1045 (let (entries level (regexp (ConTeXt-outline-regexp)))
1046 (goto-char (point-max))
1047 (while (re-search-backward regexp nil t)
1048 (let* ((name (ConTeXt-outline-name))
1049 (level (make-string (1- (ConTeXt-outline-level)) ?\ ))
1050 (label (concat level level name))
1051 (mark (make-marker)))
1052 (set-marker mark (point))
1053 (set-text-properties 0 (length label) nil label)
1054 (setq entries (cons (cons label mark) entries))))
1055 entries))
1056
1057
1058 ;; Indentation, copied from Berend's context mode.
1059 ;; TODO: doesn't work great.
1060
1061 (defvar ConTeXt-indent-allhanging t)
1062 (defvar ConTeXt-indent-arg 2)
1063 (defvar ConTeXt-indent-basic 2)
1064 (defvar ConTeXt-indent-item ConTeXt-indent-basic)
1065 (defvar ConTeXt-indent-item-re "\\\\\\(item\\|sym\\)\\>")
1066
1067 (defvar ConTeXt-indent-syntax-table (make-syntax-table TeX-mode-syntax-table)
1068 "Syntax table used while computing indentation.")
1069
1070 (progn
1071 (modify-syntax-entry ?$ "." ConTeXt-indent-syntax-table)
1072 (modify-syntax-entry ?\( "." ConTeXt-indent-syntax-table)
1073 (modify-syntax-entry ?\) "." ConTeXt-indent-syntax-table))
1074
1075 (defun ConTeXt-indent-line (&optional arg)
1076 (with-syntax-table ConTeXt-indent-syntax-table
1077 ;; TODO: Rather than ignore $, we should try to be more clever about it.
1078 (let ((indent
1079 (save-excursion
1080 (beginning-of-line)
1081 (ConTeXt-find-indent))))
1082 (if (< indent 0) (setq indent 0))
1083 (if (<= (current-column) (current-indentation))
1084 (indent-line-to indent)
1085 (save-excursion (indent-line-to indent))))))
1086
1087 (defun ConTeXt-find-indent (&optional virtual)
1088 "Find the proper indentation of text after point.
1089 VIRTUAL if non-nil indicates that we're only trying to find the
1090 indentation in order to determine the indentation of something
1091 else. There might be text before point."
1092 (save-excursion
1093 (skip-chars-forward " \t")
1094 (or
1095 ;; Trust the current indentation, if such info is applicable.
1096 (and virtual (>= (current-indentation) (current-column))
1097 (current-indentation))
1098 ;; Put leading close-paren where the matching open brace would be.
1099 (condition-case nil
1100 (and (eq (char-syntax (char-after)) ?\))
1101 (save-excursion
1102 (skip-syntax-forward " )")
1103 (backward-sexp 1)
1104 (ConTeXt-find-indent 'virtual)))
1105 (error nil))
1106 ;; Default (maybe an argument)
1107 (let ((pos (point))
1108 (char (char-after))
1109 (indent 0)
1110 up-list-pos)
1111 ;; Look for macros to be outdented
1112 (cond ((looking-at (concat (regexp-quote TeX-esc)
1113 (ConTeXt-environment-stop-name)))
1114 (setq indent (- indent ConTeXt-indent-basic)))
1115 ((looking-at ConTeXt-indent-item-re)
1116 (setq indent (- indent ConTeXt-indent-item))))
1117 ;; Find the previous point which determines our current indentation.
1118 (condition-case err
1119 (progn
1120 (backward-sexp 1)
1121 (while (> (current-column) (current-indentation))
1122 (backward-sexp 1)))
1123 (scan-error
1124 (setq up-list-pos (nth 2 err))))
1125 (cond
1126 ((= (point-min) pos) 0) ; We're really just indenting the first line.
1127 ((integerp up-list-pos)
1128 ;; Have to indent relative to the open-paren.
1129 (goto-char up-list-pos)
1130 (if (and (not ConTeXt-indent-allhanging)
1131 (> pos (progn (down-list 1)
1132 (forward-comment (point-max))
1133 (point))))
1134 ;; Align with the first element after the open-paren.
1135 (current-column)
1136 ;; We're the first element after a hanging brace.
1137 (goto-char up-list-pos)
1138 (+ indent ConTeXt-indent-basic (ConTeXt-find-indent 'virtual))))
1139 ;; We're now at the "beginning" of a line.
1140 ((not (and (not virtual) (eq (char-after) ?\\)))
1141 ;; Nothing particular here: just keep the same indentation.
1142 (+ indent (current-column)))
1143 ;; We're now looking at an item.
1144 ((looking-at ConTeXt-indent-item-re)
1145 ;; Indenting relative to an item, have to re-add the outdenting.
1146 (+ indent (current-column) ConTeXt-indent-item))
1147 ;; We're looking at an environment starter.
1148 ((and (looking-at (concat (regexp-quote TeX-esc)
1149 (ConTeXt-environment-start-name)))
1150 (not (looking-at (concat (regexp-quote TeX-esc)
1151 (ConTeXt-environment-start-name)
1152 ConTeXt-text)))) ; other environments?
1153 (+ indent (current-column) ConTeXt-indent-basic))
1154 (t
1155 (let ((col (current-column)))
1156 (if (not (and char (eq (char-syntax char) ?\()))
1157 ;; If the first char was not an open-paren, there's
1158 ;; a risk that this is really not an argument to the
1159 ;; macro at all.
1160 (+ indent col)
1161 (forward-sexp 1)
1162 (if (< (line-end-position)
1163 (save-excursion (forward-comment (point-max))
1164 (point)))
1165 ;; we're indenting the first argument.
1166 (min (current-column) (+ ConTeXt-indent-arg col))
1167 (skip-syntax-forward " ")
1168 (current-column))))))))))
1169
1170
1171 ;; XML inside ConTeXt support
1172
1173 (defun ConTeXt-last-unended-start-xml ()
1174 "Leave point at the beginning of the last `tag' that is unstopped."
1175 (while (and (re-search-backward "<[_A-Za-z][-:._A-Za-z0-9]*\\([ \t\r\n]\\|[_A-Za-z][-:._A-Za-z0-9]*\=\"[^\"]*\"\\)*>\\|</[_A-Za-z][-:_A-Za-z0-9]*>")
1176 (looking-at "</[_A-Za-z][-:._A-Za-z0-9]*>"))
1177 (ConTeXt-last-unended-start-xml)))
1178
1179 (defun ConTeXt-close-xml-tag ()
1180 "Create an </...> to match the last unclosed <...>. Not fool-proof."
1181 (interactive "*")
1182 (let ((new-line-needed (bolp)) text indentation)
1183 (save-excursion
1184 (condition-case nil
1185 (ConTeXt-last-unended-start-xml)
1186 (error (error "Couldn't find unended XML tag")))
1187 (setq indentation (current-column))
1188 (re-search-forward "<\\([_A-Za-z][-:._A-Za-z0-9]*\\)")
1189 (setq text (buffer-substring (match-beginning 1) (match-end 1))))
1190 (indent-to indentation)
1191 (insert "</" text ">")
1192 (if new-line-needed (insert ?\n))))
1193
1194
1195 ;; Key bindings
1196
1197 (defvar ConTeXt-mode-map
1198 (let ((map (make-sparse-keymap)))
1199 (set-keymap-parent map TeX-mode-map)
1200
1201 (define-key map "\e\C-a" 'ConTeXt-find-matching-start)
1202 (define-key map "\e\C-e" 'ConTeXt-find-matching-stop)
1203 ;; likely to change in the future
1204 (define-key map "\C-c!" 'ConTeXt-work-on-environment)
1205 (define-key map "\C-c\C-e" 'ConTeXt-environment)
1206 (define-key map "\C-c\n" 'ConTeXt-insert-item)
1207 (or (key-binding "\e\r")
1208 (define-key map "\e\r" 'ConTeXt-insert-item)) ;*** Alias
1209 (define-key map "\C-c]" 'ConTeXt-close-environment)
1210 (define-key map "\C-c\C-s" 'ConTeXt-section)
1211 ;; XML in ConTeXt support
1212 (define-key map "\C-c/" 'ConTeXt-close-xml-tag)
1213 map)
1214 "Keymap used in `ConTeXt-mode'.")
1215
1216
1217 ;;; Menu building
1218
1219 ;; functions to create menu entries
1220
1221 ;; ConTeXt \start... \stop... pairs
1222 ;; (Choose a different name than the one in LaTeX mode. Otherwise the
1223 ;; contents of the "Insert Environment" and "Change Environment" menus
1224 ;; will not be updated correctly upon loading and switching between
1225 ;; LaTeX and ConTeXt files. AFAICS this is due to a bug in
1226 ;; easymenu.el not returning the correct keymap when
1227 ;; `easy-menu-change' (and therefore `easy-menu-get-map') is called.
1228 ;; It just sees an entry with a matching name and returns this first
1229 ;; match.)
1230 (defvar ConTeXt-environment-menu-name "Insert Environment (C-c C-e)")
1231
1232 (defun ConTeXt-environment-menu-entry (entry)
1233 "Create an entry for the environment menu."
1234 (vector (car entry) (list 'ConTeXt-environment-menu (car entry)) t))
1235
1236 (defvar ConTeXt-environment-modify-menu-name "Change Environment (C-u C-c C-e)")
1237
1238 (defun ConTeXt-environment-modify-menu-entry (entry)
1239 "Create an entry for the change environment menu."
1240 (vector (car entry) (list 'ConTeXt-modify-environment (car entry)) t))
1241
1242 ;; ConTeXt define macros
1243 (defvar ConTeXt-define-menu-name "Define")
1244
1245 (defun ConTeXt-define-menu-entry (entry)
1246 "Create an entry for the define menu."
1247 (vector entry (list 'ConTeXt-define-menu entry)))
1248
1249 (defun ConTeXt-define-menu (define)
1250 "Insert DEFINE from menu."
1251 (ConTeXt-insert-define define))
1252
1253 ;; ConTeXt setup macros
1254 (defvar ConTeXt-setup-menu-name "Setup")
1255
1256 (defun ConTeXt-setup-menu-entry (entry)
1257 "Create an entry for the setup menu."
1258 (vector entry (list 'ConTeXt-setup-menu entry)))
1259
1260 (defun ConTeXt-setup-menu (setup)
1261 "Insert SETUP from menu."
1262 (ConTeXt-insert-setup setup))
1263
1264 ;; ConTeXt referencing macros
1265 (defvar ConTeXt-referencing-menu-name "Referencing")
1266
1267 (defun ConTeXt-referencing-menu-entry (entry)
1268 "Create an entry for the referencing menu."
1269 (vector entry (list 'ConTeXt-referencing-menu entry)))
1270
1271 (defun ConTeXt-referencing-menu (referencing)
1272 "Insert REFERENCING from menu."
1273 (ConTeXt-insert-referencing referencing))
1274
1275 ;; ConTeXt other macros
1276 (defvar ConTeXt-other-macro-menu-name "Other macro")
1277
1278 (defun ConTeXt-other-macro-menu-entry (entry)
1279 "Create an entry for the other macro menu."
1280 (vector entry (list 'ConTeXt-other-macro-menu entry)))
1281
1282 (defun ConTeXt-other-macro-menu (other-macro)
1283 "Insert OTHER MACRO from menu."
1284 (ConTeXt-insert-other-macro other-macro))
1285
1286
1287 ;; meta-structure project structure menu entries
1288
1289 (defvar ConTeXt-project-structure-menu-name "Project Structure")
1290
1291 (defun ConTeXt-project-structure-menu (project-structure)
1292 "Insert project structure from menu."
1293 (ConTeXt-project-structure
1294 (let ((l ConTeXt-project-structure-list))
1295 (- (length l) (length (member project-structure l))))))
1296
1297 (defun ConTeXt-project-structure-menu-entry (entry)
1298 "Create an ENTRY for the project structure menu."
1299 (vector entry (list 'ConTeXt-project-structure-menu entry)))
1300
1301
1302 ;; meta-structure section blocks menu entries
1303
1304 (defvar ConTeXt-section-block-menu-name "Section Block")
1305
1306 (defun ConTeXt-section-block-menu (section-block)
1307 "Insert section block from menu."
1308 (ConTeXt-section-block section-block))
1309
1310 (defun ConTeXt-section-block-menu-entry (entry)
1311 "Create an ENTRY for the section block menu."
1312 (vector entry (list 'ConTeXt-section-block-menu entry)))
1313
1314
1315 ;; section menu entries
1316
1317 (defvar ConTeXt-section-menu-name "Section (C-c C-s)")
1318
1319 (defun ConTeXt-section-enable-symbol (level)
1320 "Symbol used to enable section LEVEL in the menu bar."
1321 (intern (concat "ConTeXt-section-" (int-to-string level) "-enable")))
1322
1323 (defun ConTeXt-section-enable (entry)
1324 "Enable or disable section ENTRY from `ConTeXt-section-list'."
1325 (let ((level (nth 1 entry)))
1326 (set (ConTeXt-section-enable-symbol level)
1327 (>= level ConTeXt-largest-level))))
1328
1329 (defun ConTeXt-section-menu (level)
1330 "Insert section from menu."
1331 (let ((ConTeXt-section-hook (delq 'ConTeXt-section-heading
1332 (copy-sequence ConTeXt-section-hook))))
1333 (ConTeXt-section level)))
1334
1335 (defun ConTeXt-section-menu-entry (entry)
1336 "Create an ENTRY for the section menu."
1337 (let ((enable (ConTeXt-section-enable-symbol (nth 1 entry))))
1338 (set enable t)
1339 (vector (car entry) (list 'ConTeXt-section-menu (nth 1 entry)) enable)))
1340
1341
1342 ;; etexshow support
1343
1344 (defun ConTeXt-etexshow ()
1345 "Call etexshow, if available, to show the definition of a ConText macro."
1346 (interactive)
1347 (if (fboundp 'etexshow)
1348 (let ()
1349 (require 'etexshow)
1350 (funcall (symbol-function 'etexshow-cmd)))
1351 (error "etexshow is not installed. Get it from http://levana.de/emacs/")))
1352
1353 ;; menu itself
1354
1355 (easy-menu-define ConTeXt-mode-command-menu
1356 ConTeXt-mode-map
1357 "Command menu used in ConTeXt mode."
1358 (TeX-mode-specific-command-menu 'context-mode))
1359
1360 ;; it seems the menu is evaluated at compile/load-time
1361 ;; we don't have ConTeXt-current-interface at that time
1362 ;; so make sure to do updates based on that variable in
1363 ;; ConTeXt-menu-update
1364 (easy-menu-define ConTeXt-mode-menu
1365 ConTeXt-mode-map
1366 "Menu used in ConTeXt mode."
1367 (TeX-menu-with-help
1368 `("ConTeXt"
1369 (,ConTeXt-project-structure-menu-name)
1370 (,ConTeXt-section-block-menu-name)
1371 (,ConTeXt-section-menu-name)
1372 ["Add Table of Contents to Emacs Menu" (imenu-add-to-menubar "TOC") t]
1373 "-"
1374 ["Macro ..." TeX-insert-macro
1375 :help "Insert a macro and possibly arguments"]
1376 ["Complete" TeX-complete-symbol
1377 :help "Complete the current macro or environment name"]
1378 ["Show ConTeXt Macro Definition" ConTeXt-etexshow]
1379 "-"
1380 (,ConTeXt-environment-menu-name)
1381 (,ConTeXt-environment-modify-menu-name)
1382 ["Item" ConTeXt-insert-item
1383 :help "Insert a new \\item into current environment"]
1384 (,ConTeXt-define-menu-name)
1385 (,ConTeXt-setup-menu-name)
1386 (,ConTeXt-other-macro-menu-name)
1387 "-"
1388 ("Insert Font"
1389 ["Emphasize" (TeX-font nil ?\C-e) :keys "C-c C-f C-e"]
1390 ["Bold" (TeX-font nil ?\C-b) :keys "C-c C-f C-b"]
1391 ["Typewriter" (TeX-font nil ?\C-t) :keys "C-c C-f C-t"]
1392 ["Small Caps" (TeX-font nil ?\C-c) :keys "C-c C-f C-c"]
1393 ["Sans Serif" (TeX-font nil ?\C-f) :keys "C-c C-f C-f"]
1394 ["Italic" (TeX-font nil ?\C-i) :keys "C-c C-f C-i"]
1395 ["Slanted" (TeX-font nil ?\C-s) :keys "C-c C-f C-s"]
1396 ["Roman" (TeX-font nil ?\C-r) :keys "C-c C-f C-r"]
1397 ["Calligraphic" (TeX-font nil ?\C-a) :keys "C-c C-f C-a"])
1398 ("Replace Font"
1399 ["Emphasize" (TeX-font t ?\C-e) :keys "C-u C-c C-f C-e"]
1400 ["Bold" (TeX-font t ?\C-b) :keys "C-u C-c C-f C-b"]
1401 ["Typewriter" (TeX-font t ?\C-t) :keys "C-u C-c C-f C-t"]
1402 ["Small Caps" (TeX-font t ?\C-c) :keys "C-u C-c C-f C-c"]
1403 ["Sans Serif" (TeX-font t ?\C-f) :keys "C-u C-c C-f C-f"]
1404 ["Italic" (TeX-font t ?\C-i) :keys "C-u C-c C-f C-i"]
1405 ["Slanted" (TeX-font t ?\C-s) :keys "C-u C-c C-f C-s"]
1406 ["Roman" (TeX-font t ?\C-r) :keys "C-u C-c C-f C-r"]
1407 ["Calligraphic" (TeX-font t ?\C-a) :keys "C-u C-c C-f C-a"])
1408 ["Delete Font" (TeX-font t ?\C-d) :keys "C-c C-f C-d"]
1409 "-"
1410 ["Comment or Uncomment Region"
1411 TeX-comment-or-uncomment-region
1412 :help "Make the selected region outcommented or active again"]
1413 ["Comment or Uncomment Paragraph"
1414 TeX-comment-or-uncomment-paragraph
1415 :help "Make the current paragraph outcommented or active again"]
1416 ,TeX-fold-menu
1417 "-" . ,TeX-common-menu-entries)))
1418
1419 (defun ConTeXt-menu-update (&optional menu)
1420 "Update entries on AUCTeX menu."
1421 (or (not (memq major-mode '(context-mode)))
1422 (null ConTeXt-menu-changed)
1423 (not (fboundp 'easy-menu-change))
1424 (progn
1425 (TeX-update-style)
1426 (setq ConTeXt-menu-changed nil)
1427 (message "Updating section menu...")
1428 (mapc 'ConTeXt-section-enable ConTeXt-section-list)
1429 (message "Updating environment menu...")
1430 (easy-menu-change '("ConTeXt") ConTeXt-environment-menu-name
1431 (LaTeX-split-long-menu
1432 (mapcar 'ConTeXt-environment-menu-entry
1433 (ConTeXt-environment-list))))
1434 (message "Updating modify environment menu...")
1435 (easy-menu-change '("ConTeXt") ConTeXt-environment-modify-menu-name
1436 (LaTeX-split-long-menu
1437 (mapcar 'ConTeXt-environment-modify-menu-entry
1438 (ConTeXt-environment-list))))
1439 (message "Updating define menu...")
1440 (easy-menu-change '("ConTeXt") ConTeXt-define-menu-name
1441 (LaTeX-split-long-menu
1442 (mapcar 'ConTeXt-define-menu-entry
1443 ConTeXt-define-list)))
1444 (message "Updating setup menu...")
1445 (easy-menu-change '("ConTeXt") ConTeXt-setup-menu-name
1446 (LaTeX-split-long-menu
1447 (mapcar 'ConTeXt-setup-menu-entry
1448 ConTeXt-setup-list)))
1449 (message "Updating referencing menu...")
1450 (easy-menu-change '("ConTeXt") ConTeXt-referencing-menu-name
1451 (LaTeX-split-long-menu
1452 (mapcar 'ConTeXt-referencing-menu-entry
1453 ConTeXt-referencing-list)))
1454 (message "Updating other macro's menu...")
1455 (easy-menu-change '("ConTeXt") ConTeXt-other-macro-menu-name
1456 (LaTeX-split-long-menu
1457 (mapcar 'ConTeXt-other-macro-menu-entry
1458 ConTeXt-other-macro-list)))
1459 (message "Updating project structure menu...")
1460 (easy-menu-change '("ConTeXt") ConTeXt-project-structure-menu-name
1461 (LaTeX-split-long-menu
1462 (mapcar 'ConTeXt-project-structure-menu-entry
1463 ConTeXt-project-structure-list)))
1464 (message "Updating section block menu...")
1465 (easy-menu-change '("ConTeXt") ConTeXt-section-block-menu-name
1466 (LaTeX-split-long-menu
1467 (mapcar 'ConTeXt-section-block-menu-entry
1468 ConTeXt-section-block-list)))
1469 (message "Updating section menu...")
1470 (easy-menu-change '("ConTeXt") ConTeXt-section-menu-name
1471 (LaTeX-split-long-menu
1472 (mapcar 'ConTeXt-section-menu-entry
1473 ConTeXt-section-list)))
1474 (message "Updating...done")
1475 (and menu (easy-menu-return-item ConTeXt-mode-menu menu))
1476 )))
1477
1478 ;;; Option expander
1479
1480 (defvar ConTeXt-texexec-option-nonstop "--nonstop "
1481 "Command line option for texexec to use nonstopmode.")
1482
1483 (defun ConTeXt-expand-options ()
1484 "Expand options for texexec command."
1485 (concat
1486 (let ((engine (eval (nth 4 (assq TeX-engine (TeX-engine-alist))))))
1487 (when engine
1488 (format "--engine=%s " engine)))
1489 (unless (eq ConTeXt-current-interface "en")
1490 (format "--interface=%s " ConTeXt-current-interface))
1491 (when TeX-source-correlate-mode
1492 (format "--passon=\"%s\" "
1493 (if (eq TeX-source-correlate-method-active 'synctex)
1494 TeX-synctex-tex-flags
1495 TeX-source-specials-tex-flags)))
1496 (unless TeX-interactive-mode
1497 ConTeXt-texexec-option-nonstop)))
1498
1499 ;;; Mode
1500
1501 ;; ConTeXt variables that are interface aware
1502 ;; They are mapped to interface specific variables
1503
1504 (defvar ConTeXt-language-variable-list
1505 '(ConTeXt-define-list
1506 ConTeXt-setup-list
1507 ConTeXt-referencing-list
1508 ConTeXt-other-macro-list
1509 ConTeXt-project-structure-list
1510 ConTeXt-section-block-list
1511 ConTeXt-section-list
1512 ConTeXt-text
1513 ConTeXt-item-list
1514 ConTeXt-extra-paragraph-commands))
1515
1516 (defcustom ConTeXt-clean-intermediate-suffixes
1517 ;; See *suffixes in texutil.pl.
1518 '("\\.tui" "\\.tup" "\\.ted" "\\.tes" "\\.top" "\\.log" "\\.tmp" "\\.run"
1519 "\\.bck" "\\.rlg" "\\.mpt" "\\.mpx" "\\.mpd" "\\.mpo" "\\.tuo" "\\.tub"
1520 "\\.top" "-mpgraph\\.mp" "-mpgraph\\.mpd" "-mpgraph\\.mpo" "-mpgraph\\.mpy"
1521 "-mprun\\.mp" "-mprun\\.mpd" "-mprun\\.mpo" "-mprun\\.mpy")
1522 "List of regexps matching suffixes of files to be deleted.
1523 The regexps will be anchored at the end of the file name to be matched,
1524 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
1525 :type '(repeat regexp)
1526 :group 'TeX-command)
1527
1528 (defcustom ConTeXt-clean-output-suffixes
1529 '("\\.dvi" "\\.pdf" "\\.ps")
1530 "List of regexps matching suffixes of files to be deleted.
1531 The regexps will be anchored at the end of the file name to be matched,
1532 i.e. you do _not_ have to cater for this yourself by adding \\\\' or $."
1533 :type '(repeat regexp)
1534 :group 'TeX-command)
1535
1536 (TeX-abbrev-mode-setup context-mode)
1537
1538 (defun ConTeXt-mode-common-initialization ()
1539 "Initialization code that is common for all ConTeXt interfaces."
1540 ;; `plain-TeX-common-initialization' kills all local variables, but
1541 ;; we need to keep ConTeXt-current-interface, so save and restore
1542 ;; it.
1543 (let (save-ConTeXt-current-interface)
1544 (setq save-ConTeXt-current-interface ConTeXt-current-interface)
1545 (plain-TeX-common-initialization)
1546 (setq ConTeXt-current-interface save-ConTeXt-current-interface))
1547 (setq major-mode 'context-mode)
1548
1549 (setq local-abbrev-table context-mode-abbrev-table)
1550
1551 ;; Make language specific variables buffer local
1552 (dolist (symbol ConTeXt-language-variable-list)
1553 (make-variable-buffer-local symbol))
1554
1555 (require (intern (concat "context-" ConTeXt-current-interface)))
1556 (dolist (symbol ConTeXt-language-variable-list)
1557 (set symbol (symbol-value (intern (concat (symbol-name symbol) "-"
1558 ConTeXt-current-interface)))))
1559
1560 ;; Create certain regular expressions based on language
1561 (setq ConTeXt-indent-item-re (concat "\\\\\\(" (mapconcat 'identity ConTeXt-item-list "\\|") "\\)\\>"))
1562
1563 ;; What's the deepest level at we can collapse a document?
1564 ;; set only if user has not set it. Need to be set before menu is created.
1565 ;; level 2 is "section"
1566 (or ConTeXt-largest-level
1567 (setq ConTeXt-largest-level 2))
1568
1569 ;; keybindings
1570 (use-local-map ConTeXt-mode-map)
1571
1572 ;; Indenting
1573 (set (make-local-variable 'indent-line-function) 'ConTeXt-indent-line)
1574 (set (make-local-variable 'fill-indent-according-to-mode) t)
1575
1576 ;; Paragraph formatting
1577 (set (make-local-variable 'LaTeX-syntactic-comments) nil)
1578 (set (make-local-variable 'LaTeX-paragraph-commands-regexp)
1579 (ConTeXt-paragraph-commands-regexp))
1580 (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
1581 (set (make-local-variable 'fill-paragraph-function) 'LaTeX-fill-paragraph)
1582 (set (make-local-variable 'adaptive-fill-mode) nil)
1583 (setq paragraph-start
1584 (concat
1585 "[ \t]*\\("
1586 (ConTeXt-paragraph-commands-regexp) "\\|"
1587 "\\$\\$\\|" ; Plain TeX display math
1588 "$\\)"))
1589 (setq paragraph-separate
1590 (concat
1591 "[ \t]*\\("
1592 "\\$\\$" ; Plain TeX display math
1593 "\\|$\\)"))
1594
1595 ;; Keybindings and menu
1596 (use-local-map ConTeXt-mode-map)
1597 (easy-menu-add ConTeXt-mode-menu ConTeXt-mode-map)
1598 (easy-menu-add ConTeXt-mode-command-menu ConTeXt-mode-map)
1599 (setq ConTeXt-menu-changed t)
1600
1601 (if (= emacs-major-version 20)
1602 (make-local-hook 'activate-menubar-hook))
1603 (add-hook 'activate-menubar-hook 'ConTeXt-menu-update nil t)
1604
1605 ;; Outline support
1606 (require 'outline)
1607 (set (make-local-variable 'outline-level) 'ConTeXt-outline-level)
1608 (set (make-local-variable 'outline-regexp) (ConTeXt-outline-regexp t))
1609 ;;(make-local-variable 'outline-heading-end-regexp)
1610 (setq TeX-header-end (ConTeXt-header-end)
1611 TeX-trailer-start (ConTeXt-trailer-start))
1612
1613 ;; font switch support
1614 (set (make-local-variable 'TeX-font-list) ConTeXt-font-list)
1615
1616 ;; imenu support
1617 (set (make-local-variable 'imenu-create-index-function)
1618 'ConTeXt-imenu-create-index-function)
1619
1620 ;; run hooks
1621 (setq TeX-command-default "ConTeXt")
1622 (setq TeX-sentinel-default-function 'TeX-ConTeXt-sentinel)
1623 (TeX-run-mode-hooks 'text-mode-hook 'TeX-mode-hook 'ConTeXt-mode-hook))
1624
1625 (defun context-guess-current-interface ()
1626 "Guess what ConTeXt interface the current buffer is using."
1627 (interactive)
1628 (save-excursion
1629 (goto-char (point-min))
1630 (setq ConTeXt-current-interface
1631 (cond ((re-search-forward "%.*?interface=en" (+ 512 (point)) t)
1632 "en")
1633 ((re-search-forward "%.*?interface=nl" (+ 512 (point)) t)
1634 "nl")
1635 ((re-search-forward "\\\\starttext" (+ 1024 (point)) t)
1636 "en")
1637 ((re-search-forward "\\\\starttekst" (+ 1024 (point)) t)
1638 "nl")
1639 (t
1640 ConTeXt-default-interface)))))
1641
1642 ;;;###autoload
1643 (defalias 'ConTeXt-mode 'context-mode)
1644
1645 ;;;###autoload
1646 (defun context-mode ()
1647 "Major mode in AUCTeX for editing ConTeXt files.
1648
1649 Special commands:
1650 \\{ConTeXt-mode-map}
1651
1652 Entering `context-mode' calls the value of `text-mode-hook',
1653 then the value of `TeX-mode-hook', and then the value
1654 of context-mode-hook."
1655 (interactive)
1656 (context-guess-current-interface)
1657 (require (intern (concat "context-" ConTeXt-current-interface)))
1658 (funcall (intern (concat "context-" ConTeXt-current-interface "-mode"))))
1659
1660 (provide 'context)
1661
1662 ;;; context.el ends here