-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagma-interactive.el
1058 lines (885 loc) · 35.5 KB
/
magma-interactive.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
;;; magma-interactive.el --- Interaction with an external magma process. ; -*- lexical-binding: t; -*-
;; Copyright (C) 2007-2014 Luk Bettale
;; 2013-2014 Thibaut Verron
;; Licensed under the GNU General Public License.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 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
;; General Public License for more details.
;;; Commentary:
;; Documentation available in README.org or on
;; https://github.com/ThibautVerron/magma-mode
;;; Code:
(require 'comint)
(require 'term)
(require 'compile)
(require 'magma-vars)
(require 'magma-completion)
(require 'magma-q)
(declare-function magma-mode "magma-mode.el")
(declare-function magma-run "magma-interactive.el" t t)
(declare-function magma-interactive-mode "magma-interactive.el" t t)
(declare-function magma-send "magma-interactive.el" t t)
(declare-function magma-help-word-text "magma-interactive.el" t t)
(defvar magma-mode-hook)
(defun magma-interactive-program-pred (val)
(member val '("magma")))
(defcustom magma-interactive-program "magma"
"Program to be launched to use magma (usually magma)."
:group 'magma
:type 'string
:risky nil
:safe 'magma-interactive-program-pred)
;;;###autoload (put 'magma-interactive-program 'safe-local-variable 'magma-interactive-program-pred)
(defcustom magma-interactive-arguments '()
"Commandline arguments to pass to magma."
:group 'magma
:type 'sexp)
(defvar magma-working-buffer-number 0
"Should this buffer send instructions to a different magma buffer.")
(defun magma--valid-working-buffer-number (num)
"Is NUM a valid working buffer number?
If this predicate is satisfied, the value is safe as file-local."
(or (integerp num)
(char-or-string-p num)))
(put 'magma-working-buffer-number
'safe-local-variable
#'magma--valid-working-buffer-number)
(defvar magma-active-buffers-list '()
"List of active magma buffers.")
(defvar-local magma-pending-input (magma-q-create)
"List of strings to be sent to the magma process.
Do not modify this variable directly, the consequences could be
unprevisible. This variable can be useful for use in a
user-function sending input to a process.")
(defvar-local magma-ready t
"Whether there is still input or output pending in that buffer.")
(defvar-local magma-timer (current-time))
(put 'magma-ready 'permanent-local t)
(put 'magma-pending-input 'permanent-local t)
(put 'magma-timer 'permanent-local t)
(defcustom magma-interactive-buffer-name "magma"
"Name of the buffer to be used for using magma interactively.
\(will be surrounded by `*')"
:group 'magma
:type 'string)
(defun magma-set-skip (symbol value)
(set-default symbol value)
(when (and magma-interactive-skip-comments
(not magma-interactive-skip-empty-lines))
(warn "magma-interactive-skip-empty-lines is nil, magma-interactive-skip-comments is t. Expect lots of empty lines replacing the comments.")))
(defcustom magma-interactive-skip-empty-lines nil
"If non-nil, strip empty lines before sending input to the magma process.
This variable can be set to nil even if
`magma-interactive-skip-comments' is set to t. However,
this will probably lead to unwanted behavior, since the
commented lines are replaced with blank lines."
:group 'magma
;; :set 'magma-set-skip ;; FIXME: find a way to uncomment this without error
:type 'boolean)
(defcustom magma-interactive-skip-comments nil
"If non-nil, strip comment lines before sending input to the magma process."
:group 'magma
:set 'magma-set-skip
:type 'boolean)
(defcustom magma-interactive-method 'line
"How should we send instructions to the magma process?
Can be one of the following symbols
- 'whole : send all at once
- 'expr : send one expression at a time
- 'line : send one line at a time
- 'file : write the region to a temporary file, and use
magma's load feature to evaluate it
If `magma-interactive-wait-between-inputs' is nil, this
setting does not change anything to the visible
result. However, it should prevent some edge cases, when the
input is too long to be sent to the magma process at once. In
this case, `emacs' will cut the input in half at an arbitrary
location, effectively confusing `magma'. Sending line per line
or expression per expression reduces the risk of having too
long input by forcing cuts at syntactically correct places."
:group 'magma
:options '(whole expr line file)
:type 'symbol)
(defun magma-temp-file ()
(make-temp-file "emacs-magma-"))
(defcustom magma-interactive-use-load nil
"See `magma-eval-buffer'."
:group 'magma
:type '(choice (const :tag "Never" nil)
(const :tag "Always" t)
(const :tag "Only local" 'local)))
(defcustom magma-interactive-load-syntax 'load
"Syntax for loading magma files"
:group 'magma
:type '(choice (const :tag "load ...;" 'load)
(const :tag "Attach(...);" 'attach)))
(defcustom magma-interactive-auto-save 'confirm
"Auto-save before sending-input?
This variable controls what to do if
`magma-interactive-use-load' is non-nil, and if the current
buffer has been modified. It takes one of the following three values:
- `confirm' : ask for confirmation (default)
- `always' : always save, no confirmation
- `never' : never save, no confirmation"
:group 'magma
:options '(confirm always never)
:type 'symbol)
(defcustom magma-interactive-wait-between-inputs t
"Wait for output before sending next input?
If non nil and `magma-interactive-method' is set to `expr' or
`line', wait for the magma process to output before sending the
next input.
With the current implementation, this should not induce any
overhead, so this variable is set to t by default. If you need
to set it to nil, please file an issue explaining why, it is
likely to be a bug or a design flaw.
Setting this variable has no effect in term mode."
:group 'magma
:type 'boolean)
;; (defvar magma--comint-interactive-escape-map
;; (if magma-interactive-comint-emulates-term
;; (let ((map (copy-keymap ctl-x-map)))
;; (define-key map (kbd "C-c C-c") 'comint-interrupt-subjob)
;; map)
;; nil))
(defvar magma-comint-interactive-mode-map
(let ((map (nconc (make-sparse-keymap) comint-mode-map )))
(define-key map "\t" 'completion-at-point)
(define-key map (kbd "RET") 'comint-send-input)
(define-key map (kbd "C-a") 'comint-bol-or-process-mark)
(define-key map (kbd "C-c p") 'magma-comint-toggle-pause-output)
;(define-key map (kbd "C-c") magma--comint-interactive-escape-map)
map)
"Keymap for magma-interactive-mode.")
(defvar magma-term-interactive-mode-map
(let ((map (nconc (make-sparse-keymap) term-mode-map)))
map)
"Keymap for magma-interactive-mode.")
(defconst magma-prompt-regexp "^[^ <\n]*>"
"Regexp matching the magma prompt.")
(defvar magma-prompt-read-only t
"Should the prompt in the magma buffer be read-only?
Setting this to nil can be confusing to users, but we expose
this setting for elisp calls.")
;; (defvar-local magma--output-finished t)
(defcustom magma-interactive-use-comint t
"If non-nil, communication with the magma process is done using comint.
Otherwise, it uses ‘term-mode’. After setting this variable,
restarting Emacs is required (or reloading the magma-mode load
file)."
:group 'magma
:type 'boolean)
;; (defcustom magma-interactive-comint-emulates-term nil
;; "Should comint buffers try to emulate term buffers?
;; If non-nil, interactive buffers using comint-mode try to emulate
;; the behavior of term-mode buffers. At the moment, it only means
;; that `C-c' can be used as a synonym for `C-x' (e.g. `C-c o' for
;; other-buffer), with the exception of `C-c C-c' which remains
;; bound to comint-interrupt-subjob.
;; After setting this variable,
;; restarting emacs is required (or reloading the magma-mode load
;; file)."
;; :group 'magma
;; :type 'boolean)
(defun magma-get-buffer-name (&optional i app)
(let ((name
(if app (concat magma-interactive-buffer-name "-" app)
magma-interactive-buffer-name)))
(if (not i) (magma-get-buffer-name magma-working-buffer-number)
(if (integerp i)
(if (= i 0) name
(concat name "-" (int-to-string i)))
(concat name "-" i)))))
(defun magma-set-working-buffer (i)
"Set the I'th buffer as the working buffer."
(interactive "NBuffer number ?: ")
(save-window-excursion
(magma-run i))
(setq magma-working-buffer-number i)
(message (concat "Working buffer set to " (int-to-string i)))
(magma-get-buffer i))
(defun magma-set-working-buffer-locally ()
"Change the working buffer number."
(interactive)
(make-local-variable 'magma-working-buffer-number)
(call-interactively #'magma-set-working-buffer))
(defun magma-make-buffer-name (&optional i app)
"Return the name of the I'th magma buffer.
If APP is not nil, it is a string which will be added before
the buffer number."
(concat "*" (magma-get-buffer-name i app) "*"))
(defun magma-get-buffer (&optional i norun)
"Return the I'th magma buffer.
If optional arg NORUN is nil (the default) and the buffer
doesn't exist or doesn't have a running process, start a magma
process. If NORUN is t, in that case, return nil."
(if norun
(get-buffer (magma-make-buffer-name i))
(magma-run i)))
;; magma-run only returns the buffer if it exists and has a process, and runs a process
(defcustom magma-interactive-prompt t
"If non nil, prompt for the path to the magma program and its arguments."
:group 'magma
:type 'boolean)
(defun magma--interactive-read-spec (prog args)
"Read the program to run and the arguments, if needed.
If `magma-interactive-prompt' is set to `t', prompt the user for
the command line, and split it in words. Otherwise, return the
program and arguments given as input.
The output is a list whose car is the program and cdr is the
arguments.
This function is meant for internal use only."
(if magma-interactive-prompt
(let* ((default (concat prog (or args "")))
(prompt "Program to run: ")
(readval (read-string prompt default nil default)))
(split-string readval))
(cons prog args)))
;; Comint definitions
(defun magma-comint-run (&optional i)
"Run an inferior instance of magma inside emacs, using comint."
(let ((bufname (magma-make-buffer-name i)))
(unless (comint-check-proc bufname) ; The buffer is new
(let* ((file (buffer-file-name))
(directory (or magma-default-directory
(and file (f-dirname file))
"~/"))
(progargs (magma--interactive-read-spec
magma-interactive-program
magma-interactive-arguments))
(program (car progargs))
(args (cdr progargs))
(new-interactive-buffer
(apply #'make-comint-in-buffer
(magma-get-buffer-name i)
(magma-make-buffer-name i)
program
nil
args)))
(push (or i 0) magma-active-buffers-list)
(set-buffer new-interactive-buffer)
(cd directory)
(setq magma-pending-input (magma-q-create))
(setq magma-ready t)
(magma-interactive-mode)
(when (not (comint-check-proc bufname))
(error (format "Failed to start process '%s'" program)))
(let ((dir-send-name (magma-make-send-name directory)))
(when dir-send-name
(magma-comint-send-string
(concat "ChangeDirectory(\"" dir-send-name "\");") i t)))))
(with-current-buffer bufname (current-buffer))))
(defun magma-comint-int (&optional i)
"Interrupt the magma process in buffer I."
(with-current-buffer (magma-get-buffer i t)
(setq magma-active-buffers-list
(remove (or i 0) magma-active-buffers-list))
(or (not (comint-check-proc (current-buffer)))
(comint-interrupt-subjob))
(setq magma-ready t)
(setq magma-pending-input (magma-q-create))))
(defun magma-comint-kill (&optional i)
"Kill the magma process in buffer I."
;;(interactive "P")
(setq magma-active-buffers-list
(remove (or i 0) magma-active-buffers-list))
(let ((buf (magma-get-buffer i t)))
(when buf
(with-current-buffer buf
(or (not (comint-check-proc (current-buffer)))
(comint-kill-subjob))
(setq magma-ready t)
(setq magma-pending-input (magma-q-create))))))
(defun magma-comint-toggle-pause-output ()
"Pause the autoscroll in the comint buffer."
(interactive)
(setq comint-scroll-to-bottom-on-output
(not comint-scroll-to-bottom-on-output)))
(defun magma-comint-send-string (expr &optional i norun)
"Send the expression EXPR to the magma buffer for evaluation.
If optional NORUN is t, do not attempt to start a process."
(let ((command (concat expr "\n")))
(run-hook-with-args 'comint-input-filter-functions expr)
(comint-send-string (magma-get-buffer i norun) command)))
(defun magma-comint-send (expr &optional i)
"Send the expression EXPR to the magma buffer for evaluation.
If the magma process is currently processing some previous input,
pushes `expr' onto the `magma-pending-input' queue."
(let ((buffer (magma-get-buffer i))
(oldbuf (current-buffer)))
(with-current-buffer buffer
(if magma-ready
(progn
(setq magma-ready nil)
(setq magma-timer (current-time))
(magma-comint-evaluate-here expr))
(magma-q-push magma-pending-input expr)))
(or
; First try for a window in same frame
(display-buffer-reuse-window buffer '((reusable-frames . nil)))
; Then a window in another frame
(display-buffer-reuse-window buffer '((reusable-frames . t)
(inhibit-switch-frame . t)
))
; Then pop the buffer in another window
(pop-to-buffer buffer))
;; Then go back to the edition window
(select-window (get-buffer-window oldbuf))
))
;; (defun magma--comint-get-old-input-before-send ()
;; "Function for the variable 'comint-get-old-input
;; This function is supposed to be the content of the variable
;; 'comint-get-old-input when evaluating content in the regular
;; way. In that case, if the point is not at the current input line,
;; we *don't* want input to take into account the current of the
;; magma buffer around the point.
;; "
;; (save-excursion
;; (comint-goto-process-mark)
;; (comint-get-old-input-default)
;; )
;; )
(defun magma-comint-next-input (_string)
"Send next input if the buffer is ready for it.
This function should only be called when the current buffer is a
magma evaluation buffer.
This function is added to `comint-output-filter-functions' and
takes an argument which is ignored."
;; (message (concat "-> " string " <-"))
;; (setq magma-ready t)
(save-excursion
(goto-char (point-max))
(when (or
(not magma-interactive-wait-between-inputs)
;; (string-match-p "\(.*\n\)?[^ <\n]*> $" string)
;;(looking-back "> " (- (point) 2))
;;(string-match-p ".*> $" string)
(<= (point) 2)
(save-excursion
(forward-char -2)
(looking-at "> ")
)
)
(if (magma-q-is-empty? magma-pending-input)
(setq magma-ready t)
;(let ((comint-get-old-input 'magma--comint-get-old-input-before-send))
(magma-comint-evaluate-here (magma-q-pop magma-pending-input))))))
(defun magma-comint-evaluate-here (expr)
"Evaluate the expression EXPR in the current buffer.
This function should only be called when the current buffer is a
magma evaluation buffer."
(save-excursion
(let ((command (magma-preinput-filter expr)))
; (unless (s-equals? command "")
(run-hook-with-args 'comint-input-filter-functions command)
;(goto-char (point-max))
(comint-goto-process-mark)
(insert command)
(comint-send-input))))
(defun magma-comint-help-word (topic)
"Call-up the handbook in an interactive buffer for TOPIC."
(interactive "sMagma help topic: ")
(make-comint-in-buffer (magma-get-buffer-name "help")
(magma-make-buffer-name "help")
magma-interactive-program
magma-interactive-arguments)
(with-current-buffer (magma-get-buffer "help")
(magma-interactive-mode))
(comint-send-string
(magma-get-buffer "help")
(format "?%s\n" topic))
(display-buffer (magma-get-buffer "help")))
(defun magma-comint-send-now (expr &optional i)
"Prompt for an expression then send it immediately.
The expression is sent to the comint buffer without waiting until
ready.
The primary use-case is to be able to send a value to a `read' or
`readi' prompt, without having to switch buffers.
This can be used to force a new line feed when the magma prompt
is hanging at the end of the line, preventing comint to
acknowledge that the magma process is ready."
(interactive "sExpression: ")
(let ((buf (magma-get-buffer (magma-choose-buffer i))))
(with-current-buffer buf
(save-excursion
(goto-char (point-max))
(insert expr)
(comint-send-input)))))
;; Term-mode definitions
(defun magma-term-run (&optional i)
"Run an inferior instance of magma inside emacs, using term."
(let* ((magma-buffer-name (magma-get-buffer-name i))
(reusing-buff
(get-buffer-process (concat "*" magma-buffer-name "*")))
(new-interactive-buffer
(make-term magma-buffer-name magma-interactive-program)))
(save-excursion
(push (or i 0) magma-active-buffers-list)
(set-buffer new-interactive-buffer)
(unless reusing-buff
(insert
(concat "// WARNING: term mode for the magma interactive buffer is\n"
"// deprecated and may be removed from future releases.\n"
"// You can activate comint mode (recommended) by setting\n "
"// the variable `magma-interactive-use-comint' to `t'.\n"
"// If you encounter problems with comint but not with term,\n"
"// please report them by mail or through the issue tracker.\n")))
(magma-interactive-mode)
(term-char-mode))))
(defun magma-term-int (&optional i)
"Interrupt the magma process in buffer I."
(setq magma-active-buffers-list
(remove (or i 0) magma-active-buffers-list))
(if (term-check-proc (magma-get-buffer i))
(with-current-buffer (magma-get-buffer i)
(term-send-string (magma-get-buffer i) "\C-c"))))
(defun magma-term-kill (&optional i)
"Kill the magma process in buffer I."
(setq magma-active-buffers-list
(remove (or i 0) magma-active-buffers-list))
(if (term-check-proc (magma-get-buffer i))
(with-current-buffer (magma-get-buffer i)
(term-kill-subjob))))
(defun magma-term-send (expr &optional _i)
"Send the expression EXPR to the magma buffer for evaluation."
;; The variable `_i' is not used, this is probably a bug. But term-based
;; interaction will go away at some point anyway
(save-window-excursion
(let ((command (magma-preinput-filter expr)))
;(unless (s-equals? command "")
(magma-switch-to-interactive-buffer)
(goto-char (point-max))
(insert command)
(term-send-input))))
;)
(defun magma-term-help-word (topic)
"Call-up the handbook in an interactive buffer for TOPIC."
(interactive "sMagma help topic: ")
(make-term (magma-get-buffer-name "help")
magma-interactive-program)
(with-current-buffer (magma-get-buffer "help")
(magma-interactive-mode)
(term-line-mode)
(term-show-maximum-output))
(term-send-string
(magma-get-buffer "help")
(format "?%s\n" topic))
(display-buffer (magma-get-buffer "help")))
;; Wrappers
(defun magma-send-expression (expr &optional i)
"Send the expression."
(interactive "iP")
(let* ((initval (if (region-active-p)
(buffer-substring-no-properties (region-beginning)
(region-end))))
(expr
(or expr
(read-string "Expr:" initval))))
(magma-send-or-broadcast expr i)))
(defun magma-restart (&optional i)
"Restart the magma process in buffer I."
(interactive "P")
(magma-kill-or-broadcast i)
(sleep-for 2)
(magma-run-or-broadcast i))
(defun magma-switch-to-interactive-buffer (&optional i)
"Switch to the magma process in buffer I in another frame."
(interactive "P")
(magma-run i)
(switch-to-buffer-other-frame (magma-get-buffer i)))
(defun magma-switch-to-interactive-buffer-same-frame (&optional i)
"Switch to the magma process in buffer I, in another window on the same frame."
(interactive "P")
(magma-run i)
(pop-to-buffer (magma-get-buffer i)))
(defun magma--at-end (end)
(or (looking-at "\\([[:blank:]]\\|\n\\)*\\'")
(>= (point) end)))
(defun magma-eval-region (beg end &optional i)
"Evaluate the current region.
The behavior of this function depends on the value of
`magma-interactive-method':
- if `whole', send the whole region to comint. Emacs may decide
that this block of text is too long for input, and cut it and
send it in batches. In this case, it will cut it
at (apparently) random points, which may cause syntax errors, if
the break is in the middle of an identifier for example;
- if `line', send the region one line at a time;
- if `expr', send the region one expr at a time;
- if `file', copy the region to a temporary file and use \"load ...;\"
to evaluate it in magma.
Additionally, if `magma-interactive-wait-between-inputs' is non
nil, and if `magma-interactive-method' is either `line' or
`expr', emacs will wait until magma has processed the input
before sending the next part. The result is that the buffer is
more nicely structured, with each output located right after the
corresponding input."
(interactive "rP")
(save-restriction
(narrow-to-region beg end)
(cl-case magma-interactive-method
('whole
(let ((str (buffer-substring-no-properties beg end)))
(magma-send-or-broadcast str i)))
('expr
(save-excursion
(goto-char beg)
(let ((magma-interactive-method 'whole))
(while (not (magma--at-end end))
(magma-eval-next-statement i)))))
('line
(save-excursion
(goto-char beg)
(while (not (magma--at-end end))
(magma-eval-line i))))
('file
(let ((buf (current-buffer))
(tmp (magma-temp-file)))
(with-temp-buffer
(find-file-literally tmp)
(erase-buffer)
(insert-buffer-substring-no-properties buf beg end)
(let ((magma-interactive-use-load t)
(magma-interactive-auto-save 'always))
(magma-eval-buffer i))
(kill-buffer)
(delete-file tmp)))))))
(defun magma-eval-line ( &optional i)
"Evaluate current line."
(interactive "P")
(while (looking-at "^$")
(forward-line))
(let* ((beg (save-excursion
(back-to-indentation)
(point)))
(end (save-excursion
(end-of-line)
(point))))
;; (message (format "eval-line: %s %s" beg end))
(let ((magma-interactive-method 'whole))
(magma-eval-region beg end i))
(end-of-line)
(or (eobp) (forward-line 1))))
(defun magma-eval-paragraph ( &optional i)
"Evaluate current paragraph (space separated block)."
(interactive "P")
(forward-paragraph)
(let ((end (point)))
(backward-paragraph)
(magma-eval-region (point) end i)
(goto-char end)))
(defun magma-end-of-expr-or-line ()
"Go to the end of line or expression according to the evaluation method.
If `magma-interactive-method' is `line', go to the end of the
line, otherwise the end of the expression."
(interactive)
(magma-end-of-expr)
(if (eq magma-interactive-method 'line)
(progn (forward-line 1)
(forward-char -1)
(or (looking-at "$") (forward-char 1)))))
(defun magma-eval-next-statement ( &optional i)
"Evaluate current or next statement."
(interactive "P")
(let ((regbeg (progn
(magma-beginning-of-expr)
(point)))
(regend (progn
(magma-end-of-expr-or-line)
(point))))
(magma-eval-region regbeg regend i)
(goto-char regend)
(or (eobp) (forward-char 1))))
(defun magma-eval (&optional i)
"Evaluate dwim.
The function evaluates the current region if set and the current
statement otherwise"
(interactive "P")
(if mark-active
(magma-eval-region (region-beginning) (region-end) i)
(progn
(magma-eval-next-statement i)
;(when (looking-at "[[:space:]]*$") (forward-line 1))
)))
(defun magma-eval-defun (&optional i)
"Evaluate the current defun."
(interactive "P")
(let ((curpt (point))
(regbeg (save-excursion
(magma-beginning-of-defun)
(point)))
(regend (save-excursion
(magma-end-of-defun)
(point))))
(if (or (> regbeg curpt) (< regend curpt))
(message "Not in a function, procedure or intrinsic definition")
(progn
(magma-eval-region regbeg regend i)
(goto-char regend)))))
(defun magma-eval-until ( &optional i)
"Evaluates all code from the beginning of the buffer to the point."
(interactive "P")
(magma-eval-region (point-min) (point) i))
(defvar magma-load-file-path-conversion '()
"Path conversions to perform before sending a path to magma.
Each entry is a cons cell `(from . to)' where `old' and `new' are
absolute paths. The effect is that any path starting by `old' is
transformed into the same path, but starting by `new'.
It can be useful when running magma over ssh with files
synchronized outside of emacs' control.")
(defun magma-load-file-convert-dir (path)
(dolist (conv magma-load-file-path-conversion)
(setq path
(replace-regexp-in-string (concat "^" (regexp-quote (car conv)))
(cdr conv) path)))
path)
(defun magma-load-file-strip-remote (filename)
"Remove ssh prefix inserted by tramp.
This function allows to edit and evaluate magma files over tramp."
(if (and (eq magma-interactive-use-load 'only-local)
(file-remote-p filename))
nil
(file-local-name filename)))
(defcustom magma-load-file-transformation-functions
'(magma-load-file-strip-remote
magma-load-file-convert-dir)
"Functions to run to get the filepath to send using load or attach.
The functions are run in order with the output of the previous
one as input, starting with the absolute filename of the working
buffer.
If one of the functions returns nil, the buffer is not sent using
load or attach."
:group 'magma
:type '(list function))
(defun magma-make-send-name (filename)
(let ((funs magma-load-file-transformation-functions))
(while (and filename funs)
(setq filename (funcall (car funs) filename))
(setq funs (cdr funs)))
filename))
(defun magma-eval-buffer ( &optional i)
"Evaluates all code in the buffer.
If `magma-interactive-use-load' is non-nil and if the current
buffer is associated to a file, use \"load ...;\" to evaluate the
buffer, instead of sending it line per line. Note that if you use
magma on a remote host, this method will require that you save
the file to the remote host when needed.
If needed, confirm saving through `magma-confirm-save-buffer'.
Otherwise, send the whole buffer to `magma-eval-region'."
(interactive "P")
(let ((send-name
(and (buffer-file-name)
magma-interactive-use-load
(magma-make-send-name
(f-long (buffer-file-name))))))
(if send-name
(progn
(when (buffer-modified-p)
(magma-confirm-save-buffer))
(magma-send-or-broadcast
(format
(if (equal magma-interactive-load-syntax 'load) "load \"%s\";"
"Attach(\"%s\");")
send-name) i))
(magma-eval-region (point-min) (point-max) i))))
(defun magma-confirm-save-buffer ()
"Prompt for confirmation, then save the current buffer.
The behavior of this function is controlled by
`magma-interactive-auto-save'."
(let ((should-save
(cl-case magma-interactive-auto-save
('always t)
('never nil)
('confirm
(let ((prompt (format "File %s modified. Save? "
(buffer-file-name))))
(yes-or-no-p prompt))))))
(when should-save
(save-buffer))))
(defun magma-help-word (&optional browser)
"Call-up the handbook in the interactive buffer for the current word."
(interactive "P")
(let ((topic (read-string
(format "Magma help topic (default %s): " (current-word))
nil nil (current-word))))
(if browser
(magma-help-word-browser topic)
(magma-help-word-text topic))))
(defun magma-help-word-browser (&optional topic)
"Open the magma help page in a web browser for TOPIC."
(interactive)
(let ((topic (or topic
(read-string
(format "Magma help topic (default %s): " (current-word))
nil nil (current-word)))))
(let ((urlprefix "http://magma.maths.usyd.edu.au/magma/handbook/")
(urlsuffix "&chapters=1&examples=1&intrinsics=1"))
(browse-url (concat urlprefix "search?query=" topic urlsuffix)))))
(defun magma-show-word (&optional i)
"Show the current word in magma."
(interactive "P")
(let ((word (current-word)))
(magma-run-or-broadcast i)
(magma-send-or-broadcast
(concat word ";") i)))
(defun magma-broadcast-fun (fun)
(mapc
#'(lambda (i) (save-excursion (funcall fun i)))
magma-active-buffers-list))
(defun magma-choose-buffer (i)
"Choose what buffer to work on (internal).
Given an input I in raw prefix form, decides what buffers we
should be working on. The input can be an integer, in which
case it returns that integer; or it can be the list '(4), in
which case it prompts for an integer; or it can be the list
'(16), in which case it returns the symbol 'broadcast, meaning
we should work on all buffers"
(cond
((not i) magma-working-buffer-number)
((integerp i)
i)
((and (listp i) (eq (car i) 4))
(read-number "In buffer?" magma-working-buffer-number))
((and (listp i) (eq (car i) 16))
'broadcast)
(t (message "Invalid buffer specified") magma-working-buffer-number)))
(defun magma-broadcast-if-needed (fun i)
(let ((buf (magma-choose-buffer i)))
(cond
((integerp buf)
(funcall fun buf))
((eq buf 'broadcast)
(magma-broadcast-fun fun))
(t (message "Invalid buffer specified")))))
(defun magma-send-or-broadcast (expr i)
(magma-broadcast-if-needed (apply-partially 'magma-send expr) i))
(defun magma-kill-or-broadcast (i)
(magma-broadcast-if-needed 'magma--kill-cmd i))
(defun magma-int-or-broadcast (i)
(magma-broadcast-if-needed 'magma--int-cmd i))
(defun magma-run-or-broadcast (i)
(magma-broadcast-if-needed 'magma-run i))
(defun magma-kill (i)
"Kill the magma process."
(interactive "P")
(magma-kill-or-broadcast i))
(defun magma-int (i)
"Interrupt the magma process."
(interactive "P")
(magma-int-or-broadcast i))
;;(defvar magma--echo-complete nil)
;; (defun magma-message-raw-output (output)
;; (message output)
;; output)
;; FIXME: If the input line is long, the output also contains the end
;; of the line, which may lead to broken syntax (eg emacs thinks we
;; are in a string)
(defun magma-comint-delete-reecho (output)
;(message output)
(with-temp-buffer
(insert output)
(flush-lines "\" (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))
)
(defun magma-preinput-filter (input)
(with-temp-buffer
(insert input)
(let ((magma-mode-hook nil))
(magma-mode))
(when magma-interactive-skip-comments
(goto-char (point-min))
(insert "\n")
(magma--comment-kill-no-kill-ring (count-lines (point-min) (point-max))))
(when magma-interactive-skip-empty-lines
(flush-lines "^[[:blank:]]*$" (point-min) (point-max)))
(buffer-substring-no-properties (point-min) (point-max))))
;; (defun magma-comint-delete-reecho (output)
;; (when (string-match "^\\(\\(.\\|\n\\)*\n\\)[[:alnum:]|]*> \\1" output)
;; (setq output (replace-match "" nil nil output)))
;; output)
(defun magma-comint-send-input ()
"Obsolete."
(interactive)
(message "`magma-comint-send-input' is deprecated, use `comint-send-input' instead.")
(comint-send-input))
(defconst magma-interactive-modeline-ready-face
"success")
(defconst magma-interactive-modeline-run-face
"warning")
(defconst magma-interactive-modeline-stop-face
"error")
(defun magma-interactive-make-mode-line-process ()
(format
":%s"
(if (comint-check-proc (current-buffer))
(if magma-ready
(propertize "ready" 'face magma-interactive-modeline-ready-face)
(concat
(propertize "run" 'face magma-interactive-modeline-run-face)
":["
(format-seconds
"%d:%h:%02m:%z%02s]"
(float-time (time-subtract (current-time) magma-timer)))))
(propertize "stop" 'face magma-interactive-modeline-stop-face))))
(defun magma-interactive-common-settings ()
"Settings common to comint and term modes."
;; Compilation shell mode
(add-to-list
'compilation-error-regexp-alist
'("^In file \"\\(.*?\\)\", line \\([0-9]+\\), column \\([0-9]+\\):$"
1 2 3 2 1))
(setq-local compilation-mode-font-lock-keywords nil)
(compilation-shell-minor-mode 1)