forked from emacsmirror/auctex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.el
1837 lines (1564 loc) · 67.7 KB
/
context.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; context.el --- Support for ConTeXt documents. -*- lexical-binding: t; -*-
;; Copyright (C) 2003-2022 Free Software Foundation, Inc.
;; Maintainer: Berend de Boer <[email protected]>
;; Keywords: tex
;; This file is part of AUCTeX.
;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING. If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;; This is in progress ConTeXt support for AUCTeX. Please report
;; anomalies or things you believe should be added.
;; AUCTeX is closely interwoven with LaTeX. We have to split up
;; things without breaking 'em.
;; some parts are stolen from latex.el and adapted to ConTeXt.
;; TODO
;; 1. indentation still bad.
;; 2. paragraph refilling doesn't work 100%, and is very slow.
;; 4. Remove dependency on LaTeX by moving LaTeX commands to TeX.
;; 5. Most ConTeXt macro's don't currently have lisp code to query for
;; arguments. As ConTeXt arguments are quite complex, the LaTeX way
;; of querying for arguments just doesn't cut it.
;; 6. Check auto-parsing: does it detect % interface=nl for example?
;; 7. Complete adding ConTeXt macro's. Perhaps parse cont-en.xml and
;; generate the interfaces?
;; 8. Add to menu: make TeX hash (mktexlsr), context format and metapost format.
;; TODO Documentation
;; 1. multifile done differently with ConTeXt
;;; Code:
(require 'tex)
(require 'latex) ; for functions like `TeX-look-at' and `LaTeX-split-long-menu'
(require 'plain-tex) ; for `plain-TeX-common-initialization'
;; Silence the compiler:
(defvar ConTeXt-extra-paragraph-commands)
(defgroup ConTeXt-macro nil
"Special support for ConTeXt macros in AUCTeX."
:prefix "TeX-"
:group 'ConTeXt
:group 'TeX-macro)
;; others
(defvar ConTeXt-known-interfaces '("cz" "de" "en" "it" "nl" "ro" "uk"))
(defcustom ConTeXt-default-interface "en"
"Default interface to be used when running ConTeXt."
:group 'ConTeXt
:type 'string)
(defvar ConTeXt-current-interface "en"
"Interface to be used for inserting macros and ConTeXt run.")
(make-variable-buffer-local 'ConTeXt-current-interface)
(defvar ConTeXt-menu-changed nil)
;; Need to update ConTeXt menu.
(make-variable-buffer-local 'ConTeXt-menu-changed)
(defvar ConTeXt-largest-level nil
"Largest sectioning level within current document.")
(make-variable-buffer-local 'ConTeXt-largest-level)
(defun ConTeXt-largest-level ()
(TeX-update-style)
ConTeXt-largest-level)
;;; Syntax
(defvar ConTeXt-optop "["
"The ConTeXt optional argument opening character.")
(defvar ConTeXt-optcl "]"
"The ConTeXt optional argument closing character.")
;; Define a ConTeXt macro
(defvar ConTeXt-define-list ()
"Calls ConTeXt-XX-define-list where XX is the current interface.")
(defun ConTeXt-define-command (what)
"The ConTeXt macro to define WHAT."
(funcall
(intern (concat "ConTeXt-define-command-" ConTeXt-current-interface)) what))
(defun ConTeXt-insert-define (define)
"Insert the ConTeXt define macro DEFINE."
(insert TeX-esc (ConTeXt-define-command define))
(newline)
(indent-according-to-mode)
(ConTeXt-arg-setup nil))
;; Setup a ConTeXt macro
(defvar ConTeXt-setup-list ()
"Calls ConTeXt-XX-setup-list where XX is the current interface.")
(defun ConTeXt-setup-command (what)
"The ConTeXt macro to setup WHAT."
(funcall
(intern (concat "ConTeXt-setup-command-" ConTeXt-current-interface)) what))
(defun ConTeXt-insert-setup (setup)
"Insert the ConTeXt setup macro SETUP."
(insert TeX-esc (ConTeXt-setup-command setup))
(newline)
(indent-according-to-mode)
(ConTeXt-arg-setup nil))
;; Referencing ConTeXt macro's
(defvar ConTeXt-referencing-list ()
"Calls ConTeXt-XX-referencing-list where XX is the current interface.")
(defun ConTeXt-referencing-command (what)
"The ConTeXt macro to call WHAT is itself, no interface specific calls."
what)
(defun ConTeXt-insert-referencing (what)
"Insert the ConTeXt referencing WHAT."
(insert TeX-esc (ConTeXt-referencing-command what))
(newline)
(indent-according-to-mode)
(ConTeXt-arg-setup nil))
;; Other ConTeXt macro's
(defvar ConTeXt-other-macro-list ()
"Calls ConTeXt-XX-other-macro-list where XX is the current interface.")
(defun ConTeXt-other-macro-command (what)
"The ConTeXt macro to call WHAT is itself, no interface specific calls."
what)
(defun ConTeXt-insert-other-macro (other-macro)
"Insert the ConTeXt other macro's macro OTHER-MACRO."
(insert TeX-esc (ConTeXt-other-macro-command other-macro))
(newline)
(indent-according-to-mode)
(ConTeXt-arg-setup nil))
;;; Project structure
(defvar ConTeXt-project-structure-list ()
"Calls ConTeXt-XX-project-structure where XX is the current interface.")
(defun ConTeXt-project-structure (N)
"Insert a ConTeXt project structure.
N is in index into `ConTeXt-project-structure-list'."
(funcall (intern(concat
"ConTeXt-project-"
(nth N ConTeXt-project-structure-list)
"-insert"))))
(defun ConTeXt-project-project-insert ()
"Insert a basic template for a new ConTeXt project."
(save-excursion
(insert "% The following names are examples only\n")
(insert TeX-esc (ConTeXt-environment-start-name) (nth 0 ConTeXt-project-structure-list) " myproject")
(newline 2)
(insert TeX-esc (nth 1 ConTeXt-project-structure-list) " myenvironment")
(newline 2)
(insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
(newline 2)
(insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct2")
(newline 2)
(insert TeX-esc (ConTeXt-environment-stop-name) (nth 0 ConTeXt-project-structure-list))))
(defun ConTeXt-project-environment-insert ()
"Insert a basic template for the environment of a ConTeXt project."
(save-excursion
(insert "% The name 'myenvironment' is an example only.\n"
"% It must match the name in your project file.\n")
(insert TeX-esc (ConTeXt-environment-start-name)
(nth 1 ConTeXt-project-structure-list) " myenvironment\n\n")
(insert "% Put environment charateristics that must be defined at the "
"highest level here\n\n")
(insert TeX-esc (ConTeXt-environment-stop-name)
(nth 1 ConTeXt-project-structure-list))))
(defun ConTeXt-project-product-insert ()
"Insert a basic template for a product of a ConTeXt project."
(save-excursion
(insert "% The following names are examples only\n")
(insert TeX-esc (ConTeXt-environment-start-name)
(nth 2 ConTeXt-project-structure-list) " myproduct1")
(newline 2)
(insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject")
(newline 2)
(insert "% Components are optional. "
"You can also just start your document here.\n")
(insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent1")
(newline 2)
(insert TeX-esc (nth 3 ConTeXt-project-structure-list) " mycomponent2")
(newline 2)
(insert TeX-esc (ConTeXt-environment-stop-name)
(nth 2 ConTeXt-project-structure-list))))
(defun ConTeXt-project-component-insert ()
"Insert a basic template for a component of a ConTeXt project."
(save-excursion
(insert "% The following names are examples only\n")
(insert TeX-esc (ConTeXt-environment-start-name)
(nth 3 ConTeXt-project-structure-list) " mycomponent1")
(newline 2)
(insert TeX-esc (nth 0 ConTeXt-project-structure-list) " myproject\n")
(insert TeX-esc (nth 2 ConTeXt-project-structure-list) " myproduct1")
(newline 2)
(insert "% ... text here ...")
(newline 2)
(insert TeX-esc (ConTeXt-environment-stop-name)
(nth 3 ConTeXt-project-structure-list))))
;;; Section blocks
(defvar ConTeXt-section-block-list ()
"Calls ConTeXt-XX-section-list where XX is the current interface.")
(defun ConTeXt-section-block (section-block)
"Insert the ConTeXt section block SECTION-BLOCK."
(ConTeXt-environment-menu section-block))
;;; Sections
;; Declare dynamically scoped vars.
(defvar ConTeXt-title nil "Dynamically bound by `ConTeXt-section'.")
(defvar ConTeXt-name nil "Dynamically bound by `ConTeXt-section'.")
(defvar ConTeXt-level nil "Dynamically bound by `ConTeXt-section'.")
(defvar ConTeXt-done-mark nil "Dynamically bound by `ConTeXt-section'.")
(defvar ConTeXt-reference nil "Dynamically bound by `ConTeXt-section'.")
(defun ConTeXt-section (arg)
"Insert a template for a ConTeXt section.
Determinate the type of section to be inserted, by the argument ARG.
If ARG is nil or missing, use the current level.
If ARG is a list (selected by \\[universal-argument]), go downward one level.
If ARG is negative, go up that many levels.
If ARG is positive or zero, use absolute level:
0 : part
1 : chapter
2 : section
3 : subsection
4 : subsubsection
5 : subsubsubsection
Or:
0 : title
1 : subject
2 : subsubject
3 : subsubsubject
The following variables can be set to customize:
`ConTeXt-numbered-section-hook' Hooks to run when inserting a section.
`ConTeXt-section-ref' Prefix to all section references."
(interactive "*P")
(let* ((val (prefix-numeric-value arg))
(ConTeXt-level (cond ((null arg)
(ConTeXt-current-section))
((listp arg)
(ConTeXt-down-section))
((< val 0)
(ConTeXt-up-section (- val)))
(t val)))
(ConTeXt-name (ConTeXt-numbered-section-name ConTeXt-level))
(ConTeXt-title "")
(ConTeXt-reference nil)
(ConTeXt-done-mark (make-marker)))
(newline)
(run-hooks 'ConTeXt-numbered-section-hook)
(newline)
(if (marker-position ConTeXt-done-mark)
(goto-char (marker-position ConTeXt-done-mark)))
(set-marker ConTeXt-done-mark nil)))
;; LaTeX has a max function here, which makes no sense.
;; I think you want to insert a section that is max ConTeXt-largest-level
(defun ConTeXt-current-section ()
"Return the level of the section that contain point.
See also `ConTeXt-section' for description of levels."
(save-excursion
(min (ConTeXt-largest-level)
(if (re-search-backward outline-regexp nil t)
(+ 1 (- (ConTeXt-outline-level) (ConTeXt-outline-offset)))
(ConTeXt-largest-level)))))
(defun ConTeXt-down-section ()
"Return the value of a section one level under the current.
Tries to find what kind of section that have been used earlier in the
text, if this fail, it will just return one less than the current
section."
(save-excursion
(let ((current (ConTeXt-current-section))
(next nil)
(regexp outline-regexp))
(if (not (re-search-backward regexp nil t))
(1+ current)
(while (not next)
(cond
((eq (ConTeXt-current-section) current)
(if (re-search-forward regexp nil t)
(if (<= (setq next (ConTeXt-current-section)) current) ;Wow!
(setq next (1+ current)))
(setq next (1+ current))))
((not (re-search-backward regexp nil t))
(setq next (1+ current)))))
next))))
(defun ConTeXt-up-section (arg)
"Return the value of the section ARG levels above this one."
(save-excursion
(if (zerop arg)
(ConTeXt-current-section)
(let ((current (ConTeXt-current-section)))
(while (and (>= (ConTeXt-current-section) current)
(re-search-backward outline-regexp
nil t)))
(ConTeXt-up-section (1- arg))))))
(defvar ConTeXt-numbered-section-list ()
"ConTeXt-XX-numbered-section-list where XX is the current interface.")
(defvar ConTeXt-unnumbered-section-list ()
"ConTeXt-XX-unnumbered-section-list where XX is the current interface.")
(defvar ConTeXt-section-list
(append ConTeXt-numbered-section-list ConTeXt-unnumbered-section-list)
)
(defun ConTeXt-numbered-section-name (level)
"Return the name of the section corresponding to LEVEL."
(let ((entry (TeX-member level ConTeXt-numbered-section-list
(function (lambda (a b) (equal a (nth 1 b)))))))
(if entry
(nth 0 entry)
nil)))
(defun ConTeXt-unnumbered-section-name (level)
"Return the name of the section corresponding to LEVEL."
(let ((entry (TeX-member level ConTeXt-unnumbered-section-list
(function (lambda (a b) (equal a (nth 1 b)))))))
(if entry
(nth 0 entry)
nil)))
(defun ConTeXt-numbered-section-level (name)
"Return the level of the section NAME."
(let ((entry (TeX-member name ConTeXt-numbered-section-list
(function (lambda (a b) (equal a (nth 0 b)))))))
(if entry
(nth 1 entry)
nil)))
(defun ConTeXt-unnumbered-section-level (name)
"Return the level of the section NAME."
(let ((entry (TeX-member name ConTeXt-unnumbered-section-list
(function (lambda (a b) (equal a (nth 0 b)))))))
(if entry
(nth 1 entry)
nil)))
;;; Section Hooks.
(defcustom ConTeXt-numbered-section-hook
'(ConTeXt-numbered-section-heading
ConTeXt-section-title
ConTeXt-section-ref
ConTeXt-section-section)
"List of hooks to run when a new section is inserted.
The following variables are set before the hooks are run
`ConTeXt-level' - numeric section level, see the documentation of
`ConTeXt-section'.
`ConTeXt-name' - name of the sectioning command, derived from
`ConTeXt-level'.
`ConTeXt-title' - The title of the section, default to an empty
string.
`ConTeXt-done-mark' - Position of point afterwards, default nil
(meaning end).
The following standard hooks exist -
ConTeXt-numbered-section-heading: Query the user about the name
of the sectioning command. Modifies `ConTeXt-level' and
`ConTeXt-name'.
ConTeXt-section-title: Query the user about the title of the
section. Modifies `ConTeXt-title'.
ConTeXt-section-section: Insert ConTeXt section command according
to `ConTeXt-name', `ConTeXt-title', and `ConTeXt-reference'. If
`ConTeXt-title' is an empty string, `ConTeXt-done-mark' will be
placed at the point they should be inserted.
ConTeXt-section-ref: Insert a reference for this section command.
To get a full featured `ConTeXt-section' command, insert
(setq ConTeXt-numbered-section-hook
\\='(ConTeXt-numbered-section-heading
ConTeXt-section-title
ConTeXt-section-section
ConTeXt-section-ref))
in your init file such as .emacs.d/init.el or .emacs."
:group 'ConTeXt-macro
:type 'hook
:options
'(ConTeXt-numbered-section-heading
ConTeXt-section-title
ConTeXt-section-ref
ConTeXt-section-section))
(defcustom ConTeXt-unnumbered-section-hook
'(ConTeXt-unnumbered-section-heading
ConTeXt-section-title
ConTeXt-section-ref
ConTeXt-section-section)
;; FIXME: I can't see where this variable is used!
"List of hooks to run when a new section is inserted.
The following variables are set before the hooks are run
`ConTeXt-level' - numeric section level, see the documentation of
`ConTeXt-section'.
`ConTeXt-name' - name of the sectioning command, derived from
`ConTeXt-level'.
`ConTeXt-title' - The title of the section, default to an empty
string.
`ConTeXt-done-mark' - Position of point afterwards, default nil
(meaning end).
The following standard hooks exist -
ConTeXt-unnumbered-section-heading: Query the user about the name
of the sectioning command. Modifies `ConTeXt-level' and
`ConTeXt-name'.
ConTeXt-section-title: Query the user about the title of the
section. Modifies `ConTeXt-title'.
ConTeXt-section-section: Insert ConTeXt section command according
to `ConTeXt-name', `ConTeXt-title', and `ConTeXt-reference'. If
`ConTeXt-title' is an empty string, `ConTeXt-done-mark' will be
placed at the point they should be inserted.
ConTeXt-section-ref: Insert a reference for this section command.
To get a full featured `ConTeXt-section' command, insert
(setq ConTeXt-unnumbered-section-hook
\\='(ConTeXt-unnumbered-section-heading
ConTeXt-section-title
ConTeXt-section-section
ConTeXt-section-ref))
in your init file such as .emacs.d/init.el or .emacs."
:group 'ConTeXt-macro
:type 'hook
:options
'(ConTeXt-unnumbered-section-heading
ConTeXt-section-title
ConTeXt-section-ref
ConTeXt-section-section))
;; Define before first use.
(defcustom ConTeXt-Mark-version "II"
"ConTeXt Mark version used for running ConTeXt."
:type 'string
:group 'TeX-command)
(make-variable-buffer-local 'ConTeXt-Mark-version)
(put 'ConTeXt-Mark-version 'safe-local-variable #'stringp)
(defun ConTeXt-numbered-section-heading ()
"Hook to prompt for ConTeXt section name.
Insert this hook into `ConTeXt-numbered-section-hook' to allow
the user to change the name of the sectioning command inserted
with `\\[ConTeXt-section]'."
(let ((string (completing-read
(concat "Select level (default " ConTeXt-name "): ")
ConTeXt-numbered-section-list
nil nil nil)))
;; Update name
(if (not (zerop (length string)))
(setq ConTeXt-name string))))
(defun ConTeXt-unnumbered-section-heading ()
"Hook to prompt for ConTeXt section name.
Insert this hook into `ConTeXt-unnumbered-section-hook' to allow
the user to change the name of the sectioning command inserted
with `\\[ConTeXt-section]'."
(let ((string (completing-read
(concat "Select level (default " ConTeXt-name "): ")
ConTeXt-unnumbered-section-list
nil nil nil)))
;; Update name
(if (not (zerop (length string)))
(setq ConTeXt-name string))))
(defun ConTeXt-section-title ()
"Hook to prompt for ConTeXt section title.
Insert this hook into `ConTeXt-(un)numbered-section-hook' to
allow the user to change the title of the section inserted with
`\\[ConTeXt-section]."
(setq ConTeXt-title (TeX-read-string "What title: ")))
(defun ConTeXt-section-section ()
"Hook to insert ConTeXt section command into the file.
Insert this hook into `ConTeXt-numbered-section-hook' after those hooks
which sets the `ConTeXt-name', `ConTeXt-title', and
`ConTeXt-reference' variables, but before those hooks which
assumes the section already is inserted."
(insert TeX-esc ConTeXt-name)
(cond ((null ConTeXt-reference))
((zerop (length ConTeXt-reference))
(insert ConTeXt-optop)
(set-marker ConTeXt-done-mark (point))
(insert ConTeXt-optcl))
(t
(insert ConTeXt-optop ConTeXt-reference ConTeXt-optcl)))
(insert TeX-grop)
(if (zerop (length ConTeXt-title))
(set-marker ConTeXt-done-mark (point)))
(insert ConTeXt-title TeX-grcl)
(newline)
;; If RefTeX is available, tell it that we've just made a new section
(and (fboundp 'reftex-notice-new-section)
(funcall (symbol-function 'reftex-notice-new-section))))
(defun ConTeXt-section-ref ()
"Hook to insert a reference after the sectioning command.
Insert this hook into `ConTeXt-numbered-section-hook' to prompt
for a label to be inserted after the sectioning command."
(setq ConTeXt-reference
(completing-read
(TeX-argument-prompt t nil
"Comma separated list of references")
(LaTeX-label-list) nil nil))
;; No reference or empty string entered?
(if (string-equal "" ConTeXt-reference)
(setq ConTeXt-reference nil)))
;; Various
(defun TeX-ConTeXt-sentinel (process name)
"Cleanup TeX output buffer after running ConTeXt."
(cond
;; Mark IV
((with-current-buffer TeX-command-buffer
(string= ConTeXt-Mark-version "IV"))
(cond ((TeX-TeX-sentinel-check process name))
((re-search-forward "fatal error: " nil t)
(message (concat name ": problems after "
(TeX-current-pages)))
(setq TeX-command-next TeX-command-default))
(t
(message (concat name ": successfully formatted "
(TeX-current-pages)))
(setq TeX-command-next TeX-command-Show))))
;; Mark II
(t
(cond ((TeX-TeX-sentinel-check process name))
((save-excursion
;; in a full ConTeXt run there will multiple texutil
;; outputs. Just looking for "another run needed" would
;; find the first occurence
(goto-char (point-max))
(re-search-backward "TeXUtil " nil t)
(re-search-forward "another run needed" nil t))
(message (concat "You should run ConTeXt again "
"to get references right, "
(TeX-current-pages)))
(setq TeX-command-next TeX-command-default))
((re-search-forward "removed files :" nil t)
(message "sucessfully cleaned up"))
((re-search-forward "^ ?TeX\\(Exec\\|Util\\)" nil t) ;; strange regexp --pg
(message (concat name ": successfully formatted "
(TeX-current-pages)))
(setq TeX-command-next TeX-command-Show))
(t
(message (concat name ": problems after "
(TeX-current-pages)))
(setq TeX-command-next TeX-command-default)))))
(unless TeX-error-list
(run-hook-with-args 'TeX-after-compilation-finished-functions
(with-current-buffer TeX-command-buffer
(expand-file-name
(TeX-active-master (TeX-output-extension)))))))
;;; Environments
(defgroup ConTeXt-environment nil
"Environments in ConTeXt."
:group 'ConTeXt-macro)
;; TODO: interface awareness
(defcustom ConTeXt-default-environment "itemize"
"The default environment when creating new ones with `ConTeXt-environment'."
:group 'ConTeXt-environment
:type 'string)
(make-variable-buffer-local 'ConTeXt-default-environment)
(TeX-auto-add-type "environment" "ConTeXt")
(advice-add 'ConTeXt-add-environments :after #'ConTeXt--invalidate-menu)
(defun ConTeXt--invalidate-menu (&rest _)
"Mark the menu as being in need of a refresh."
(setq ConTeXt-menu-changed t))
;; (defvar ConTeXt-environment-list ()
;; "ConTeXt-environment-list-XX where XX is the current interface.")
(defvar ConTeXt-environment-history nil)
(defun ConTeXt-environment-start-name ()
"Return the \\start translated to the language in current interface."
;; it is "inizia", others are "start"
(cond ((equal ConTeXt-current-interface "it")
"inizia")
((member ConTeXt-current-interface ConTeXt-known-interfaces)
"start")
(t
;; this should not happen
(error "Unknown interface: %s" ConTeXt-current-interface))))
(defun ConTeXt-environment-stop-name ()
"Return the \\stop translated to the language in current interface."
;; it is "termina", others are "stop"
(cond ((equal ConTeXt-current-interface "it")
"termina")
((member ConTeXt-current-interface ConTeXt-known-interfaces)
"stop")
(t
;; this should not happen
(error "Unknown interface: %s" ConTeXt-current-interface))))
(defun ConTeXt-environment (arg)
"Make ConTeXt environment (\\start...-\\stop... pair).
With optional ARG, modify current environment."
(interactive "*P")
(let* ((default (cond
((TeX-near-bobp) "text")
(t ConTeXt-default-environment)))
(environment
(completing-read (concat "Environment type (default " default "): ")
ConTeXt-environment-list nil nil nil
'ConTeXt-environment-history default)))
;; Use `environment' as default for the next time only if it is different
;; from the current default.
(unless (equal environment default)
(setq ConTeXt-default-environment environment))
(let ((entry (assoc environment ConTeXt-environment-list)))
(if (null entry)
(ConTeXt-add-environments (list environment)))
(if arg
(ConTeXt-modify-environment environment)
(ConTeXt-environment-menu environment)))))
(defun ConTeXt-modify-environment (environment)
"Modify current environment."
(save-excursion
(ConTeXt-find-matching-stop)
(re-search-backward (concat (regexp-quote TeX-esc)
(ConTeXt-environment-stop-name)
" *\\([a-zA-Z]*\\)")
(save-excursion (beginning-of-line 1) (point)))
(replace-match
(concat TeX-esc (ConTeXt-environment-stop-name) environment) t t)
(beginning-of-line 1)
(ConTeXt-find-matching-start)
(re-search-forward (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)
" *\\([a-zA-Z]*\\)")
(save-excursion (end-of-line 1) (point)))
(replace-match
(concat TeX-esc (ConTeXt-environment-start-name) environment) t t)))
(defun ConTeXt-environment-menu (environment)
"Insert ENVIRONMENT around point or region."
(let ((entry (assoc environment ConTeXt-environment-list)))
(cond ((not (and entry (nth 1 entry)))
(ConTeXt-insert-environment environment))
((numberp (nth 1 entry))
(let ((count (nth 1 entry))
(args ""))
(while (> count 0)
(setq args (concat args TeX-grop TeX-grcl))
(setq count (- count 1)))
(ConTeXt-insert-environment environment args)))
((stringp (nth 1 entry))
(let ((prompts (cdr entry))
(args ""))
(while prompts
(setq args (concat args
TeX-grop
(read-from-minibuffer
(concat (car prompts) ": "))
TeX-grcl))
(setq prompts (cdr prompts)))
(ConTeXt-insert-environment environment args)))
(t
(apply (nth 1 entry) environment (nthcdr 2 entry))))))
(defun ConTeXt-close-environment ()
"Insert \\stop... to match the current environment."
(interactive "*")
(beginning-of-line)
(let ((empty-line (looking-at "[ \t]*$")))
(end-of-line)
(if (not empty-line)
(newline)))
(insert TeX-esc (ConTeXt-environment-stop-name)
(ConTeXt-current-environment))
;; indent broken, so don't do it.
;;(indent-according-to-mode)
(end-of-line)
(newline))
(defun ConTeXt-insert-environment (environment &optional extra)
"Insert ENVIRONMENT, with optional argument EXTRA."
(if (and (TeX-active-mark)
(not (eq (mark) (point))))
(save-excursion
(if (< (mark) (point))
(exchange-point-and-mark))
(insert TeX-esc (ConTeXt-environment-start-name) environment)
(newline)
(forward-line -1)
(indent-according-to-mode)
(if extra (insert extra))
(goto-char (mark))
(or (TeX-looking-at-backward "^[ \t]*")
(newline))
(insert TeX-esc (ConTeXt-environment-stop-name) environment)
(newline)
(forward-line -1)
(indent-according-to-mode)
;;(goto-char (point))
)
(or (TeX-looking-at-backward "^[ \t]*")
(newline))
(insert TeX-esc (ConTeXt-environment-start-name) environment)
(indent-according-to-mode)
(if extra (insert extra))
(end-of-line)
(newline-and-indent)
(newline)
(insert TeX-esc (ConTeXt-environment-stop-name) environment)
(or (looking-at "[ \t]*$")
(save-excursion (newline-and-indent)))
(indent-according-to-mode)
(end-of-line 0)))
;; with the following we can call a function on an environment. Say
;; you have metapost stuff within your TeX file, go to the environment
;; and run ConTeXt-work-on-environment (suggested Key: C-c !). AUCTeX
;; sees that you are inside e.g. \startMPpage....\stopMPpage and
;; looks in ConTeXt-environment-helper for a function to be called.
;; % so pressing C-c ! inside the following ...
;;\startuseMPgraphic{Logo}{Scale}
;; % Top rectangle
;; filldraw (0,0)--(2cm,0)--(2cm,1cm)--(0,1cm)--cycle withcolor blue ;
;; % Bottom black rectangle
;; drawfill (0,0)--(2cm,0)--(2cm,-1cm)--(0,-1cm)--cycle withcolor black;
;; % White Text
;; draw btex \bf AB etex withcolor white ;
;; % resize to size
;; currentpicture := currentpicture scaled \MPvar{Scale} ;
;; \stopuseMPgraphic
;; % ...should give you a "new buffer" (currently narrowed to region
;; % and switched to metapost-mode and recursive-edit)
;; % Top rectangle
;; filldraw (0,0)--(2cm,0)--(2cm,1cm)--(0,1cm)--cycle withcolor blue ;
;; % Bottom black rectangle
;; drawfill (0,0)--(2cm,0)--(2cm,-1cm)--(0,-1cm)--cycle withcolor black;
;; % White Text
;; draw btex \bf AB etex withcolor white ;
;; % resize to size
;; currentpicture := currentpicture scaled \MPvar{Scale} ;
(defvar ConTeXt-environment-helper
'(("useMPgraphic" . ConTeXt-mp-region)
("MPpage" . ConTeXt-mp-region))
"Alist that holds functions to call for working on regions.
An entry looks like: (\"environment\" . function)")
(defun ConTeXt-mp-region ()
"Edit region in `metapost-mode'."
(ConTeXt-mark-environment t)
(narrow-to-region (mark) (point))
(metapost-mode)
(message "Type `M-x exit-recursive-edit' to get back")
(recursive-edit)
(context-mode)
(widen))
;; find smarter name. Suggestions welcome
(defun ConTeXt-work-on-environment ()
"Takes current environment and does something on it (todo: documentation)."
(interactive)
(let ((fun (cdr (assoc (ConTeXt-current-environment)
ConTeXt-environment-helper))))
(when (functionp fun)
(funcall fun))))
(defun ConTeXt-current-environment ()
"Return the name of the current environment."
;; don't make this interactive.
(let ((beg))
(save-excursion
(ConTeXt-last-unended-start)
(setq beg (+ (point) (length (ConTeXt-environment-start-name)) 1))
(goto-char (match-end 0))
(skip-chars-forward "a-zA-Z")
(buffer-substring beg (point)))))
(defun ConTeXt-last-unended-start ()
"Leave point at the beginning of the last unstopped `\\start...'.
Look back from the current cursor."
(while (and (re-search-backward "\\\\start[a-zA-Z]*\\|\\\\stop[a-zA-Z]*")
(looking-at "\\\\stop[a-zA-Z]*"))
(ConTeXt-last-unended-start)))
(defun ConTeXt-mark-environment (&optional inner)
"Set mark to end of current environment (\\start...-\\stop...) and
point to the matching begin.
If optional INNER is not nil, include \\start... and \\stop, otherwise only
the contents."
(interactive)
(let ((cur (point)))
(ConTeXt-find-matching-stop inner)
(push-mark (point))
(goto-char cur)
(ConTeXt-find-matching-start inner)
(TeX-activate-region)))
(defun ConTeXt-find-matching-stop (&optional inner)
"Find end of current \\start...\\stop-Pair.
If INNER is non-nil, go to the point just past before
\\stop... macro. Otherwise goto the point just past \\stop..."
(interactive)
(let ((regexp (concat (regexp-quote TeX-esc)
"\\("
(ConTeXt-environment-start-name)
"\\|"
(ConTeXt-environment-stop-name)
"\\)"
))
(level 1))
;;jump over the \start... when at the beginning of it.
(when (looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)))
(re-search-forward regexp nil t))
(while (and (> level 0)
(re-search-forward regexp nil t)
(goto-char (1- (match-beginning 1)))
(cond ((looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)))
(re-search-forward regexp nil t)
(setq level (1+ level)))
((looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-stop-name)))
(re-search-forward regexp nil t)
(setq level (1- level))))))
;; now we have to look if we want to start behind the \start... macro
(if inner
(beginning-of-line)
(skip-chars-forward "a-zA-Z"))))
(defun ConTeXt-find-matching-start (&optional inner)
"Find beginning of current \\start...\\stop-Pair.
If INNER is non-nil, go to the point just past the \\start... macro."
(interactive)
(let ((regexp (concat (regexp-quote TeX-esc)
"\\("
(ConTeXt-environment-start-name)
"\\|"
(ConTeXt-environment-stop-name)
"\\)"
))
(level 1)
(pos))
(while (and (> level 0)
(re-search-backward regexp nil t)
(cond ((looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-stop-name)))
(setq level (1+ level)))
((looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)))
(setq level (1- level))))))
;; now we have to look if we want to start behind the \start... macro
(when inner
;; \startfoo can have 0 or more {} and [] pairs. I assume that
;; skipping all those parens will be smart enough. It fails when
;; the first part in the \start-\stop-environment is { or [, like
;; in \startquotation {\em important} \stopquotation. There is
;; yet another pitfall: \startsetups SomeSetup foo bar
;; \stopsetups will use SomeSetup as the argument and the
;; environment
(skip-chars-forward "\\\\a-zA-Z")
(save-excursion
(while (progn
(skip-chars-forward "\t\n ")
(forward-comment 1)
(skip-chars-forward "\t\n ")
(looking-at "\\s\("))
(forward-list 1)
(setq pos (point))))
(when pos
(goto-char pos))
(unless (bolp)
(forward-line)))))
;;; items
(defun ConTeXt-insert-item ()
"Insert a new item."
(interactive "*")
(or (TeX-looking-at-backward "^[ \t]*")
(newline))
(TeX-insert-macro "item")
(indent-according-to-mode))
;;; Macro Argument Hooks
(defun ConTeXt-optional-argument-insert (arg &optional _prefix)
"Insert ARG surrounded by square brackets."
(insert ConTeXt-optop)
(insert arg)
(insert ConTeXt-optcl))
(defun ConTeXt-required-argument-insert (arg &optional _prefix)
"Insert ARG surrounded by curly braces."
(insert TeX-grop)
(insert arg)
(insert TeX-grcl))
(defun ConTeXt-argument-insert (arg optional &optional prefix)
"Insert ARG surrounded by curly braces.
If OPTIONAL, only insert it if not empty, and then use square brackets."
(if optional
(if
(not (string-equal arg ""))
(ConTeXt-optional-argument-insert arg prefix))
(ConTeXt-required-argument-insert arg prefix)))
(defun ConTeXt-arg-ref (optional &optional prompt definition)
"Prompt for a reference completing with known references."
(let ((ref (completing-read (TeX-argument-prompt optional prompt "ref")
(LaTeX-label-list))))
(if (and definition (not (string-equal "" ref)))
(LaTeX-add-labels ref))
(ConTeXt-argument-insert ref optional)))