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