forked from dylan-lang/dylan-emacs-support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dime-repl.el
1495 lines (1291 loc) · 55.2 KB
/
dime-repl.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
;;; dime-repl.el --- Dylan interaction mode -*- lexical-binding: t -*-
;; Lineage: SLIME, Open Dylan
;; Copyright (C) 20?? Helmut Eller (SLIME)
;; Copyright (C) 2011, 2012, 2013 Hannes Mehnert
;; Copyright (C) 2021 Lassi Kortela
;; SPDX-License-Identifier: GPL-2.0-or-later
;; URL: https://opendylan.org/
;; Package-Requires: ((emacs "25.1"))
;;; Commentary:
;; Dylan read-eval-print loop for Dime.
;; To install, call dime-setup and include 'dime-repl as argument:
;;
;; (dime-setup '(dime-repl [others conribs ...]))
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'dime)
(defvar-local dime-repl-project-stack nil
"The stack of projects visited in this repl.")
(defvar-local dime-repl-directory-stack nil
"The stack of default directories associated with this repl.")
(defvar-local dime-repl-prompt-start-mark nil)
(defvar-local dime-repl-input-start-mark nil)
(defvar-local dime-repl-old-input-counter 0
"Counter used to generate unique `dime-repl-old-input' properties.
This property value must be unique to avoid having adjacent inputs be
joined together.")
(define-dime-contrib dime-repl
"Read-Eval-Print Loop written in Emacs Lisp.
This contrib implements a Dylan Listener along with some niceties like
a persistent history and various \"shortcut\" commands. Nothing here
depends on comint.el; I/O is multiplexed over Dime's socket.
This used to be the default REPL for Dime, but it was hard to
maintain."
(:authors "too many to mention")
(:license "GPL-2.0-or-later")
(:on-load
(add-hook 'dime-event-hooks 'dime-repl-event-hook-function)
(add-hook 'dime-connected-hook 'dime-repl-connected-hook-function)
(setq dime-find-buffer-project-function 'dime-repl-find-buffer-project))
(:on-unload (dime-repl-remove-hooks)))
;;;;; dime-repl
(defgroup dime-repl nil
"The Read-Eval-Print Loop (*dime-repl* buffer)."
:prefix "dime-repl-"
:group 'dime)
(defcustom dime-repl-shortcut-dispatch-char ?\,
"Character used to distinguish repl commands from dylan forms."
:type '(character)
:group 'dime-repl)
(defcustom dime-repl-only-save-dylan-buffers t
"Save only Dylan buffers, instead of all buffers?
When T we only attempt to save `dylan-mode' file buffers. When NIL
dime will attempt to save all buffers (as per `save-some-buffers').
This applies to all ASDF related repl shortcuts."
:type '(boolean)
:group 'dime-repl)
(defface dime-repl-prompt-face
(if (dime-face-inheritance-possible-p)
'((t (:inherit font-lock-keyword-face)))
'((((class color) (background light)) (:foreground "Purple"))
(((class color) (background dark)) (:foreground "Cyan"))
(t (:weight bold))))
"Face for the prompt in the Dime REPL."
:group 'dime-repl)
(defface dime-repl-output-face
(if (dime-face-inheritance-possible-p)
'((t (:inherit font-lock-string-face)))
'((((class color) (background light)) (:foreground "RosyBrown"))
(((class color) (background dark)) (:foreground "LightSalmon"))
(t (:slant italic))))
"Face for Dylan output in the Dime REPL."
:group 'dime-repl)
(defface dime-repl-input-face
'((t (:bold t)))
"Face for previous input in the Dime REPL."
:group 'dime-repl)
(defface dime-repl-result-face
'((t ()))
"Face for the result of an evaluation in the Dime REPL."
:group 'dime-repl)
(defcustom dime-repl-history-file "~/.dime-history.eld"
"File to save the persistent REPL history to."
:type 'string
:group 'dime-repl)
(defcustom dime-repl-history-size 200
"*Maximum number of lines for persistent REPL history."
:type 'integer
:group 'dime-repl)
(defcustom dime-repl-history-file-coding-system
(cond ((dime-find-coding-system 'utf-8-unix) 'utf-8-unix)
(t dime-net-coding-system))
"*The coding system for the history file."
:type 'symbol
:group 'dime-repl)
;; dummy defvar for compiler
(defvar dime-repl-read-mode)
(defun dime-repl-reading-p ()
"True if Dylan is currently reading input from the REPL."
(with-current-buffer (dime-repl-output-buffer)
dime-repl-read-mode))
;;;; Stream output
(dime-def-connection-var dime-connection-output-buffer nil
"The buffer for the REPL. May be nil or a dead buffer.")
(defvar-local dime-repl-output-start nil
"Marker for the start of the output for the evaluation.")
(defvar-local dime-repl-output-end nil
"Marker for end of output. New output is inserted at this mark.")
(defun dime-repl-output-buffer (&optional noprompt)
"Return the output buffer, create it if necessary.
If NOPROMPT is non-nil, omit prompt."
(let ((buffer (dime-connection-output-buffer)))
(or (if (buffer-live-p buffer) buffer)
(setf (dime-connection-output-buffer)
(let ((connection (dime-connection)))
(with-current-buffer (dime-repl-buffer t connection)
(unless (eq major-mode 'dime-repl-mode)
(dime-repl-mode))
(setq dime-buffer-connection connection)
(dime-repl-reset-markers)
(unless noprompt
(dime-repl-insert-prompt))
(current-buffer)))))))
(defun dime-repl-target-to-marker (target)
"Return marker corresponding to the given TARGET, nil if none."
(cl-case target
((nil)
(with-current-buffer (dime-repl-output-buffer)
dime-repl-output-end))
(:repl-result
(with-current-buffer (dime-repl-output-buffer)
dime-repl-input-start-mark))
(t nil)))
(defvar dime-repl-banner-function 'dime-repl-insert-banner)
(defun dime-repl-update-banner ()
(funcall dime-repl-banner-function)
(dime-move-point (point-max))
(dime-repl-mark-output-start)
(dime-repl-mark-input-start)
(dime-repl-insert-prompt))
(defun dime-repl-insert-banner ()
(when (zerop (buffer-size))
(let ((welcome (concat "; Dime " (or (dime-changelog-date)
"- ChangeLog file not found"))))
(insert welcome))))
(defun dime-repl-init-output-buffer (connection)
(with-current-buffer (dime-repl-output-buffer t)
(setq dime-buffer-connection connection
dime-repl-directory-stack '()
dime-repl-project-stack '())
(dime-repl-update-banner)))
(defun dime-repl-display-output-buffer ()
"Display the output buffer and scroll to bottom."
(with-current-buffer (dime-repl-output-buffer)
(goto-char (point-max))
(unless (get-buffer-window (current-buffer) t)
(display-buffer (current-buffer) t))
(dime-repl-show-maximum-output)))
(defun dime-repl-output-filter (process string)
(with-current-buffer (process-buffer process)
(when (and (cl-plusp (length string))
(eq (process-status dime-buffer-connection) 'open))
(dime-write-string string))))
(defvar dime-open-stream-hooks)
(defun dime-repl-open-stream-to-dylan (port)
(let ((stream (open-network-stream "*dylan-output-stream*"
(dime-with-connection-buffer ()
(current-buffer))
dime-dylan-host port)))
(dime-set-query-on-exit-flag stream)
(set-process-filter stream 'dime-repl-output-filter)
(let ((pcs (process-coding-system (dime-current-connection))))
(set-process-coding-system stream (car pcs) (cdr pcs)))
(when-let ((secret (dime-secret)))
(dime-net-send secret stream))
(run-hook-with-args 'dime-open-stream-hooks stream)
stream))
(defun dime-repl-write-string (string &optional target)
(cl-case target
((nil) (dime-repl-emit string))
(:repl-result (dime-repl-emit-result string))
(t (dime-emit-to-target string target))))
(defvar dime-repl-popup-on-output nil
"Display the output buffer when some output is written.
This is set to nil after displaying the buffer.")
(defmacro dime-repl-save-marker (marker &rest body)
(declare (indent 1))
(let ((pos (cl-gensym "pos")))
`(let ((,pos (marker-position ,marker)))
(prog1 (progn . ,body)
(set-marker ,marker ,pos)))))
(defun dime-repl-emit (string)
;; insert the string STRING in the output buffer
(with-current-buffer (dime-repl-output-buffer)
(save-excursion
(goto-char dime-repl-output-end)
(dime-repl-save-marker dime-repl-output-start
(dime-propertize-region '(face dime-repl-output-face
rear-nonsticky (face))
(insert-before-markers string)
(when (and (= (point) dime-repl-prompt-start-mark)
(not (bolp)))
(insert-before-markers "\n")
(set-marker dime-repl-output-end (1- (point)))))))
(when dime-repl-popup-on-output
(setq dime-repl-popup-on-output nil)
(display-buffer (current-buffer)))
(dime-repl-show-maximum-output)))
(defun dime-repl-emit-result (string &optional bol)
;; insert STRING and mark it as evaluation result
(with-current-buffer (dime-repl-output-buffer)
(save-excursion
(dime-repl-save-marker dime-repl-output-start
(dime-repl-save-marker dime-repl-output-end
(goto-char dime-repl-input-start-mark)
(when (and bol (not (bolp))) (insert-before-markers "\n"))
(dime-propertize-region `(face dime-repl-result-face
rear-nonsticky (face))
(insert-before-markers string)))))
(dime-repl-show-maximum-output)))
(defun dime-repl-switch-to-output-buffer ()
"Select the output buffer, when possible in an existing window.
Hint: You can use `display-buffer-reuse-frames' and
`special-display-buffer-names' to customize the frame in which
the buffer should appear."
(interactive)
(pop-to-buffer (dime-repl-output-buffer))
(goto-char (point-max)))
;;;; REPL
;;
;; The REPL uses some markers to separate input from output. The
;; usual configuration is as follows:
;;
;; ... output ... ... result ... prompt> ... input ...
;; ^ ^ ^ ^ ^
;; output-start output-end prompt-start input-start point-max
;;
;; input-start is a right inserting marker, because
;; we want it to stay behind when the user inserts text.
;;
;; We maintain the following invariant:
;;
;; output-start <= output-end <= input-start.
;;
;; This invariant is important, because we must be prepared for
;; asynchronous output and asynchronous reads. ("Asynchronous" means,
;; triggered by Dylan and not by Emacs.)
;;
;; All output is inserted at the output-end marker. Some care must be
;; taken when output-end and input-start are at the same position: if
;; we insert at that point, we must move the right markers. We should
;; also not leave (window-)point in the middle of the new output. The
;; idiom we use is a combination to dime-repl-save-marker,
;; insert-before-markers, and manually updating window-point
;; afterwards.
;;
;; A "synchronous" evaluation request proceeds as follows: the user
;; inserts some text between input-start and point-max and then hits
;; return. We send that region to Dylan, move the output and input
;; makers to the line after the input and wait. When we receive the
;; result, we insert it together with a prompt between the output-end
;; and input-start mark. See `dime-repl-insert-prompt'.
;;
;; It is possible that some output for such an evaluation request
;; arrives after the result. This output is inserted before the
;; result (and before the prompt).
;;
;; If we are in "reading" state, e.g., during a call to Y-OR-N-P,
;; there is no prompt between output-end and input-start.
;;
(dime-def-connection-var dime-dylan-project-prompt-string
"opendylan"
"The current project name of the Superior dylan.
This is automatically synchronized from Dylan.")
(defun dime-repl-reset-markers ()
(dolist (markname '(dime-repl-output-start
dime-repl-output-end
dime-repl-prompt-start-mark
dime-repl-input-start-mark))
(set markname (make-marker))
(set-marker (symbol-value markname) (point))))
;;;;; REPL mode setup
(defvar dime-repl-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map lisp-mode-map)
map))
(dime-define-keys dime-prefix-map
("\C-z" 'dime-repl-switch-to-output-buffer)
("\M-p" 'dime-repl-set-project))
(dime-define-keys dime-mode-map
("\C-c~" 'dime-sync-project-and-directory))
(dime-define-keys dime-connection-list-mode-map
((kbd "RET") 'dime-repl-goto-connection)
([return] 'dime-repl-goto-connection))
(dime-define-keys dime-repl-mode-map
("\C-m" 'dime-repl-return)
([return] 'dime-repl-return)
("\C-j" 'dime-repl-newline-and-indent)
("\C-\M-m" 'dime-repl-closing-return)
([(control return)] 'dime-repl-closing-return)
("\C-a" 'dime-repl-bol)
([home] 'dime-repl-bol)
("\M-p" 'dime-repl-previous-input)
((kbd "C-<up>") 'dime-repl-backward-input)
("\M-n" 'dime-repl-next-input)
((kbd "C-<down>") 'dime-repl-forward-input)
("\M-r" 'dime-repl-previous-matching-input)
("\M-s" 'dime-repl-next-matching-input)
("\C-c\C-c" 'dime-interrupt)
;; ("\t" 'dime-complete-symbol)
("\t" 'dime-indent-and-complete-symbol)
("\M-\t" 'dime-complete-symbol)
(" " 'dime-space)
("\C-c\C-o" 'dime-repl-clear-output)
("\C-c\M-o" 'dime-repl-clear-buffer)
("\C-c\C-u" 'dime-repl-kill-input)
("\C-c\C-n" 'dime-repl-next-prompt)
("\C-c\C-p" 'dime-repl-previous-prompt)
("\C-c\C-z" 'dime-nop))
(dime-define-keys dime-inspector-mode-map
((kbd "M-RET") 'dime-repl-inspector-copy-down-to-repl))
(dime-define-keys dime-debug-mode-map
("\C-y" 'dime-repl-insert-debug-frame-call-to-repl))
(define-dime-selector-method ?r
"Dime Read-Eval-Print-Loop."
(dime-repl-output-buffer))
(define-minor-mode dime-repl-map-mode
"Minor mode which makes dime-repl-mode-map available.
\\{dime-repl-mode-map}"
nil
nil
dime-repl-mode-map)
(defun dime-repl-mode ()
"Major mode for interacting with a superior Dylan.
\\{dime-repl-mode-map}"
(interactive)
(kill-all-local-variables)
(setq major-mode 'dime-repl-mode)
(dime-editing-mode 1)
(dime-repl-map-mode 1)
(lisp-mode-variables t)
(setq font-lock-defaults nil)
(setq mode-name "REPL")
(setq dime-current-thread :repl-thread)
(setq dime-write-string-function 'dime-repl-write-string)
(set (make-local-variable 'scroll-conservatively) 20)
(set (make-local-variable 'scroll-margin) 0)
(when dime-repl-history-file
(dime-repl-safe-load-history)
(add-hook 'kill-buffer-hook
'dime-repl-safe-save-merged-history nil t))
(add-hook 'kill-emacs-hook 'dime-repl-save-all-histories)
(dime-setup-command-hooks)
;; At the REPL, we define beginning-of-defun and end-of-defun to be
;; the start of the previous prompt or next prompt respectively.
;; Notice the interplay with DIME-REPL-BEGINNING-OF-DEFUN.
(set (make-local-variable 'beginning-of-defun-function)
'dime-repl-mode-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function)
'dime-repl-mode-end-of-defun)
(run-mode-hooks 'dime-repl-mode-hook))
(defun dime-repl-buffer (&optional create connection)
"Get the REPL buffer for the current connection; optionally create."
(funcall (if create #'get-buffer-create #'get-buffer)
(format "*dime-repl %s*" (dime-connection-name connection))))
(defun dime-repl ()
(interactive)
(dime-repl-switch-to-output-buffer))
(defun dime-repl-mode-beginning-of-defun (&optional arg)
(if (and arg (< arg 0))
(dime-repl-mode-end-of-defun (- arg))
(dotimes (_i (or arg 1))
(dime-repl-previous-prompt))))
(defun dime-repl-mode-end-of-defun (&optional arg)
(if (and arg (< arg 0))
(dime-repl-mode-beginning-of-defun (- arg))
(dotimes (_i (or arg 1))
(dime-repl-next-prompt))))
(defun dime-repl-send-string (string &optional _command-string)
(if dime-repl-read-mode (dime-repl-return-string string)
(dime-repl-eval-string string)))
(defun dime-repl-eval-string (string)
(dime-rex ()
((list 'swank:listener-eval string) (dime-current-project))
((:ok result)
(dime-repl-insert-result result))
((:abort condition)
(dime-repl-show-abort condition))))
(defun dime-repl-insert-result (result)
(with-current-buffer (dime-repl-output-buffer)
(save-excursion
(when result
(dime--destructuring-case result
((:values &rest strings)
(cond ((null strings)
(dime-repl-emit-result "; No value\n" t))
(t
(dolist (s strings)
(dime-repl-emit-result s t)))))))
(dime-repl-insert-prompt))
(dime-repl-show-maximum-output)))
(defun dime-repl-show-abort (condition)
(with-current-buffer (dime-repl-output-buffer)
(save-excursion
(dime-repl-save-marker dime-repl-output-start
(dime-repl-save-marker dime-repl-output-end
(goto-char dime-repl-output-end)
(insert-before-markers
(format "; Evaluation aborted on %s.\n" condition))
(dime-repl-insert-prompt))))
(dime-repl-show-maximum-output)))
(defun dime-repl-insert-prompt ()
"Insert the prompt (before markers!).
Set point after the prompt.
Return the position of the prompt beginning."
(goto-char dime-repl-input-start-mark)
(dime-repl-save-marker dime-repl-output-start
(dime-repl-save-marker dime-repl-output-end
(unless (bolp) (insert-before-markers "\n"))
(let ((prompt-start (point))
(prompt (format "%s> " (dime-dylan-project-prompt-string))))
(dime-propertize-region
'(face dime-repl-prompt-face read-only t intangible t
dime-repl-prompt t
rear-nonsticky
(dime-repl-prompt read-only face intangible))
(insert-before-markers prompt))
(set-marker dime-repl-prompt-start-mark prompt-start)
prompt-start))))
(defun dime-repl-show-maximum-output ()
"Put the end of the buffer at the bottom of the window."
(when (eobp)
(let ((win (if (eq (window-buffer) (current-buffer))
(selected-window)
(get-buffer-window (current-buffer) t))))
(when win
(with-selected-window win
(set-window-point win (point-max))
(recenter -1))))))
(defvar dime-repl-current-input-hooks)
(defun dime-repl-current-input (&optional until-point-p)
"Return the current input as string.
The input is the region from after the last prompt to the end of
buffer."
(or (run-hook-with-args-until-success 'dime-repl-current-input-hooks
until-point-p)
(buffer-substring-no-properties dime-repl-input-start-mark
(if until-point-p
(point)
(point-max)))))
(defun dime-repl-property-position (text-property &optional object)
"Return the first position of TEXT-PROPERTY, or nil."
(if (get-text-property 0 text-property object)
0
(next-single-property-change 0 text-property object)))
(defun dime-repl-mark-input-start ()
(set-marker dime-repl-input-start-mark (point) (current-buffer)))
(defun dime-repl-mark-output-start ()
(set-marker dime-repl-output-start (point))
(set-marker dime-repl-output-end (point)))
(defun dime-repl-mark-output-end ()
;; Don't put dime-repl-output-face again; it would remove the
;; special presentation face, for instance in the SBCL inspector.
(add-text-properties dime-repl-output-start dime-repl-output-end
'(;;face dime-repl-output-face
rear-nonsticky (face))))
(defun dime-repl-bol ()
"Go to the beginning of line or the prompt."
(interactive)
(cond ((and (>= (point) dime-repl-input-start-mark)
(dime-same-line-p (point) dime-repl-input-start-mark))
(goto-char dime-repl-input-start-mark))
(t (beginning-of-line 1))))
(defun dime-repl-in-input-area-p ()
(<= dime-repl-input-start-mark (point)))
(defun dime-repl-at-prompt-start-p ()
;; This will not work on non-current prompts.
(= (point) dime-repl-input-start-mark))
(defun dime-repl-beginning-of-defun ()
"Move to beginning of defun."
(interactive)
;; We call BEGINNING-OF-DEFUN if we're at the start of a prompt
;; already, to trigger DIME-REPL-MODE-BEGINNING-OF-DEFUN by means
;; of the locally bound BEGINNING-OF-DEFUN-FUNCTION, in order to
;; jump to the start of the previous prompt.
(if (and (not (dime-repl-at-prompt-start-p))
(dime-repl-in-input-area-p))
(goto-char dime-repl-input-start-mark)
(beginning-of-defun))
t)
;; FIXME: this looks very strange
(defun dime-repl-end-of-defun ()
"Move to next of defun."
(interactive)
;; C.f. DIME-REPL-BEGINNING-OF-DEFUN.
(if (and (not (= (point) (point-max)))
(dime-repl-in-input-area-p))
(goto-char (point-max))
(end-of-defun))
t)
(defun dime-repl-previous-prompt ()
"Move backward to the previous prompt."
(interactive)
(dime-repl-find-prompt t))
(defun dime-repl-next-prompt ()
"Move forward to the next prompt."
(interactive)
(dime-repl-find-prompt))
(defun dime-repl-find-prompt (&optional backward)
(let ((origin (point))
(prop 'dime-repl-prompt))
(while (progn
(dime-repl-search-property-change prop backward)
(not (or (dime-repl-end-of-proprange-p prop) (bobp) (eobp)))))
(unless (dime-repl-end-of-proprange-p prop)
(goto-char origin))))
(defun dime-repl-search-property-change (prop &optional backward)
(cond (backward
(goto-char (or (previous-single-char-property-change (point) prop)
(point-min))))
(t
(goto-char (or (next-single-char-property-change (point) prop)
(point-max))))))
(defun dime-repl-end-of-proprange-p (property)
(and (get-char-property (max 1 (1- (point))) property)
(not (get-char-property (point) property))))
(defvar dime-repl-return-hooks)
(defun dime-repl-return (&optional end-of-input)
"Evaluate the current input string, or insert a newline.
Send the current input only if a whole expression has been entered,
i.e. the parenthesis are matched.
With prefix argument send the input even if the parenthesis are not
balanced."
(interactive "P")
(dime-check-connected)
(cond (end-of-input
(dime-repl-send-input))
(dime-repl-read-mode ; bad style?
(dime-repl-send-input t))
((and (get-text-property (point) 'dime-repl-old-input)
(< (point) dime-repl-input-start-mark))
(dime-repl-grab-old-input end-of-input)
(dime-repl-recenter-if-needed))
((run-hook-with-args-until-success 'dime-repl-return-hooks))
((dime-input-complete-p dime-repl-input-start-mark (point-max))
(dime-repl-send-input t))
(t
(dime-repl-newline-and-indent)
(message "[input not complete]"))))
(defun dime-repl-recenter-if-needed ()
"Make sure that (point) is visible."
(unless (pos-visible-in-window-p (point-max))
(save-excursion
(goto-char (point-max))
(recenter -1))))
(defun dime-repl-send-input (&optional newline)
"Goto to the end of the input and send the current input.
If NEWLINE is true then add a newline at the end of the input."
(unless (dime-repl-in-input-area-p)
(error "No input at point."))
(goto-char (point-max))
(let ((end (point))) ; end of input, without the newline
(dime-repl-add-to-input-history
(buffer-substring dime-repl-input-start-mark end))
(when newline
(insert "\n")
(dime-repl-show-maximum-output))
(let ((inhibit-modification-hooks t))
(add-text-properties dime-repl-input-start-mark
(point)
`(dime-repl-old-input
,(cl-incf dime-repl-old-input-counter))))
(let ((overlay (make-overlay dime-repl-input-start-mark end)))
;; These properties are on an overlay so that they won't be taken
;; by kill/yank.
(overlay-put overlay 'read-only t)
(overlay-put overlay 'face 'dime-repl-input-face)))
(let ((input (dime-repl-current-input)))
(goto-char (point-max))
(dime-repl-mark-input-start)
(dime-repl-mark-output-start)
(dime-repl-send-string input)))
(defun dime-repl-grab-old-input (replace)
"Resend the old REPL input at point.
If replace is non-nil the current input is replaced with the old
input; otherwise the new input is appended. The old input has the
text property `dime-repl-old-input'."
(cl-multiple-value-bind (beg end)
(dime-property-bounds 'dime-repl-old-input)
(let ((old-input (buffer-substring beg end)) ;;preserve
;;properties, they will be removed later
(offset (- (point) beg)))
;; Append the old input or replace the current input
(cond (replace (goto-char dime-repl-input-start-mark))
(t (goto-char (point-max))
(unless (eq (char-before) ?\ )
(insert " "))))
(delete-region (point) (point-max))
(save-excursion
(insert old-input)
(when (equal (char-before) ?\n)
(delete-char -1)))
(forward-char offset))))
(defun dime-repl-closing-return ()
"Evaluate the current input string after closing all open lists."
(interactive)
(goto-char (point-max))
(save-restriction
(narrow-to-region dime-repl-input-start-mark (point))
(while (ignore-errors (save-excursion (backward-up-list 1)) t)
(insert ")")))
(dime-repl-return))
(defun dime-repl-newline-and-indent ()
"Insert a newline, then indent the next line.
Restrict the buffer from the prompt for indentation, to avoid being
confused by strange characters (like unmatched quotes) appearing
earlier in the buffer."
(interactive)
(save-restriction
(narrow-to-region dime-repl-prompt-start-mark (point-max))
(insert "\n")
(dylan-indent-line)))
(defun dime-repl-delete-current-input ()
"Delete all text from the prompt."
(interactive)
(delete-region dime-repl-input-start-mark (point-max)))
(defun dime-repl-kill-input ()
"Kill all text from the prompt to point."
(interactive)
(cond ((< (marker-position dime-repl-input-start-mark) (point))
(kill-region dime-repl-input-start-mark (point)))
((= (point) (marker-position dime-repl-input-start-mark))
(dime-repl-delete-current-input))))
(defun dime-repl-replace-input (string)
(dime-repl-delete-current-input)
(insert-and-inherit string))
(defun dime-repl-input-line-beginning-position ()
(save-excursion
(goto-char dime-repl-input-start-mark)
(line-beginning-position)))
(defvar dime-repl-clear-buffer-hook)
(defun dime-repl-clear-buffer ()
"Delete the output generated by the Dylan process."
(interactive)
(let ((inhibit-read-only t))
(delete-region (point-min) dime-repl-prompt-start-mark)
(delete-region dime-repl-output-start dime-repl-output-end)
(when (< (point) dime-repl-input-start-mark)
(goto-char dime-repl-input-start-mark))
(recenter t))
(run-hooks 'dime-repl-clear-buffer-hook))
(defun dime-repl-clear-output ()
"Delete the output inserted since the last input."
(interactive)
(let ((start (save-excursion
(dime-repl-previous-prompt)
(ignore-errors (forward-sexp))
(forward-line)
(point)))
(end (1- (dime-repl-input-line-beginning-position))))
(when (< start end)
(let ((inhibit-read-only t))
(delete-region start end)
(save-excursion
(goto-char start)
(insert ";;; output flushed"))))))
(defun dime-repl-set-project (project)
"Set the project of the REPL buffer to PROJECT."
(interactive (list (let* ((p (dime-current-project)))
(dime-read-project-name "Project: " p))))
(with-current-buffer (dime-repl-output-buffer)
(let ((previous-point (- (point) dime-repl-input-start-mark)))
(cl-destructuring-bind (name prompt-string)
(dime-repl-shortcut-eval `(swank:set-package ,project))
(setf (dime-dylan-project-prompt-string) prompt-string)
(setf dime-buffer-project name)
(dime-repl-insert-prompt)
(when (cl-plusp previous-point)
(goto-char (+ previous-point dime-repl-input-start-mark)))))))
;;;;; History
(defcustom dime-repl-wrap-history nil
"*T to wrap history around when the end is reached."
:type 'boolean
:group 'dime-repl)
(defcustom dime-repl-history-remove-duplicates nil
"*When T all duplicates are removed except the last one."
:type 'boolean
:group 'dime-repl)
(defcustom dime-repl-history-trim-whitespaces nil
"*When T strip all whitespaces from the beginning and end."
:type 'boolean
:group 'dime-repl)
(defvar-local dime-repl-input-history '()
"History list of strings read from the REPL buffer.")
;; TODO: Use the standard `string-trim' function from `subr-x'?
(defun dime-repl-string-trim (character-bag string)
(cl-flet ((find-bound
(&optional from-end)
(cl-position-if-not (lambda (char) (memq char character-bag))
string :from-end from-end)))
(let ((start (find-bound))
(end (find-bound t)))
(if start
(cl-subseq string start (1+ end))
""))))
(defun dime-repl-add-to-input-history (string)
"Add STRING to the input history.
Empty strings and duplicates are ignored."
(when dime-repl-history-trim-whitespaces
(setq string (dime-repl-string-trim '(?\n ?\ ?\t) string)))
(unless (equal string "")
(when dime-repl-history-remove-duplicates
(setq dime-repl-input-history
(remove string dime-repl-input-history)))
(unless (equal string (car dime-repl-input-history))
(push string dime-repl-input-history))))
;; These two vars contain the state of the last history search. We
;; only use them if `last-command' was 'dime-repl-history-replace,
;; otherwise we reinitialize them.
(defvar dime-repl-input-history-position -1
"Newer items have smaller indices.")
(defvar dime-repl-history-pattern nil
"The regexp most recently used for finding input history.")
(defun dime-repl-history-replace (direction &optional regexp)
"Replace the current input with the next line in DIRECTION.
DIRECTION is 'forward' or 'backward' (in the history list).
If REGEXP is non-nil, only lines matching REGEXP are considered."
(setq dime-repl-history-pattern regexp)
(let* ((min-pos -1)
(max-pos (length dime-repl-input-history))
(pos0 (cond ((dime-repl-history-search-in-progress-p)
dime-repl-input-history-position)
(t min-pos)))
(pos (dime-repl-position-in-history pos0 direction (or regexp "")
(dime-repl-current-input)))
(msg nil))
(cond ((and (< min-pos pos) (< pos max-pos))
(dime-repl-replace-input (nth pos dime-repl-input-history))
(setq msg (format "History item: %d" pos)))
((not dime-repl-wrap-history)
(setq msg (cond ((= pos min-pos) "End of history")
((= pos max-pos) "Beginning of history"))))
(dime-repl-wrap-history
(setq pos (if (= pos min-pos) max-pos min-pos))
(setq msg "Wrapped history")))
(when (or (<= pos min-pos) (<= max-pos pos))
(when regexp
(setq msg (concat msg "; no matching item"))))
;;(message "%s [%d %d %s]" msg start-pos pos regexp)
(message "%s%s" msg (cond ((not regexp) "")
(t (format "; current regexp: %s" regexp))))
(setq dime-repl-input-history-position pos)
(setq this-command 'dime-repl-history-replace)))
(defun dime-repl-history-search-in-progress-p ()
(eq last-command 'dime-repl-history-replace))
(defun dime-repl-terminate-history-search ()
(setq last-command this-command))
(defun dime-repl-position-in-history (start-pos direction regexp
&optional exclude-string)
"Return the position of the history item matching REGEXP.
Return -1 resp. the length of the history if no item matches.
If EXCLUDE-STRING is specified then it's excluded from the search."
;; Loop through the history list looking for a matching line
(let* ((step (cl-ecase direction
(forward -1)
(backward 1)))
(history dime-repl-input-history)
(len (length history)))
(cl-loop for pos = (+ start-pos step) then (+ pos step)
if (< pos 0) return -1
if (<= len pos) return len
for history-item = (nth pos history)
if (and (string-match regexp history-item)
(not (equal history-item exclude-string)))
return pos)))
(defun dime-repl-previous-input ()
"Cycle backwards through input history.
If the `last-command' was a history navigation command use the
same search pattern for this command.
Otherwise use the current input as search pattern."
(interactive)
(dime-repl-history-replace 'backward (dime-repl-history-pattern t)))
(defun dime-repl-next-input ()
"Cycle forwards through input history.
See `dime-repl-previous-input'."
(interactive)
(dime-repl-history-replace 'forward (dime-repl-history-pattern t)))
(defun dime-repl-forward-input ()
"Cycle forwards through input history."
(interactive)
(dime-repl-history-replace 'forward (dime-repl-history-pattern)))
(defun dime-repl-backward-input ()
"Cycle backwards through input history."
(interactive)
(dime-repl-history-replace 'backward (dime-repl-history-pattern)))
(defun dime-repl-previous-matching-input (regexp)
(interactive (list (dime-read-from-minibuffer
"Previous element matching (regexp): ")))
(dime-repl-terminate-history-search)
(dime-repl-history-replace 'backward regexp))
(defun dime-repl-next-matching-input (regexp)
(interactive (list (dime-read-from-minibuffer
"Next element matching (regexp): ")))
(dime-repl-terminate-history-search)
(dime-repl-history-replace 'forward regexp))
(defun dime-repl-history-pattern (&optional use-current-input)
"Return the regexp for the navigation commands."
(cond ((dime-repl-history-search-in-progress-p)
dime-repl-history-pattern)
(use-current-input
(cl-assert (<= dime-repl-input-start-mark (point)))
(let ((str (dime-repl-current-input t)))
(cond ((string-match "^[ \t\n]*$" str) nil)
(t (concat "^" (regexp-quote str))))))
(t nil)))
(defun dime-repl-delete-from-input-history (string)
"Delete STRING from the repl input history.
When string is not provided then clear the current repl input and
use it as an input. This is useful to get rid of unwanted repl
history entries while navigating the repl history."
(interactive (list (dime-repl-current-input)))
(let ((merged-history
(dime-repl-merge-histories dime-repl-input-history
(dime-repl-read-history nil t))))
(setq dime-repl-input-history
(cl-delete string merged-history :test #'string=))
(dime-repl-save-history))
(dime-repl-delete-current-input))
;;;;; Persistent History
(defun dime-repl-merge-histories (old-hist new-hist)
"Merge entries from OLD-HIST and NEW-HIST."
;; Newer items in each list are at the beginning.
(let* ((ht (make-hash-table :test #'equal))
(test (lambda (entry)
(or (gethash entry ht)
(progn (setf (gethash entry ht) t)
nil)))))
(append (cl-remove-if test new-hist)
(cl-remove-if test old-hist))))
(defun dime-repl-load-history (&optional filename)
"Set the current Dime REPL history.
It can be read either from FILENAME or `dime-repl-history-file' or
from a user defined filename."
(interactive (list (dime-repl-read-history-filename)))
(let ((file (or filename dime-repl-history-file)))
(setq dime-repl-input-history (dime-repl-read-history file t))))
(defun dime-repl-read-history (&optional filename noerror)
"Read and return the history from FILENAME.
The default value for FILENAME is `dime-repl-history-file'.
If NOERROR is non-nil and the file is not readable return nil."
(let ((filename (or filename dime-repl-history-file)))
(unless (and noerror (not (file-readable-p filename)))
(with-temp-buffer
(insert-file-contents filename)
(read (current-buffer))))))
(defun dime-repl-read-history-filename ()
(read-file-name "Use Dime REPL history from file: "
dime-repl-history-file))
(defun dime-repl-save-merged-history (&optional filename)