forked from dylan-lang/dylan-emacs-support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dime.el
7289 lines (6331 loc) · 275 KB
/
dime.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.el --- Dylan interaction mode -*- lexical-binding: t -*-
;; Lineage: SLIME, Open Dylan
;; Copyright (C) 2003 Eric Marsden, Luke Gorrie, Helmut Eller (SLIME)
;; Copyright (C) 2004, 2005, 2006 Luke Gorrie, Helmut Eller (SLIME)
;; Copyright (C) 2007, 2008, 2009 Helmut Eller, Tobias C. Rittweiler (SLIME)
;; Copyright (C) 2011, 2012, 2013, 2014 Hannes Mehnert
;; Copyright (C) 2013 Erik Charlebois
;; Copyright (C) 2014 Bruce Mitchener
;; Copyright (C) 2021 Lassi Kortela
;; SPDX-License-Identifier: GPL-2.0-or-later
;; URL: https://opendylan.org/
;; Package-Version: 3.0
;; Package-Requires: ((emacs "25.1") (dylan "3.0"))
;;; Commentary:
;; This file contains extensions for programming in Open Dylan.
;; The main features are:
;; * A socket-based communication/RPC interface between Emacs and
;; Dylan, enabling introspection and remote development.
;; * The `dime-mode' minor-mode complementing `dylan-mode'. This new
;; mode includes many commands for interacting with the Open Dylan
;; process.
;; * A Dylan debugger written in Emacs Lisp. The debugger pops up
;; an Emacs buffer similar to the Emacs/Elisp debugger.
;; * A Open Dylan inspector to interactively look at run-time data.
;; * Trapping compiler messages and creating annotations in the source
;; file on the appropriate forms.
;; In order to run Dime, a supporting Dylan server called Swank is
;; required.
;;; Code:
;;;; Dependencies and setup
(require 'apropos)
(require 'arc-mode)
(require 'cl-lib)
(require 'comint)
(require 'compile)
(require 'easymenu)
(require 'etags)
(require 'font-lock)
(require 'gud)
(require 'hideshow)
(require 'outline)
(require 'pp)
(require 'subr-x)
(require 'thingatpt)
(require 'timer)
(require 'dylan)
(defvar dime-buffer-project nil)
(defvar dime-buffer-connection nil)
(defvar dime-dispatching-connection nil
"Network process currently executing.
This is dynamically bound while handling messages from Dylan; it
overrides `dime-buffer-connection' and `dime-default-connection'.")
(defvar-local dime-current-thread t
"The id of the current thread on the Dylan side.
t means the \"current\" thread;
:repl-thread the thread that executes REPL requests;
fixnum a specific thread.")
(defvar dime-buffer-project nil
"The Dylan project associated with the current buffer.
This is set only in buffers bound to specific projects.")
(eval-and-compile
(defvar dime-path
(let ((path (or (locate-library "dime") load-file-name)))
(and path (file-name-directory path)))
"Directory containing the Dime package.
This is used to load the supporting Open Dylan library, Swank.
The default value is automatically computed from the location of the
Emacs Lisp package."))
(defvar dime-dylan-modes '(dylan-mode))
(defvar dime-setup-contribs nil)
;;;###autoload
(defun dime-setup (&optional contribs)
"Setup Emacs so that `dylan-mode' buffers always use Dime.
CONTRIBS is a list of contrib packages to load."
(when (member 'dylan-mode dime-dylan-modes)
(add-hook 'dylan-mode-hook 'dime-dylan-mode-hook))
(setq dime-setup-contribs contribs)
(dime-setup-contribs))
(defun dime-setup-contribs ()
"Load and initialize contribs."
(dolist (c dime-setup-contribs)
(require c)
(let ((init (intern (format "%s-init" c))))
(when (fboundp init)
(funcall init)))))
(defun dime-dylan-mode-hook ()
"Hook function to enable Dime in `dylan-mode-hook'."
(dime-mode 1))
(eval-and-compile
(defun dime-changelog-date (&optional interactivep)
"Return the datestring of the latest entry in the ChangeLog file.
Return nil if the ChangeLog file cannot be found."
(interactive "p")
(let ((changelog (concat dime-path "ChangeLog"))
(date nil))
(when (file-exists-p changelog)
(with-temp-buffer
(insert-file-contents-literally changelog nil 0 100)
(goto-char (point-min))
(setq date (symbol-name (read (current-buffer))))))
(when interactivep
(message "Dime ChangeLog dates %s." date))
date)))
(defvar dime-protocol-version "2011-02-13")
;;;; Customize groups
;;
;;;;; dime
(defgroup dime nil
"Interaction with the Dylan environment."
:prefix "dime-"
:group 'applications)
;;;;; dime-ui
(defgroup dime-ui nil
"Interaction with the Dylan environment."
:prefix "dime-"
:group 'dime)
(defcustom dime-truncate-lines t
"Set `truncate-lines' in popup buffers.
This applies to buffers that present lines as rows of data, such as
debugger backtraces and apropos listings."
:type 'boolean
:group 'dime-ui)
(defcustom dime-kill-without-query-p nil
"If non-nil, kill Dime processes without query when quitting Emacs.
This applies to the *inferior-dylan* buffer and the network connections."
:type 'boolean
:group 'dime-ui)
;;;;; dime-dylan
(defgroup dime-dylan nil
"Dylan server configuration."
:prefix "dime-"
:group 'dime)
(defcustom dime-backend "swank-loader.dylan"
"The name of the Dylan file that loads the Swank server.
This name is interpreted relative to the directory containing
dime.el, but could also be set to an absolute filename."
:type 'string
:group 'dime-dylan)
(defcustom dime-connected-hook nil
"List of functions to call when Dime connects to Dylan."
:type 'hook
:group 'dime-dylan)
(defcustom dime-enable-evaluate-in-emacs nil
"*If non-nil, the inferior Dylan can evaluate arbitrary forms in Emacs.
The default is nil, as this feature can be a security risk."
:type '(boolean)
:group 'dime-dylan)
(defcustom dime-dylan-host "127.0.0.1"
"The default hostname (or IP address) to connect to."
:type 'string
:group 'dime-dylan)
(defcustom dime-port 4005
"Port to use as the default for `dime-connect'."
:type 'integer
:group 'dime-dylan)
(defvar dime-connect-host-history (list dime-dylan-host))
(defvar dime-connect-port-history (list (prin1-to-string dime-port)))
(defvar dime-net-valid-coding-systems
'((iso-latin-1-unix nil "iso-latin-1-unix")
(iso-8859-1-unix nil "iso-latin-1-unix")
(binary nil "iso-latin-1-unix")
(utf-8-unix t "utf-8-unix")
(emacs-mule-unix t "emacs-mule-unix")
(euc-jp-unix t "euc-jp-unix"))
"A list of valid coding systems.
Each element is of the form: (NAME MULTIBYTEP CL-NAME)")
(defun dime-find-coding-system (name)
"Return the coding system for the symbol NAME.
The result is either an element in `dime-net-valid-coding-systems'
of nil."
(let ((probe (assq name dime-net-valid-coding-systems)))
(when (and probe
(ignore-errors (check-coding-system (car probe))))
probe)))
(defcustom dime-net-coding-system
(car (cl-find-if 'dime-find-coding-system
dime-net-valid-coding-systems :key 'car))
"Coding system used for network connections.
See also `dime-net-valid-coding-systems'."
:type (cons 'choice
(mapcar (lambda (x)
(list 'const (car x)))
dime-net-valid-coding-systems))
:group 'dime-dylan)
;;;;; dime-mode
(defgroup dime-mode nil
"Settings for dime-mode Dylan source buffers."
:prefix "dime-"
:group 'dime)
(defcustom dime-find-definitions-function 'dime-find-definitions-rpc
"Function to find definitions for a name.
The function is called with the definition name, a string, as its
argument."
:type 'function
:group 'dime-mode
:options '(dime-find-definitions-rpc
dime-etags-definitions
(lambda (name)
(append (dime-find-definitions-rpc name)
(dime-etags-definitions name)))
(lambda (name)
(or (dime-find-definitions-rpc name)
(and tags-table-list
(dime-etags-definitions name))))))
(defcustom dime-complete-symbol-function 'dime-simple-complete-symbol
"*Function to perform symbol completion."
:group 'dime-mode
:type '(choice (const :tag "Simple" dime-simple-complete-symbol)
(const :tag "Compound" dime-complete-symbol*)
(const :tag "Fuzzy" dime-fuzzy-complete-symbol)))
;;;;; dime-mode-faces
(defgroup dime-mode-faces nil
"Faces in dime-mode source code buffers."
:prefix "dime-"
:group 'dime-mode)
(defface dime-error-face
`((((class color) (background light))
(:underline "red"))
(((class color) (background dark))
(:underline "red"))
(t (:underline t)))
"Face for errors from the compiler."
:group 'dime-mode-faces)
(defface dime-warning-face
`((((class color) (background light))
(:underline "orange"))
(((class color) (background dark))
(:underline "coral"))
(t (:underline t)))
"Face for warnings from the compiler."
:group 'dime-mode-faces)
(defface dime-style-warning-face
`((((class color) (background light))
(:underline "brown"))
(((class color) (background dark))
(:underline "gold"))
(t (:underline t)))
"Face for style-warnings from the compiler."
:group 'dime-mode-faces)
(defface dime-note-face
`((((class color) (background light))
(:underline "brown4"))
(((class color) (background dark))
(:underline "light goldenrod"))
(t (:underline t)))
"Face for notes from the compiler."
:group 'dime-mode-faces)
(defun dime-face-inheritance-possible-p ()
"Return non-nil if the :inherit face attribute is supported."
(assq :inherit custom-face-attributes))
(defface dime-highlight-face
(if (dime-face-inheritance-possible-p)
'((t (:inherit highlight :underline nil)))
'((((class color) (background light))
(:background "darkseagreen2"))
(((class color) (background dark))
(:background "darkolivegreen"))
(t (:inverse-video t))))
"Face for compiler notes while selected."
:group 'dime-mode-faces)
;;;;; dime-debug
(defgroup dime-debug nil
"Backtrace options and fontification."
:prefix "dime-debug-"
:group 'dime)
(defmacro define-dime-debug-faces (&rest faces)
"Define the set of dime-debug faces.
Each specifiation in FACES is (NAME DESCRIPTION &optional PROPERTIES).
NAME is a symbol; the face will be called dime-debug-NAME-face.
DESCRIPTION is a one-liner for the customization buffer.
PROPERTIES specifies any default face properties."
`(progn ,@(cl-loop for face in faces
collect `(define-dime-debug-face ,@face))))
(defmacro define-dime-debug-face (name description &optional default)
"Helper to define Dime debug face.
NAME, DESCRIPTION, DEFAULT are as for `defface'."
(let ((facename (intern (format "dime-debug-%s-face" (symbol-name name)))))
`(defface ,facename
(list (list t ,default))
,(format "Face for %s." description)
:group 'dime-debug)))
(define-dime-debug-faces
(topline "the top line describing the error")
(condition "the condition class")
(section "the labels of major sections in the debug buffer")
(frame-label "backtrace frame numbers")
(restart-type "restart names."
(if (dime-face-inheritance-possible-p)
'(:inherit font-lock-keyword-face)))
(restart "restart descriptions")
(restart-number "restart numbers (correspond to keystrokes to invoke)"
'(:bold t))
(frame-line "function names and arguments in the backtrace")
(restartable-frame-line
"frames which are surely restartable"
'(:foreground "lime green"))
(non-restartable-frame-line
"frames which are surely not restartable")
(detailed-frame-line
"function names and arguments in a detailed (expanded) frame")
(local-name "local variable names")
(local-value "local variable values")
(catch-tag "catch tags"))
;;;; Minor modes
;;;;; dime-mode
(defvar dime-mode-indirect-map (make-sparse-keymap)
`"Empty keymap which has `dime-mode-map' as its parent.
This is a hack so that we can reinitilize the real `dime-mode-map'
more easily. See `dime-init-keymaps'.")
(define-minor-mode dime-mode
"\\<dime-mode-map>\
Dime: Dylan interaction mode for Emacs (minor-mode).
Commands to compile the current buffer's source file and visually
highlight any resulting compiler notes and warnings:
\\[dime-compile-and-load-file] - Compile and load the current buffer's file.
\\[dime-compile-file] - Compile (but not load) the current buffer's file.
\\[dime-compile-defun] - Compile the top-level form at point.
Commands for visiting compiler notes:
\\[dime-next-note] - Goto the next form with a compiler note.
\\[dime-previous-note] - Goto the previous form with a compiler note.
\\[dime-remove-notes] - Remove compiler-note annotations in buffer.
Finding definitions:
\\[dime-edit-definition] - Edit the definition of the function called at point.
\\[dime-pop-find-definition-stack] - Pop the definition stack to go back from a definition.
Documentation commands:
\\[dime-describe-symbol] - Describe symbol.
\\[dime-apropos] - Apropos search.
\\[dime-disassemble-symbol] - Disassemble a function.
Evaluation commands:
\\[dime-eval-defun] - Evaluate top-level from containing point.
\\[dime-eval-last-expression] - Evaluate sexp before point.
\\[dime-pprint-eval-last-expression] - Evaluate sexp before point, pretty-print result.
Full set of commands:
\\{dime-mode-map}"
nil
nil
dime-mode-indirect-map
(dime-setup-command-hooks))
;;;;;; Modeline
(add-to-list 'minor-mode-alist
`(dime-mode '(:eval (dime-modeline-string))))
(defun dime-modeline-string ()
"Return the string to display in the modeline.
\"Dime\" only appears if we aren't connected. If connected,
include project-name, connection-name, and possibly some state
information."
(let ((conn (dime-current-connection)))
;; Bail out early in case there's no connection, so we won't
;; implicitly invoke `dime-connection' which may query the user.
(if (not conn)
(and dime-mode " Dime")
(let ((local (eq conn dime-buffer-connection))
(pkg (dime-current-project)))
(concat " "
(if local "{" "[")
(if pkg pkg "?")
" "
;; ignore errors for closed connections
(ignore-errors (dime-connection-name conn))
(dime-modeline-state-string conn)
(if local "}" "]"))))))
(defun dime-modeline-state-string (conn)
"Return a string possibly describing CONN's state."
(cond ((not (eq (process-status conn) 'open))
(format " %s" (process-status conn)))
((let ((pending (length (dime-rex-continuations conn)))
(dime-debugs (length (dime-debug-buffers conn))))
(cond ((and (zerop dime-debugs) (zerop pending)) nil)
((zerop dime-debugs) (format " %s" pending))
(t (format " %s/%s" pending dime-debugs)))))))
;;;;; Key bindings
(defvar dime-parent-map nil
"Parent keymap for shared between all Dime related modes.")
(defvar dime-parent-bindings
'(("\M-." dime-edit-definition)
("\M-," dime-pop-find-definition-stack)
("\M-_" dime-edit-uses) ; for German layout
("\M-?" dime-edit-uses) ; for USian layout
("\C-x4." dime-edit-definition-other-window)
("\C-x5." dime-edit-definition-other-frame)
("\C-x\C-e" dime-eval-last-expression)
("\C-\M-x" dime-eval-defun)
;; Include PREFIX keys...
("\C-c" dime-prefix-map)))
(defvar dime-prefix-map nil
"Keymap for commands prefixed with `dime-prefix-key'.")
(defvar dime-prefix-bindings
'(("\C-r" dime-eval-region)
(":" dime-interactive-eval)
("\C-e" dime-interactive-eval)
("E" dime-edit-value)
("\C-l" dime-load-file)
("\C-b" dime-interrupt)
("\M-d" dime-disassemble-symbol)
("\C-t" dime-toggle-trace-fdefinition)
("I" dime-inspect)
("\C-xt" dime-list-threads)
("\C-xn" dime-cycle-connections)
("\C-xc" dime-list-connections)
("<" dime-list-callers)
(">" dime-list-callees)
;; Include DOC keys...
("\C-d" dime-doc-map)
;; Include XREF WHO-FOO keys...
("\C-w" dime-who-map)))
(defvar dime-editing-map nil
"Dime keys for buffers where the user can edit S-expressions.
E.g. source buffers and the REPL.")
(defvar dime-editing-keys
`(;; Arglist display & completion
("\M-\t" dime-complete-symbol)
(" " dime-space)
;; Evaluating
;;("\C-x\M-e" dime-eval-last-expression-display-output :inferior t)
("\C-c\C-p" dime-pprint-eval-last-expression)
;; Macroexpand
("\C-c\C-m" dime-expand-1)
("\C-c\M-m" dime-macroexpand-all)
;; Misc
("\C-c\C-u" dime-undefine-function)
(,(kbd "C-M-.") dime-next-location)
(,(kbd "C-M-,") dime-previous-location)
;; Obsolete, redundant bindings
("\C-c\C-i" dime-complete-symbol)
;;("\M-*" pop-tag-mark) ; almost to clever
))
(defvar dime-mode-map nil
"Dime mode keymap.")
(defvar dime-keys
'( ;; Compiler notes
("\M-p" dime-previous-note)
("\M-n" dime-next-note)
("\C-c\M-c" dime-remove-notes)
("\C-c\C-k" dime-compile-and-load-file)
("\C-c\M-k" dime-compile-file)
("\C-c\C-c" dime-compile-defun)))
(defun dime-nop ()
"The null command. Used to shadow currently-unused keybindings."
(interactive)
(call-interactively 'undefined))
(defvar dime-doc-map nil
"Keymap for documentation commands. Bound to a prefix key.")
(defvar dime-doc-bindings
'((?a dime-apropos)
(?z dime-apropos-all)
(?p dime-apropos-project)
(?d dime-describe-symbol)
(?f dime-describe-function)))
(defvar dime-who-map nil
"Keymap for who-xref commands. Bound to a prefix key.")
(defvar dime-who-bindings
'((?c dime-who-calls)
(?w dime-calls-who)
(?r dime-who-references)
(?b dime-who-binds)
(?s dime-who-sets)
(?m dime-who-macroexpands)
(?a dime-who-specializes)))
(defun dime-init-keymaps ()
"(Re)initialize the keymaps for Dime mode."
(interactive)
(dime-init-keymap 'dime-doc-map t t dime-doc-bindings)
(dime-init-keymap 'dime-who-map t t dime-who-bindings)
(dime-init-keymap 'dime-prefix-map t nil dime-prefix-bindings)
(dime-init-keymap 'dime-parent-map nil nil dime-parent-bindings)
(dime-init-keymap 'dime-editing-map nil nil dime-editing-keys)
(set-keymap-parent dime-editing-map dime-parent-map)
(dime-init-keymap 'dime-mode-map nil nil dime-keys)
(set-keymap-parent dime-mode-map dime-editing-map)
(set-keymap-parent dime-mode-indirect-map dime-mode-map))
(defun dime-init-keymap (keymap-name prefixp bothp bindings)
"Helper to initialize one of the Dime keymaps."
(set keymap-name (make-sparse-keymap))
(when prefixp (define-prefix-command keymap-name))
(dime-bind-keys (eval keymap-name) bothp bindings))
(defun dime-bind-keys (keymap bothp bindings)
"Add BINDINGS to KEYMAP.
If BOTHP is true also add bindings with control modifier."
(cl-loop for (key command) in bindings do
(cond (bothp
(define-key keymap `[,key] command)
(unless (equal key ?h) ; But don't bind C-h
(define-key keymap `[(control ,key)] command)))
(t (define-key keymap key command)))))
(dime-init-keymaps)
(define-minor-mode dime-editing-mode
"Minor mode which makes dime-editing-map available.
\\{dime-editing-map}"
nil
nil
dime-editing-map)
;;;; Setup initial `dime-mode' hooks
(defvar-local dime-pre-command-actions nil
"List of functions to execute before the next Emacs command.
This list of flushed between commands.")
(defun dime-pre-command-hook ()
"Execute all functions in `dime-pre-command-actions', then NIL it."
(dolist (undo-fn dime-pre-command-actions)
(funcall undo-fn))
(setq dime-pre-command-actions nil))
(defun dime-post-command-hook ()
"Install Dime `pre-command-hook'."
(when (null pre-command-hook) ; sometimes this is lost
(add-hook 'pre-command-hook 'dime-pre-command-hook)))
(defun dime-setup-command-hooks ()
"Setup a buffer-local `pre-command-hook' to call `dime-pre-command-hook'."
(add-hook 'pre-command-hook 'dime-pre-command-hook nil t)
(add-hook 'post-command-hook 'dime-post-command-hook nil t))
;;;; Framework'ey bits
;;;
;;; This section contains some standard Dime idioms: basic macros,
;;; ways of showing messages to the user, etc. All the code in this
;;; file should use these functions when applicable.
;;;
;;;;; Syntactic sugar
(defmacro dime--destructuring-case (value &rest patterns)
"Dispatch VALUE to one of PATTERNS.
A cross between `cl-case' and `cl-destructuring-bind'.
The pattern syntax is:
((HEAD . ARGS) . BODY)
The list of patterns is searched for a HEAD `eq' to the car of
VALUE. If one is found, the BODY is executed with ARGS bound to the
corresponding values in the CDR of VALUE."
(declare (indent 1))
(let ((operator (cl-gensym "op-"))
(operands (cl-gensym "rand-"))
(tmp (cl-gensym "tmp-")))
`(let* ((,tmp ,value)
(,operator (car ,tmp))
(,operands (cdr ,tmp)))
(cl-case ,operator
,@(mapcar (lambda (clause)
(if (eq (car clause) t)
`(t ,@(cdr clause))
(cl-destructuring-bind ((op &rest rands) &rest body) clause
`(,op (cl-destructuring-bind ,rands ,operands
. ,body)))))
patterns)
,@(if (eq (caar (last patterns)) t)
'()
`((t (error "ELISP dime--destructuring-case failed: %S" ,tmp))))))))
(defmacro dime-define-keys (keymap &rest key-command)
"Define keys in KEYMAP. Each KEY-COMMAND is a list of (KEY COMMAND)."
(declare (indent 1))
`(progn . ,(mapcar (lambda (k-c) `(define-key ,keymap . ,k-c))
key-command)))
(cl-defmacro dime--with-struct ((conc-name &rest slots) struct &body body)
"Like `with-slots' but works only for structs.
\(fn (CONC-NAME &rest SLOTS) STRUCT &body BODY)"
(declare (indent 2))
(cl-flet ((reader (slot) (intern (concat (symbol-name conc-name)
(symbol-name slot)))))
(let ((struct-var (cl-gensym "struct")))
`(let ((,struct-var ,struct))
(cl-symbol-macrolet
,(mapcar (lambda (slot)
(cl-etypecase slot
(symbol `(,slot (,(reader slot) ,struct-var)))
(cons `(,(cl-first slot)
(,(reader (cl-second slot))
,struct-var)))))
slots)
. ,body)))))
;;;;; Very-commonly-used functions
(defvar dime-message-function 'message)
;; Interface
(defun dime-buffer-name (type &optional hidden)
(cl-assert (keywordp type))
(concat (if hidden " " "")
(format "*dime-%s*" (substring (symbol-name type) 1))))
;; Interface
(defun dime-message (format &rest args)
"Like `message' but with special support for multi-line messages.
Single-line messages use the echo area."
(apply dime-message-function format args))
(defun dime-display-warning (message &rest args)
(display-warning '(dime warning) (apply #'format message args)))
(defvar dime-background-message-function 'dime-display-oneliner)
;; Interface
(defun dime-background-message (format-string &rest format-args)
"Display a message in passing.
This is like `dime-message', but less distracting because it
will never pop up a buffer or display multi-line messages.
It should be used for \"background\" messages such as argument lists."
(apply dime-background-message-function format-string format-args))
(defun dime-display-oneliner (format-string &rest format-args)
(let* ((msg (apply #'format format-string format-args)))
(unless (minibuffer-window-active-p (minibuffer-window))
(message "%s" (dime-oneliner msg)))))
(defun dime-oneliner (string)
"Return STRING truncated to fit in a single echo-area line."
(substring string 0 (min (length string)
(or (cl-position ?\n string) most-positive-fixnum)
(1- (frame-width)))))
;; Interface
(defun dime-set-truncate-lines ()
"Apply `dime-truncate-lines' to the current buffer."
(when dime-truncate-lines
(set (make-local-variable 'truncate-lines) t)))
;; Interface
(defun dime-read-project-name (prompt &optional initial-value)
"Read a project name from the minibuffer, prompting with PROMPT."
(let ((completion-ignore-case t))
(completing-read prompt (dime-bogus-completion-alist
(dime-eval
`(swank:list-all-package-names t)))
nil t initial-value)))
;; Interface
(defun dime-read-symbol-name (prompt &optional query)
"Either read a symbol name or choose the one at point.
The user is prompted if a prefix argument is in effect, if there is no
symbol at point, or if QUERY is non-nil."
(cond ((or current-prefix-arg query (not (thing-at-point 'dime-symbol)))
(dime-read-from-minibuffer prompt (thing-at-point 'dime-symbol)))
(t (thing-at-point 'dime-symbol))))
;; Interface
(defmacro dime-propertize-region (props &rest body)
"Execute BODY and add PROPS to all the text it inserts.
More precisely, PROPS are added to the region between the point's
positions before and after executing BODY."
(declare (indent 1))
(let ((start (cl-gensym)))
`(let ((,start (point)))
(prog1 (progn ,@body)
(add-text-properties ,start (point) ,props)))))
(defun dime-add-face (face string)
(declare (indent 1))
(add-text-properties 0 (length string) (list 'face face) string)
string)
;; Interface
(defsubst dime-insert-propertized (props &rest args)
"Insert all ARGS and then add text-PROPS to the inserted text."
(dime-propertize-region props (apply #'insert args)))
(defmacro dime-with-rigid-indentation (level &rest body)
"Execute BODY and then rigidly indent its text insertions.
Assumes all insertions are made at point."
(declare (indent 1))
(let ((start (cl-gensym)) (l (cl-gensym)))
`(let ((,start (point)) (,l ,(or level '(current-column))))
(prog1 (progn ,@body)
(dime-indent-rigidly ,start (point) ,l)))))
(defun dime-indent-rigidly (start end column)
;; Similar to `indent-rigidly' but doesn't inherit text props.
(let ((indent (make-string column ?\ )))
(save-excursion
(goto-char end)
(beginning-of-line)
(while (and (<= start (point))
(progn
(insert-before-markers indent)
(zerop (forward-line -1))))))))
(defun dime-insert-indented (&rest strings)
"Insert all arguments rigidly indented."
(dime-with-rigid-indentation nil
(apply #'insert strings)))
(defun dime-property-bounds (prop)
"Return two the positions of the previous and next changes to PROP.
PROP is the name of a text property."
(cl-assert (get-text-property (point) prop))
(let ((end (next-single-char-property-change (point) prop)))
(list (previous-single-char-property-change end prop) end)))
(defun dime-curry (fun &rest args)
"Partially apply FUN to ARGS. The result is a new function.
This idiom is preferred over `let'."
`(lambda (&rest more) (apply ',fun (append ',args more))))
(defun dime-rcurry (fun &rest args)
"Like `dime-curry' but ARGS on the right are applied."
`(lambda (&rest more) (apply ',fun (append more ',args))))
;;;;; Temporary popup buffers
(defvar dime-popup-restore-data nil
"Data needed when closing popup windows.
This is used as buffer local variable.
The format is (POPUP-WINDOW SELECTED-WINDOW OLD-BUFFER).
POPUP-WINDOW is the window used to display the temp buffer.
That window may have been reused or freshly created.
SELECTED-WINDOW is the window that was selected before displaying
the popup buffer.
OLD-BUFFER is the buffer that was previously displayed in POPUP-WINDOW.
OLD-BUFFER is nil if POPUP-WINDOW was newly created.
See `view-return-to-alist' for a similar idea.")
;; Interface
(cl-defmacro dime-with-popup-buffer ((name &key project connection select mode)
&body body)
"Similar to `with-output-to-temp-buffer'.
Bind standard-output and initialize some buffer-local variables.
Restore window configuration when closed.
NAME is the name of the buffer to be created.
PROJECT is the value `dime-buffer-project'.
CONNECTION is the value for `dime-buffer-connection',
if nil, no explicit connection is associated with
the buffer. If t, the current connection is taken.
MODE is the name of a major mode which will be enabled.
"
(declare (indent 1))
`(let* ((vars% (list ,(if (eq project t) '(dime-current-project) project)
,(if (eq connection t) '(dime-connection) connection)))
(standard-output (dime-make-popup-buffer ,name vars% ,mode)))
(with-current-buffer standard-output
(prog1 (progn ,@body)
(cl-assert (eq (current-buffer) standard-output))
(setq buffer-read-only t)
(set-window-point (dime-display-popup-buffer ,(or select nil))
(point))))))
(defun dime-make-popup-buffer (name buffer-vars mode)
"Return a temporary buffer called NAME.
The buffer also uses the minor-mode `dime-popup-buffer-mode'."
(with-current-buffer (get-buffer-create name)
(kill-all-local-variables)
(when mode
(funcall mode))
(setq buffer-read-only nil)
(erase-buffer)
(set-syntax-table lisp-mode-syntax-table)
(dime-init-popup-buffer buffer-vars)
(current-buffer)))
(defun dime-init-popup-buffer (buffer-vars)
(dime-popup-buffer-mode 1)
(cl-multiple-value-setq (dime-buffer-project dime-buffer-connection)
buffer-vars))
(defun dime-display-popup-buffer (select)
"Display the current buffer.
Save the selected-window in a buffer-local variable, so that we
can restore it later."
(let ((selected-window (selected-window))
(old-windows))
(walk-windows (lambda (w) (push (cons w (window-buffer w)) old-windows))
nil t)
(let ((new-window (display-buffer (current-buffer))))
(unless dime-popup-restore-data
(set (make-local-variable 'dime-popup-restore-data)
(list new-window
selected-window
(cdr (cl-find new-window old-windows :key #'car)))))
(when select
(select-window new-window))
new-window)))
(defun dime-close-popup-window ()
(when dime-popup-restore-data
(cl-destructuring-bind (popup-window selected-window old-buffer)
dime-popup-restore-data
(kill-local-variable 'dime-popup-restore-data)
(bury-buffer)
(when (eq popup-window (selected-window))
(cond ((and (not old-buffer) (not (one-window-p)))
(delete-window popup-window))
((and old-buffer (buffer-live-p old-buffer))
(set-window-buffer popup-window old-buffer))))
(when (window-live-p selected-window)
(select-window selected-window)))))
(defmacro dime-save-local-variables (vars &rest body)
(declare (indent 1))
(let ((vals (make-symbol "vals")))
`(let ((,vals (mapcar (lambda (var)
(if (local-variable-p var (current-buffer))
(cons var (eval var))))
',vars)))
(prog1 (progn . ,body)
(mapc (lambda (var+val)
(when (consp var+val)
(set (make-local-variable (car var+val)) (cdr var+val))))
,vals)))))
(define-minor-mode dime-popup-buffer-mode
"Mode for displaying read only stuff"
nil
nil
'(("q" . dime-popup-buffer-quit-function)
("\M-." . dime-edit-definition)))
(add-to-list 'minor-mode-alist
`(dime-popup-buffer-mode
'(:eval (unless dime-mode
(dime-modeline-string)))))
(set-keymap-parent dime-popup-buffer-mode-map dime-parent-map)
(defvar-local dime-popup-buffer-quit-function 'dime-popup-buffer-quit
"The function that is used to quit a temporary popup buffer.")
(defun dime-popup-buffer-quit-function (&optional kill-buffer-p)
"Wrapper to invoke the value of `dime-popup-buffer-quit-function'."
(interactive)
(funcall dime-popup-buffer-quit-function kill-buffer-p))
;; Interface
(defun dime-popup-buffer-quit (&optional kill-buffer-p)
"Get rid of the current (temp) buffer without asking.
Restore the window configuration unless it was changed since we
last activated the buffer."
(interactive)
(let ((buffer (current-buffer)))
(dime-close-popup-window)
(when kill-buffer-p
(kill-buffer buffer))))
;;;;; Filename translation
;;;
;;; Filenames passed between Emacs and Dylan should be translated using
;;; these functions. This way users who run Emacs and Dylan on separate
;;; machines have a chance to integrate file operations somehow.
(defvar dime-to-dylan-filename-function #'convert-standard-filename
"Function to translate Emacs filenames to CL namestrings.")
(defvar dime-from-dylan-filename-function #'identity
"Function to translate CL namestrings to Emacs filenames.")
(defun dime-to-dylan-filename (filename)
"Translate the string FILENAME to a Dylan filename."
(funcall dime-to-dylan-filename-function filename))
(defun dime-from-dylan-filename (filename)
"Translate the Dylan filename FILENAME to an Emacs filename."
(funcall dime-from-dylan-filename-function filename))
;;;; Starting Dime
;;;
;;; This section covers starting an inferior-dylan, compiling and
;;; starting the server, initiating a network connection.
;;;;; Entry points
;; TODO: We used to use the inf-dylan variable
;; `inferior-dylan-program' for compatibility. However, our variable
;; has now been renamed to `dime-dylan-program' so it has the same
;; symbol prefix as everything else defined in this file. We should
;; probably get rid of this variable altogether, and use
;; `dime-default-dylan' and `dime-dylan-implementations' instead.
(defvar dime-dylan-program "dylan-compiler"
"*Program name for invoking an inferior Dylan with for Inferior Dylan mode.")
(defvar dime-dylan-implementations nil
"*A list of known Dylan implementations.
The list should have the form:
((NAME (PROGRAM PROGRAM-ARGS...) &key KEYWORD-ARGS) ...)
NAME is a symbol for the implementation.
PROGRAM and PROGRAM-ARGS are strings used to start the Dylan process.
For KEYWORD-ARGS see `dime-start'.
Here's an example:
((cmucl (\"/opt/cmucl/bin/dylan\" \"-quiet\") :init dime-init-command)
(acl (\"acl7\") :coding-system emacs-mule))")
(defvar dime-default-dylan nil
"*The name of the default Dylan implementation.
See `dime-dylan-implementations'")
;; dummy definitions for the compiler
(defvar dime-net-processes)
(defvar dime-default-connection)
;;;###autoload
(defun dime (&optional command coding-system)
"Start Dylan and connect to its Swank server."
(interactive)
(let ((dime-dylan-program (or command dime-dylan-program))
(dime-net-coding-system (or coding-system dime-net-coding-system)))
(dime-start* (cond ((and command (symbolp command))
(dime-dylan-options command))