summaryrefslogtreecommitdiff
path: root/graphics/asymptote/base/asy-mode.el
blob: c1960e23035a18e6811aef20746f11b4a5bcefde (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
;;; 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 http://asymptote.sourceforge.net

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) ;; 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

      (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)
                 (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 ()
(eval-after-load "latex"
  '(progn
     (add-hook 'LaTeX-mode-hook 'asy-insinuate-latex-maybe)
     (setq lasy-mode-map (copy-keymap LaTeX-mode-map))
     (setq LaTeX-mode-map-backup (copy-keymap LaTeX-mode-map))

     (defadvice TeX-add-local-master (after asy-adjust-local-variable ())
       "Delete the line that defines the mode in a file .tex because two-mode-mode reread
the local variables after switching mode."
       (when (string= (file-name-extension buffer-file-name) "tex")
         (save-excursion
           (goto-char (point-max))
           (delete-matching-lines
            "mode: latex"
            (re-search-backward "^\\([^\n]+\\)Local Variables:"
                                (- (point-max) 3000) t)
            (re-search-forward (regexp-quote
                                (concat (match-string 1)
                                        "End:"))) nil))))
     (ad-activate 'TeX-add-local-master)
     ;; (ad-deactivate 'TeX-add-local-master)

     (when lasy-extra-key
       (define-key lasy-mode-map (kbd "<C-return>")
         (lambda ()
           (interactive)
           (lasy-view-ps nil nil t)))
       (define-key lasy-mode-map (kbd "<C-S-return>")
         (lambda ()
           (interactive)
           (lasy-view-ps t nil t)))
       (define-key lasy-mode-map (kbd "<M-return>")
         (lambda ()
           (interactive)
           (lasy-view-pdf-via-pdflatex nil nil t)))
       (define-key lasy-mode-map (kbd "<M-S-return>")
         (lambda ()
           (interactive)
           (lasy-view-pdf-via-pdflatex t nil t)))
       (define-key lasy-mode-map (kbd "<C-M-return>")
         (lambda ()
           (interactive)
           (lasy-view-pdf-via-ps2pdf nil nil t)))
       (define-key lasy-mode-map (kbd "<C-M-S-return>")
         (lambda ()
           (interactive)
           (lasy-view-pdf-via-ps2pdf t nil t)))
       (define-key lasy-mode-map  (kbd "<f4>") 'asy-goto-error))

     (easy-menu-define asy-latex-mode-menu lasy-mode-map "Asymptote insinuates LaTeX" asy-latex-menu-item)
     ))
;; ))

(defvar asy-insinuate-latex-p nil
  "Not nil when current buffer is insinuated by Asymptote.
May be a local variable.
For internal use.")

(defvar asy-insinuate-latex-globally-p nil
  "Not nil when all latex-mode buffers is insinuated by Asymptote.
For internal use.")

(defun asy-set-latex-asy-indentation ()
  "Set the indentation of environnment 'asy' like the environnment 'verbatim' is."
  ;; Regexp matching environments with indentation at col 0 for begin/end.
  (set (make-local-variable 'LaTeX-verbatim-regexp)
       (concat (default-value 'LaTeX-verbatim-regexp) "\\|asy"))
  ;; Alist of environments with special indentation.
  (make-local-variable 'LaTeX-indent-environment-list)
  (add-to-list 'LaTeX-indent-environment-list
               '("asy" current-indentation)))

(defun asy-unset-latex-asy-indentation ()
  "Unset the indentation of environnment 'asy' like the environnment 'verbatim' is."
  (set (make-local-variable 'LaTeX-verbatim-regexp)
       (default-value 'LaTeX-verbatim-regexp))
  (set (make-local-variable 'LaTeX-indent-environment-list)
       (default-value 'LaTeX-indent-environment-list)))

(defun asy-no-insinuate-locally ()
  (interactive)
  (set (make-local-variable 'asy-insinuate-latex-p) nil)
  (setq asy-insinuate-latex-globally-p nil)
  (asy-unset-latex-asy-indentation)
  (if running-xemacs-p
      (easy-menu-remove-item nil nil "Asymptote")
    (menu-bar-update-buffers))
  (if (and (boundp 'two-mode-bool) two-mode-bool)
      (lasy-mode))
  (use-local-map LaTeX-mode-map-backup))


(defun asy-no-insinuate-globally ()
  (interactive)
  (if running-xemacs-p
      (easy-menu-remove-item nil nil "Asymptote")
    (easy-menu-remove-item LaTeX-mode-map nil "Asymptote"))
  (kill-local-variable asy-insinuate-latex-p)
  (setq-default asy-insinuate-latex-p nil)
  (setq asy-insinuate-latex-globally-p nil)
  (if (not running-xemacs-p)
      (menu-bar-update-buffers))
  (setq LaTeX-mode-map (copy-keymap LaTeX-mode-map-backup))
  ;;Disable lasy-mode in all latex-mode buffers.
  (when (featurep 'two-mode-mode)
    (mapc (lambda (buffer)
            (with-current-buffer buffer
              (when (and (buffer-file-name) (string= (file-name-extension (buffer-file-name)) "tex"))
                (asy-unset-latex-asy-indentation)
                (latex-mode)
                (setq asy-insinuate-latex-p nil))))
          (buffer-list))))

;;;###autoload
(defun asy-insinuate-latex (&optional global)
  "Add a menu bar in current 'latex-mode' buffer and activate asy keys bindings.
If the optional parameter (only for internal use) 'global' is 't' then all the FUTURE 'latex-mode' buffers are insinuated.
To insinuate all (current and future) 'latex-mode' buffers, use 'asy-insinuate-latex-globally' instead.
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 insinuates globally Latex.
        ))"
  (interactive)
  (if (and (not asy-insinuate-latex-globally-p) (or global (string= major-mode "latex-mode")))
      (progn
        (asy-set-latex-asy-indentation)
        (if global
            (progn
              (setq asy-insinuate-latex-p t)
              (setq asy-insinuate-latex-globally-p t)
              (setq LaTeX-mode-map (copy-keymap lasy-mode-map))
              (if running-xemacs-p
                  (add-hook 'LaTeX-mode-hook
                            (lambda ()
                              (if asy-insinuate-latex-globally-p
                                  (easy-menu-add asy-latex-mode-menu lasy-mode-map))))))
          (progn
            (use-local-map lasy-mode-map)
            (easy-menu-add asy-latex-mode-menu lasy-mode-map)
            (set (make-local-variable 'asy-insinuate-latex-p) t)))
        )))

(defun asy-insinuate-latex-globally ()
  "Insinuates all (current and future) 'latex-mode' buffers.
See `asy-insinuate-latex'."
  (interactive)
  (asy-insinuate-latex t)
  (if running-xemacs-p
      (add-hook 'LaTeX-mode-hook
                (lambda ()
                  (if asy-insinuate-latex-globally-p
                      (easy-menu-add asy-latex-mode-menu lasy-mode-map)))))
  (mapc (lambda (buffer)
          (with-current-buffer buffer
            (when (and
                   (buffer-file-name)
                   (string= (file-name-extension (buffer-file-name)) "tex"))
              (setq asy-insinuate-latex-p t)
              (use-local-map LaTeX-mode-map)
              (use-local-map lasy-mode-map)
              (asy-set-latex-asy-indentation)
              (easy-menu-add asy-latex-mode-menu lasy-mode-map))))
        (buffer-list)))

(defun lasy-inline-p()
  "Return nil if the option 'inline' is not used or if `lasy-compilation-inline-auto-detection' value is nil."
  (if lasy-compilation-inline-auto-detection
      (save-excursion
        (re-search-backward "^[^%]* *\\\\usepackage\\[ *inline *\\]{ *asymptote *}" 0 t))
    nil))

(defvar lasy-run-tex nil)
(defun lasy-asydef()
  "Return the content between the tags \\begin{asydef} and \\end{asydef}."
  (save-excursion
    (if (re-search-backward "\\\\begin{asydef}" 0 t)
        (buffer-substring
         (progn (next-line)(beginning-of-line)(point))
         (progn (re-search-forward "\\\\end{asydef}")
                (previous-line)(end-of-line)
                (point)))
      "")))

(defun lasy-compile-tex()
  "Compile region between \\begin{asy}[text with backslash] and \\end{asy} through a reconstructed file .tex."
  (interactive)
  (setq lasy-run-tex t)
  (save-excursion
    (let* ((Filename (asy-get-temp-file-name t))
           (FilenameTex (concat Filename ".tex"))
           (asydef (lasy-asydef)))
      (save-excursion
        (beginning-of-buffer)
        (write-region (point)
                      (progn
                        (re-search-forward "\\\\begin{document}.*\n")
                        (point)) FilenameTex)
        (write-region (concat "\\begin{asydef}\n" asydef "\n\\end{asydef}\n") 0 FilenameTex t))
      (re-search-backward "\\\\begin{asy}")
      (write-region (point) (progn
                              (re-search-forward "\\\\end{asy}")
                              (point)) FilenameTex t)
      (with-temp-file FilenameTex
        (insert-file FilenameTex)
        (end-of-buffer)
        (insert "\n\\end{document}"))
      (let ((default-directory asy-temp-dir))
        (lasy-view-ps t Filename)))))

(defun lasy-compile()
  "Compile region between \\begin{asy} and \\end{asy}."
  (interactive)
  (if (or (lasy-inline-p) (progn ;; find \begin{asy}[any backslash]
                            (save-excursion
                              (re-search-forward "\\\\end{asy}" (point-max) t)
                              (re-search-backward "\\\\begin{asy}.*\\(\\[.*\\\\.*\\]\\)" 0 t))
                            (match-string 1)))
      (progn
        (lasy-compile-tex)) ;; a temporary TeX file must be reconstructed.
    (progn
      (setq lasy-run-tex nil)
      (save-excursion
        (let ((Filename (asy-get-temp-file-name))
              (asydef (lasy-asydef)))
          (write-region (match-string 0) 0 Filename)
          (re-search-backward "\\\\begin{asy}")
          (write-region (point) (progn
                                  (re-search-forward "\\\\end{asy}")
                                  (point)) Filename)
          (with-temp-file Filename
            (insert-file-contents Filename)
            (beginning-of-buffer)
            (if (re-search-forward "\\\\begin{asy}\\[\\(.*\\)\\]" (point-max) t)
                (let ((sz (match-string 1)))
                  (replace-match "")
                  (insert (concat asydef "\nsize(" sz ");")))
              (when (re-search-forward "\\\\begin{asy}" (point-max) t)
                (replace-match "")
                (insert asydef)))
            (while (re-search-forward "\\\\end{asy}" (point-max) t)
              (replace-match "")))
          (let* ((asy-compile-command
                  (concat  asy-command-location
                           asy-command
                           (if (eq asy-compilation-buffer 'never)
                               " " " -wait ")
                           (asy-protect-file-name Filename))))
            (asy-internal-compile
             asy-compile-command t
             (not (eq asy-compilation-buffer 'never)))))))))

(defun asy-set-master-tex ()
  "Set the local variable 'asy-TeX-master-file.
This variable is used by 'asy-master-tex-view-ps"
  (interactive)
  (set (make-local-variable 'asy-TeX-master-file)
       (file-name-sans-extension
        (file-relative-name
         (expand-file-name
          (read-file-name "TeX document: ")))))
  (if (string= (concat default-directory asy-TeX-master-file)
               (file-name-sans-extension buffer-file-name))
      (prog1
          (set (make-local-variable 'asy-TeX-master-file) nil)
        (error "You should never give the same name to the TeX file and the Asymptote file"))
    (save-excursion
      (end-of-buffer)
      (if (re-search-backward "asy-TeX-master-file\\(.\\)*$" 0 t)
          (replace-match (concat "asy-TeX-master-file: \"" asy-TeX-master-file "\""))
        (insert (concat "
/// Local Variables:
/// asy-TeX-master-file: \"" asy-TeX-master-file "\"
/// End:")) t))))

(defun asy-unset-master-tex ()
  "Set the local variable 'asy-TeX-master-file to 'nil.
This variable is used by 'asy-master-tex-view-ps"
  (interactive)
  (set (make-local-variable 'asy-TeX-master-file) nil)
  (save-excursion
    (end-of-buffer)
    (if (re-search-backward "^.*asy-TeX-master-file:.*\n" 0 t)
        (replace-match ""))))

(defun asy-master-tex-error ()
  "Asy-mode internal use..."
  (if (y-or-n-p "You try to compile the TeX document that contains this picture.
You must set the local variable asy-TeX-master-file.
Do you want set this variable now ?")
      (asy-set-master-tex) nil))

(defun asy-master-tex-view (Func-view &optional Force fromtex)
  "Compile the LaTeX document that contains the picture of the current Asymptote code with the function Func-view.
Func-view can be one of 'lasy-view-ps, 'lasy-view-pdf-via-pdflatex, 'lasy-view-pdf-via-ps2pdf."
  (interactive)
  (if (or
       (and (boundp two-mode-bool) two-mode-bool)
       (string-match "latex" (downcase  mode-name)))
      (progn ;; Current mode is lasy-mode or latex-mode not asy-mode
        (funcall Func-view Force nil fromtex))
    (if asy-TeX-master-file
        (if (string= asy-TeX-master-file
                     (file-name-sans-extension buffer-file-name))
            (error "You should never give the same name to the TeX file and the Asymptote file")
          (funcall Func-view  Force asy-TeX-master-file fromtex))
      (if (asy-master-tex-error)
          (funcall Func-view Force asy-TeX-master-file fromtex)))))

(defvar asy-last-compilation-code nil
  "Code returned by the last compilation with `compile'.")

(defvar asy-compilation-auto-close nil
  "Variable to communicate with `asy-compilation-finish-function'.
Do not set this variable in any fashion.")

(defun asy-compilation-finish-function (buf msg)
  "Function to automatically close the compilation buffer '*asy-compilation*'
when no error or warning occurs."
  (when (string-match "*asy-compilation*" (buffer-name buf))
    (when (and asy-compilation-auto-close
               (eq asy-compilation-buffer 'none))
      (setq asy-compilation-auto-close nil)
      (if (not (string-match "exited abnormally" msg))
          (progn
            (save-excursion
              (set-buffer buf)
              (beginning-of-buffer)
              (if (not (search-forward-regexp "[wW]arning" nil t))
                  (when (not (eq asy-compilation-buffer 'visible))
                    ;;no errors/Warning, make the compilation window go away
                    (run-at-time 0.5 nil (lambda (buf_)
                                           (delete-windows-on buf_)
                                           (kill-buffer buf_)) buf)
                    (message (replace-regexp-in-string "\n" "" msg)))
                (message "Compilation warnings..."))))))))

(if  running-xemacs-p
    (setq compilation-finish-function 'asy-compilation-finish-function)
  (add-to-list 'compilation-finish-functions
               'asy-compilation-finish-function))

(defun asy-compilation-wait(&optional pass auto-close)
  "Wait for process in *asy-compilation* exits.
If pass is 't' don't wait.
If auto-close is 't' close the window if the process exit with success."
  (setq asy-compilation-auto-close auto-close)
  (let* ((buff (get-buffer "*asy-compilation*"))
         (comp-proc (get-buffer-process buff)))
    (while (and comp-proc
                (not (eq (process-status comp-proc) 'exit))
                (not pass))
      (setq comp-proc (get-buffer-process buff))
      (sit-for 1)
      (message "Waiting process...") ;; need message in Windows system
      )
    (message "") ;; Erase previous message.
    (if (and (not pass) comp-proc)
        (setq asy-last-compilation-code (process-exit-status comp-proc))
      (setq asy-last-compilation-code 0))
    (when (and (eq asy-compilation-buffer 'available)
               (zerop asy-last-compilation-code))
      (delete-windows-on buff))))


(defun asy-internal-shell (command &optional pass)
  "Execute 'command' in a inferior shell discarding output and
redirecting stderr in the file given by the command `asy-log-filename'.
`asy-internal-shell' waits for PROGRAM to terminate and returns a numeric exit status.
The variable `asy-last-compilation-code' is always set to the exit status.
The optional argument pass, for compatibility, is not used."
  (let* ((log-file (asy-log-filename))
         (discard (if pass 0 nil))
         (status
          (progn
            (let ((view-inhibit-help-message t))(write-region "" 0 log-file nil))
            (message "%s" command)
            (call-process shell-file-name nil (list nil log-file) nil shell-command-switch command))))
    (setq asy-last-compilation-code (if status status 0))
    (if status status nil)))

;; (defun asy-internal-shell (command &optional pass)
;;   "Execute 'command' in a inferior shell discarding output and
;; redirecting stderr in the file given by the command `asy-log-filename'.
;; pass non-nil means `asy-internal-shell' returns immediately with nil value.
;; Otherwise it waits for PROGRAM to terminate and returns a numeric exit status.
;; The variable `asy-last-compilation-code' is always set to the exit status or 0 if the
;; process returns immediately."
;;   (let* ((log-file (asy-log-filename))
;;         (discard (if pass 0 nil))
;;         (status
;;          (progn
;;            (let ((inhibit-redisplay t))(write-region "" 0 log-file nil))
;;            (message "%s" command)
;;            (call-process shell-file-name nil (list discard log-file) nil shell-command-switch command))))
;;     (setq asy-last-compilation-code (if status status 0))
;;     (when pass (sit-for 1))
;;     (if status status nil)))

(defun asy-internal-compile (command &optional pass auto-close stderr)
  "Execute command.
pass non-nil means don't wait the end of the process.
auto-close non-nil means automatically close the compilation buffer.
stderr non-nil means redirect the standard output error to the file
returned by `asy-log-filename'.
In this case command is running in an inferior shell without any output and
the parameter auto-close is not used (see `asy-internal-shell')."
  (setq asy-last-compilation-code -1)
  (let* ((compilation-buffer-name "*asy-compilation*")
         (compilation-buffer-name-function (lambda (mj) compilation-buffer-name)))
    (if (or stderr (eq asy-compilation-buffer 'never))
        (progn
          (asy-internal-shell command pass)
          (asy-error-message t))
      (progn
        (let ((comp-proc (get-buffer-process compilation-buffer-name)))
          (if comp-proc
	      (condition-case ()
		  (progn
		    (interrupt-process comp-proc)
		    (sit-for 1)
		    (delete-process comp-proc)
		    (when (and asy-compilation-auto-close
			       (eq asy-compilation-buffer 'none)
			       (not (eq asy-compilation-buffer 'visible)))
		      (sit-for 0.6)))
		(error ""))
	    ))
        (let ((view-inhibit-help-message t))
          (write-region "" 0 (asy-log-filename) nil))
        (compile command))
      (asy-compilation-wait pass auto-close))))

(defun asy-open-file(Filename)
  "Open the ps or pdf file Filename.
In unix-like system the variables `ps-view-command' and `pdf-view-command' are used.
In Windows the associated system file type is used instead."
  (let ((command
         (if running-unix-p
             (let ((ext (file-name-extension Filename)))
               (cond
                ((string= ext "ps") ps-view-command)
                ((string= ext "pdf") pdf-view-command)
                (t (error "Extension Not Supported."))))
           (asy-protect-file-name (file-name-nondirectory Filename))))
        )
    (if running-unix-p
        (start-process "" nil command Filename)
      (call-process-shell-command command nil 0))))

(defun lasy-TeX-master-file ()
  "Return the file name of the master file for the current document.
The returned string contain the directory but does not contain the extension of the file."
  (expand-file-name
   (concat (TeX-master-directory) (TeX-master-file nil t))))

(defun lasy-must-compile-p (TeX-Master-File out-file &optional Force)
  ""
  (or Force
      (file-newer-than-file-p
       (concat TeX-Master-File ".tex") out-file)
      (and (stringp (TeX-master-file)) ;; current buffer is not a mater tex file
           (file-newer-than-file-p buffer-file-name out-file))))

(defun lasy-view-ps (&optional Force  Filename fromtex)
  "Compile a LaTeX document embedding Asymptote code with latex->asy->latex->dvips and/or view the PostScript output.
If optional argument Force is t then force compilation."
  (interactive)
  (setq lasy-run-tex t)
  (setq lasy-compile-tex fromtex)
  (if (buffer-modified-p) (save-buffer))
  (when (eq asy-compilation-buffer 'never) (write-region "" 0 (asy-log-filename) nil))
  (let*
      ((b-b-n  (if Filename Filename (lasy-TeX-master-file)))
       (b-b-n-tex (asy-protect-file-name (concat b-b-n ".tex")))
       (b-b-n-ps (asy-protect-file-name (concat b-b-n ".ps")))
       (b-b-n-dvi (asy-protect-file-name (concat b-b-n ".dvi")))
       (b-b-n-asy (asy-protect-file-name (concat b-b-n ".asy")))
       (stderr (eq asy-compilation-buffer 'never)))
    (if (lasy-must-compile-p b-b-n (concat b-b-n ".ps") Force)
        (progn
          (let ((default-directory (file-name-directory b-b-n)))
            (asy-internal-compile (concat lasy-latex-command " " b-b-n-tex))
            (when (and (zerop asy-last-compilation-code) (file-readable-p (concat b-b-n ".asy")))
              (asy-internal-compile (concat asy-command-location lasy-command " " b-b-n-asy)  nil nil stderr)
              (when (zerop asy-last-compilation-code)
                (asy-internal-compile (concat lasy-latex-command " " b-b-n-tex))))
            (when (zerop asy-last-compilation-code)
              (asy-internal-compile (concat lasy-dvips-command " " b-b-n-dvi " -o " b-b-n-ps) nil t)
              (when (zerop asy-last-compilation-code)
                (asy-open-file (concat b-b-n ".ps"))))))
      (asy-open-file (concat b-b-n ".ps")))))

(defun lasy-view-pdf-via-pdflatex (&optional Force Filename fromtex)
  "Compile a LaTeX document embedding Asymptote code with pdflatex->asy->pdflatex and/or view the PDF output.
If optional argument Force is t then force compilation."
  (interactive)
  (setq lasy-run-tex t)
  (setq lasy-compile-tex fromtex)
  (if (buffer-modified-p) (save-buffer))
  (when (eq asy-compilation-buffer 'never) (write-region "" 0 (asy-log-filename) nil))
  (let*
      ((b-b-n  (if Filename Filename (lasy-TeX-master-file)))
       (b-b-n-tex (asy-protect-file-name (concat b-b-n ".tex")))
       (b-b-n-pdf (asy-protect-file-name (concat b-b-n ".pdf")))
       (b-b-n-asy (asy-protect-file-name (concat b-b-n ".asy")))
       ;; (stderr (or (eq asy-compilation-buffer 'never) lasy-compile-tex)))
       (stderr (eq asy-compilation-buffer 'never)))
    (if (lasy-must-compile-p b-b-n (concat b-b-n ".pdf") Force)
        (progn
          (let ((default-directory (file-name-directory b-b-n)))
            (asy-internal-compile (concat lasy-pdflatex-command " " b-b-n-tex))
            (when (and (zerop asy-last-compilation-code) (file-readable-p (concat b-b-n ".asy")))
              (asy-internal-compile (concat asy-command-location lasy-command " " b-b-n-asy) nil nil stderr)
              (when (zerop asy-last-compilation-code)
                (asy-internal-compile (concat lasy-pdflatex-command " " b-b-n-tex) t)))
            (when (zerop asy-last-compilation-code)
              (asy-open-file (concat b-b-n ".pdf")))))
      (asy-open-file (concat b-b-n ".pdf")))))

(defun lasy-view-pdf-via-ps2pdf (&optional Force Filename fromtex)
  "Compile a LaTeX document embedding Asymptote code with latex->asy->latex->dvips->ps2pdf14 and/or view the PDF output.
If optional argument Force is t then force compilation."
  (interactive)
  (setq lasy-run-tex t)
  (setq lasy-compile-tex fromtex)
  (if (buffer-modified-p) (save-buffer))
  (when (eq asy-compilation-buffer 'never) (write-region "" 0 (asy-log-filename) nil))
  (let*
      ((b-b-n  (if Filename Filename (lasy-TeX-master-file)))
       (b-b-n-tex (asy-protect-file-name (concat b-b-n ".tex")))
       (b-b-n-ps (asy-protect-file-name (concat b-b-n ".ps")))
       (b-b-n-dvi (asy-protect-file-name (concat b-b-n ".dvi")))
       (b-b-n-pdf (asy-protect-file-name (concat b-b-n ".pdf")))
       (b-b-n-asy (asy-protect-file-name (concat b-b-n ".asy")))
       ;; (stderr (or (eq asy-compilation-buffer 'never) lasy-compile-tex)))
       (stderr (eq asy-compilation-buffer 'never)))
    (if (lasy-must-compile-p b-b-n (concat b-b-n ".pdf") Force)
        (progn
          (let ((default-directory (file-name-directory b-b-n)))
            (asy-internal-compile (concat lasy-latex-command " " b-b-n-tex))
            (when (and (zerop asy-last-compilation-code) (file-readable-p (concat b-b-n ".asy")))
              (asy-internal-compile (concat asy-command-location lasy-command " " b-b-n-asy) nil nil stderr)
              (when (zerop asy-last-compilation-code)
                (asy-internal-compile (concat lasy-latex-command " " b-b-n-tex))))
            (when (zerop asy-last-compilation-code)
              (asy-internal-compile (concat lasy-dvips-pre-pdf-command " " b-b-n-dvi " -o " b-b-n-ps))
              (when (zerop asy-last-compilation-code)
                (asy-internal-compile (concat lasy-ps2pdf-command " " b-b-n-ps " " b-b-n-pdf) t)
                (when (zerop asy-last-compilation-code)
                  (asy-open-file (concat b-b-n ".pdf")))))))
      (asy-open-file (concat b-b-n ".pdf")))))

;; Goto error of last compilation
(define-key asy-mode-map  (kbd "<f4>") 'asy-goto-error)

;; Save and compile the file with option -V
(define-key asy-mode-map  (kbd "C-c C-c") 'asy-compile)

;; Show the definitions of command at point
(define-key asy-mode-map  (kbd "C-c ?") 'asy-show-function-at-point)

;; new line and indent
(define-key asy-mode-map (kbd "RET") 'newline-and-indent)

(defun asy-master-tex-view-ps ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-ps nil t))
(define-key asy-mode-map (kbd "<C-return>") 'asy-master-tex-view-ps)

(defun asy-master-tex-view-ps-f ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-ps t t))
(define-key asy-mode-map (kbd "<C-S-return>") 'asy-master-tex-view-ps-f)

(defun asy-master-tex-view-pdflatex ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-pdf-via-pdflatex nil t))
(define-key asy-mode-map (kbd "<M-return>") 'asy-master-tex-view-pdflatex)

(defun asy-master-tex-view-pdflatex-f ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-pdf-via-pdflatex t t))
(define-key asy-mode-map (kbd "<M-S-return>") 'asy-master-tex-view-pdflatex-f)

(defun asy-master-tex-view-ps2pdf ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-pdf-via-ps2pdf nil t))
(define-key asy-mode-map (kbd "<C-M-return>") 'asy-master-tex-view-ps2pdf)

(defun asy-master-tex-view-ps2pdf-f ()
  "Look at `asy-master-tex-view'"
  (interactive)
  (asy-master-tex-view 'lasy-view-pdf-via-ps2pdf t t))
(define-key asy-mode-map (kbd "<C-M-S-return>") 'asy-master-tex-view-ps2pdf-f)

(provide `asy-mode)
;;; asy-mode.el ends here