1 ;;; context.el --- Support for ConTeXt documents.
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
5 ;; Maintainer: Berend de Boer <berend@pobox.com>
8 ;; This file is part of AUCTeX.
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)
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.
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
27 ;; This is in progress ConTeXt support for AUCTeX. Please report
28 ;; anomalies or things you believe should be added.
30 ;; AUCTeX is closely interwoven with LaTeX. We have to split up
31 ;; things without breaking 'em.
33 ;; some parts are stolen from latex.el and adapted to ConTeXt.
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.
48 ;; 1. multifile done differently with ConTeXt
54 ;; need functions like TeX-look-at and LaTeX-split-long-menu
57 (defgroup ConTeXt-macro nil
58 "Special support for ConTeXt macros in AUCTeX."
66 ;; globals used in certain macro's.
68 "Position of point afterwards, default nil (meaning end).")
71 "Set by `ConTeXt-section-ref', used by `ConTeXt-section-section'.")
74 "Set by `ConTeXt-section-title', used by `ConTeXt-section-section'.")
79 (defvar ConTeXt-known-interfaces '("cz" "de" "en" "it" "nl" "ro" "uk"))
81 (defcustom ConTeXt-default-interface "en"
82 "Default interface to be used when running ConTeXt."
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)
90 (defvar ConTeXt-menu-changed nil)
91 ;; Need to update ConTeXt menu.
92 (make-variable-buffer-local 'ConTeXt-menu-changed)
94 (defvar ConTeXt-largest-level nil
95 "Largest sectioning level within current document.")
96 (make-variable-buffer-local 'ConTeXt-largest-level)
98 (defun ConTeXt-largest-level ()
100 ConTeXt-largest-level)
105 (defvar ConTeXt-optop "["
106 "The ConTeXt optional argument opening character.")
108 (defvar ConTeXt-optcl "]"
109 "The ConTeXt optional argument closing character.")
112 ;; Define a ConTeXt macro
114 (defvar ConTeXt-define-list ()
115 "Calls ConTeXt-XX-define-list where XX is the current interface.")
117 (defun ConTeXt-define-command (what)
118 "The ConTeXt macro to define WHAT."
120 (intern (concat "ConTeXt-define-command-" ConTeXt-current-interface)) what))
122 (defun ConTeXt-insert-define (define)
123 "Insert the ConTeXt define macro DEFINE."
124 (insert TeX-esc (ConTeXt-define-command define))
126 (indent-according-to-mode)
127 (ConTeXt-arg-setup nil))
130 ;; Setup a ConTeXt macro
132 (defvar ConTeXt-setup-list ()
133 "Calls ConTeXt-XX-setup-list where XX is the current interface.")
135 (defun ConTeXt-setup-command (what)
136 "The ConTeXt macro to setup WHAT."
138 (intern (concat "ConTeXt-setup-command-" ConTeXt-current-interface)) what))
140 (defun ConTeXt-insert-setup (setup)
141 "Insert the ConTeXt setup macro SETUP."
142 (insert TeX-esc (ConTeXt-setup-command setup))
144 (indent-according-to-mode)
145 (ConTeXt-arg-setup nil))
148 ;; Referencing ConTeXt macro's
150 (defvar ConTeXt-referencing-list ()
151 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
153 (defun ConTeXt-referencing-command (what)
154 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
157 (defun ConTeXt-insert-referencing (what)
158 "Insert the ConTeXt referencing SETUP."
159 (insert TeX-esc (ConTeXt-referencing-command what))
161 (indent-according-to-mode)
162 (ConTeXt-arg-setup nil))
165 ;; Other ConTeXt macro's
167 (defvar ConTeXt-other-macro-list ()
168 "Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
170 (defun ConTeXt-other-macro-command (what)
171 "The ConTeXt macro to call WHAT is itself, no interface specific calls."
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))
178 (indent-according-to-mode)
179 (ConTeXt-arg-setup nil))
182 ;;; Project structure
184 (defvar ConTeXt-project-structure-list ()
185 "Calls ConTeXt-XX-project-structure where XX is the current interface.")
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
191 (nth N ConTeXt-project-structure-list)
194 (defun ConTeXt-project-project-insert ()
195 "Insert a basic template for a new ConTeXt project."
197 (insert "% The following names are examples only\n")
198 (insert TeX-esc (ConTeXt-environment-start-name) (nth 0 ConTeXt-project-structure-list) " myproject")
200 (insert TeX-esc (nth 1 ConTeXt-project-structure-list) " myenvironment")
202 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
204 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct2")
206 (insert TeX-esc (ConTeXt-environment-stop-name) (nth 0 ConTeXt-project-structure-list))))
208 (defun ConTeXt-project-environment-insert ()
209 "Insert a basic template for the environment of a ConTeXt project."
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))))
220 (defun ConTeXt-project-product-insert ()
221 "Insert a basic template for a product of a ConTeXt project."
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")
227 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject")
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")
233 (insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent2")
235 (insert TeX-esc (ConTeXt-environment-stop-name)
236 (nth 2 ConTeXt-project-structure-list))))
238 (defun ConTeXt-project-component-insert ()
239 "Insert a basic template for a component of a ConTeXt project."
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")
245 (insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject\n")
246 (insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
248 (insert "% ... text here ...")
250 (insert TeX-esc (ConTeXt-environment-stop-name)
251 (nth 3 ConTeXt-project-structure-list))))
256 (defvar ConTeXt-section-block-list ()
257 "Calls ConTeXt-XX-section-list where XX is the current interface.")
259 (defun ConTeXt-section-block (section-block)
260 "Insert the ConTeXt section block SECTION-BLOCK."
261 (ConTeXt-environment-menu section-block))
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.
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:
289 The following variables can be set to customize:
291 `ConTeXt-section-hook' Hooks to run when inserting a section.
292 `ConTeXt-section-ref' Prefix to all section references."
295 (let* ((val (prefix-numeric-value arg))
296 (level (cond ((null arg)
297 (ConTeXt-current-section))
299 (ConTeXt-down-section))
301 (ConTeXt-up-section (- val)))
303 (name (ConTeXt-section-name level))
306 (done-mark (make-marker)))
308 (run-hooks 'ConTeXt-section-hook)
310 (if (marker-position done-mark)
311 (goto-char (marker-position done-mark)))
312 (set-marker done-mark nil)))
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."
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)))))
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
331 (let ((current (ConTeXt-current-section))
333 (regexp outline-regexp))
334 (if (not (re-search-backward regexp nil t))
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)))))
347 (defun ConTeXt-up-section (arg)
348 "Return the value of the section ARG levels above this one."
351 (ConTeXt-current-section)
352 (let ((current (ConTeXt-current-section)))
353 (while (and (>= (ConTeXt-current-section) current)
354 (re-search-backward outline-regexp
356 (ConTeXt-up-section (1- arg))))))
358 (defvar ConTeXt-section-list ()
359 "ConTeXt-XX-section-list where XX is the current interface.")
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)))))))
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)))))))
381 (defcustom ConTeXt-section-hook
382 '(ConTeXt-section-heading
383 ConTeXt-section-title
385 ConTeXt-section-section)
386 "List of hooks to run when a new section is inserted.
388 The following variables are set before the hooks are run
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).
395 The following standard hook exist -
397 ConTeXt-section-heading: Query the user about the name of the
398 sectioning command. Modifies `level' and `name'.
400 ConTeXt-section-title: Query the user about the title of the
401 section. Modifies `title'.
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.
407 ConTeXt-section-ref: Insert a reference for this section command.
409 To get a full featured `ConTeXt-section' command, insert
411 (setq ConTeXt-section-hook
412 '(ConTeXt-section-heading
413 ConTeXt-section-title
414 ConTeXt-section-section
415 ConTeXt-section-ref))
417 in your .emacs file."
418 :group 'ConTeXt-macro
421 '(ConTeXt-section-heading
422 ConTeXt-section-title
424 ConTeXt-section-section))
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 ") ")
435 (if (not (zerop (length string)))
436 (setq name string))))
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: ")))
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))
456 (insert ConTeXt-optop reference ConTeXt-optcl)))
458 (if (zerop (length title))
459 (set-marker done-mark (point)))
460 (insert title TeX-grcl)
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))))
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."
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)))
481 (defun TeX-ConTeXt-sentinel (process name)
482 "Cleanup TeX output buffer after running ConTeXt."
483 (cond ((TeX-TeX-sentinel-check process name))
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))
502 (message (concat name ": problems after "
503 (TeX-current-pages)))
504 (setq TeX-command-next TeX-command-default))))
509 (defgroup ConTeXt-environment nil
510 "Environments in AUCTeX."
511 :group 'ConTeXt-macro)
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
518 (make-variable-buffer-local 'ConTeXt-default-environment)
520 (TeX-auto-add-type "environment" "ConTeXt")
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))
529 ;; (defvar ConTeXt-environment-list ()
530 ;; "ConTeXt-environment-list-XX where XX is the current interface.")
532 (defvar ConTeXt-environment-history nil)
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")
539 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
542 ;; this should not happen
543 (error "Unknown interface: %s" ConTeXt-current-interface))))
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")
550 ((member ConTeXt-current-interface ConTeXt-known-interfaces)
553 ;; this should not happen
554 (error "Unknown interface: %s" ConTeXt-current-interface))))
557 (defun ConTeXt-environment (arg)
558 "Make ConTeXt environment (\\start...-\\stop... pair).
559 With optional ARG, modify current environment."
562 completing-read (concat "Environment type: (default "
565 ConTeXt-default-environment)
567 ConTeXt-environment-list
569 'ConTeXt-environment-history)
572 (cond ((and (zerop (length environment))
574 (setq environment "text"))
575 ((zerop (length environment))
576 (setq environment ConTeXt-default-environment))
578 (setq ConTeXt-default-environment environment)))
580 (let ((entry (assoc environment ConTeXt-environment-list)))
582 (ConTeXt-add-environments (list environment)))
584 (ConTeXt-modify-environment environment)
585 (ConTeXt-environment-menu environment)))))
587 (defun ConTeXt-modify-environment (environment)
588 "Modify current environment."
590 (ConTeXt-find-matching-stop)
591 (re-search-backward (concat (regexp-quote TeX-esc)
592 (ConTeXt-environment-stop-name)
594 (save-excursion (beginning-of-line 1) (point)))
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)
602 (save-excursion (end-of-line 1) (point)))
604 (concat TeX-esc (ConTeXt-environment-start-name) environment) t t)))
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))
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))
623 (setq args (concat args
625 (read-from-minibuffer
626 (concat (car prompts) ": "))
628 (setq prompts (cdr prompts)))
629 (ConTeXt-insert-environment environment args)))
631 (apply (nth 1 entry) environment (nthcdr 2 entry))))))
633 (defun ConTeXt-close-environment ()
634 "Insert \\stop... to match the current environment."
637 (let ((empty-line (looking-at "[ \t]*$")))
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)
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))))
653 (if (< (mark) (point))
654 (exchange-point-and-mark))
655 (insert TeX-esc (ConTeXt-environment-start-name) environment)
658 (indent-according-to-mode)
659 (if extra (insert extra))
661 (or (TeX-looking-at-backward "^[ \t]*")
663 (insert TeX-esc (ConTeXt-environment-stop-name) environment)
666 (indent-according-to-mode)
667 ;;(goto-char (point))
669 (or (TeX-looking-at-backward "^[ \t]*")
671 (insert TeX-esc (ConTeXt-environment-start-name) environment)
672 (indent-according-to-mode)
673 (if extra (insert extra))
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)
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.
690 ;; % so pressing C-c ! inside the following ...
691 ;;\startuseMPgraphic{Logo}{Scale}
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;
697 ;; draw btex \bf AB etex withcolor white ;
699 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
702 ;; % ...should give you a "new buffer" (currently narrowed to region
703 ;; % and switched to metapost-mode and recursive-edit)
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;
710 ;; draw btex \bf AB etex withcolor white ;
712 ;; currentpicture := currentpicture scaled \MPvar{Scale} ;
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)")
721 (defun ConTeXt-mp-region ()
722 "Edit region in `metapost-mode'."
723 (ConTeXt-mark-environment t)
724 (narrow-to-region (mark) (point))
726 (message "Type `M-x exit-recursive-edit' to get back")
731 ;; find smarter name. Suggestions welcome
732 (defun ConTeXt-work-on-environment ()
733 "Takes current environment and does something on it (todo: documentation)."
735 (let ((fun (cdr (assoc (ConTeXt-current-environment)
736 ConTeXt-environment-helper))))
737 (when (functionp fun)
740 (defun ConTeXt-current-environment ()
741 "Return the name of the current environment."
742 ;; don't make this interactive.
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)))))
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)))
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
764 (ConTeXt-find-matching-stop inner)
767 (ConTeXt-find-matching-start inner)
768 (TeX-activate-region)))
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..."
775 (let ((regexp (concat (regexp-quote TeX-esc)
777 (ConTeXt-environment-start-name)
779 (ConTeXt-environment-stop-name)
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
802 (skip-chars-forward "a-zA-Z"))))
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."
808 (let ((regexp (concat (regexp-quote TeX-esc)
810 (ConTeXt-environment-start-name)
812 (ConTeXt-environment-stop-name)
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
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
834 (skip-chars-forward "\\\\a-zA-Z")
837 (skip-chars-forward "\t\n ")
839 (skip-chars-forward "\t\n ")
840 (looking-at "\\s\("))
850 (defun ConTeXt-insert-item ()
853 (or (TeX-looking-at-backward "^[ \t]*")
855 (TeX-insert-macro "item")
856 (indent-according-to-mode))
859 ;;; Macro Argument Hooks
861 (defun ConTeXt-optional-argument-insert (arg &optional prefix)
862 "Insert ARG surrounded by square brackets."
863 (insert ConTeXt-optop)
865 (insert ConTeXt-optcl))
867 (defun ConTeXt-required-argument-insert (arg &optional prefix)
868 "Insert ARG surrounded by curly braces."
873 (defun ConTeXt-argument-insert (arg optional &optional prefix)
874 "Insert ARG surrounded by curly braces.
876 If OPTIONAL, only insert it if not empty, and then use square brackets."
879 (not (string-equal arg ""))
880 (ConTeXt-optional-argument-insert arg prefix))
881 (ConTeXt-required-argument-insert arg prefix)))
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)))
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))
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)))
902 ;; paragraph (re)-formatting
904 (defvar ConTeXt-item-list ()
905 "List of macro's considered items.")
907 (defvar ConTeXt-extra-paragraph-commands
909 "List of ConTeXt macros that should have their own line besides the section(-block) commands.")
911 (defun ConTeXt-paragraph-commands-regexp ()
912 "Return a regexp matching macros that should have their own line."
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\\|")
921 (mapconcat 'identity ConTeXt-item-list "\\b\\|") "\\b\\)"))
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))
931 (defun ConTeXt-outline-regexp (&optional anywhere)
932 "Return regexp for ConTeXt section blocks and sections.
934 If optional argument ANYWHERE is not nil, do not require that the
935 header is at the start of a line."
939 (regexp-quote TeX-esc)
941 (mapconcat 'ConTeXt-environment-full-start-name ConTeXt-section-block-list "\\|") "\\|"
942 (mapconcat 'car ConTeXt-section-list "\\|")
944 (if TeX-outline-extra
947 (mapconcat 'car TeX-outline-extra "\\|")
948 "\\|" (ConTeXt-header-end) "\\b"
949 "\\|" (ConTeXt-trailer-start) "\\b"))
951 (defvar ConTeXt-text "Name of ConTeXt macro that begins the text body.")
953 (defun ConTeXt-header-end ()
954 "Default end of header marker for ConTeXt documents."
956 (regexp-quote TeX-esc)
957 (ConTeXt-environment-start-name)
960 (defun ConTeXt-trailer-start ()
961 "Default start of trailer marker for ConTeXt documents."
963 (regexp-quote TeX-esc)
964 (ConTeXt-environment-stop-name)
967 (defun ConTeXt-outline-offset ()
968 "Offset to add to `ConTeXt-section-list' levels to get outline level."
969 (- 4 (ConTeXt-largest-level)))
971 (defun ConTeXt-start-environment-regexp (list)
972 "Regular expression that matches a start of all environments mentioned in LIST."
975 (mapconcat 'identity list "\\|")
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))))
989 (skip-chars-forward " \t")
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))))
997 (error "Unrecognized header")))))))
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 " "}")
1010 "List of fonts used by `TeX-font'.
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."
1022 :value (?\C-a "" "")
1023 (character :tag "Key")
1024 (string :tag "Prefix")
1025 (string :tag "Suffix")
1028 (string :tag "Math Prefix")
1029 (string :tag "Math Suffix")))
1030 (option (sexp :format "Replace\n" :value t)))))
1035 (defun ConTeXt-outline-name ()
1036 "Guess a name for the current header line."
1038 (if (re-search-forward "{\\([^\}]*\\)}" (point-at-eol) t)
1040 (buffer-substring-no-properties (point) (point-at-eol)))))
1042 ;; This imenu also includes commented out chapters. Perhaps a feature
1043 ;; for LaTeX, not sure we want or need that for ConTeXt.
1045 (defun ConTeXt-imenu-create-index-function ()
1046 "Imenu support function for ConTeXt."
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))))
1061 ;; Indentation, copied from Berend's context mode.
1062 ;; TODO: doesn't work great.
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\\>")
1070 (defvar ConTeXt-indent-syntax-table (make-syntax-table TeX-mode-syntax-table)
1071 "Syntax table used while computing indentation.")
1074 (modify-syntax-entry ?$ "." ConTeXt-indent-syntax-table)
1075 (modify-syntax-entry ?\( "." ConTeXt-indent-syntax-table)
1076 (modify-syntax-entry ?\) "." ConTeXt-indent-syntax-table))
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.
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))))))
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."
1096 (skip-chars-forward " \t")
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.
1103 (and (eq (char-syntax (char-after)) ?\))
1105 (skip-syntax-forward " )")
1107 (ConTeXt-find-indent 'virtual)))
1109 ;; Default (maybe an argument)
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.
1124 (while (> (current-column) (current-indentation))
1127 (setq up-list-pos (nth 2 err))))
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))
1137 ;; Align with the first element after the open-paren.
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))
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
1165 (if (< (line-end-position)
1166 (save-excursion (forward-comment (point-max))
1168 ;; we're indenting the first argument.
1169 (min (current-column) (+ ConTeXt-indent-arg col))
1170 (skip-syntax-forward " ")
1171 (current-column))))))))))
1174 ;; XML inside ConTeXt support
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)))
1182 (defun ConTeXt-close-xml-tag ()
1183 "Create an </...> to match the last unclosed <...>. Not fool-proof."
1185 (let ((new-line-needed (bolp)) text indentation)
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))))
1200 (defvar ConTeXt-mode-map
1201 (let ((map (make-sparse-keymap)))
1202 (set-keymap-parent map TeX-mode-map)
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)
1217 "Keymap used in `ConTeXt-mode'.")
1222 ;; functions to create menu entries
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
1233 (defvar ConTeXt-environment-menu-name "Insert Environment (C-c C-e)")
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))
1239 (defvar ConTeXt-environment-modify-menu-name "Change Environment (C-u C-c C-e)")
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))
1245 ;; ConTeXt define macros
1246 (defvar ConTeXt-define-menu-name "Define")
1248 (defun ConTeXt-define-menu-entry (entry)
1249 "Create an entry for the define menu."
1250 (vector entry (list 'ConTeXt-define-menu entry)))
1252 (defun ConTeXt-define-menu (define)
1253 "Insert DEFINE from menu."
1254 (ConTeXt-insert-define define))
1256 ;; ConTeXt setup macros
1257 (defvar ConTeXt-setup-menu-name "Setup")
1259 (defun ConTeXt-setup-menu-entry (entry)
1260 "Create an entry for the setup menu."
1261 (vector entry (list 'ConTeXt-setup-menu entry)))
1263 (defun ConTeXt-setup-menu (setup)
1264 "Insert SETUP from menu."
1265 (ConTeXt-insert-setup setup))
1267 ;; ConTeXt referencing macros
1268 (defvar ConTeXt-referencing-menu-name "Referencing")
1270 (defun ConTeXt-referencing-menu-entry (entry)
1271 "Create an entry for the referencing menu."
1272 (vector entry (list 'ConTeXt-referencing-menu entry)))
1274 (defun ConTeXt-referencing-menu (referencing)
1275 "Insert REFERENCING from menu."
1276 (ConTeXt-insert-referencing referencing))
1278 ;; ConTeXt other macros
1279 (defvar ConTeXt-other-macro-menu-name "Other macro")
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)))
1285 (defun ConTeXt-other-macro-menu (other-macro)
1286 "Insert OTHER MACRO from menu."
1287 (ConTeXt-insert-other-macro other-macro))
1290 ;; meta-structure project structure menu entries
1292 (defvar ConTeXt-project-structure-menu-name "Project Structure")
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))))))
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)))
1305 ;; meta-structure section blocks menu entries
1307 (defvar ConTeXt-section-block-menu-name "Section Block")
1309 (defun ConTeXt-section-block-menu (section-block)
1310 "Insert section block from menu."
1311 (ConTeXt-section-block section-block))
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)))
1318 ;; section menu entries
1320 (defvar ConTeXt-section-menu-name "Section (C-c C-s)")
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")))
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))))
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)))
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))))
1342 (vector (car entry) (list 'ConTeXt-section-menu (nth 1 entry)) enable)))
1347 (defun ConTeXt-etexshow ()
1348 "Call etexshow, if available, to show the definition of a ConText macro."
1350 (if (fboundp 'etexshow)
1353 (funcall (symbol-function 'etexshow-cmd)))
1354 (error "etexshow is not installed. Get it from http://levana.de/emacs/")))
1358 (easy-menu-define ConTeXt-mode-command-menu
1360 "Command menu used in ConTeXt mode."
1361 (TeX-mode-specific-command-menu 'context-mode))
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
1369 "Menu used in ConTeXt mode."
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]
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]
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)
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"])
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"]
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"]
1420 "-" . ,TeX-common-menu-entries)))
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))
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))
1483 (defvar ConTeXt-texexec-option-nonstop "--nonstop "
1484 "Command line option for texexec to use nonstopmode.")
1486 (defun ConTeXt-expand-options ()
1487 "Expand options for texexec command."
1489 (let ((engine (nth 4 (assq TeX-engine (TeX-engine-alist)))))
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)))
1504 ;; ConTeXt variables that are interface aware
1505 ;; They are mapped to interface specific variables
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))
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)
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)
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
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)
1544 ;; Make language specific variables buffer local
1545 (dolist (symbol ConTeXt-language-variable-list)
1546 (make-variable-buffer-local symbol))
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)))))
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))
1561 (use-local-map ConTeXt-mode-map)
1564 (set (make-local-variable 'indent-line-function) 'ConTeXt-indent-line)
1565 (set (make-local-variable 'fill-indent-according-to-mode) t)
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
1577 (ConTeXt-paragraph-commands-regexp) "\\|"
1578 "\\$\\$\\|" ; Plain TeX display math
1580 (setq paragraph-separate
1583 "\\$\\$" ; Plain TeX display math
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)
1592 (if (= emacs-major-version 20)
1593 (make-local-hook 'activate-menubar-hook))
1594 (add-hook 'activate-menubar-hook 'ConTeXt-menu-update nil t)
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))
1604 ;; font switch support
1605 (set (make-local-variable 'TeX-font-list) ConTeXt-font-list)
1608 (set (make-local-variable 'imenu-create-index-function)
1609 'ConTeXt-imenu-create-index-function)
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))
1616 (defun context-guess-current-interface ()
1617 "Guess what ConTeXt interface the current buffer is using."
1620 (goto-char (point-min))
1621 (setq ConTeXt-current-interface
1622 (cond ((re-search-forward "%.*?interface=en" (+ 512 (point)) t)
1624 ((re-search-forward "%.*?interface=nl" (+ 512 (point)) t)
1626 ((re-search-forward "\\\\starttext" (+ 1024 (point)) t)
1628 ((re-search-forward "\\\\starttekst" (+ 1024 (point)) t)
1631 ConTeXt-default-interface)))))
1634 (defalias 'ConTeXt-mode 'context-mode)
1637 (defun context-mode ()
1638 "Major mode in AUCTeX for editing ConTeXt files.
1641 \\{ConTeXt-mode-map}
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."
1647 (context-guess-current-interface)
1648 (require (intern (concat "context-" ConTeXt-current-interface)))
1649 (funcall (intern (concat "context-" ConTeXt-current-interface "-mode"))))
1653 ;;; context.el ends here