-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpari.el
2199 lines (1953 loc) · 84.4 KB
/
pari.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
;; pari.el -- GP/PARI editing support package.
;; Copyright (C) 1997-2014 The PARI group.
;; This file is part of the PARIEMACS package.
;; PARIEMACS is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation. It is distributed in the hope that it
;; will be useful, but WITHOUT ANY WARRANTY WHATSOEVER.
;; Check the License for details. You should have received a copy of
;; it, along with the package; see the file 'COPYING'. If not, write
;; to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;; pari.el version 3.10 (21-August-2014): Olivier Ramare (ramare AT math.univ-lille1.fr).
;; Major mode for editing GP scripts. It provides functions for editing
;; the code and evaluating it . See the documentation of gp-script-mode
;; and read the file pariemacs-3.09.txt.
;; Maintainer (01-March-2003): Olivier Ramare (ramare AT math.univ-lille1.fr).
;; Maintainer (07-November-2013): Olivier Ramare (ramare AT math.univ-lille1.fr).
;; KNOWN DEFICIENCIES:
;; -- The fontifying part may have troubles with `}'. A `}' followed by
;; a newline indicates the end of a function-definition starting with
;; `{'. Spaces, or tab are *not* allowed. So if you use `}' as a string
;; DON'T have it followed by a newline.
;; This file is split in six parts :
;; PART I : MAIN CONSTANTS (contains a macro).
;; Some of them may have to be modified by the user.
;; PART II : KEYMAPS AND OTHER VARIABLES
;; including 'gp-define-locked-keys.
;; PART III : gp-mode AND gp-script-mode
;; Also the gp-locked*
;; PART IV : GENERAL FUNCTIONS
;; Contains: HANDLING THE WINDOWS ...
;; THE GP PROCESS
;; META-COMMANDS
;; PART VI : MENU-BAR
;; Contains: MENU BUILDERS (contains 3 constants)
;; MENU-BAR ITEM USED IN GP-SCRIPT-MODE
;; MENU-BAR ITEM USED IN GP-MODE
;; Note: emacs version should be higher than 23 (I think !)
(provide 'pari)
;;; Initial message:
(message "\n====\nInitial message from pari.el:\n pari.el loads in three parts in 'gp-script-mode.\n See variable 'gp-script-menu-map-level.\n The mode 'gp-mode requires four levels,\n see variable 'gp-menu-map-level.\n====")
;; The first "three" is gp-script-menu-map-level,
;; while the latter "four" is gp-menu-map-level.
(defgroup gp-indentation nil
"GP customization subgroup concerning indentation
and furthering of constructs"
:group 'gp :prefix "gp-")
(defgroup gp-shell nil
"GP customization subgroup specific to `gp-shell-mode'"
:group 'gp :prefix "gp-")
(defgroup gp-font-lock-and-completion nil
"GP customization subgroup concerning colors and completion"
:group 'gp :prefix "gp-")
(defgroup gp-miscellana nil
"GP customization subgroup dedicated to less important switches"
:group 'gp :prefix "gp-")
(eval-when-compile
;; for development:
;;(setq byte-compile-warnings (list 'free-args 'unresolved 'callargs 'redefine 'obsolete))
;; for users:
(setq byte-compile-warnings (list 'unresolved 'redefine 'obsolete))
)
(require 'pari-completion) ;; Provides: functions: gp-quit-cpl-edit.
(eval-and-compile
(defvar gp-prompt-pattern
"^\\([?>]\\|break\\(\\[[0-9]+\\]\\)?>\\) [\n\t ]*"
"Regexp used to match gp prompts.
Can be set with `gp-set-prompt' (bound to M-\\ p)")
(require 'pari-conf)
(require 'pari-messages) ;; Provides: functions: gp-messager.
(require 'pari-completion) ;; Provides: functions: gp-quit-cpl-edit.
;; The following file uses variable gp-c-array which is defined and
;; created by 'pari-completion:
(require 'pari-help) ;; Provides: functions: gp-menu-quit.
(require 'pari-fontification)
;; Provides: variable: gp-fontification-keywords
;; functions: gp-update-fontification, gp-find-global-var
(require 'pari-history) ;; CHECK WHETHER THIS ONE CAN BE REMOVED!!!!!
;; Provides: functions: gp-store-line
(require 'sli-tools))
(unless (fboundp 'gp-update-fontification)
(defun gp-update-fontification nil nil))
(unless (fboundp 'gp-store-line)
(defun gp-store-line nil nil))
(unless (boundp 'gp-fontification-keywords)
(defvar gp-fontification-keywords nil nil))
;(unless (fboundp 'gp-in-commentp)
; (defun gp-in-commentp (arg) nil))
;; The use of gp-find-global-var if protected by a fboundp.
(eval-and-compile
(require 'imenu)
(require 'backquote)) ; This file is used in macros.
;;--------------------------
;; PART I : MAIN CONSTANTS
;;--------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Individual users may want to re-set some of the variables in this section
;; in a gp-mode-hook in their .emacs file (see pariemacs.txt for examples).
(defcustom gp-stack-size 10000000
"Default stack size passed to gp."
:type 'integer :group 'gp-shell)
(defcustom gp-prime-limit 500000
"Default prime limit passed to gp."
:type 'integer :group 'gp-shell)
(defcustom gp-prompt-for-args nil
"*A non-nil value makes M-x gp act like C-u M-x gp,
ie prompt for the command line arguments."
:type 'boolean :group 'gp-shell)
(defcustom gp-keep-PARI-buffer-when-quitting t
"T means what it says..."
:type 'boolean :group 'gp-shell)
(defcustom gp-locked-modep t
"t means you cannot write above the last prompt.
If you try to modify an earlier input, emacs will automatically copy
it at the bottom of your file."
:type 'boolean
:initialize 'custom-initialize-default ;if you use :set, you should specify :initialize!
:set (lambda (sym val) (setq gp-locked-modep val) (gp-define-locked-keys))
:group 'gp-miscellana)
(defcustom gp-tutorial-requiredp t
"T if comments should be given for some functions."
:type 'boolean :group 'gp-miscellana)
;; The functions concerned are : 'gp-make-cpl-file
(defcustom gp-menu-barp t
"A nil value means that we do not want any menu-bar"
:type 'boolean :group 'gp-miscellana)
(defcustom gp-separate-window-for-mistakes nil
"T means errors under the gp calculator will be
displayed on a separate window."
:type 'boolean :group 'gp-miscellana)
(defcustom gp-worryp t
"In gp-mode, finding \"input\" sets trust mode automatically,
except if this value is nil."
:type 'boolean :group 'gp-miscellana)
(defconst gp-temp-directory
(cond ((boundp 'temporary-file-directory)
;; emacs 20 up has `temporary-file-directory'
temporary-file-directory)
((fboundp 'temp-directory)
;; xemacs 21.4 has `temp-directory' function
;; (doesn't have temporary-file-directory)
(temp-directory))
(t ;; otherwise
"/tmp/"))
"*Directory in which to create temporary files.")
(defvar gp-temp-file
(expand-file-name (make-temp-name "gp_#") gp-temp-directory)
"Temporary file name used for text being sent as input to GP.")
(defvar gp-el-temp-file
(expand-file-name (make-temp-name "gp_#.el") gp-temp-directory)
"Temporary file name used for text being sent as input to emacs.")
(defconst gp-max-saved-wind-conf 30
"Maximal number of saved window configurations")
;;----------------------------------------
;; PART II : KEYMAPS AND OTHER VARIABLES
;;----------------------------------------
(defvar gp-input-filter-hook nil
"Hook run in `gp-input-filter'.")
(defvar gp-process nil "t if a GP process is running.")
(defvar gp-input-start nil
"Beginning of the expression to be send to GP. See `gp-copy-input'.")
(defvar gp-input-end nil
"End of the expression to be send to GP. See `gp-copy-input'.")
(defvar gp-complete-expression nil
"t if expression to be send to GP is complete. See `gp-copy-input'.")
(defvar gp-input-start-bracketp nil
"t if expression to be send to GP starts with a {.")
(defvar gp-reads-this-buffer nil
"name of the buffer gp is interpreting.")
(defvar gp-latest-error nil
"Regexp matching latest execution error. It contains a grouping
whose closing parenthesis corresponds to the point where gp
has detected a mistake.")
(defvar gp-registers-list nil
"List of registers from 0 to (1- gp-max-saved-wind-conf)
where window-configurations are stored.
See `gp-store-wind-conf' and `gp-restore-wind-conf'.")
(defvar gp-should-wait-for-outputp t
"t if gp should wait for output and fontify it
in `gp-send-input'. Automatically reset to t after each
input. See also `gp-input-filter'.")
(defvar gp-trust-mode nil
"nil is usual value.
If set to t, then the user is on its own, which means:
anything between (process-mark) and \\n is send to gp.
See also `gp-worryp'.")
(defconst gp-separator (list "----------") "")
(defconst gp-letters-list
(string-to-list "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=+-*/|^:!#()[]{}~%$,;.&?'`<> \"\\")
"See `gp-define-locked-keys'.")
(defvar gp-syntax-table nil
"Syntax table in use in `gp-mode' and `gp-script-mode' buffers.")
(when (null gp-syntax-table)
(setq gp-syntax-table (make-syntax-table))
(mapcar (lambda (acons) (modify-syntax-entry (car acons) (cdr acons) gp-syntax-table))
'((?( . "()") (?) . ")(") (?[ . "(]") (?] . ")[") (?{ . "(}") (?} . "){") ; parenthesis
(?# . ".") (?~ . "_") (?! . "_") (?% . "_") ; symbol constituent
(?\\ . ". 12b") (?/ . ". 14") (?* . ". 23") ; comments
(?> . "." ) (?| . "." ) (?+ . ".") (?- . ".") (?= . ".") (?< . "." ) ; ponctuation
(?. . "w") (?' . "w") (?$ . "w") (?_ . "w"))) ; word constituent
(if (string-match "XEmacs" emacs-version)
(progn
(modify-syntax-entry ?\n ">b" gp-syntax-table)
;; Give CR the same syntax as newline, for selective-display
(modify-syntax-entry ?\^m ">b" gp-syntax-table))
(modify-syntax-entry ?\n "> b" gp-syntax-table)
;; Give CR the same syntax as newline, for selective-display
(modify-syntax-entry ?\^m "> b" gp-syntax-table)))
(defvar gp-map nil
"Local keymap used in buffer *PARI*.")
(defun gp-define-locked-keys nil
(mapcar
(lambda (achar)
(define-key gp-map (vector achar)
(if gp-locked-modep
'gp-locked-self-insert-command
'self-insert-command)))
gp-letters-list)
(if gp-locked-modep
(progn
(define-key gp-map [mouse-2] 'gp-locked-mouse-2)
(define-key gp-map "\C-?" 'gp-locked-backward-delete-char-untabify)
(define-key gp-map "\C-d" 'gp-locked-delete-char)
(define-key gp-map "\C-k" 'gp-locked-kill-line)
(define-key gp-map "\C-y" 'gp-locked-yank))
(define-key gp-map [mouse-2] 'mouse-yank-at-click)
(define-key gp-map "\C-?" 'backward-delete-char-untabify)
(define-key gp-map "\C-d" 'delete-char)
(define-key gp-map "\C-k" 'kill-line)
(define-key gp-map "\C-y" 'yank)))
(when (null gp-map)
(let ((map (make-sparse-keymap)))
(define-key map "\C-m" (function gp-send-local-input))
(define-key map "\M-c" (function gp-copy-input))
(define-key map "\M-\C-m" (function gp-C-j))
(define-key map "\C-j" (function gp-C-j))
(define-key map "\C-c" (function gp-interrupt))
(define-key map "\M-\\\\" (function gp-break-long-line))
(define-key map "\M-\\a" (function gp-meta-a))
(define-key map "\M-\\b" (function gp-meta-b))
(define-key map "\M-\\d" (function gp-meta-d))
(define-key map "\M-\\m" (function gp-meta-m))
(define-key map "\M-\\p" (function gp-set-prompt))
(define-key map "\M-\\q" (function gp-meta-q))
(define-key map "\M-\\r" (function gp-meta-r))
(define-key map "\M-\\s" (function gp-meta-s))
(define-key map "\M-\\t" (function gp-meta-t))
(define-key map "\M-\\v" (function gp-meta-v))
(define-key map "\M-\\w" (function gp-meta-w))
(define-key map "\M-\\x" (function gp-meta-x))
(define-key map "\C-a" (function gp-beginning-of-line))
(define-key map [kp-home] (function gp-beginning-of-line))
(define-key map [home] (function gp-beginning-of-line))
(define-key map "\C-p" (function previous-line))
(define-key map "\C-n" (function next-line))
(define-key map "\M-p" (function gp-previous-cmd))
(define-key map "\M-n" (function gp-next-cmd))
(define-key map "\M-s" (function gp-skip-to-error))
(define-key map [C-kp-subtract] (function gp-remove-last-output))
(define-key map [M-kp-subtract] (function gp-remove-last-action))
(setq gp-map map)
(gp-define-locked-keys)))
(defvar gp-script-map nil
"Local keymap used in gp-script-mode.")
(when (null gp-script-map)
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" (function gp-maid))
(define-key map "\C-c\C-f" (function gp-tutor))
(define-key map "\M-\\\\" (function gp-break-long-line))
(define-key map "\M-\\d" (function gp-meta-d))
(define-key map "\M-\\t" (function gp-meta-t))
(define-key map "\M-\\v" (function gp-meta-v))
(define-key map "\M-\\z" (function gp-run-in-region))
(define-key map "\M-s" (function gp-skip-to-error))
;(define-key map "\C-c\C-c" (function gp-run-gp))
(define-key map "\C-c\C-e" (function gp-run-gp))
(setq gp-script-map map)))
;; Global keys. They *should* be global.
(define-key esc-map "o" (function gp-restore-wind-conf))
(define-key completion-list-mode-map [mouse-2] (function gp-mouse-2))
;; Maps used for the menu-bar.
(defvar GP-menu-map nil
"Keymap used for the menu-bar item GP in `gp-mode'")
(defvar GP-script-menu-map nil
"Keymap used for the menu-bar item GP in `gp-script-mode'")
(defvar gp-menu-map-level 0
"Integer qualifying the level of definition of
the GP-menu-map.
Level 0: the main menu is not yet defined;
Level 1: the main menu is defined;
Level 2: pari-completion is loaded;
Level 3: pari-fontification is loaded;
Level 4: pari-history is loaded;
pari-help is in fact loaded if present just after level 1.")
(defvar gp-script-menu-map-level 0
"Integer qualifying the level of definition of
the GP-menu-map.
Level 0: the main menu is not yet defined;
Level 1: the main menu is defined;
Level 2: pari-completion is loaded;
Level 3: pari-fontification is loaded;
pari-help is in fact loaded if present just after level 1.")
(defvar pari-menu-bar-update-hook nil)
;; this is to avoid using menu-bar-update-hook which is run
;; very often. We run pari-menu-bar-update-hook only when
;; the mode (gp or gp-script) is started.
;;---------------------------------------
;; PART ??? : sli-tools
;;---------------------------------------
(defcustom gp-tab-always-indent t
"Non-nil means TAB in MuPAD-mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used."
:type 'boolean :group 'gp-indentation)
(defcustom gp-indent-level 3
"Indentation used after \"{\"."
:type 'integer :group 'gp-indentation)
(put 'gp-indent-level 'safe-local-variable 'integerp)
(defun gp-set-and-recompute-indentation (sym val)
(set sym val)
(save-current-buffer
(mapcar
(lambda (bf)
(set-buffer bf)
(when (eq major-mode 'gp-script-mode)
(gp-learns-indentation)))
(buffer-list))))
(defcustom gp-structures
'((["for(" head 3] [")" end])
(["parfor(" head 6] [")" end])
(["forvec(" head 6] [")" end])
(["forstep(" head 7] [")" end])
(["fordiv(" head 6] [")" end])
(["forell(" head 6] [")" end])
(["forsubgroup(" head 7] [")" end])
(["sum(" head 6] [")" end])
(["parsum(" head 6] [")" end])
(["apply(" head 5] [")" end])
(["parapply(" head 8] [")" end])
(["return(" head 3] [")" end])
(["(" head 1] [")" end])
(["[" head 1] ["]" end])
(["{" head gp-indent-level] ["}" end])
;(["{" head 0] ["local" strong 0] ["}" end])
(["=" math-relation 1]) ;that's the last item of any relation, like in '=='
(["<" math-relation 1])
([">" math-relation 1])
)
"See `sli-structures'."
:type '(repeat (repeat (restricted-sexp :match-alternatives (vectorp listp))))
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defcustom gp-shift-alist '()
"See `sli-shift-alist'."
:type '(repeat (cons (vector string string) sexp))
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defcustom gp-no-heredity-list '()
"See `sli-no-heredity-alist'."
:type '(repeat (cons (vector string string) sexp))
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defvar gp-separators '(";" ",")
"See `sli-separators'.")
(defcustom gp-fixed-keys-alist '(("{" . 0))
;'(("local" . gp-indent-level) ("}" . 0))
"See `sli-fixed-keys-alist'."
:type '(repeat (cons string sexp))
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defcustom gp-keys-with-newline '(";")
"See `sli-keys-with-newline'."
:type '(repeat string)
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defcustom gp-add-to-key-alist '()
"See `sli-add-to-key-alist'."
:type '(repeat (cons string string))
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
(defcustom gp-more-maidp t
"Set it to nil if do not want `gp-maid'
to use `gp-add-to-key-alist'. Thus
if so 'end_proc' will not be followed by
a ':' and so on. See `sli-more-maidp'."
:type 'boolean
:initialize 'custom-initialize-default
:set 'gp-set-and-recompute-indentation
:group 'gp-indentation)
;; Not done !! In case I have some courage one day ...
(fset 'gp-indent-comment 'indent-for-tab-command)
;;---------------------------------------
;; PART III : gp-mode AND gp-script-mode
;;---------------------------------------
(defun file-really-exists-p (file)
(and (not (string= file "")) (file-exists-p file)))
(defun gp-kill-buffer-safely (abuffer)
(let ((b (get-buffer abuffer)))
(if b (kill-buffer b))))
(defun gp-learn-sexp nil
"To teach emacs some elements of gp-syntax."
;; Treat comments as white spaces in sexp:
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments t)
;; Care about capital or not (always local):
(setq case-fold-search nil)
;; Comments in sexp (We handle only one kind of comments):
(make-local-variable 'comment-start)
(setq comment-start "\\\\") ;; A *string*, NOT a regexp.
(make-local-variable 'comment-end)
(setq comment-end "")
(make-local-variable 'comment-start-skip)
; old version: (setq comment-start-skip "\\\\\\\\.*$\\|/\\*\\([^\\*]\\|\\*[^/]\\)*\\*/")
(setq comment-start-skip "\\\\\\\\.*$\\|/\\*\\([^*]\\|\\*+[^*/]\\)*\\*+/"))
(defun pari-mode nil
"Common part of '`gp-mode' and '`gp-script-mode'"
(gp-learn-sexp)
(set-syntax-table gp-syntax-table))
(defun gp-learns-indentation nil
(require 'sli-tools)
(sli-tools gp-structures gp-shift-alist gp-separators
'sli-is-a-separatorp-default
gp-fixed-keys-alist
"\\(^/\\*--+--\\*/\\|^}[ \t]*\\)\||\\(\n^\\){" ;; safe-place
gp-keys-with-newline nil gp-add-to-key-alist
'("//" "\\\\") gp-no-heredity-list)
(setq sli-more-maidp gp-more-maidp
sli-tab-always-indent gp-tab-always-indent))
;; The line ";;;###autoload" is useless.
;; It will be useful when pari.el will be part
;; of the usual distribution of emacs.
;;;###autoload
(defun gp-script-mode nil
"Major mode for editing GP input files.
The following bindings are available:
\\{gp-script-map}"
(interactive)
(kill-all-local-variables) ; exit from previous mode
(setq major-mode 'gp-script-mode mode-name "GP script")
(pari-mode)
; buffer-local:
(setq imenu-generic-expression
'((nil "^[{\t ]*\\([a-zA-Z]\\w*\\)(\\([^)]*\\)) *=[^=]" 1))
imenu-case-fold-search nil)
(set (make-local-variable 'block-comment-start) "/*")
(set (make-local-variable 'block-comment-end) "*/")
(set (make-local-variable 'comment-indent-function) 'gp-indent-comment)
(gp-learns-indentation)
(use-local-map gp-script-map) ; Make gp-script-map the local map in this mode.
;; Major modes should run-hooks as their last thing. But the hooks here
;; have been run earlier and for compatibility this is retained for now.
;; The idea was that hook functions might set variables like gp-menu-barp
;; to influence the mode setups. (Maybe the menus could be unconditional,
;; or gp-menu-barp dynamic with :included if the same was possible for the
;; GP-functions imenu, or expect the user to only set from .emacs or
;; customize. Maybe fontification could go by `font-lock-mode' rather
;; than gp-fontifyp.)
(run-hooks 'pari-mode-hook 'gp-script-mode-hook)
(run-mode-hooks 'gp-script-mode-hook) ; Set up user preferences.
(gp-add-imenu-index)
(gp-init-script-menu-bar) ; Start menu-bar.
(gp-update-fontification)
)
;; The line ";;;###autoload" is useless.
;; It will be useful when pari.el will be part
;; of the usual distribution of emacs.
;;;###autoload
(defun gp-mode (&optional no-fontificationp)
"Major mode for running a gp-process.
The following bindings are available:
\\{gp-map}"
(interactive)
(kill-all-local-variables) ; exit from previous mode
(setq major-mode 'gp-mode mode-name "GP")
(run-hooks 'pari-mode-hook)
(run-hooks 'gp-mode-hook) ; Set up user preferences.
(pari-mode)
; buffer-local:
(setq imenu-generic-expression
'((nil "\\<\\([a-zA-Z]\\w*\\)(\\([^)]*\\)) *=[^=]" 1))
imenu-case-fold-search nil)
(use-local-map gp-map) ; Make gp-map the local map of buffer *PARI*.
(gp-add-imenu-index)
(gp-init-menu-bar) ; Start menu-bar.
(unless no-fontificationp (gp-update-fontification))
)
(defun gp-add-imenu-index nil
(if (and gp-menu-barp
(progn (require 'easymenu) (featurep 'easymenu)))
(imenu-add-to-menubar "GP-functions")))
(defun gp-clear-temp-files nil
"Remove temporary files that may have been created"
(if (file-exists-p gp-temp-file)
(progn (delete-file gp-temp-file)
(message (gp-messager 2) gp-temp-file)))
(if (file-exists-p gp-el-temp-file)
(progn (delete-file gp-el-temp-file)
(message (gp-messager 2) gp-el-temp-file))))
(defun gp-save-setting-kill-emacs nil
"Remove temporary files."
(gp-clear-temp-files))
(add-hook 'kill-emacs-hook (function gp-save-setting-kill-emacs))
(defun gp-displace-input nil
"Returns t if input has been displaced and nil otherwise"
(if (and (save-excursion (re-search-forward gp-prompt-pattern nil t))
(save-excursion (re-search-backward gp-prompt-pattern nil t)))
(let ((where (- (point) (match-end 0))) last-prompt)
(kill-region (save-excursion
(goto-char (point-max))
(re-search-backward gp-prompt-pattern nil t)
(setq last-prompt (match-end 0))) (point-max))
(message (gp-messager 87))
(gp-copy-input)
(goto-char (+ where last-prompt))
t)
nil))
(defun gp-beginning-of-line nil
(interactive)
(beginning-of-line)
(when (looking-at gp-prompt-pattern)
(goto-char (match-end 0))))
;; For simple commands, displace and do.
;; For complocated ones, displace and dont do.
(defun gp-locked-self-insert-command nil
(interactive)
(gp-displace-input)
(insert-char last-command-event))
(defun gp-locked-mouse-2 (anevent arg)
(interactive "e\nP")
(mouse-set-point anevent)
(unless (gp-displace-input)
(yank arg)))
(defun gp-locked-yank (arg)
"Yank from the kill ring or copy a previous GP command.
If point is in the GP prompt then `yank' in the usual way
\(including \\[universal-argument] variations).
If point is back in a previous GP command then copy it to the GP
prompt."
(interactive "P")
(unless (gp-displace-input)
(yank)))
(defun gp-locked-backward-delete-char-untabify nil
(interactive)
(unless (gp-displace-input)
(backward-delete-char-untabify 1)))
(defun gp-locked-kill-line nil
(interactive)
(unless (gp-displace-input)
(kill-line)))
(defun gp-locked-delete-char nil
(interactive)
(unless (gp-displace-input)
(delete-char 1)))
;;-----------------------------
;; PART IV : GENERAL FUNCTIONS
;;-----------------------------
;;--------------------------
;; HANDLING THE WINDOWS ...
;;--------------------------
;; At the beginning, the user has asked for one window, but s/he may well
;; have introduced another window in-between (or even several ones).
;; We should then use only one other fixed window for everything else.
;; But since the list of the buffers displayed in a window does not exist,
;; and since the user may well change of window by ITself, we can't do much.
(defun gp-depile-wind-conf nil (setq gp-registers-list (cdr gp-registers-list)))
(defun gp-backward-wind-conf nil
"Restore previously stored window configuration."
(if (not (equal gp-registers-list nil))
(progn
(jump-to-register (car gp-registers-list))
(setq gp-registers-list (cdr gp-registers-list)))))
(defun gp-store-wind-conf nil
"Add a the current window configuration to the pile. If the pile
has more than 'gp-max-saved-wind-conf items
(0,1,...,(1- gp-max-saved-wind-conf)) then the first item is lost."
(if (= (length gp-registers-list) gp-max-saved-wind-conf)
(setq gp-registers-list (nreverse (cdr (nreverse gp-registers-list)))))
(let ((next (if (equal gp-registers-list nil) 0
(if (= (car gp-registers-list) (1- gp-max-saved-wind-conf)) 0
(1+ (car gp-registers-list))))))
(window-configuration-to-register next)
(setq gp-registers-list (cons next gp-registers-list))))
(defun gp-restore-wind-conf (&optional arg)
"Restore the previous window-configuration, killing the *gp-help* buffer
if it was and is no more displayed. When called with prefix C-u, end the
edition of the completion-file (if any were edited)."
(interactive "P")
(if (and arg (= (car arg) 4)) ;; Meaning that the call has been C-u M-o
(gp-quit-cpl-edit)
(let ((had-help-windowp (and (get-buffer "*gp-help*")
(get-buffer-window "*gp-help*")))
(had-message-windowp (and (get-buffer "*gp-messages*")
(get-buffer-window "*gp-messages*"))))
(gp-backward-wind-conf)
;; Kill the buffer *gp-help* if it is not displayed anymore:
(if had-help-windowp
(if (not (get-buffer-window "*gp-help*"))
(kill-buffer "*gp-help*")))
(if had-message-windowp
(if (not (get-buffer-window "*gp-messages*"))
(kill-buffer "*gp-messages*"))))
;; When called from menu-bar, write nothing in the minibuffer:
(message "")))
(defun gp-info-wind-conf nil (message (gp-messager 4)))
(defun buffer-visiblep (abuffer-name)
(if (get-buffer-window abuffer-name) t nil))
(defun gp-pgrmp (abuffer)
"Set buffer ABUFFER and return t if ABUFFER is in gp-script-mode."
(set-buffer abuffer) (eq major-mode 'gp-script-mode))
(defun gp-possible-file-name nil
"Try to guess the name of a likely gp-program."
;; First tries the existing windows, then the existing buffers.
(let ((pgrm nil))
(walk-windows
(lambda (wind)
(if (gp-pgrmp (window-buffer wind))
(setq pgrm
(cons (buffer-name (window-buffer wind)) pgrm)))))
(if pgrm (car pgrm) ;Return value if a window is displaying
;a candidate gp-program.
(mapcar
(lambda (abuffer)
(if (gp-pgrmp abuffer)
(setq pgrm (cons (buffer-name abuffer) pgrm))))
(buffer-list))
(if pgrm (car pgrm) ;Return value if a buffer is a candidate gp-program.
nil ;Return value if fail.
))))
(defun gp-window-manager (my-buffer-name option)
"Takes care of the windows in gp-mode and gp-script-mode.
Displays the buffer MY-BUFFER-NAME in a proper window.
The variable OPTION is
-- gp-beginning when we handle the beginning of a procedure. If a buffer
already exists with this name, only store the wind-conf.
-- gp-beginning-temp when we handle the beginning of a procedure. If a
buffer already exists with this name, store it.
-- gp-remove-help-now to remove help-window,
-- gp-remove-help-old-config to wait and remove help-window without
touching to the other windows.
-- gp-remove-help-now-old-config to remove help-window without
touching to the other windows.
-- gp-show-help which is similar to gp-beginning for the help buffer
except that we do not erase the content of this buffer.
-- nil when it is the end of a call.
The variable MY-BUFFER-NAME is one of
\"*PARI*\" \"*gp-help*\" \"*gp-menu*\". "
(cond ((and (string= my-buffer-name "*PARI*")
(eq option 'gp-beginning)
(get-buffer-window "*PARI*"))
;; We go to *PARI* and a window already exists with this buffer.
(gp-store-wind-conf)
(select-window (get-buffer-window "*PARI*")))
((and (string= my-buffer-name "*PARI*")
(eq option 'gp-beginning)
(not (get-buffer-window "*PARI*")))
;; We go to *PARI* and a window doesn't exist with this buffer.
(if (= (count-windows) 1)
;; If there is only 1 window containing anything but *scratch*
;; split the window in 2, else use this window:
(progn (if (not (string= (buffer-name) "*scratch*"))
(select-window (split-window-vertically)))
(switch-to-buffer "*PARI*"))
;; At least two windows exist. Do not create another one
;; and first try to use the help window, else the
;; starting window.
(gp-store-wind-conf)
(cond ((get-buffer-window "*gp-help*")
(select-window (get-buffer-window "*gp-help*"))
(switch-to-buffer "*PARI*"))
(t (switch-to-buffer-other-window "*PARI*")))))
((and (string= my-buffer-name "*PARI*")
(not option)
(get-buffer "*PARI*"))
;; We want to exit from *PARI*.
(if (> (count-windows) 1)
(delete-windows-on "*PARI*")
;; Else only one window.
(if (string= (buffer-name (window-buffer)) "*PARI*")
;; This only window displays "*PARI*"
(let ((next-buffer (gp-possible-file-name)))
(if next-buffer (switch-to-buffer next-buffer)
;; Else, don't know what to do !
(gp-restore-wind-conf)
))))
(unless gp-keep-PARI-buffer-when-quitting
(with-current-buffer (get-buffer "*PARI*")
(let ((inhibit-read-only t))
(remove-text-properties (point-min) (point-max) '(read-only nil))))
(kill-buffer "*PARI*")))
((and (get-buffer my-buffer-name)
(member my-buffer-name '("*gp-help*" "*gp-menu*"))
(eq option 'gp-remove-help-now)
(get-buffer-window my-buffer-name))
;; A buffer displaying "*gp-help*" or "*gp-menu*" exists.
;; We want to remove the message.
(if (or (string= my-buffer-name "*gp-help*")
(not (get-buffer "*gp-help*")))
;; Exit from help or the gp-menu is alone:
(gp-restore-wind-conf)
(if (string= my-buffer-name "*gp-menu*")
;; The previous condition should always be verified!
;; We should remove the window displaying gp-menu:
(progn
(if (and (= (count-windows) 2)
(get-buffer "*gp-help*"))
(progn
(gp-depile-wind-conf)
(switch-to-buffer "*gp-help*")
(other-window 1))
(gp-restore-wind-conf)))))
;; We have to kill the buffer (in any case) and select
;; a proper buffer for this window in case this killing
;; made something weird appear:
(gp-kill-buffer-safely my-buffer-name)
;; since it may have been destroyed by 'gp-restore-wind-conf.
(let ((buffer-to-select ""))
(save-excursion
(let ((abufferlist (buffer-list)))
(while (and (string= buffer-to-select "")
abufferlist)
(set-buffer (car abufferlist))
(if (memq major-mode '(gp-script-mode gp-mode))
(setq buffer-to-select (buffer-name)))
(setq abufferlist (cdr abufferlist)))))
;; Last weird case to handle: the buffer we have selected
;; is already being shown on another window.
;; Then kill our window.
(if nil ;(buffer-visiblep buffer-to-select)
(delete-window)
(or (string= buffer-to-select "") ;; Let it be !
(switch-to-buffer buffer-to-select)))))
((and (get-buffer my-buffer-name)
(member my-buffer-name '("*gp-help*" "*gp-menu*"))
(memq option '(gp-remove-help-old-config
gp-remove-help-now-old-config)))
;; A buffer displaying "*gp-help*" or gp-menu exists.
;; We want to remove the message without touching
;; to the window-configuration.
(cond ((eq option 'gp-remove-help-old-config)
(message (gp-messager 5))
(read-event)))
(kill-buffer my-buffer-name))
((and (string= my-buffer-name "*gp-help*")
(memq option '(gp-beginning gp-show-help))
(get-buffer-window "*gp-help*"))
;; We go to *gp-help* and a window already exists with this buffer.
(select-window (get-buffer-window "*gp-help*"))
(or (eq option 'gp-show-help) (erase-buffer)))
((and (string= my-buffer-name "*gp-help*")
(eq option 'gp-beginning-temp)
(get-buffer-window "*gp-help*"))
;; We go temporarily to *gp-help* and a window already exists with
;; this buffer.
(gp-store-wind-conf)
(select-window (get-buffer-window "*gp-help*"))
(erase-buffer))
((and (get-buffer my-buffer-name)
(member my-buffer-name '("*gp-help*" "*gp-menu*"))
(eq option 'gp-remove-help-now))
;; Since it got here, my-buffer-name is not displayed.
(gp-kill-buffer-safely my-buffer-name))
((and (string= my-buffer-name "*gp-help*")
(memq option '(gp-beginning gp-beginning-temp gp-show-help))
(not (get-buffer-window "*gp-help*")))
;; We go to *gp-help* and a window doesn't exist with this buffer.
(gp-store-wind-conf)
(if (= (count-windows) 1)
(progn (select-window (split-window-vertically))
(switch-to-buffer "*gp-help*"))
(cond ((and (get-buffer-window "*PARI*")
(not (eq (get-buffer-window "*PARI*") (selected-window))))
(select-window (get-buffer-window "*PARI*"))
(switch-to-buffer "*gp-help*"))
(t (switch-to-buffer-other-window "*gp-help*"))))
(or (eq option 'gp-show-help) (erase-buffer)))
((and (string= my-buffer-name "*gp-menu*")
(eq option 'gp-beginning))
;; We go to gp-menu.
(if (get-buffer "*gp-menu*")
;; A gp-menu already exists. Kill it first:
(save-excursion
(set-buffer "*gp-menu*")
(gp-menu-quit)))
(gp-store-wind-conf)
(if (get-buffer-window "*gp-help*")
(progn
(select-window (get-buffer-window "*gp-help*"))
(switch-to-buffer
(get-buffer-create "*gp-menu*"))
(kill-buffer "*gp-help*"))
(if (= (count-windows) 1)
(split-window-vertically))
(switch-to-buffer-other-window
(get-buffer-create "*gp-menu*"))))
)) ; end of 'gp-window-manager
;;----------------
;; THE GP PROCESS
;;----------------
(defun gp-make-gp-prompt-pattern (a-pattern)
"Add regexp a-pattern at beginning of line followed by any
amount of space/tab/newline to gp-prompt-pattern."
;; gp-prompt-pattern matches:
;; (New prompt plus any following white space) OR (Old pattern).
(let ((aux (concat "^\\(" a-pattern "\\) [\n\t ]*")))
;(font-lock-unset-defaults)
(setq font-lock-set-defaults nil)
(setq gp-prompt-pattern (concat aux "\\|" gp-prompt-pattern)
gp-fontification-keywords
(append
(list (list aux '(1 gp-prompt t)))
gp-fontification-keywords
)
font-lock-defaults '(gp-fontification-keywords nil nil nil))
(font-lock-set-defaults)))
(defun gp-beginning-of-last-line nil
(goto-char (point-max))
(re-search-backward gp-prompt-pattern)
(goto-char (match-end 0)))
(defun gp-stiffen-prompt nil
(save-excursion
(when (re-search-backward gp-prompt-pattern nil t) ; should be beginning of line ...
(let ((inhibit-read-only t)) ; in case of "quit" command.
(put-text-property (1- (match-end 0)) (match-end 0) 'rear-nonsticky t)
(put-text-property (1- (match-beginning 0)) (match-end 0) 'read-only t)))))
(defun gp-wait-for-output (point-init &optional nomessage process nostiff)
"Hang around until the prompt appears.
PROCESS defaults to gp-process."
(let ((notdone t))
(or process (setq process gp-process))
(setq nostiff (or nostiff (not (eq process gp-process))))
(while notdone
;; Wait till something comes out:
(while (and (not (accept-process-output process 0 300))
(not (= point-init (point)))
;; Following line is required for the \q command:
(eq 'run (process-status process))))
(let ((p (point)))
;(print (list "gp-wait-for-output:" (point-max)))
(if (or
;; Following lines are required for the \q command:
(not (and (processp process)
(eq 'run (process-status process))))
(save-excursion
(if (re-search-backward gp-prompt-pattern point-init t)
t ;(= (match-end 0) (point-max))
nil)))
;; If gp is not running, or the prompt has appeared, stop.
(progn
;(print (list "gp-wait-for-output:" (match-end 0) (point-max)))
(or nomessage (message (gp-messager 6)))
(setq notdone nil))
;; Else flush the buffer and wait a bit longer.
(progn (or nomessage (message (gp-messager 7)))))
(goto-char p))))
(sit-for 0)
(goto-char (point-max))
(unless nostiff (gp-stiffen-prompt)) ;(print "Out of stiffening !")
(set-marker (process-mark process) (point)))
(defun gp-get-shell (process-name process-buffer-name cmd)
"Explicit. Distinguishes bash/sh and [t]csh. Aimed at command gp+parameters."
;; We put the number of lines to 1000 so that no break will
;; occur when giving long comment like with "?6". We do not
;; want any "Return to continue", the editing job should
;; be done by emacs and not by gp.
;; Suspect that gp version 2.3 and up, may suppress "Return to
;; continue" anyway when running as "gp --emacs".
; (if (member (file-name-nondirectory shell-file-name) '("bash" "sh"))