-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.lisp
1533 lines (1386 loc) · 59.2 KB
/
commands.lisp
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
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
;;; (c) copyright 1998,1999,2000 by Michael McDonald ([email protected])
;;; (c) copyright 2000 by Robert Strandh ([email protected])
;;; (c) copyright 2002 by Tim Moore ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library 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
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
(in-package :clim-internals)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Command tables
(defgeneric command-table-name (command-table))
(defgeneric command-table-inherit-from (command-table))
;;; Container for info about a command
(defclass command-item ()
((command-name :initarg :command-name :reader command-item-name
:initarg nil)
(command-line-name :initarg :command-line-name :reader command-line-name)))
(defmethod print-object ((obj command-item) stream)
(print-unreadable-object (obj stream :identity t :type t)
(cond ((slot-boundp obj 'command-line-name)
(format stream "~A" (command-line-name obj)))
((slot-boundp obj 'command-name)
(format stream "~S" (command-item-name obj)))
(t nil))))
;;; According to the specification, command menu items are stored as
;;; lists. This way seems better, and I hope nothing will break.
(defclass %menu-item (command-item)
((menu-name :reader command-menu-item-name :initarg :menu-name)
(type :initarg :type :reader command-menu-item-type)
(value :initarg :value :reader command-menu-item-value)
(documentation :initarg :documentation)
(text-style :initarg :text-style :initform nil)
(keystroke :initarg :keystroke)))
(defmethod print-object ((item %menu-item) stream)
(print-unreadable-object (item stream :identity t :type t)
(when (slot-boundp item 'menu-name)
(format stream "~S" (command-menu-item-name item)))
(when (slot-boundp item 'keystroke)
(format stream "~:[~; ~]keystroke ~A"
(slot-boundp item 'menu-name)
(slot-value item 'keystroke)))))
(defun command-menu-item-options (menu-item)
(with-slots (documentation text-style) menu-item
(list ':documentation documentation ':text-style text-style)))
(defclass standard-command-table (command-table)
((name :initarg :name :reader command-table-name)
(inherit-from :initarg :inherit-from
:initform '()
:reader command-table-inherit-from
:type list)
(commands :accessor commands :initarg :commands
:initform (make-hash-table :test #'eq))
(command-line-names :accessor command-line-names
:initform (make-hash-table :test #'equal))
(presentation-translators :reader presentation-translators
:initform (make-instance 'translator-table))
(inherit-menu :reader inherit-menu
:initform nil
;; We interpret :menu to mean "inherit menu items
;; without keystrokes" and :keystrokes to mean
;; "inherit menu items with keystrokes".
:type (member nil t :menu :keystrokes)
:initarg :inherit-menu)
(menu :initarg :menu :initform '())
(keystroke-accelerators :initform nil)
(keystroke-items :initform nil)))
(defmethod print-object ((table standard-command-table) stream)
(print-unreadable-object (table stream :identity t :type t)
(format stream "~S" (command-table-name table))))
;;; We store command-table designators, but this function should
;;; return command table objects.
(defmethod command-table-inherit-from :around
((command-table standard-command-table))
(mapcar #'find-command-table (call-next-method)))
;;; Franz user manual says that this slot is setf-able
(defgeneric (setf command-table-inherit-from) (inherit-from table))
(defmethod (setf command-table-inherit-from)
(inherit (table standard-command-table))
(invalidate-translator-caches)
(setf (slot-value table 'inherit-from) inherit))
(defun inherit-keystrokes (command-table)
"Return true if `command-table' (which must be a command table
designator) inherits keystrokes."
(let ((inherit-menu (inherit-menu (find-command-table command-table))))
(or (eq inherit-menu t)
(eq inherit-menu :keystrokes))))
(defun inherit-menu-items (command-table)
"Return true if `command-table' (which must be a command table
designator) inherits menu items."
(let ((inherit-menu (inherit-menu (find-command-table command-table))))
(or (inherit-keystrokes command-table)
(eq inherit-menu :menu))))
(defparameter *command-tables* (make-hash-table :test #'eq))
(define-condition command-table-error (simple-error)
((command-table-name :reader error-command-table-name
:initform nil
:initarg :command-table-name))
(:default-initargs :format-control "" :format-arguments nil))
(defmethod print-object ((object command-table-error) stream)
(print-unreadable-object (object stream :type t :identity t)
(when (error-command-table-name object)
(princ (error-command-table-name object) stream))))
(defun command-table-designator-as-name (designator)
"Return the name of `designator' if it is a command table,
`designator' otherwise."
(if (typep designator 'standard-command-table)
(command-table-name designator)
designator))
(define-condition command-table-not-found (command-table-error)
())
(define-condition command-table-already-exists (command-table-error)
())
(define-condition command-not-present (command-table-error)
())
(define-condition command-not-accessible (command-table-error)
())
(define-condition command-already-present (command-table-error)
())
(defun find-command-table (name &key (errorp t))
(cond ((command-table-p name) name)
((gethash name *command-tables*))
(errorp (error 'command-table-not-found :command-table-name name))
(t nil)))
(define-presentation-method present (object (type command-table) stream
(view textual-view)
&key acceptably for-context-type)
(declare (ignore for-context-type))
(let ((name (command-table-name object)))
(if acceptably
(prin1 name stream)
(princ name stream))))
(define-presentation-method accept ((type command-table) stream
(view textual-view)
&key)
(multiple-value-bind (table success string)
(completing-from-suggestions (stream)
(loop
for name being the hash-key of *command-tables*
using (hash-value table)
do (suggest (symbol-name name) table)))
(if success
table
(simple-parse-error "~A is not the name of a command table" string))))
(defun menu-items-from-list (menu)
(mapcar
#'(lambda (item)
(destructuring-bind (name type value
&rest args)
item
(apply #'make-menu-item name type value
args)))
menu))
(setf (gethash 'global-command-table *command-tables*)
(make-instance 'standard-command-table
:name 'global-command-table
:inherit-from nil
:menu nil))
; adjusted to allow anonymous command-tables for menu-bars
(defun make-command-table (name &key inherit-from menu inherit-menu (errorp t))
(unless inherit-from
(setq inherit-from '(global-command-table)))
(if (and name errorp (gethash name *command-tables*))
(error 'command-table-already-exists :command-table-name name)
(let ((result (make-instance 'standard-command-table :name name
:inherit-from inherit-from
:inherit-menu inherit-menu
:menu (menu-items-from-list menu))))
(when name
(setf (gethash name *command-tables*) result))
result)))
(make-command-table 'user-command-table)
(defmacro define-command-table (name &key inherit-from menu inherit-menu)
`(let ((old-table (gethash ',name *command-tables* nil))
(inherit-from-arg (or ',inherit-from '(global-command-table))))
(if old-table
(with-slots (inherit-from menu) old-table
(setq inherit-from inherit-from-arg
menu (menu-items-from-list ',menu))
old-table)
(make-command-table ',name
:inherit-from inherit-from-arg
:inherit-menu ,inherit-menu
:menu ',menu
:errorp nil))))
(defun remove-command-from-command-table (command-name
command-table
&key (errorp t))
(let* ((table (find-command-table command-table))
(item (gethash command-name (commands table))))
(if (null item)
(when errorp
(error 'command-not-present :command-table-name (command-table-name command-table)))
(progn
(when (typep item '%menu-item)
(remove-menu-item-from-command-table table
(command-menu-item-name item)
:errorp nil))
(when (command-item-name item)
(remhash (command-item-name item) (command-line-names table)))
(remhash command-name (commands table))))))
(defun add-command-to-command-table (command-name
command-table
&key name menu keystroke (errorp t)
(menu-command (and menu
`(,command-name))))
(let ((table (find-command-table command-table))
(name (cond ((stringp name)
name)
(name
(command-name-from-symbol command-name))
(t nil))))
(multiple-value-bind (menu-name menu-options)
(cond ((null menu)
nil)
((stringp menu)
menu)
((eq menu t)
(if (stringp name)
name
(command-name-from-symbol command-name)))
((consp menu)
(values (car menu) (cdr menu))))
(when keystroke
(add-keystroke-to-command-table table keystroke
:command command-name :errorp nil))
(let* ((item (if menu
(apply #'make-menu-item
menu-name :command menu-command
:command-name command-name
:command-line-name name
`(,@(and keystroke `(:keystroke ,keystroke))
,@menu-options))
(make-instance 'command-item
:command-name command-name
:command-line-name name)))
(after (getf menu-options :after)))
(when (and errorp (gethash command-name (commands table)))
(error 'command-already-present :command-table-name command-table))
(remove-command-from-command-table command-name table :errorp nil)
(setf (gethash command-name (commands table)) item)
(when name
(setf (gethash name (command-line-names table)) command-name))
(when menu
(%add-menu-item table item after))))))
(defun apply-with-command-table-inheritance (fun command-table)
(funcall fun command-table)
(mapc #'(lambda (inherited-command-table)
(apply-with-command-table-inheritance
fun (find-command-table inherited-command-table)))
(command-table-inherit-from command-table)))
;;; do-command-table-inheritance has been shipped off to utils.lisp.
(defun map-over-command-table-commands (function command-table
&key (inherited t))
(let ((command-table (find-command-table command-table)))
(flet ((map-func (table)
(maphash #'(lambda (key val)
(declare (ignore val))
(funcall function key))
(slot-value table 'commands))))
(if inherited
(apply-with-command-table-inheritance #'map-func command-table)
(map-func command-table)))))
(defun map-over-command-table-names (function command-table &key (inherited t))
(let ((command-table (find-command-table command-table)))
(flet ((map-func (table)
(maphash function (slot-value table 'command-line-names))))
(if inherited
(apply-with-command-table-inheritance #'map-func command-table)
(map-func command-table)))))
(defun command-present-in-command-table-p (command-name command-table)
(let ((table (find-command-table command-table)))
(if (gethash command-name (slot-value table 'commands))
table
nil)))
(defun command-accessible-in-command-table-p (command-name command-table)
(or (command-present-in-command-table-p command-name command-table)
(some #'(lambda (table)
(command-accessible-in-command-table-p
command-name
(find-command-table table)))
(command-table-inherit-from (find-command-table command-table)))))
(defun find-command-from-command-line-name (name command-table &key (errorp t))
(apply-with-command-table-inheritance
#'(lambda (table)
(let ((value (gethash name (command-line-names table))))
(when value
(return-from find-command-from-command-line-name
(values value table)))))
(find-command-table command-table))
(if errorp
(error 'command-not-accessible :command-table-name command-table)))
(defun command-line-name-for-command (command-name command-table
&key (errorp t))
(do-command-table-inheritance (table command-table)
(let* ((command-item (gethash command-name (slot-value table 'commands)))
(command-line-name (and command-item
(command-line-name command-item))))
(when (stringp command-line-name)
(return-from command-line-name-for-command command-line-name))))
(cond ((eq errorp :create)
(command-name-from-symbol command-name))
(errorp
(error 'command-not-accessible :command-table-name
(command-table-designator-as-name command-table)))
(t nil)))
(defun find-menu-item (menu-name command-table &key (errorp t))
(let* ((table (find-command-table command-table))
(mem (member menu-name (slot-value table 'menu)
:key #'command-menu-item-name :test #'string-equal)))
(if mem
(values (car mem) command-table)
(or (find-if #'(lambda (table)
(find-menu-item menu-name table :errorp nil))
(command-table-inherit-from table))
(when errorp
(error 'command-not-accessible :command-table-name
(command-table-designator-as-name table)))))))
(defun remove-menu-item-from-command-table (command-table string
&key (errorp t))
(let ((table (find-command-table command-table))
(item (find-menu-item string command-table :errorp nil)))
(with-slots (menu) table
(if (and errorp (not item))
(error 'command-not-present :command-table-name
(command-table-designator-as-name table))
(setf menu (delete string menu
:key #'command-menu-item-name
:test #'string-equal))))))
(defun make-menu-item (name type value
&key (documentation nil documentationp)
(keystroke nil keystrokep)
(text-style nil text-style-p)
(command-name nil command-name-p)
(command-line-name nil command-line-name-p)
&allow-other-keys)
;; v-- this may be wrong, we do this to allow
;; text-style to contain make-text-style calls
;; so we use a very limited evaluator - FIXME
(when (and (consp text-style)
(eq (first text-style) 'make-text-style))
(setq text-style (apply #'make-text-style (rest text-style))))
(apply #'make-instance '%menu-item
:menu-name name :type type :value value
`(,@(and documentationp `(:documentation ,documentation))
,@(and keystrokep `(:keystroke ,keystroke))
,@(and text-style-p `(:text-style ,text-style))
,@(and command-name-p `(:command-name ,command-name))
,@(and command-line-name-p
`(:command-line-name ,command-line-name)))))
(defun %add-menu-item (command-table item after)
(with-slots (menu)
command-table
(when (null menu)
(setf after :start))
(case after
(:start (push item menu))
((:end nil) (setf menu (nconc menu (list item))))
(:sort (setf menu (sort (cons item menu)
#'string-lessp
:key #'command-menu-item-name)))
(t (push item
(cdr (member after menu
:key #'command-menu-item-name
:test #'string-equal))))))
(when (and (slot-boundp item 'keystroke)
(slot-value item 'keystroke))
(%add-keystroke-item command-table (slot-value item 'keystroke) item nil)))
(defun add-menu-item-to-command-table (command-table
string type value
&rest args
&key documentation (after :end)
keystroke text-style (errorp t))
(declare (ignore documentation keystroke text-style))
(let* ((table (find-command-table command-table))
(old-item (find-menu-item string command-table :errorp nil)))
(cond ((and errorp old-item)
(error 'command-already-present :command-table-name
(command-table-designator-as-name table)))
(old-item
(remove-menu-item-from-command-table command-table string))
(t nil))
(%add-menu-item table
(apply #'make-menu-item string type value args)
after)))
(defun map-over-command-table-menu-items (function command-table)
"Applies function to all of the items in `command-table's
menu. `Command-table' must be a command table or the name of a
command table. `Function' must be a function of three arguments,
the menu name, the keystroke accelerator gesture (which will be
NIL if there is none), and the command menu item; it has dynamic
extent. The command menu items are mapped over in the order
specified by `add-menu-item-to-command-table'. `Command-table' is
a command table designator. Any inherited menu items will be
mapped over after `command-table's own menu items.
`Map-over-command-table-menu-items' does not descend into
sub-menus. If the programmer requires this behavior, he should
examine the type of the command menu item to see if it is
`:menu'."
(let ((table-object (find-command-table command-table)))
(flet ((map-table-entries (table)
(mapc #'(lambda (item)
(with-slots (menu-name keystroke) item
(funcall function
menu-name
(and (slot-boundp item 'keystroke) keystroke)
item)))
(slot-value table 'menu))))
(map-table-entries table-object)
(when (inherit-menu-items table-object)
(dolist (table (command-table-inherit-from table-object))
(map-over-command-table-menu-items function table))))
(values)))
(defun map-over-command-table-translators
(function command-table &key (inherited t))
(flet ((map-func (table)
(maphash #'(lambda (k v)
(declare (ignore k))
(funcall function v))
(slot-value
(presentation-translators table)
'translators))))
(let ((command-table (find-command-table command-table)))
(if inherited
(apply-with-command-table-inheritance #'map-func command-table)
(map-func command-table)))))
;(defun add-presentation-translator-to-command-table
; (command-table translator-name &key (errorp t)))
; - fixme; spec says this fun is given a translator name, but that
; find-presentation-translator needs a translator name and a command
; table designator
(defun add-actual-presentation-translator-to-command-table
(command-table translator &key (errorp t))
(let ((translators
(presentation-translators
(find-command-table command-table))))
(when (and errorp
(second
(multiple-value-list
(gethash (name translator)
(slot-value translators 'translators)))))
(error 'command-already-present
:command-table-name command-table))
(add-translator translators translator)))
;; At this point we should still see the gesture name as supplied by the
;; programmer in 'gesture'
(defun %add-keystroke-item (command-table gesture item errorp)
(with-slots (keystroke-accelerators keystroke-items)
command-table
(let* ((gesture (if (and (symbolp gesture) ; symbolic gesture name?
(gethash gesture *gesture-names*))
gesture
(multiple-value-list (realize-gesture-spec :keyboard gesture))))
(in-table (position gesture keystroke-accelerators :test #'equal)))
(when (and in-table errorp)
(error 'command-already-present :command-table-name
(command-table-designator-as-name command-table)))
(if in-table
(setf (nth in-table keystroke-items) item)
(progn
(push gesture keystroke-accelerators)
(push item keystroke-items))))))
;; FIXME: According to the spec, we need to remove the menu item if already
;; present. Also, you could argue we don't signal 'command-already-present
;; in quite the right circumstance (see above).
(defun add-keystroke-to-command-table (command-table gesture type value
&key documentation (errorp t))
(let ((command-table (find-command-table command-table)))
(%add-keystroke-item command-table
gesture
(make-instance '%menu-item
:type type :value value
:keystroke gesture
:documentation documentation)
errorp)))
(defun remove-keystroke-from-command-table (command-table gesture
&key (errorp t))
(let ((command-table (find-command-table command-table)))
(with-slots (keystroke-accelerators keystroke-items)
command-table
(let ((in-table (position gesture keystroke-accelerators :test #'equal)))
(if in-table
(if (zerop in-table)
(setq keystroke-accelerators (cdr keystroke-accelerators)
keystroke-items (cdr keystroke-items))
(let ((accel-tail (nthcdr (1- in-table)
keystroke-accelerators))
(items-tail (nthcdr (1- in-table) keystroke-items)))
(setf (cdr accel-tail) (cddr accel-tail))
(setf (cdr items-tail) (cddr items-tail))))
(when errorp
(error 'command-not-present :command-table-name
(command-table-designator-as-name command-table)))))))
nil)
(defun map-over-command-table-keystrokes (function command-table)
(let ((command-table (find-command-table command-table)))
(with-slots (keystroke-accelerators keystroke-items)
command-table
(loop for gesture in keystroke-accelerators
for item in keystroke-items
do (funcall function
(and (slot-boundp item 'menu-name)
(command-menu-item-name item))
gesture
item)))))
(defun find-keystroke-item (gesture command-table
&key (test #'event-matches-gesture-name-p)
(errorp t))
(let ((command-table (find-command-table command-table)))
(loop for keystroke in (slot-value command-table 'keystroke-accelerators)
for item in (slot-value command-table 'keystroke-items)
if (funcall test gesture keystroke)
do (return-from find-keystroke-item (values item command-table)))
(if errorp
(error 'command-not-present :command-table-name
(command-table-designator-as-name command-table))
nil)))
(defun lookup-keystroke-item (gesture command-table
&key (test #'event-matches-gesture-name-p))
(let ((command-table (find-command-table command-table)))
(multiple-value-bind (item table)
(find-keystroke-item gesture command-table :test test :errorp nil)
(when table
(return-from lookup-keystroke-item (values item table)))
(map-over-command-table-menu-items
#'(lambda (name keystroke item)
(declare (ignore name keystroke))
(when (eq (command-menu-item-type item) :menu)
(multiple-value-bind (sub-item sub-command-table)
(lookup-keystroke-item gesture
(command-menu-item-value item)
:test test)
(when sub-command-table
(return-from lookup-keystroke-item
(values sub-item sub-command-table))))))
command-table))))
(defun partial-command-from-name (command-name command-table)
(let ((parser (gethash command-name *command-parser-table*)))
(if (null parser)
(error 'command-not-present :command-table-name
(command-table-designator-as-name command-table))
(cons command-name
(mapcar #'(lambda (foo)
(declare (ignore foo))
*unsupplied-argument-marker*)
(required-args parser))))))
;;; XXX The spec says that GESTURE may be a gesture name, but also that the
;;; default test is event-matches-gesture-name-p. Uh...
(defun lookup-keystroke-command-item (gesture command-table
&key test (numeric-arg 1))
(let ((item (lookup-keystroke-item
gesture command-table
:test (or test
#'(lambda (gesture gesture-name)
(or (equal gesture gesture-name)
(event-matches-gesture-name-p
gesture
gesture-name)))))))
(if item
(let* ((value (command-menu-item-value item))
(command (case (command-menu-item-type item)
(:command
value)
(:function
(funcall value gesture numeric-arg))
;; XXX What about the :menu case?
(otherwise nil))))
(if command
; Return a literal command, or create a partial command from a command-name
(substitute-numeric-argument-marker (if (symbolp command)
(partial-command-from-name command command-table)
command)
numeric-arg)
gesture))
gesture)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Commands
(defclass command-parsers ()
((parser :accessor parser :initarg :parser)
(partial-parser :accessor partial-parser :initarg :partial-parser)
(argument-unparser :accessor argument-unparser
:initarg :argument-unparser)
(required-args :accessor required-args :initarg :required-args)
(keyword-args :accessor keyword-args :initarg :keyword-args))
(:documentation "A container for a command's parsing functions and
data for unparsing"))
(defparameter *command-parser-table* (make-hash-table)
"Mapping from command names to argument parsing functions.")
(defvar *unsupplied-argument-marker* '%unsupplied-argument-marker%)
(defvar *numeric-argument-marker* '%numeric-argument-marker%)
(defvar *command-name-delimiters* '(command-delimiter))
(defvar *command-argument-delimiters* '(command-delimiter))
;;; A type indicating empty input. For example, if one types <space>
;;; to get the default value of a keyword argument, and then types
;;; <return>, we don't want to see "None" in the output history. So,
;;; we define this subtype of null that has no output. It's not meant
;;; to be read if the form is ever accepted again.
(define-presentation-type empty ()
:inherit-from 'null)
(define-presentation-method present
(object (type empty) stream (view textual-view) &key &allow-other-keys)
(declare (ignore object stream)))
(defun accept-form-for-argument (stream arg)
(let ((accept-keys '(:default :default-type :display-default
:prompt :documentation :insert-default)))
(destructuring-bind (name ptype &rest key-args
&key (mentioned-default nil mentioned-default-p)
&allow-other-keys)
arg
(declare (ignore name))
`(accept ,ptype :stream ,stream
,@(loop for (key val) on key-args by #'cddr
when (member key accept-keys)
append `(,key ,val) into args
finally (return (if mentioned-default-p
`(:default ,mentioned-default
,@args)
args)))))))
;;; In the partial command reader accepting-values dialog, default
;;; values come either from the input command arguments, if a value
;;; was supplied, or from the default option for the command argument.
;;;
;;; accept for the partial command reader. Can this be refactored to
;;; share code with accept-form-for-argument? Probably not.
;;;
;;; original-command-arg is value entered by the user, or
;;; *unsupplied-argument-marker*. command-arg is the current value for the
;;; argument, originally bound to original-command-arg and now possibly
;;; changed by the user.
(defun accept-form-for-argument-partial (stream ptype-arg command-arg
original-command-arg )
(let ((accept-keys '(:default :default-type :display-default
:prompt :documentation :insert-default)))
(destructuring-bind (name ptype &rest key-args)
ptype-arg
(declare (ignore name))
(let ((args (loop
for (key val) on key-args by #'cddr
if (eq key :default)
append `(:default (if (eq ,command-arg
*unsupplied-argument-marker*)
,val
,command-arg))
else if (member key accept-keys :test #'eq)
append `(,key ,val))))
(setq args (append args `(:query-identifier ',(gensym "COMMAND-PROMPT-ID"))))
(if (member :default args :test #'eq)
`(accept ,ptype :stream ,stream ,@args)
`(if (eq ,original-command-arg *unsupplied-argument-marker*)
(accept ,ptype :stream ,stream ,@args)
(accept ,ptype :stream ,stream :default ,command-arg
,@args)))))))
(defun make-keyword (sym)
(intern (symbol-name sym) :keyword))
(defun make-key-acceptors (stream keyword-args key-results)
;; We don't use the name as a variable, and we do want a symbol in the
;; keyword package.
(when (null keyword-args)
(return-from make-key-acceptors nil))
(setq keyword-args (mapcar #'(lambda (arg)
(cons (make-keyword (car arg)) (cdr arg)))
keyword-args))
(let ((key-possibilities (gensym "KEY-POSSIBILITIES"))
(member-ptype (gensym "MEMBER-PTYPE"))
(key-result (gensym "KEY-RESULT"))
(val-result (gensym "VAL-RESULT")))
`(let ((,key-possibilities nil))
,@(mapcar #'(lambda (key-arg)
(destructuring-bind (name ptype
&key (when t) &allow-other-keys)
key-arg
(declare (ignore ptype))
(let ((key-arg-name (concatenate
'string
":"
(keyword-arg-name-from-symbol
name))))
`(when ,when
(push `(,,key-arg-name ,,name)
,key-possibilities)))))
keyword-args)
(setq ,key-possibilities (nreverse ,key-possibilities))
(when ,key-possibilities
(input-editor-format ,stream "(keywords) ")
(let ((,member-ptype `(token-or-type ,,key-possibilities empty)))
(loop
(let* ((,key-result (prog1 (accept ,member-ptype
:stream ,stream
:prompt nil
:default nil)
(eat-delimiter-or-activator)))
(,val-result
(case ,key-result
,@(mapcar
#'(lambda (key-arg)
`(,(car key-arg)
,(accept-form-for-argument stream
key-arg)))
keyword-args))))
(setq ,key-results (list* ,key-result
,val-result
,key-results)))
(eat-delimiter-or-activator))))
,key-results)))
(defun make-argument-accept-fun (name required-args keyword-args)
(let ((stream-var (gensym "STREAM"))
(required-arg-names (mapcar #'car required-args))
(key-results (gensym "KEY-RESULTS")))
`(defun ,name (,stream-var)
(let (,@(mapcar #'(lambda (arg)
`(,arg *unsupplied-argument-marker*))
required-arg-names)
(,key-results nil))
(block activated
(flet ((eat-delimiter-or-activator ()
(let ((gesture (read-gesture :stream ,stream-var)))
(when (or (null gesture)
(activation-gesture-p gesture))
(return-from activated nil))
(unless (delimiter-gesture-p gesture)
(unread-gesture gesture
:stream ,stream-var)))))
(declare (ignorable (function eat-delimiter-or-activator)))
(let ((gesture (read-gesture :stream ,stream-var
:timeout 0
:peek-p t)))
(cond ((and gesture (activation-gesture-p gesture))
(return-from activated nil)))
,@(mapcan #'(lambda (arg)
(copy-list
`((setq ,(car arg)
,(accept-form-for-argument stream-var
arg))
(eat-delimiter-or-activator))))
required-args)
,(make-key-acceptors stream-var keyword-args key-results))))
(list* ,@required-arg-names ,key-results)))))
(defun make-partial-parser-fun (name required-args)
(with-gensyms (command-table stream partial-command
command-name command-line-name)
(let* ((required-arg-names (mapcar #'car required-args))
(original-args (mapcar #'(lambda (arg)
(gensym (format nil "~A-ORIGINAL"
(symbol-name arg))))
required-arg-names)))
;; We don't need fresh gensyms of these variables for each accept form.
(with-gensyms (value ptype changedp)
`(defun ,name (,command-table ,stream ,partial-command)
(do ((still-missing nil t))
(nil)
(destructuring-bind (,command-name ,@original-args)
,partial-command
(let ((,command-line-name (command-line-name-for-command
,command-name
,command-table
:errorp nil))
,@(mapcar #'list required-arg-names original-args))
(accepting-values (,stream :select-first-query t
:align-prompts t)
(format ,stream
"You are being prompted for arguments to ~S~%"
,command-line-name)
,@(loop
for var in required-arg-names
for original-var in original-args
for parameter in required-args
for first-arg = t then nil
append `((multiple-value-bind (,value ,ptype ,changedp)
,(accept-form-for-argument-partial
stream parameter var original-var)
(declare (ignore ,ptype))
,@(unless first-arg `((terpri ,stream)))
(when ,changedp
(setq ,var ,value)))))
(when still-missing
(format ,stream
"~&Please supply all arguments.~%")))
(setf ,partial-command (list ,command-name ,@required-arg-names))
(unless (partial-command-p ,partial-command)
(return ,partial-command))))))))))
;;; XXX What do to about :acceptably? Probably need to wait for Goatee "buffer
;;; streams" so we can insert an accept-result-extent in the buffer for
;;; unacceptable objects. -- moore
(defun make-unprocessor-fun (name required-args key-args)
(with-gensyms (command command-args stream key key-arg-val seperator arg-tail)
;; Bind the argument variables because expressions in the
;; following arguments (including the presentation type!) might
;; reference them.
(let ((required-arg-bindings nil)
(key-case-clauses nil))
(loop
for (arg ptype-form) in required-args
collect `(,arg (progn
(write-char ,seperator ,stream)
(present (car ,command-args) ,ptype-form
:stream ,stream)
(pop ,command-args)))
into arg-bindings
finally (setq required-arg-bindings arg-bindings))
(loop
for (arg ptype-form) in key-args
for arg-key = (make-keyword arg)
collect `(,arg-key
(format ,stream "~C:~A~C"
,seperator
,(keyword-arg-name-from-symbol arg)
,seperator)
(present ,key-arg-val ,ptype-form
:stream ,stream))
into key-clauses
finally (setq key-case-clauses key-clauses))
`(defun ,name (,command ,stream)
,(declare-ignorable-form* stream)
(let* ((,seperator #\Space) (,command-args (cdr ,command))
,@required-arg-bindings)
(declare (ignorable ,seperator ,command-args
,@(mapcar #'car required-arg-bindings )))
,@(when key-args
`((loop
for ,arg-tail on ,command-args by #'cddr
for (,key ,key-arg-val) = ,arg-tail
do (progn
(case ,key
,@key-case-clauses)
(when (cddr ,arg-tail)
(write-char ,seperator ,stream)))))))))))
(defun make-command-translators (command-name command-table args)
"Helper function to create command presentation translators for a command."
(loop with readable-command-name = (command-name-from-symbol command-name) ; XXX or :NAME
for arg in args
for arg-index from 0
append (when (getf (cddr arg) :gesture)
(destructuring-bind (name ptype
&key gesture &allow-other-keys)
arg
(let ((command-args (loop for a in args
for i from 0
if (eql i arg-index)
collect 'object
else
collect (getf (cddr a) :default)
end))
(translator-name (intern (format nil
".~A-ARG~D."
command-name
arg-index)
(symbol-package name))))
(multiple-value-bind (gesture translator-options)
(if (listp gesture)
(values (car gesture) (cdr gesture))
(values gesture nil))
(destructuring-bind (&key (documentation
`((object stream)
(orf stream *standard-output*)
(format stream "~A "
,readable-command-name)
(present object (presentation-type-of object) ; type?
:stream stream
:acceptably nil
:sensitive nil))
documentationp)
&allow-other-keys)
translator-options
`(define-presentation-to-command-translator
,translator-name
(,(eval ptype) ,command-name ,command-table
:gesture ,gesture
,@(unless documentationp `(:documentation ,documentation))
,@translator-options)
(object)
(list ,@command-args)))))))))
;;; Vanilla define-command, as defined by the standard
(defmacro %define-command (name-and-options args &body body)
(unless (listp name-and-options)
(setq name-and-options (list name-and-options)))
(destructuring-bind (func &key command-table name menu keystroke)
name-and-options
(multiple-value-bind (required-args keyword-args)
(loop for arg-tail on args
for (arg) = arg-tail
until (eq arg '&key)
collect arg into required
finally (return (values required (cdr arg-tail))))
(let* ((command-func-args
`(,@(mapcar #'car required-args)
,@(and
keyword-args
`(&key ,@(mapcar #'(lambda (arg-clause)
(destructuring-bind (arg-name ptype
&key default
&allow-other-keys)
arg-clause
(declare (ignore ptype))
`(,arg-name ,default)))
keyword-args)))))
(accept-fun-name (gentemp (format nil "~A%ACCEPTOR%"
(symbol-name func))