-
Notifications
You must be signed in to change notification settings - Fork 89
/
asy-mode.el
1619 lines (1468 loc) · 70.3 KB
/
asy-mode.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
;;; asy-mode.el --- Major mode for editing Asymptote source code.
;; Copyright (C) 2006-8
;; Author: Philippe IVALDI 20 August 2006
;; Maintainer: John Bowman
;; URL: https://github.com/vectorgraphics/asymptote
;; Version: 1.6
;; Keywords: language, mode
;;; License:
;; This program is free software ; you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as published by
;; the Free Software Foundation ; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program 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
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with this program ; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;; Commentary
;; Major mode for editing Asymptote source code.
;; INSTALLATION:
;; Place this file (asy-mode.el) and asy-keywords.el in your Emacs load path.
;; Then choose ONE of the following installation methods:
;; * Method 1:
;; Copy and uncomment the following lines to your .emacs initialization file:
;;(autoload 'asy-mode "asy-mode.el" "Asymptote major mode." t)
;;(autoload 'lasy-mode "asy-mode.el" "hybrid Asymptote/Latex major mode." t)
;;(autoload 'asy-insinuate-latex "asy-mode.el" "Asymptote insinuate LaTeX." t)
;;(add-to-list 'auto-mode-alist '("\\.asy$" . asy-mode))
;; * Method 2:
;; Copy and uncomment the following line to your .emacs initialization file:
;;(require 'asy-mode)
;; Notes:
;;
;; For full functionality the two-mode-mode package should also be installed
;; (http://www.dedasys.com/freesoftware/files/two-mode-mode.el).
;;
;; See also paragraph II of the documentation below to automate asy-insinuate-latex.
;;; Code:
(defvar asy-mode-version "1.6")
;;;###autoload
(define-derived-mode asy-mode objc-mode "Asymptote"
"Emacs mode for editing Asymptote source code.
For full functionality the `two-mode-mode' package should also be installed
(http://www.dedasys.com/freesoftware/files/two-mode-mode.el).
I. This package provides two modes:
1- asy-mode:
All the files with the extension '.asy' are edited in this mode, which provides the following features:
* Syntax color highlighting;
* Compiling and viewing current buffer with the key binding C-c C-c;
* Moving cursor to the error by pressing the key F4.
* Showing the available function prototypes for the command at the cursor with the key binding C-c ?
* Compiling and viewing a TeX document linked with the current buffer (usually a document that includes the output picture).
To link a Tex document try 'M-x asy-set-master-tex' follow by C-Return (see descriptions further of the key binding C-Return, C-S-Return, M-Return, M-S-Return etc within 2- lasy-mode)
2- lasy-mode
Editing a TeX file that contains Asymptote code is facilitated with the hybrid mode 'lasy-mode'.
Toggle lasy-mode with M-x lasy-mode.
In this hybrid mode the major mode is LaTeX when the cursor is in LaTeX code and becomes asy-mode when the cursor is between '\\begin{asy}' and '\\end{asy}'.
All the features of asy-mode are provided and the key binding C-c C-c of asy-mode compiles and views only the code of the picture where the cursor is.
Note that some keys binding are added to the LaTeX-mode-map in lasy-mode if the value of the variable lasy-extra-key is t (the default)
.
* C-return: compile (if the buffer/file is modified) and view the PostScript output with sequence [latex->[asy->latex]->dvips]->PSviewer
* M-return: same with pdf output and with the sequence [pdflatex->[asy->pdflatex]]->PDFviewer
* C-M-return: same with pdf output and with the sequence [latex->[asy->latex]->dvips->ps2pdf]->PSviewer
* Add the Shift key to the sequence of keys to compile even if the file is not modified.
II. To add a menu bar in current 'latex-mode' buffer and activate hot keys, use 'M-x asy-insinuate-latex <RET>'.
You can automate this feature for all the 'latex-mode' buffers by inserting the five following lines in your .emacs initialization file:
(eval-after-load \"latex\"
'(progn
;; Add here your personal features for 'latex-mode':
(asy-insinuate-latex t) ;; Asymptote globally insinuates Latex.
))
You can access this help within Emacs by the key binding C-h f asy-mode <RET>
BUGS:
This package has been tested in:
* Linux Debian Etch
- GNU Emacs 22.0.50.1
- GNU Emacs 21.4.1 (only basic errors management and basic font-lock features within lasy-mode are supported)
* WindowsXP
- GNU Emacs 22.0.990.1 (i386-mingw-nt5.1.2600)
This package seems to work with XEmacs 21.4 but not all the features are available (in particular syntax highlighting).
Report bugs to https://github.com/vectorgraphics/asymptote/issues
Some variables can be customized: M-x customize-group <RET> asymptote <RET>."
(setq c++-font-lock-extra-types (cons "true" c++-font-lock-extra-types)))
(require 'font-lock)
(require 'cc-mode)
(require 'cl-lib) ;; Common Lisp extensions for Emacs
(require 'compile)
(require 'wid-edit)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.asy$" . asy-mode))
(defvar running-xemacs-p (featurep 'xemacs))
(defvar running-unix-p (not (string-match "windows-nt\\|ms-dos" (symbol-name system-type))))
(when running-xemacs-p
(defalias 'turn-on-font-lock-if-enabled 'ignore)
(defalias 'line-number-at-pos 'line-number)
(defvar temporary-file-directory (temp-directory))
(defun replace-regexp-in-string (regexp rep string)
(replace-in-string string regexp rep))
)
(when (or (< emacs-major-version 22) running-xemacs-p)
;; Add regexp for parsing the compilation errors of asy
(add-to-list 'compilation-error-regexp-alist
'("\\(.*?.asy\\): \\(.*?\\)\\.\\(.*?\\):" 1 2 3)))
(when (< emacs-major-version 22)
(defun line-number-at-pos (&optional pos)
"Return (narrowed) buffer line number at position POS.
If POS is nil, use current buffer location.
Counting starts at (point-min), so the value refers
to the contents of the accessible portion of the buffer."
(let ((opoint (or pos (point))) start)
(save-excursion
(goto-char (point-min))
(setq start (point))
(goto-char opoint)
(forward-line 0)
(1+ (count-lines start (point)))))))
(defcustom lasy-extra-key t
"* If on, the folowing binding keys are added in lasy-mode :
(define-key lasy-mode-map (kbd \"<C-return>\") 'lasy-view-ps)
(define-key lasy-mode-map (kbd \"<C-S-return>\") 'asy-master-tex-view-ps-f)
(define-key lasy-mode-map (kbd \"<M-return>\") 'lasy-view-pdf-via-pdflatex)
(define-key lasy-mode-map (kbd \"<M-S-return>\") 'asy-master-tex-view-pdflatex-f)
(define-key lasy-mode-map (kbd \"<C-M-return>\") 'lasy-view-pdf-via-ps2pdf)
(define-key lasy-mode-map (kbd \"<C-M-S-return>\") 'asy-master-tex-view-ps2pdf-f)
If you also want this feature in pure latex-mode, you can set this variable to `nil' and add these lines in your .emacs:
(require 'asy-mode)
(eval-after-load \"latex\"
'(progn
(define-key LaTeX-mode-map (kbd \"<C-return>\") 'lasy-view-ps)
(define-key LaTeX-mode-map (kbd \"<C-S-return>\") 'asy-master-tex-view-ps-f)
(define-key LaTeX-mode-map (kbd \"<M-return>\") 'lasy-view-pdf-via-pdflatex)
(define-key LaTeX-mode-map (kbd \"<M-S-return>\") 'asy-master-tex-view-pdflatex-f)
(define-key LaTeX-mode-map (kbd \"<C-M-return>\") 'lasy-view-pdf-via-ps2pdf)
(define-key LaTeX-mode-map (kbd \"<C-M-S-return>\") 'asy-master-tex-view-ps2pdf-f)))"
:type 'boolean
:group 'asymptote)
(defcustom asy-compilation-buffer 'none
" 'visible means keep compilation buffer visible ;
'available means keep compilation buffer available in other buffer but not visible;
'none means delete compilation buffer automatically after a *successful* compilation.
'never means don't open any window or buffer attached to the compilation process.
If the value is 'never':
* Emacs is suspended until the child program returns;
* the management of errors is poorer than with other value;
* the compilation doesn't modify your current window configuration."
:type '(choice (const visible) (const available) (const none) (const never))
:group 'asymptote)
(defcustom lasy-ask-about-temp-compilation-buffer t
"* If t, ask before visiting a temporary buffer of compilation."
:type 'boolean
:group 'asymptote)
(defcustom lasy-compilation-inline-auto-detection nil
"* If t, lasy-mode detects automatically if the option 'inline' is passed to asymptote.sty.
In case of 'inline' option, the compilation of a figure separately of the document is processed by rebuilding the preamble and compiling it as a file '.tex' containing only this picture.
If nil (the default), the compilation of a figure separately of the document is processed by building a file '.asy', without the features of the LaTeX preamble."
:type 'boolean
:group 'asymptote)
(defcustom asy-command-location ""
"* If not in the path, you can put here the name of the directory containing Asy's binary files.
this variable must end in /."
:type 'directory
:group 'asymptote)
(defcustom asy-command "asy -V"
"* Command invoked to compile a Asymptote file.
You can define the location of this command with the variable `asy-command-location'."
:type 'string
:group 'asymptote)
(defcustom lasy-command "asy"
"* Command invoked to compile a Asymptote file generated compiling a .tex file.
You can define the location of this command with the variable `asy-command-location'."
:type 'string
:group 'asymptote)
(defcustom lasy-latex-command "latex -halt-on-error"
"* Command invoked to compile a .tex file with LaTeX."
:type 'string
:group 'asymptote)
(defcustom lasy-pdflatex-command "pdflatex -halt-on-error"
"* Command invoked to compile a .tex file with pdflaTex."
:type 'string
:group 'asymptote)
(defcustom lasy-dvips-pre-pdf-command "dvips -Ppdf"
"* Command invoked to convert a .dvi file to a temporary .ps file in order to
generate a final .pdf file."
:type 'string
:group 'asymptote)
(defcustom lasy-dvips-command "dvips -q"
"* Command invoked to convert a .dvi file to a final .ps file."
:type 'string
:group 'asymptote)
(defcustom lasy-ps2pdf-command "ps2pdf14"
"* Command invoked to convert a .dvi file to .ps file."
:type 'string
:group 'asymptote)
(defcustom asy-temp-dir temporary-file-directory
"*The name of a directory for Asy's temporary files.
Such files are generated by functions like
`asy-compile' when lasy-mode is enable."
:type 'directory
:group 'asymptote)
(defcustom ps-view-command (if running-unix-p "gv" "")
"Command to view a PostScript file generated by compiling a tex file within lasy-mode.
This variable is not used when running the Windows OS.
See `asy-open-file'."
:type 'string
:group 'asymptote)
(defcustom pdf-view-command
(if running-unix-p
"xpdf" "")
"Command to view a pdf file generated by compiling a tex file within lasy-mode.
This variable is not used when running the Windows OS.
See `asy-open-file'."
:type 'string
:group 'asymptote)
(defvar asy-TeX-master-file nil
"TeX file associate with current asymptote code.
This variable must be modified only using the function 'asy-set-master-tex by M-x asy-set-master-tex <RET>.")
(make-variable-buffer-local 'asy-TeX-master-file)
(defvar lasy-compile-tex nil
"* Internal use. t if LaTeX compilation come from latex-mode.")
(when (fboundp 'font-lock-add-keywords)
(if (< max-specpdl-size 2000) (setq max-specpdl-size 2000))
(defun asy-add-function-keywords (function-keywords face-name)
(let* ((keyword-list (mapcar #'(lambda (x)
(symbol-name x))
function-keywords))
(keyword-regexp (concat "\\<\\("
(regexp-opt keyword-list)
"\\)(")))
(font-lock-add-keywords 'asy-mode
`((,keyword-regexp 1 ',face-name)))))
(defun asy-add-variable-keywords (function-keywords face-name)
(let* ((keyword-list (mapcar #'(lambda (x)
(symbol-name x))
function-keywords))
(keyword-regexp (concat "\\<[0-9]*\\("
(regexp-opt keyword-list)
"\\)\\(?:[^(a-zA-Z]\\|\\'\\)")))
(font-lock-add-keywords 'asy-mode
`((,keyword-regexp 1 ',face-name)))))
;; External definitions of keywords:
;; asy-function-name and asy-variable-name
(if (locate-library "asy-keywords.el")
(load "asy-keywords.el")
(progn
;; Use dummy keyword definitions if asy-keywords.el is not found:
(defvar asy-keyword-name nil)
(defvar asy-type-name nil)
(defvar asy-function-name nil)
(defvar asy-variable-name nil)))
(defcustom asy-extra-type-name '()
"Extra user type names highlighted with 'font-lock-type-face"
:type '(repeat symbol)
:group 'asymptote)
(defcustom asy-extra-function-name
'()
"Extra user function names highlighted with 'font-lock-function-name-face"
:type '(repeat symbol)
:group 'asymptote)
(defcustom asy-extra-variable-name '()
"Extra user variable names highlighted with 'font-lock-constant-face"
:type '(repeat symbol)
:group 'asymptote)
(asy-add-variable-keywords
asy-keyword-name
'font-lock-builtin-face)
(asy-add-variable-keywords
(nconc asy-type-name asy-extra-type-name)
'font-lock-type-face)
(asy-add-function-keywords
(nconc asy-function-name asy-extra-function-name)
'font-lock-function-name-face)
(asy-add-variable-keywords
(nconc asy-variable-name asy-extra-variable-name)
'font-lock-constant-face)
(defface asy-environment-face
`((t
(:underline t :inverse-video t)))
"Face used to highlighting the keywords '\\begin{asy}' and '\\end{asy}' within lasy-mode."
:group 'asymptote)
(font-lock-add-keywords
'asy-mode
'(("\\\\begin{asy}.*" . 'asy-environment-face)
("\\\\end{asy}" . 'asy-environment-face)))
(defface asy-link-face ;; widget-field-face
`((t
(:underline t)))
"Face used to highlighting the links."
:group 'asymptote)
(font-lock-add-keywords
'asy-mode
'(("\\[.*?\\.asy\\]" . 'asy-link-face)))
)
(setq buffers-menu-max-size nil)
(setq mode-name "Asymptote")
(if running-xemacs-p
(defvar asy-menu
'("Asy"
["Toggle lasy-mode" lasy-mode :active (and (featurep 'two-mode-mode) two-mode-bool)]
["Compile/View" asy-compile t]
["Go to error" asy-goto-error t]
["Describe command" asy-show-function-at-point t]"--"
("Master TeX file"
["Set/Change value" (asy-set-master-tex) :active (not (and (boundp two-mode-bool) two-mode-bool))]
["Erase value" (asy-unset-master-tex) :active (not (and (boundp two-mode-bool) two-mode-bool))]
("Compile OR View"
["PS" asy-master-tex-view-ps :active t]
["PDF (pdflatex)" asy-master-tex-view-pdflatex :active t]
["PDF (ps2pdf)" asy-master-tex-view-ps2pdf :active t])
("Compile AND View"
["PS" asy-master-tex-view-ps-f :active t]
["PDF (pdflatex)" asy-master-tex-view-pdflatex-f :active t]
["PDF (ps2pdf)" asy-master-tex-view-ps2pdf-f :active t]))
["Asymptote insinuates globally LaTeX" asy-insinuate-latex-globally :active (not asy-insinuate-latex-globally-p)]"--"
("Debugger Buffer"
["Visible" (setq asy-compilation-buffer 'visible) :style radio :selected (eq asy-compilation-buffer 'visible) :active t]
["Available" (setq asy-compilation-buffer 'available) :style radio :selected (eq asy-compilation-buffer 'available) :active t]
["None" (setq asy-compilation-buffer 'none) :style radio :selected (eq asy-compilation-buffer 'none) :active t]
["Never" (setq asy-compilation-buffer 'never) :style radio :selected (eq asy-compilation-buffer 'never) :active t])
("Compilation Options" :included (and (featurep 'two-mode-mode) two-mode-bool)
["Enable Automatic Detection of Option" (setq lasy-compilation-inline-auto-detection t) :style radio :selected lasy-compilation-inline-auto-detection :active t]
["Disable Automatic Detection of Option" (setq lasy-compilation-inline-auto-detection nil) :style radio :selected (not lasy-compilation-inline-auto-detection) :active t])
["Customize" (customize-group "asymptote") :active t]
["Help" (describe-function 'asy-mode) :active t]
))
(defvar asy-menu
'("Asy"
["Toggle Lasy-Mode" lasy-mode :visible (and (featurep 'two-mode-mode) two-mode-bool)]
["Compile/View" asy-compile t]
["Go to Error" asy-goto-error t]
["Describe Command" asy-show-function-at-point t]"--"
("Master TeX File"
["Set/Change Value" (asy-set-master-tex) :active (not (and (boundp two-mode-bool) two-mode-bool)) :key-sequence nil]
["Erase Value" (asy-unset-master-tex) :active (not (and (boundp two-mode-bool) two-mode-bool)) :key-sequence nil]
("Compile or View"
["PS" asy-master-tex-view-ps :active t]
["PDF (pdflatex)" asy-master-tex-view-pdflatex :active t]
["PDF (ps2pdf)" asy-master-tex-view-ps2pdf :active t])
("Compile and View"
["PS" asy-master-tex-view-ps-f :active t]
["PDF (pdflatex)" asy-master-tex-view-pdflatex-f :active t]
["PDF (ps2pdf)" asy-master-tex-view-ps2pdf-f :active t]))
["Asymptote Insinuates Globally LaTeX" asy-insinuate-latex-globally :active (not asy-insinuate-latex-globally-p)]"--"
("Debugger Buffer"
["Visible" (setq asy-compilation-buffer 'visible) :style radio :selected (eq asy-compilation-buffer 'visible) :active t :key-sequence nil]
["Available" (setq asy-compilation-buffer 'available) :style radio :selected (eq asy-compilation-buffer 'available) :active t :key-sequence nil]
["None" (setq asy-compilation-buffer 'none) :style radio :selected (eq asy-compilation-buffer 'none) :active t :key-sequence nil]
["Never" (setq asy-compilation-buffer 'never) :style radio :selected (eq asy-compilation-buffer 'never) :active t :key-sequence nil])
("Compilation Options" :visible (and (featurep 'two-mode-mode) two-mode-bool)
["Enable Automatic Detection of Option" (setq lasy-compilation-inline-auto-detection t) :style radio :selected lasy-compilation-inline-auto-detection :active t :key-sequence nil]
["Disable Automatic Detection of Option" (setq lasy-compilation-inline-auto-detection nil) :style radio :selected (not lasy-compilation-inline-auto-detection) :active t :key-sequence nil])
["Customize" (customize-group "asymptote") :active t :key-sequence nil]
["Help" (describe-function 'asy-mode) :active t :key-sequence nil]
)))
(easy-menu-define asy-mode-menu asy-mode-map "Asymptote Mode Commands" asy-menu)
;; On the hook for XEmacs only.
(if running-xemacs-p
(add-hook 'asy-mode-hook
(lambda ()
(and (eq major-mode 'asy-mode)
(easy-menu-add asy-mode-menu asy-mode-map)))))
(defun asy-protect-file-name(Filename)
(concat "\"" Filename "\""))
(defun asy-get-temp-file-name(&optional noext)
"Get a temp file name for printing."
(if running-xemacs-p
(concat (make-temp-name asy-temp-dir) (if noext "" ".asy"))
(concat (make-temp-file
(expand-file-name "asy" asy-temp-dir)) (if noext "" ".asy"))))
(defun asy-log-filename()
(concat buffer-file-name ".log"))
(defun asy-compile()
"Compile Asymptote code."
(interactive)
(if (and (boundp two-mode-bool) two-mode-bool)
(lasy-compile) ;; compile asy code in a TeX file.
(progn ;; compile asy code in a asy file.
(let*
((buffer-base-name (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
(asy-compile-command
(concat asy-command-location asy-command
(if (eq asy-compilation-buffer 'never)
" " " -wait ")
(asy-protect-file-name buffer-base-name))))
(if (buffer-modified-p) (save-buffer))
(message "%s" asy-compile-command)
(asy-internal-compile asy-compile-command t t)))))
(defun asy-error-message(&optional P)
(let ((asy-last-error
(asy-log-field-string
(asy-log-filename) 0)))
(if (and asy-last-error (not (string= asy-last-error "")))
(message (concat asy-last-error (if P "\nPress F4 to go to error" "")))
(when (and (boundp two-mode-bool) two-mode-bool lasy-run-tex (not (zerop asy-last-compilation-code)))
(message "The LaTeX code may be incorrect.")))))
(defun asy-log-field-string(Filename Field)
"Return field of first line of file filename.
Fields are defined as 'field1: field2.field3:field4' . Field=0 <-> all fields"
(let ((view-inhibit-help-message t))
(with-temp-buffer
(progn
(insert-file Filename)
(beginning-of-buffer)
(if (re-search-forward "^\\(.*?\\): \\(.*?\\)\\.\\(.*?\\):\\(.*\\)$" (point-max) t)
(match-string Field) nil)))))
(defun asy-next-error(arg reset)
(if (> emacs-major-version 21)
(next-error arg reset)
(next-error arg)))
(defun lasy-ask-visit-tem-compilation-buffer()
"* Ask before visiting a temporary compilation buffer depending the value of `lasy-ask-about-temp-compilation-buffer'."
(if lasy-ask-about-temp-compilation-buffer
(y-or-n-p "Visit temporary buffer of compilation ? ") t))
(defun lasy-place-cursor-to-error(Filename li co)
(save-excursion
(with-temp-buffer
(insert-file-contents
(if running-unix-p Filename
(replace-regexp-in-string
"//" ":/"
(replace-regexp-in-string "/cygdrive/" "" Filename)))) ;; Not right,
;;;maybe take a look at the code of compilation-find-file
(beginning-of-buffer)
(next-line (1- (string-to-number li)))
(setq line-err
(buffer-substring-no-properties
(progn (beginning-of-line) (point))
(progn (end-of-line) (point))))))
(beginning-of-buffer)
(search-forward line-err)
(beginning-of-line)
(forward-char (1- (string-to-number co))))
(defun asy-goto-error(&optional arg reset)
"Go to point of last error within asy/lasy-mode."
(interactive "P")
(if (or (eq asy-compilation-buffer 'never)
(and (boundp two-mode-bool) two-mode-bool))
(let* ((log-file (asy-log-filename))
(li_ (asy-log-field-string log-file 2))
(co_ (asy-log-field-string log-file 3)))
(if (and (boundp two-mode-bool) two-mode-bool) ;; Within Lasy-mode
(progn ;; lasy-mode need the compilation of file.tex
;; the error can be in Tex commands or in Asymptote commands
(if (eq asy-compilation-buffer 'never) ;; Find error in the log file.
(if li_ ;; Asy error found in the log-file
(progn
(lasy-place-cursor-to-error
(asy-log-field-string log-file 1) li_ co_)
(asy-error-message))
(message "There is an error in your LaTeX code..."))
(if (or running-xemacs-p (< emacs-major-version 22))
(when (lasy-ask-visit-tem-compilation-buffer)
(next-error arg))
(let ((msg)) ;; Find error in the compilation buffer
(save-excursion
(set-buffer (next-error-find-buffer))
(when reset
(setq compilation-current-error nil))
(let* ((columns compilation-error-screen-columns)
(last 1)
(loc (compilation-next-error (or arg 1) nil
(or compilation-current-error
compilation-messages-start
(point-min))))
(end-loc (nth 2 loc))
(marker (point-marker)))
(setq compilation-current-error (point-marker)
overlay-arrow-position
(if (bolp)
compilation-current-error
(copy-marker (line-beginning-position)))
loc (car loc)))
(if (re-search-forward "^\\(.*?\\): \\(.*?\\)\\.\\(.*?\\):\\(.*\\)$" (point-max) t)
(progn
(setq msg (match-string 0)
log-file (match-string 1)
li_ (match-string 2)
co_ (match-string 3)))
(error "Not other errors.")))
(lasy-place-cursor-to-error log-file li_ co_)
(message msg)))))
(if li_ ;;Pure asy-mode and compilation with shell-command
(progn
(goto-line (string-to-number li_))
(forward-char (1- (string-to-number co_)))
(asy-error-message))
(progn (message "No error.")))))
(asy-next-error arg reset)))
(defun asy-grep (Regexp)
"Internal function used by asymptote."
(let ((Strout "")
(case-fold-search-asy case-fold-search))
(progn
(beginning-of-buffer)
(setq case-fold-search nil)
(while (re-search-forward Regexp (point-max) t)
(setq Strout (concat Strout (match-string 0) "\n\n")))
(setq case-fold-search case-fold-search-asy)
(if (string= Strout "") "No match.\n" Strout))))
(defun asy-widget-open-file-at-pos (widget &optional event)
""
(kill-buffer (current-buffer))
(find-file (widget-get widget :follow-link))
(goto-line (string-to-number (widget-get widget :value))))
(defun asy-show-function-at-point()
"Show the Asymptote definitions of the command at point."
(interactive)
(save-excursion
(let ((cWord (current-word))
(cWindow (selected-window)))
(switch-to-buffer-other-window "*asy-help*")
(fundamental-mode)
(setq default-directory "/")
(if (> emacs-major-version 21)
(call-process-shell-command
(concat asy-command-location "asy -l --where") nil t nil)
(insert (shell-command-to-string "asy -l --where")))
(let ((rHelp (asy-grep (concat "^.*\\b" cWord "(\\(.\\)*?$")))
(tag)(file)(line))
(erase-buffer)
(insert rHelp)
(beginning-of-buffer)
(while (re-search-forward "\\(.*\\): \\([0-9]*\\)\\.\\([0-9]*\\)" (point-max) t)
(setq file (match-string 1)
line (match-string 2)
tag (file-name-nondirectory file))
(widget-create `(file-link
:tag ,tag
:follow-link ,file
:value ,line
:action asy-widget-open-file-at-pos
))))
(beginning-of-buffer)
(while (re-search-forward "\\(.*: [0-9]*\\.[0-9]*\\)" (point-max) t)
(replace-match ""))
(asy-mode)
(use-local-map widget-keymap)
(widget-setup)
(goto-char (point-min))
(select-window cWindow))))
(add-hook 'asy-mode-hook
(lambda ()
(c-set-style "gnu");
(c-set-offset (quote topmost-intro-cont) 0 nil)
(make-local-variable 'c-label-minimum-indentation)
(setq c-label-minimum-indentation 0)
(when (fboundp 'flyspell-mode) (flyspell-mode -1))
(turn-on-font-lock)
(column-number-mode t)
))
;;;###autoload (defun lasy-mode ())
;;; ************************************
;;; asy-mode mixed with LaTeX-mode: lasy
;;; ************************************
(if (locate-library "two-mode-mode")
(progn
;; patch two-mode-mode.el for Emacs >= 23.
(defun make-local-hook (func))
(defvar lasy-fontify-asy-p nil
"Variable to communicate with `font-lock-unfontify-region'.
Internal use, don't set in any fashion.")
(setq lasy-fontify-asy-p nil)
(eval-after-load "two-mode-mode"
'(progn
;; Redefine `two-mode-mode-update-mode' to use regexp.
(defun two-mode-mode-update-mode ()
"Redefined in `asy-mode.el' to use regexp"
(when (and two-mode-bool two-mode-update)
(setq two-mode-update 0)
(let ((mode-list second-modes)
(flag 0))
(while mode-list
(let ((mode (car mode-list))
(lm -1)
(rm -1))
(save-excursion
(if (search-backward-regexp (cadr mode) nil t)
(setq lm (point))
(setq lm -1)))
(save-excursion
(if (search-backward-regexp (car (cddr mode)) nil t)
(setq rm (point))
(setq rm -1)))
(if (and (not (and (= lm -1) (= rm -1))) (>= lm rm))
(progn
(setq flag 1)
(setq mode-list '())
(two-mode-change-mode (car mode) (car (cdr (cddr mode)))))))
(setq mode-list (cdr mode-list)))
(if (= flag 0)
(two-mode-change-mode (car default-mode) (cadr default-mode))))))
(defun two-mode-change-mode (to-mode func)
"Redefined in asy-mode.
Change the variable `lasy-fontify-asy-p' according to the value of func and
the current mode."
(if (string= to-mode mode-name)
t
(progn
(setq lasy-fontify-asy-p (eq func 'asy-mode))
(funcall func)
(hack-local-variables)
;; avoid infinite loop in two-mode-mode
;; (two-mode-mode-setup)
(if two-mode-switch-hook
(run-hooks 'two-mode-switch-hook))
(if (eq font-lock-mode t)
(font-lock-fontify-buffer))
(turn-on-font-lock-if-enabled))))
))
(require 'two-mode-mode)
(defun lasy-mode ()
"Treat, in some cases, the current buffer as a literal Asymptote program."
(interactive)
(save-excursion
(let ((prefix
(progn
(goto-char (point-max))
(re-search-backward "^\\([^\n]+\\)Local Variables:"
(- (point-max) 3000) t)
(match-string 1)))
(pos-b (point)))
(when
(and prefix
(progn
(re-search-forward (regexp-quote
(concat prefix
"End:")) (point-max) t)
(re-search-backward (concat "\\(" prefix "mode: .*\\)") pos-b t))
)
(error (concat "lasy-mode can not work if a mode is specified as local file variable.
You should remove the line " (int-to-string (line-number-at-pos)))))))
(set (make-local-variable 'asy-insinuate-latex-p) asy-insinuate-latex-p)
(make-local-variable 'lasy-fontify-asy-p)
(when (< emacs-major-version 22)
(make-local-variable 'font-lock-keywords-only))
(setq default-mode '("LaTeX" latex-mode)
second-modes '(("Asymptote"
"^\\\\begin{asy}.*$"
"^\\\\end{asy}"
asy-mode)))
(if two-mode-bool
(progn
(latex-mode)
(asy-insinuate-latex))
(progn
(two-mode-mode)
)))
(when (not running-xemacs-p)
(defadvice TeX-command-master (around asy-choose-compile act)
"Hack to circumvent the preempt of 'C-c C-c' by AucTeX within `lasy-mode'."
(if (string-match "asymptote" (downcase mode-name))
(asy-compile)
ad-do-it)))
(add-hook 'two-mode-switch-hook
(lambda ()
(if (eq major-mode 'latex-mode)
(progn ;; Switch to latex-mode
;; Disable LaTeX-math-Mode within lasy-mode (because of incompatibility)
(when LaTeX-math-mode (LaTeX-math-mode -1))
(asy-insinuate-latex)
(when (< emacs-major-version 22)
(setq font-lock-keywords-only nil)))
(progn ;; Switch to asy-mode
(when (< emacs-major-version 22)
(setq font-lock-keywords-only t))
))))
;; (setq two-mode-switch-hook nil)
;; Solve a problem restoring a TeX file via desktop.el previously in lasy-mode.
(if (boundp 'desktop-buffer-mode-handlers)
(progn
(defun asy-restore-desktop-buffer (desktop-b-f-name d-b-n d-b-m)
(find-file desktop-b-f-name))
(add-to-list 'desktop-buffer-mode-handlers
'(asy-mode . asy-restore-desktop-buffer))))
;; Functions and 'advises' to restrict 'font-lock-unfontify-region'
;; and 'font-lock-fontify-syntactically-region' within lasy-mode
;; Special thanks to Olivier Ramaré for his help.
(when (and (fboundp 'font-lock-add-keywords) (> emacs-major-version 21))
(defun lasy-mode-at-pos (pos &optional interior strictly)
"If point at POS is in an asy environment return the list (start end)."
(save-excursion
(save-match-data
(goto-char pos)
(let* ((basy
(progn
(unless strictly (end-of-line))
(when (re-search-backward "^\\\\begin{asy}" (point-min) t)
(when interior (next-line))
(point))))
(easy
(and basy
(progn
(when (re-search-forward "^\\\\end{asy}" (point-max) t)
(when interior (previous-line)(beginning-of-line))
(point))))))
(and basy easy
(> pos (- basy (if interior 12 0)))
(< pos (+ easy (if interior 10 0)))
(list basy easy))))))
(defun lasy-region (start end &optional interior)
"If the region 'start to end' contains the beginning or
the end of an asy environment return the list of points where
the asy environment starts and ends."
(let* ((beg (min start end))
(lim (max start end)))
(or (lasy-mode-at-pos beg interior)
(save-match-data
(save-excursion
(goto-char beg)
(and (re-search-forward "^\\\\begin{asy}" lim t)
(lasy-mode-at-pos (point) interior)))))))
(defun lasy-tags (start end)
"Return associated list of points where the tags starts and ends
restricted to the region (start end).
\"b\" associated with (start-beginTag end-beginTag),
\"e\" associated with (start-endTag end-endTag)."
(let*
((beg (min start end))
(lim (max start end))
out)
(save-excursion
(goto-char beg)(beginning-of-line)
(while
(when (re-search-forward "^\\\\begin{asy}.*" lim t)
(push (list
(progn (beginning-of-line)(point))
(progn (end-of-line)(point))) out)))
(goto-char beg)(beginning-of-line)
(while
(when (re-search-forward "^\\\\end{asy}" lim t)
(push (list
(progn (beginning-of-line)(point))
(progn (end-of-line)(point))) out)))
out)))
(defun lasy-restrict-region (start end &optional interior)
"If the region 'start to end' contains the beginning or
the end of an asy environment, returns the list of points wich
restricts the region to the asy environment.
Else, return (start end)."
(let*
((beg (min start end))
(lim (max start end))
(be (if (lasy-mode-at-pos beg)
beg
(or (save-excursion
(goto-char beg)
(when (re-search-forward "^\\\\begin{asy}.*" lim t)
(unless interior (beginning-of-line))
(point)))
beg)))
(en (or (save-excursion
(goto-char be)
(when (re-search-forward "^\\\\end{asy}" lim t)
(when interior (beginning-of-line))
(point)))
lim)))
(list be en)))
(defun lasy-parse-region (start end)
"Return a list ((a (start1 end1)) (b (start2 end2)) [...]).
where a, b, ... are nil or t; t means the region from 'startX' through 'endX' (are points)
is in a asy environnement."
(let (regasy out rr brr err tags)
(save-excursion
(goto-char start)
(while (< (point) end)
(setq regasy (lasy-region (point) end))
(if regasy
(progn
(setq rr (lasy-mode-at-pos (point)))
(setq brr (and rr (nth 0 rr))
err (and rr (nth 1 rr)))
(if rr
(progn
(push (list t (list (max 1 (1- (point))) (min end err))) out)
(goto-char (min end err)))
(progn
(push (list nil (list (point) (nth 0 regasy))) out)
(goto-char (1+ (nth 0 regasy))))))
(progn
(push (list nil (list (min (1+ (point)) end) end)) out)
(goto-char end)))
))
;; Put start and end of tag in latex fontification.
(setq tags (lasy-tags start end))
(dolist (tag tags) (push (list nil tag) out))
(reverse out)))
(defadvice font-lock-unfontify-region
(around asy-font-lock-unfontify-region (beg end))
(if two-mode-bool
(let ((rstate (lasy-parse-region beg end))
curr reg asy-fontify latex-fontify)
(while (setq curr (pop rstate))
(setq reg (nth 1 curr))
(setq asy-fontify (and (nth 0 curr) lasy-fontify-asy-p)
latex-fontify (and (not (nth 0 curr))
(not lasy-fontify-asy-p)))
(when (or asy-fontify latex-fontify)
(setq beg (nth 0 reg)
end (nth 1 reg))
(save-excursion
(save-restriction
(narrow-to-region beg end)
ad-do-it
(widen))))))
ad-do-it))
(ad-activate 'font-lock-unfontify-region)
;; (ad-deactivate 'font-lock-unfontify-region)
(defadvice font-lock-fontify-syntactically-region
(around asy-font-lock-fontify-syntactically-region
(start end &optional loudly))
(if (and two-mode-bool (eq major-mode 'asy-mode))
(let*((reg (lasy-restrict-region start end)))
(save-restriction
(setq start (nth 0 reg) end (nth 1 reg))
(narrow-to-region start end)
(condition-case nil
ad-do-it
(error nil))
(widen)
))
ad-do-it))
(ad-activate 'font-lock-fontify-syntactically-region)
;; (ad-deactivate 'font-lock-fontify-syntactically-region)
(defadvice font-lock-default-fontify-region
(around asy-font-lock-default-fontify-region
(beg end loudly))
(if two-mode-bool
(let ((rstate (lasy-parse-region beg end))
asy-fontify latex-fontify curr reg)
(while (setq curr (pop rstate))
(setq reg (nth 1 curr))
(setq asy-fontify (and (nth 0 curr) lasy-fontify-asy-p)
latex-fontify (and (not (nth 0 curr))
(not lasy-fontify-asy-p)))
(when (or asy-fontify latex-fontify)
(setq beg (nth 0 reg)
end (nth 1 reg))
(save-excursion
(save-restriction
(narrow-to-region beg end)
(condition-case nil
ad-do-it
(error nil))
(widen)
)))))
ad-do-it))
(ad-activate 'font-lock-default-fontify-region)
;; (ad-deactivate 'font-lock-default-fontify-region)
))
(progn
(defvar two-mode-bool nil)
(defun lasy-mode ()
(message "You must install the package two-mode-mode.el."))))
(setq asy-latex-menu-item
'(["Toggle lasy-mode" lasy-mode :active (featurep 'two-mode-mode)]
["View asy picture near cursor" lasy-compile :active t]"--"
("Compile OR View"
["PS" lasy-view-ps :active t]
["PDF (pdflatex)" lasy-view-pdf-via-pdflatex :active t]
["PDF (ps2pdf)" lasy-view-pdf-via-ps2pdf :active t])
("Compile AND View"
["PS" asy-master-tex-view-ps-f :active t]
["PDF (pdflatex)" asy-master-tex-view-pdflatex-f :active t]
["PDF (ps2pdf)" asy-master-tex-view-ps2pdf-f :active t])"--"
["Asymptote insinuates globally LaTeX" asy-insinuate-latex-globally :active (not asy-insinuate-latex-globally-p)]
("Disable Asymptote insinuate Latex"
["locally" asy-no-insinuate-locally :active t]
["globally" asy-no-insinuate-globally :active t])
("Debugger Buffer"
["Visible" (setq asy-compilation-buffer 'visible) :style radio :selected (eq asy-compilation-buffer 'visible) :active t]
["Available" (setq asy-compilation-buffer 'available) :style radio :selected (eq asy-compilation-buffer 'available) :active t]
["None" (setq asy-compilation-buffer 'none) :style radio :selected (eq asy-compilation-buffer 'none) :active t]
["Never" (setq asy-compilation-buffer 'never) :style radio :selected (eq asy-compilation-buffer 'never) :active t])
))
(if running-xemacs-p
(setq asy-latex-menu-item (nconc '("Asymptote") asy-latex-menu-item))
(setq asy-latex-menu-item (nconc '("Asymptote" :visible asy-insinuate-latex-p) asy-latex-menu-item)))
(defun asy-insinuate-latex-maybe ()
"This function is added to `LaTeX-mode-hook' to define the environment 'asy'
and, eventually, set its indentation.
For internal use only."
(when (or asy-insinuate-latex-globally-p
(save-excursion
(beginning-of-buffer)
(save-match-data
(search-forward "\\begin{asy}" nil t))))
(asy-insinuate-latex))
(LaTeX-add-environments
'("asy" (lambda (env &rest ignore)
(unless asy-insinuate-latex-p (asy-insinuate-latex))
(LaTeX-insert-environment env)))))
;; (add-hook 'after-init-hook
;; (lambda ()