1 ;;; context.el --- Support for ConTeXt documents.
3 ;; Copyright (C) 2003-2006, 2008, 2010, 2012
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: Berend de Boer <berend@pobox.com>
9 ;; This file is part of AUCTeX.
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)
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.
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
28 ;; This is in progress ConTeXt support for AUCTeX. Please report
29 ;; anomalies or things you believe should be added.
31 ;; AUCTeX is closely interwoven with LaTeX. We have to split up
32 ;; things without breaking 'em.
34 ;; some parts are stolen from latex.el and adapted to ConTeXt.
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.
49 ;; 1. multifile done differently with ConTeXt
55 (require 'latex) ; for functions like `TeX-look-at' and `LaTeX-split-long-menu'
56 (require 'plain-tex) ; for `plain-TeX-common-initialization'
58 (defgroup ConTeXt-macro nil
59 "Special support for ConTeXt macros in AUCTeX."
67 ;; globals used in certain macro's.
69 "Position of point afterwards, default nil (meaning end).")
72 "Set by `ConTeXt-section-ref', used by `ConTeXt-section-section'.")
75 "Set by `ConTeXt-section-title', used by `ConTeXt-section-section'.")
80 (defvar ConTeXt-known-interfaces '("cz" "de" "en" "it" "nl" "ro" "uk"))
82 (defcustom ConTeXt-default-interface "en"
83 "Default interface to be used when running ConTeXt."
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)
91 (defvar ConTeXt-menu-changed nil)
92 ;; Need to update ConTeXt menu.
93 (make-variable-buffer-local 'ConTeXt-menu-changed)
95 (defvar ConTeXt-largest-level nil
96 "Largest sectioning level within current document.")
97 (make-variable-buffer-local 'ConTeXt-largest-level)
99 (defun ConTeXt-largest-level ()
101 ConTeXt-largest-level)
106 (defvar ConTeXt-optop "["
107 "The ConTeXt optional argument opening character.")
109 (defvar ConTeXt-optcl "]"
110 "The ConTeXt optional argument closing character.")
113 ;; Define a ConTeXt macro
115 (defvar ConTeXt-define-list ()
116 "Calls ConTeXt-XX-define-list where XX is the current interface.")
118 (defun ConTeXt-define-command (what)
119 "The ConTeXt macro to define WHAT."
121 (intern (concat "ConTeXt-define-command-" ConTeXt-current-interface)) what))
123 (defun ConTeXt-insert-define (define)
124 "Insert the ConTeXt define macro DEFINE."
125 (insert TeX-esc (ConTeXt-define-command define))
127 (indent-according-to-mode)
128 (ConTeXt-arg-setup nil))
131 ;; Setup a ConTeXt macro
133 (defvar ConTeXt-setup-list ()
134 "Calls ConTeXt-XX-setup-list where XX is the current interface.")
136 (defun ConTeXt-setup-command (what)
137 "The ConTeXt macro to setup WHAT."
139 (intern (concat "ConTeXt-setup-command-" ConTeXt-current-interface)) what))
141 (defun ConTeXt-insert-setup (setup)
142 "Insert the ConTeXt setup macro SETUP."
143 (insert TeX-esc (ConTeXt-setup-command setup))
145 (indent-according-to-mode)
146 (ConTeXt-arg-setup nil))
149 ;; Referencing ConTeXt macro's
151 (defvar ConTeXt-referencing-list ()
152 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
154 (defun ConTeXt-referencing-command (what)
155 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
158 (defun ConTeXt-insert-referencing (what)
159 "Insert the ConTeXt referencing SETUP."
160 (insert TeX-esc (ConTeXt-referencing-command what))
162 (indent-according-to-mode)
163 (ConTeXt-arg-setup nil))
166 ;; Other ConTeXt macro's
168 (defvar ConTeXt-other-macro-list ()
169 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
171 (defun ConTeXt-other-macro-command (what)
172 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
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))
179 (indent-according-to-mode)
180 (ConTeXt-arg-setup nil))
183 ;;; Project structure
185 (defvar ConTeXt-project-structure-list ()
186 "Calls ConTeXt-XX-project-structure where XX is the current interface.")
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
192 (nth N ConTeXt-project-structure-list)
195 (defun ConTeXt-project-project-insert ()
196 "Insert a basic template for a new ConTeXt project."
198 (insert "% The following names are examples only\n")
199 (insert TeX-esc (ConTeXt-environment-start-name) (nth 0 ConTeXt-project-structure-list) " myproject")
201 (insert TeX-esc (nth 1 ConTeXt-project-structure-list) " myenvironment")
203 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
205 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct2")
207 (insert TeX-esc (ConTeXt-environment-stop-name) (nth 0 ConTeXt-project-structure-list))))
209 (defun ConTeXt-project-environment-insert ()
210 "Insert a basic template for the environment of a ConTeXt project."
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))))
221 (defun ConTeXt-project-product-insert ()
222 "Insert a basic template for a product of a ConTeXt project."
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")
228 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject")
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")
234 (insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent2")
236 (insert TeX-esc (ConTeXt-environment-stop-name)
237 (nth 2 ConTeXt-project-structure-list))))
239 (defun ConTeXt-project-component-insert ()
240 "Insert a basic template for a component of a ConTeXt project."
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")
246 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject\n")
247 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
249 (insert "% ... text here ...")
251 (insert TeX-esc (ConTeXt-environment-stop-name)
252 (nth 3 ConTeXt-project-structure-list))))
257 (defvar ConTeXt-section-block-list ()
258 "Calls ConTeXt-XX-section-list where XX is the current interface.")
260 (defun ConTeXt-section-block (section-block)
261 "Insert the ConTeXt section block SECTION-BLOCK."
262 (ConTeXt-environment-menu section-block))
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.
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:
290 The following variables can be set to customize:
292 `ConTeXt-section-hook' Hooks to run when inserting a section.
293 `ConTeXt-section-ref' Prefix to all section references."
296 (let* ((val (prefix-numeric-value arg))
297 (level (cond ((null arg)
298 (ConTeXt-current-section))
300 (ConTeXt-down-section))
302 (ConTeXt-up-section (- val)))
304 (name (ConTeXt-section-name level))
307 (done-mark (make-marker)))
309 (run-hooks 'ConTeXt-section-hook)
311 (if (marker-position done-mark)
312 (goto-char (marker-position done-mark)))
313 (set-marker done-mark nil)))
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."
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)))))
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
332 (let ((current (ConTeXt-current-section))
334 (regexp outline-regexp))
335 (if (not (re-search-backward regexp nil t))
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)))))
348 (defun ConTeXt-up-section (arg)
349 "Return the value of the section ARG levels above this one."
352 (ConTeXt-current-section)
353 (let ((current (ConTeXt-current-section)))
354 (while (and (>= (ConTeXt-current-section) current)
355 (re-search-backward outline-regexp
357 (ConTeXt-up-section (1- arg))))))
359 (defvar ConTeXt-section-list ()
360 "ConTeXt-XX-section-list where XX is the current interface.")
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)))))))
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)))))))
382 (defcustom ConTeXt-section-hook
383 '(ConTeXt-section-heading
384 ConTeXt-section-title
386 ConTeXt-section-section)
387 "List of hooks to run when a new section is inserted.
389 The following variables are set before the hooks are run
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).
396 The following standard hook exist -
398 ConTeXt-section-heading: Query the user about the name of the
399 sectioning command. Modifies `level' and `name'.
401 ConTeXt-section-title: Query the user about the title of the
402 section. Modifies `title'.
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.
408 ConTeXt-section-ref: Insert a reference for this section command.
410 To get a full featured `ConTeXt-section' command, insert
412 (setq ConTeXt-section-hook
413 '(ConTeXt-section-heading
414 ConTeXt-section-title
415 ConTeXt-section-section
416 ConTeXt-section-ref))
418 in your .emacs file."
419 :group 'ConTeXt-macro
422 '(ConTeXt-section-heading
423 ConTeXt-section-title
425 ConTeXt-section-section))
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 ") ")
436 (if (not (zerop (length string)))
437 (setq name string))))
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: ")))
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))
457 (insert ConTeXt-optop reference ConTeXt-optcl)))
459 (if (zerop (length title))
460 (set-marker done-mark (point)))
461 (insert title TeX-grcl)
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))))
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."
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)))
482 (defun TeX-ConTeXt-sentinel (process name)
483 "Cleanup TeX output buffer after running ConTeXt."
484 (cond ((TeX-TeX-sentinel-check process name))
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))
503 (message (concat name ": problems after "
504 (TeX-current-pages)))
505 (setq TeX-command-next TeX-command-default))))
510 (defgroup ConTeXt-environment nil
511 "Environments in AUCTeX."
512 :group 'ConTeXt-macro)
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
519 (make-variable-buffer-local 'ConTeXt-default-environment)
521 (TeX-auto-add-type "environment" "ConTeXt")
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))
530 ;; (defvar ConTeXt-environment-list ()
531 ;; "ConTeXt-environment-list-XX where XX is the current interface.")
533 (defvar ConTeXt-environment-history nil)
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")
540 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
543 ;; this should not happen
544 (error "Unknown interface: %s" ConTeXt-current-interface))))
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")
551 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
554 ;; this should not happen
555 (error "Unknown interface: %s" ConTeXt-current-interface))))
558 (defun ConTeXt-environment (arg)
559 "Make ConTeXt environment (\\start...-\\stop... pair).
560 With optional ARG, modify current environment."
563 completing-read (concat "Environment type: (default "
566 ConTeXt-default-environment)
568 ConTeXt-environment-list
570 'ConTeXt-environment-history)
573 (cond ((and (zerop (length environment))
575 (setq environment "text"))
576 ((zerop (length environment))
577 (setq environment ConTeXt-default-environment))
579 (setq ConTeXt-default-environment environment)))
581 (let ((entry (assoc environment ConTeXt-environment-list)))
583 (ConTeXt-add-environments (list environment)))
585 (ConTeXt-modify-environment environment)
586 (ConTeXt-environment-menu environment)))))
588 (defun ConTeXt-modify-environment (environment)
589 "Modify current environment."
591 (ConTeXt-find-matching-stop)
592 (re-search-backward (concat (regexp-quote TeX-esc)
593 (ConTeXt-environment-stop-name)
595 (save-excursion (beginning-of-line 1) (point)))
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)
603 (save-excursion (end-of-line 1) (point)))
605 (concat TeX-esc (ConTeXt-environment-start-name) environment) t t)))
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))
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))
624 (setq args (concat args
626 (read-from-minibuffer
627 (concat (car prompts) ": "))
629 (setq prompts (cdr prompts)))
630 (ConTeXt-insert-environment environment args)))
632 (apply (nth 1 entry) environment (nthcdr 2 entry))))))
634 (defun ConTeXt-close-environment ()
635 "Insert \\stop... to match the current environment."
638 (let ((empty-line (looking-at "[ \t]*$")))
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)
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))))
654 (if (< (mark) (point))
655 (exchange-point-and-mark))
656 (insert TeX-esc (ConTeXt-environment-start-name) environment)
659 (indent-according-to-mode)
660 (if extra (insert extra))
662 (or (TeX-looking-at-backward "^[ \t]*")
664 (insert TeX-esc (ConTeXt-environment-stop-name) environment)
667 (indent-according-to-mode)
668 ;;(goto-char (point))
670 (or (TeX-looking-at-backward "^[ \t]*")
672 (insert TeX-esc (ConTeXt-environment-start-name) environment)
673 (indent-according-to-mode)
674 (if extra (insert extra))
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)
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.
691 ;; % so pressing C-c ! inside the following ...
692 ;;\startuseMPgraphic{Logo}{Scale}
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;
698 ;; draw btex \bf AB etex withcolor white ;
700 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
703 ;; % ...should give you a "new buffer" (currently narrowed to region
704 ;; % and switched to metapost-mode and recursive-edit)
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;
711 ;; draw btex \bf AB etex withcolor white ;
713 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
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)")
722 (defun ConTeXt-mp-region ()
723 "Edit region in `metapost-mode'."
724 (ConTeXt-mark-environment t)
725 (narrow-to-region (mark) (point))
727 (message "Type `M-x exit-recursive-edit' to get back")
732 ;; find smarter name. Suggestions welcome
733 (defun ConTeXt-work-on-environment ()
734 "Takes current environment and does something on it (todo: documentation)."
736 (let ((fun (cdr (assoc (ConTeXt-current-environment)
737 ConTeXt-environment-helper))))
738 (when (functionp fun)
741 (defun ConTeXt-current-environment ()
742 "Return the name of the current environment."
743 ;; don't make this interactive.
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)))))
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)))
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
765 (ConTeXt-find-matching-stop inner)
768 (ConTeXt-find-matching-start inner)
769 (TeX-activate-region)))
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..."
776 (let ((regexp (concat (regexp-quote TeX-esc)
778 (ConTeXt-environment-start-name)
780 (ConTeXt-environment-stop-name)
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
803 (skip-chars-forward "a-zA-Z"))))
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."
809 (let ((regexp (concat (regexp-quote TeX-esc)
811 (ConTeXt-environment-start-name)
813 (ConTeXt-environment-stop-name)
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
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
835 (skip-chars-forward "\\\\a-zA-Z")
838 (skip-chars-forward "\t\n ")
840 (skip-chars-forward "\t\n ")
841 (looking-at "\\s\("))
851 (defun ConTeXt-insert-item ()
854 (or (TeX-looking-at-backward "^[ \t]*")
856 (TeX-insert-macro "item")
857 (indent-according-to-mode))
860 ;;; Macro Argument Hooks
862 (defun ConTeXt-optional-argument-insert (arg &optional prefix)
863 "Insert ARG surrounded by square brackets."
864 (insert ConTeXt-optop)
866 (insert ConTeXt-optcl))
868 (defun ConTeXt-required-argument-insert (arg &optional prefix)
869 "Insert ARG surrounded by curly braces."
874 (defun ConTeXt-argument-insert (arg optional &optional prefix)
875 "Insert ARG surrounded by curly braces.
877 If OPTIONAL, only insert it if not empty, and then use square brackets."
880 (not (string-equal arg ""))
881 (ConTeXt-optional-argument-insert arg prefix))
882 (ConTeXt-required-argument-insert arg prefix)))
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)))
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))
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)))
903 ;; paragraph (re)-formatting
905 (defvar ConTeXt-item-list ()
906 "List of macro's considered items.")
908 (defun ConTeXt-paragraph-commands-regexp ()
909 "Return a regexp matching macros that should have their own line."
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\\|")
918 (mapconcat 'identity ConTeXt-item-list "\\b\\|") "\\b\\)"))
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))
928 (defun ConTeXt-outline-regexp (&optional anywhere)
929 "Return regexp for ConTeXt section blocks and sections.
931 If optional argument ANYWHERE is not nil, do not require that the
932 header is at the start of a line."
936 (regexp-quote TeX-esc)
938 (mapconcat 'ConTeXt-environment-full-start-name ConTeXt-section-block-list "\\|") "\\|"
939 (mapconcat 'car ConTeXt-section-list "\\|")
941 (if TeX-outline-extra
944 (mapconcat 'car TeX-outline-extra "\\|")
945 "\\|" (ConTeXt-header-end) "\\b"
946 "\\|" (ConTeXt-trailer-start) "\\b"))
948 (defvar ConTeXt-text "Name of ConTeXt macro that begins the text body.")
950 (defun ConTeXt-header-end ()
951 "Default end of header marker for ConTeXt documents."
953 (regexp-quote TeX-esc)
954 (ConTeXt-environment-start-name)
957 (defun ConTeXt-trailer-start ()
958 "Default start of trailer marker for ConTeXt documents."
960 (regexp-quote TeX-esc)
961 (ConTeXt-environment-stop-name)
964 (defun ConTeXt-outline-offset ()
965 "Offset to add to `ConTeXt-section-list' levels to get outline level."
966 (- 4 (ConTeXt-largest-level)))
968 (defun ConTeXt-start-environment-regexp (list)
969 "Regular expression that matches a start of all environments mentioned in LIST."
972 (mapconcat 'identity list "\\|")
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))))
986 (skip-chars-forward " \t")
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))))
994 (error "Unrecognized header")))))))
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 " "}")
1007 "List of fonts used by `TeX-font'.
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."
1019 :value (?\C-a "" "")
1020 (character :tag "Key")
1021 (string :tag "Prefix")
1022 (string :tag "Suffix")
1025 (string :tag "Math Prefix")
1026 (string :tag "Math Suffix")))
1027 (option (sexp :format "Replace\n" :value t)))))
1032 (defun ConTeXt-outline-name ()
1033 "Guess a name for the current header line."
1035 (if (re-search-forward "{\\([^\}]*\\)}" (point-at-eol) t)
1037 (buffer-substring-no-properties (point) (point-at-eol)))))
1039 ;; This imenu also includes commented out chapters. Perhaps a feature
1040 ;; for LaTeX, not sure we want or need that for ConTeXt.
1042 (defun ConTeXt-imenu-create-index-function ()
1043 "Imenu support function for ConTeXt."
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))))
1058 ;; Indentation, copied from Berend's context mode.
1059 ;; TODO: doesn't work great.
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\\)\\>")
1067 (defvar ConTeXt-indent-syntax-table (make-syntax-table TeX-mode-syntax-table)
1068 "Syntax table used while computing indentation.")
1071 (modify-syntax-entry ?$ "." ConTeXt-indent-syntax-table)
1072 (modify-syntax-entry ?\( "." ConTeXt-indent-syntax-table)
1073 (modify-syntax-entry ?\) "." ConTeXt-indent-syntax-table))
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.
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))))))
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."
1093 (skip-chars-forward " \t")
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.
1100 (and (eq (char-syntax (char-after)) ?\))
1102 (skip-syntax-forward " )")
1104 (ConTeXt-find-indent 'virtual)))
1106 ;; Default (maybe an argument)
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.
1121 (while (> (current-column) (current-indentation))
1124 (setq up-list-pos (nth 2 err))))
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))
1134 ;; Align with the first element after the open-paren.
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))
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
1162 (if (< (line-end-position)
1163 (save-excursion (forward-comment (point-max))
1165 ;; we're indenting the first argument.
1166 (min (current-column) (+ ConTeXt-indent-arg col))
1167 (skip-syntax-forward " ")
1168 (current-column))))))))))
1171 ;; XML inside ConTeXt support
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)))
1179 (defun ConTeXt-close-xml-tag ()
1180 "Create an </...> to match the last unclosed <...>. Not fool-proof."
1182 (let ((new-line-needed (bolp)) text indentation)
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))))
1197 (defvar ConTeXt-mode-map
1198 (let ((map (make-sparse-keymap)))
1199 (set-keymap-parent map TeX-mode-map)
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)
1214 "Keymap used in `ConTeXt-mode'.")
1219 ;; functions to create menu entries
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
1230 (defvar ConTeXt-environment-menu-name "Insert Environment (C-c C-e)")
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))
1236 (defvar ConTeXt-environment-modify-menu-name "Change Environment (C-u C-c C-e)")
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))
1242 ;; ConTeXt define macros
1243 (defvar ConTeXt-define-menu-name "Define")
1245 (defun ConTeXt-define-menu-entry (entry)
1246 "Create an entry for the define menu."
1247 (vector entry (list 'ConTeXt-define-menu entry)))
1249 (defun ConTeXt-define-menu (define)
1250 "Insert DEFINE from menu."
1251 (ConTeXt-insert-define define))
1253 ;; ConTeXt setup macros
1254 (defvar ConTeXt-setup-menu-name "Setup")
1256 (defun ConTeXt-setup-menu-entry (entry)
1257 "Create an entry for the setup menu."
1258 (vector entry (list 'ConTeXt-setup-menu entry)))
1260 (defun ConTeXt-setup-menu (setup)
1261 "Insert SETUP from menu."
1262 (ConTeXt-insert-setup setup))
1264 ;; ConTeXt referencing macros
1265 (defvar ConTeXt-referencing-menu-name "Referencing")
1267 (defun ConTeXt-referencing-menu-entry (entry)
1268 "Create an entry for the referencing menu."
1269 (vector entry (list 'ConTeXt-referencing-menu entry)))
1271 (defun ConTeXt-referencing-menu (referencing)
1272 "Insert REFERENCING from menu."
1273 (ConTeXt-insert-referencing referencing))
1275 ;; ConTeXt other macros
1276 (defvar ConTeXt-other-macro-menu-name "Other macro")
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)))
1282 (defun ConTeXt-other-macro-menu (other-macro)
1283 "Insert OTHER MACRO from menu."
1284 (ConTeXt-insert-other-macro other-macro))
1287 ;; meta-structure project structure menu entries
1289 (defvar ConTeXt-project-structure-menu-name "Project Structure")
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))))))
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)))
1302 ;; meta-structure section blocks menu entries
1304 (defvar ConTeXt-section-block-menu-name "Section Block")
1306 (defun ConTeXt-section-block-menu (section-block)
1307 "Insert section block from menu."
1308 (ConTeXt-section-block section-block))
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)))
1315 ;; section menu entries
1317 (defvar ConTeXt-section-menu-name "Section (C-c C-s)")
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")))
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))))
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)))
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))))
1339 (vector (car entry) (list 'ConTeXt-section-menu (nth 1 entry)) enable)))
1344 (defun ConTeXt-etexshow ()
1345 "Call etexshow, if available, to show the definition of a ConText macro."
1347 (if (fboundp 'etexshow)
1350 (funcall (symbol-function 'etexshow-cmd)))
1351 (error "etexshow is not installed. Get it from http://levana.de/emacs/")))
1355 (easy-menu-define ConTeXt-mode-command-menu
1357 "Command menu used in ConTeXt mode."
1358 (TeX-mode-specific-command-menu 'context-mode))
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
1366 "Menu used in ConTeXt mode."
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]
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]
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)
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"])
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"]
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"]
1417 "-" . ,TeX-common-menu-entries)))
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))
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))
1480 (defvar ConTeXt-texexec-option-nonstop "--nonstop "
1481 "Command line option for texexec to use nonstopmode.")
1483 (defun ConTeXt-expand-options ()
1484 "Expand options for texexec command."
1486 (let ((engine (eval (nth 4 (assq TeX-engine (TeX-engine-alist))))))
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)))
1501 ;; ConTeXt variables that are interface aware
1502 ;; They are mapped to interface specific variables
1504 (defvar ConTeXt-language-variable-list
1505 '(ConTeXt-define-list
1507 ConTeXt-referencing-list
1508 ConTeXt-other-macro-list
1509 ConTeXt-project-structure-list
1510 ConTeXt-section-block-list
1511 ConTeXt-section-list
1514 ConTeXt-extra-paragraph-commands))
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)
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)
1536 (TeX-abbrev-mode-setup context-mode)
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
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)
1549 (setq local-abbrev-table context-mode-abbrev-table)
1551 ;; Make language specific variables buffer local
1552 (dolist (symbol ConTeXt-language-variable-list)
1553 (make-variable-buffer-local symbol))
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)))))
1560 ;; Create certain regular expressions based on language
1561 (setq ConTeXt-indent-item-re (concat "\\\\\\(" (mapconcat 'identity ConTeXt-item-list "\\|") "\\)\\>"))
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))
1570 (use-local-map ConTeXt-mode-map)
1573 (set (make-local-variable 'indent-line-function) 'ConTeXt-indent-line)
1574 (set (make-local-variable 'fill-indent-according-to-mode) t)
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
1586 (ConTeXt-paragraph-commands-regexp) "\\|"
1587 "\\$\\$\\|" ; Plain TeX display math
1589 (setq paragraph-separate
1592 "\\$\\$" ; Plain TeX display math
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)
1601 (if (= emacs-major-version 20)
1602 (make-local-hook 'activate-menubar-hook))
1603 (add-hook 'activate-menubar-hook 'ConTeXt-menu-update nil t)
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))
1613 ;; font switch support
1614 (set (make-local-variable 'TeX-font-list) ConTeXt-font-list)
1617 (set (make-local-variable 'imenu-create-index-function)
1618 'ConTeXt-imenu-create-index-function)
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))
1625 (defun context-guess-current-interface ()
1626 "Guess what ConTeXt interface the current buffer is using."
1629 (goto-char (point-min))
1630 (setq ConTeXt-current-interface
1631 (cond ((re-search-forward "%.*?interface=en" (+ 512 (point)) t)
1633 ((re-search-forward "%.*?interface=nl" (+ 512 (point)) t)
1635 ((re-search-forward "\\\\starttext" (+ 1024 (point)) t)
1637 ((re-search-forward "\\\\starttekst" (+ 1024 (point)) t)
1640 ConTeXt-default-interface)))))
1643 (defalias 'ConTeXt-mode 'context-mode)
1646 (defun context-mode ()
1647 "Major mode in AUCTeX for editing ConTeXt files.
1650 \\{ConTeXt-mode-map}
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."
1656 (context-guess-current-interface)
1657 (require (intern (concat "context-" ConTeXt-current-interface)))
1658 (funcall (intern (concat "context-" ConTeXt-current-interface "-mode"))))
1662 ;;; context.el ends here