/
ilisp-src.el
1585 lines (1475 loc) · 60.7 KB
/
ilisp-src.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
;;; -*- Mode: Emacs-Lisp -*-
;;; ilisp-src.el --
;;;
;;; This file is part of ILISP.
;;; Please refer to the file COPYING for copyrights and licensing
;;; information.
;;; Please refer to the file ACKNOWLEGDEMENTS for an (incomplete) list
;;; of present and past contributors.
(require 'cl-lib)
;;; See ilisp.el for more information.
;;;%Source file operations
(unless (boundp 'tags-file-name)
(defvar tags-file-name nil))
(defvar lisp-last-definition nil "Last definition (name type) looked for.")
(defvar lisp-last-file nil "Last used source file.")
(defvar lisp-last-point nil "Last point in last source file.")
(defvar lisp-last-locator nil "Last source locator used.")
(defvar lisp-search nil "Set to T when searching for definitions.")
(defvar lisp-using-tags nil "Set to T when using tags.")
;;;%%lisp-directory
(defvar lisp-edit-files t
"Controls editing of of source files through Emacs' buffers.
If T, then buffers in one of 'lisp-source-modes' will be searched by
'edit-definitions-lisp' if the source cannot be found through the
inferior LISP. It can also be a list of files to edit definitions
from set up by \(\\[lisp-directory]). If it is set to nil, then no
additional files will be searched.")
;;;
(defun lisp-extensions ()
"Return a regexp for matching file extensions.
The extensions are those of files that enter one of
'lisp-source-modes' according to 'auto-mode-alist'."
(let ((entries auto-mode-alist)
(extensions nil))
(dolist (entry entries)
(when (memq (cdr entry) lisp-source-modes)
(setq extensions
(concat "\\|" (car entry) extensions))))
(substring extensions 2)))
;;;
(defun lisp-directory (directory add)
"Edit the files in DIRECTORY.
The files must have an 'auto-mode' alist entry in 'lisp-source-modes'.
With a positive prefix, add the files on to the already existing
files. With a negative prefix, clear the list. In either case set
tags-file-name to nil so that tags are not used."
(interactive
(list (if (not (eq current-prefix-arg '-))
(read-file-name "Lisp Directory: "
nil
default-directory
nil))
current-prefix-arg))
(setq tags-file-name nil)
(if (eq add '-)
(progn (setq lisp-edit-files t)
(message "No current lisp directory"))
(if add
(message "Added %s as a lisp directory" directory)
(message "%s is the lisp directory" directory))
(setq directory (expand-file-name directory))
(if (file-directory-p directory)
(setq lisp-edit-files
(append
(directory-files directory t (lisp-extensions))
(if add (if (eq lisp-edit-files t) nil lisp-edit-files))))
(error "%s is not a directory" directory))))
;;;; New M-. implementation. -- rgr, 25-Mar-03.
(defvar lisp-fall-back-on-find-tags ':ask
"*Can be one of nil for never, :ask to ask the user each time, or
anything else to always use find-tag as a last resort (when ilisp can't
find anything).")
(defvar lisp-find-definition-verbose-p nil
"Whether to generate informational messages about internal progress.")
;; these regexp fragments are shared between a number of regexp-constructing
;; definitions.
(defvar ilisp-whitespace* "\\([ \t\n]*.\\)?[ \t\n]*"
"This looks hairy because it also matches an optional Symbolics
font-change escape sequence (the \"^F.\" in the middle).")
(defvar ilisp-whitespace+ "\\([ \t\n]+\\(.[ \t\n]*\\)?\\|.[ \t\n]+\\)"
"More font-change hairiness.")
(defvar ilisp-terminal-whitespace "[ \t\n();]"
"Regexp that matches 'terminal' whitespace (whatever might end a symbol).")
(defvar lisp-match-name "[^ \t\n();#]+"
"Regexp that matches a Lisp symbol (including any package name). This
is greatly oversimplified.")
(defvar lisp-optional-package-regexp
(concat "\\(" lisp-match-simple-name ":+\\)?")
"Regexp that matches an optional package prefix.")
(defvar lisp-ws-and-opt-pkg-regexp
(concat ilisp-whitespace+ lisp-optional-package-regexp)
"Regexp that matches one or more whitespace characters, followed by an
optional package prefix.")
(defvar lisp-match-package-prefix (concat "^" lisp-match-simple-name ":+")
"Note that this will not match the prefix in (e.g.) '(pcl::method foo ...)'.")
(defstruct ilisp-defn-spec
;; Structure that describes an entry in the *Edit-Definitions* buffer, which
;; is parsed and returned by the lisp-find-next-possibility fn. This is what
;; gets stored into the lisp-locate-definition variable (formerly, it was just
;; a cons of (name . type).
(name nil) ;; an ilisp-symbol
(name-string "") ;; string version of above
(type "function")
(file nil)
(parent nil)
(hints nil))
;; [find a better place for this? -- rgr, 25-Mar-03.]
(defun ilisp-getf (disembodied-plist symbol &optional default)
;; symbol is an emacs lisp symbol. This is case insensitive for
;; disembodied-plist indicators, to paper over emacs/CL case differences.
(let ((result default) (name (downcase (symbol-name symbol))))
(while disembodied-plist
(if (string-equal (downcase (symbol-name (car disembodied-plist))) name)
(setq result (car (cdr disembodied-plist))
disembodied-plist nil)
(setq disembodied-plist (cdr (cdr disembodied-plist)))))
result))
(defun lisp-read-cl-syntax (string)
;; Given a string containing zero or more forms in Common Lisp syntax, read
;; them into emacs lisp. [Unfortunately, the quoting we have to do here will
;; be duplicated by %S on re-output, so this doesn't win by itself. -- rgr,
;; 14-Sep-02.]
(with-current-buffer (get-buffer-create " *ilisp-cl-result*")
(erase-buffer)
(insert string)
(goto-char (point-min))
;; Scan the buffer for characters that need quoting. Everything within
;; quoted strings is fine, hence the in-quoted-string-p state maintenance.
(let ((in-quoted-string-p nil)
(uninteresting-chars "^\"\\#.?"))
(skip-chars-forward uninteresting-chars)
(while (not (eobp))
(let ((char (char-after (point))))
(cond ((= char ?\")
(setq in-quoted-string-p (not in-quoted-string-p)))
((= char ?\\)
;; We don't ever want to escape a backslash, and we don't even
;; want to think about the char after it.
(forward-char 1))
(in-quoted-string-p)
((= char ?\.)
;; Dots act as the "consing dot" in dotted-pair notation in
;; both CL and elisp, but Common Lisp allows them to be
;; constituent characters (no "\" needed). Embedded and
;; trailing dots in CL atoms used to be treated as consing
;; dots by the elisp reader, but I notice that FSF emacs 20.7
;; reads them compatibly. But leading dots are not
;; compatible; even 20.7 reads "(foo .bar)" as a dotted pair,
;; whereas CL does not, so we always make this correction
;; regardless of emacs version. Note that it relies on the
;; fact that CL always prints whitespace before and after a
;; consing dot; other possibilities handled by the CL reader
;; will break. -- rgr, 14-Sep-02.
(if (not (save-excursion
(skip-chars-backward " \t\n")
(looking-at "[ \t\n]+\\.[ \t\n]+")))
(insert "\\")))
(t
(insert "\\")))
(forward-char 1))
(skip-chars-forward uninteresting-chars)))
;; Now read and return what we quoted.
(let ((result nil))
(goto-char (point-min))
(condition-case error
(while t
(setq result (cons (read (current-buffer)) result)))
;; EOF could also be incomplete syntax, or some CL syntax we can't read.
(end-of-file nil))
;; Deal with NIL vs nil. Other symbol case issues need to be addressed by
;; the caller, but the empty list has to be the empty list.
(nreverse (nsubst nil 'NIL result)))))
;;; Source code definition line matching hacks.
;; ilisp-cl-source-locator-patterns --
;;
;; Note:
;;
;; 19990804 Marco Antoniotti
;; The new ones (method and generic-fucntion) should be carefully checked.
;;
;; [updated, made more concise. -- rgr, 7-Sep-02.]
(defvar ilisp-cl-source-locator-patterns
(let (;; <ws+> used to be
;; "\\([ \t\n]+\\(.\\)?[ \t\n]*\\|[ \t\n]*.[ \t\n]+\\)". --
;; rgr, 7-Sep-02.
(<ws+> ilisp-whitespace+)
;; this used to be "\\(.\\)?[ \t\n]*"; changed to look like
;; ilisp-whitespace*, but doesn't match newlines.
(<ws*> "\\([ \t]*.\\)?[ \t]*")
;; terminal whitespace (or whatever might end a symbol).
(<tws> ilisp-terminal-whitespace))
(list
(list 'variable (concat "^" <ws*>
"(def\\(var\\|parameter\\|constant\\)"
<ws+> "%s" <tws>))
(list 'structure (concat "^" <ws*> "(defstruct" <ws+>
"(?" <ws*> "%s" <tws>))
;; [this doesn't work by itself any more, because of the two %s
;; occurrences (see lisp-make-cl-setf-fspec-finder), but is kept
;; here for readability. -- rgr, 9-Sep-02.]
(list 'setf (concat "^" <ws*> "(def\\(setf" <ws+> "%s" <tws>
"\\|un" <ws+> "(setf" <ws+> "%s" <ws*>
")\\)"))
;; this describes the structure of the rest; it has an extra "%s" to
;; insert the name of the defining form.
(list '<standard> (concat "^" <ws*> "(%s" <ws+> "%s" <tws>))
'(function <standard> "\\(defun\\|defmacro\\)")
'(macro <standard> defmacro)
'(type <standard> deftype)
'(class <standard> defclass)
'(method <standard> defmethod)
'(generic-function <standard> defgeneric)))
"*Alist of (definition-name regexp-template), where definition-name is
an elisp symbol, and regexp-template is a format string that, given a
string that matches a symbol, returns a regexp that match that kind of
definitions of that symbol. If regexp-template is a symbol, then that
is an indirect reference to another named pattern (to save having to
look at too many nearly identical regexps). Anything after the
regexp-template is prefixed to the definition name in the format call.")
(defvar lisp-match-deffoo-template
;; originally "^[ \t\n]*(def[^ \t\n]*[ \t\n]+(?%s[ \t\n(]+"
(format (car (cdr (assoc '<standard> ilisp-cl-source-locator-patterns)))
(concat "def" lisp-match-name)
"(?%s")
"Generates a regexp that matches a line that starts with
'(def<something> (?name', with liberal whitespace allowance, when given
to format with a suitable name pattern.")
(defun lisp-make-standard-cl-defn-regexp (name-regexp type)
"Look for a named definition of the given type, assuming that it is
defined using the obvious Lisp syntax. Always searches for something,
falling back on '(<type> <name-regexp> ...)' if nothing specific is known about
the definition type. Use ilisp-cl-source-locator-patterns to customize
this."
(let* ((type (intern type))
(entry (assoc type ilisp-cl-source-locator-patterns))
(pattern (car (cdr entry)))
(extras (cdr (cdr entry))))
(cond ((null entry)
;; no specific pattern; try to match "(<type> <name-regexp> ...".
(setq pattern '<standard>)
(setq extras (list (lisp-make-gf-name-matcher type)))))
(cond ((symbolp pattern)
;; internal reference to another pattern template.
(setq entry (or (assoc pattern ilisp-cl-source-locator-patterns)
(error "Internal error: %S is missing %S."
'ilisp-cl-source-locator-patterns
pattern)))
(setq pattern (car (cdr entry)))))
(if extras
(apply (function format) pattern
(append extras (list name-regexp)))
(format pattern name-regexp))))
(defun lisp-make-whitespace-and-symbol-matcher (name)
;; notice that we carefully preserve the package prefix as specified if a
;; keyword, and allow any or no package prefix otherwise.
(let ((string (cond ((stringp name) name)
((symbolp name) (symbol-name name))
;; don't die horribly.
(t (format "%s" name)))))
(cond ((string-match "^:" string)
(concat "[ \t\n]*" (regexp-quote string)))
((string-match ":+" string)
(concat lisp-ws-and-opt-pkg-regexp
(substring string (match-end 0))))
(t
(concat lisp-ws-and-opt-pkg-regexp string)))))
(defvar lisp-conc-name-regexp
(concat "(:conc-name" ilisp-whitespace+ "\\(" lisp-match-name "\\)")
"Matches '(:conc-name <foo>' form, where <foo> is returned by the
third subexpressions.")
(defun lisp-find-accessor-in-random-defstruct (accessor-symbol)
;; look for a defstruct that has (a) a structure name or :conc-name attribute
;; compatible that matches some prefix of accessor-name, and (b) that also
;; contains a slot with the remainder of accessor-name. This has to be done
;; algorithmically, because returning a single regexp that could cover all the
;; possibilities would be horrendous. The basic premise is probably pretty
;; sound, but the regexps may be a bit sloppy in places.
(let* ((accessor-name (downcase (if (stringp accessor-symbol)
accessor-symbol
(lisp-symbol-name accessor-symbol))))
(template (car (cdr (assoc 'structure
ilisp-cl-source-locator-patterns))))
(pattern (if (string-match "%" template)
(substring template 0 (match-beginning 0))
(error "internal error")))
(found-p nil))
;; search for all defstructs, parsing out name & :conc-name.
(while (and (not found-p)
(not (eobp))
(re-search-forward pattern nil 'move))
(let* ((structure-start (match-beginning 0))
(structure-name (let ((start (point)))
(forward-sexp)
(buffer-substring-no-properties start (point))))
(structure-end (save-excursion
(end-of-defun-lisp)
(point)))
(conc-name-hit
(save-excursion
(and (re-search-forward lisp-conc-name-regexp structure-end t)
(lisp-strip-package-prefix-from-symbol
(downcase (match-string 3))))))
;; the prefix should be exactly what a compliant Common Lisp would
;; decide (ignoring case).
(prefix (cond ((null conc-name-hit)
;; no :conc-name (or :conc-name without an arg)
;; means use the structure name.
(concat (lisp-strip-package-prefix-from-symbol
structure-name)
"-"))
((string-equal conc-name-hit "nil")
;; "(:conc-name nil)" means do not prepend
;; anything, so the slot name is the accessor name.
nil)
(t conc-name-hit)))
(slot-name (cond ((null prefix)
accessor-name)
((and (> (length accessor-name) (length prefix))
(string-equal (substring accessor-name
0 (length prefix))
prefix))
(substring accessor-name (length prefix))))))
(setq found-p (and slot-name
(re-search-forward (regexp-quote slot-name)
structure-end t)))
'(message "Found %S (end %S, prefix %S) slot-name %S, found %S."
structure-name structure-end prefix slot-name found-p)))
(not (null found-p))))
;;; Making regexps that match specific defmethod forms.
(defun lisp-match-defmethod-specializer (name)
;; oops; name might actually be the list "(eql :foobar)" . . .
(let* ((whitespace "[ \t\n]")
(name (lisp-strip-package-prefix-from-symbol name))
(matcher (concat "(" whitespace "*" lisp-match-name
lisp-ws-and-opt-pkg-regexp (regexp-quote name)
whitespace "*)")))
(concat whitespace "*"
(if (member name '("T" "t"))
(concat "\\(" matcher "\\|" lisp-match-name "\\)")
matcher))))
(defun lisp-pop-trivial-method-specializers (specializer-list)
;; this is so we don't have to match "T T T)" at the end of the arglist for
;; unspecialized args. The list is understood to be reversed.
(cond ((null specializer-list) nil)
((member (car specializer-list) '(t T))
(lisp-pop-trivial-method-specializers (cdr specializer-list)))
(t
specializer-list)))
(defun lisp-make-gf-name-matcher (gf-name)
;; Make a regular expression that matches the generic function name ignoring
;; packages, also handling (setf foo) names. Does not match leading or
;; trailing whitespace.
(if (consp gf-name)
;; assumes a proper list.
(concat "(" ilisp-whitespace*
(lisp-make-gf-name-matcher (car gf-name))
(apply (function concat)
(mapcar (function (lambda (x)
(concat ilisp-whitespace+
(lisp-make-gf-name-matcher x))))
(cdr gf-name)))
ilisp-whitespace* ")")
(concat lisp-optional-package-regexp
(regexp-quote (lisp-strip-package-prefix-from-symbol gf-name)))))
(defun lisp-make-cl-method-definition-regexp (spec-string)
(let* ((tail (cdr (car (read-from-string spec-string))))
(gf-name (car tail))
(setf-function-p (and (consp gf-name)
(eq (car gf-name) 'setf)))
(gf-name-matcher (lisp-make-gf-name-matcher gf-name))
(rev-tail (reverse (cdr tail)))
(specializers (car rev-tail))
(nontrivial-specializers (reverse
(lisp-pop-trivial-method-specializers
(reverse specializers))))
(method-qualifiers (reverse (cdr rev-tail)))
(whitespace "[ \t\n]")
(defmethod-matcher
(apply (function concat)
"^(defmethod"
(if setf-function-p ilisp-whitespace* ilisp-whitespace+)
gf-name-matcher
(append (mapcar (function
lisp-make-whitespace-and-symbol-matcher)
method-qualifiers)
(list whitespace "*(")
(mapcar (function lisp-match-defmethod-specializer)
nontrivial-specializers)))))
'(message "[Got %S]" (list gf-name method-qualifiers
nontrivial-specializers))
(if (and (null method-qualifiers)
(= (length specializers)
(if setf-function-p 2 1)))
;; could also be an reader/writer/accessor generated by a defclass form.
;; [***bug***: this doesn't insist on a hit in the right defclass form.
;; more rigorous would be to do a lisp-find-accessor-in-random-defstruct
;; style search only when hints require it. -- rgr, 10-Sep-02.]
(concat defmethod-matcher "\\|"
"\\(:accessor\\|:writer\\|:reader\\)"
ilisp-whitespace+
(if setf-function-p
(lisp-make-gf-name-matcher (car (cdr gf-name)))
gf-name-matcher)
ilisp-terminal-whitespace)
;; just look for defmethod forms.
defmethod-matcher)))
;;; Visiting source file possibilities.
(defun lisp-make-cl-method-fspec-finder (definition)
(lisp-make-cl-method-definition-regexp
(ilisp-defn-spec-name-string definition)))
(defun lisp-make-cl-pcl::fast-method-fspec-finder (name)
;; hack for pcl::fast-method function specs.
(lisp-make-cl-method-fspec-finder name))
(defun lisp-make-cl-setf-fspec-finder (definition)
;; This also looks for defstruct accessors if given the right hint.
(let* ((setf-fn-spec (ilisp-defn-spec-name-string definition))
(function-spec (car (read-from-string setf-fn-spec)))
(symbol (car (cdr function-spec)))
(name-string (if (symbolp symbol)
(lisp-strip-package-prefix-from-symbol symbol)
(error "'%S' is a bad SETF function spec."
setf-fn-spec)))
(type (ilisp-defn-spec-type definition))
(parent (ilisp-defn-spec-parent definition)))
(cond ((and parent
(string-equal (car parent) "structure"))
(lisp-find-accessor-in-random-defstruct name-string))
((string-equal type "generic-function")
(lisp-make-standard-cl-defn-regexp
(lisp-make-gf-name-matcher function-spec)
type))
(t
;; defsetf or defun with (setf foo) spec.
(let ((name-regexp (concat lisp-optional-package-regexp
(regexp-quote name-string)))
(setf-entry (assoc 'setf ilisp-cl-source-locator-patterns)))
(lisp-re nil (car (cdr setf-entry))
name-regexp name-regexp))))))
;;; Common Lisp definition matching interface.
(defun lisp-cl-symbolic-name-regexp (definition)
;; helper for lisp-locate-clisp below; takes care of cases where "symbol" is
;; actually known to be Lisp symbol. we ignore its package, though.
(let* ((symbol (ilisp-defn-spec-name definition))
(type (ilisp-defn-spec-type definition))
(name (lisp-symbol-name symbol))
(name-regexp (concat lisp-optional-package-regexp
;; symbol-name of the read-from-string result
;; undoes lisp-read-cl-syntax quoting.
(regexp-quote
(symbol-name (car (read-from-string name))))))
(parent (ilisp-defn-spec-parent lisp-last-definition))
(hints (ilisp-defn-spec-hints lisp-last-definition)))
(cond ((or (not (equal type "function"))
(and (null parent) (null hints)))
;; all nonfunction cases (for which no hints are supported yet), or
;; a simple function case, named with a symbol either way. These
;; are totally trivial (we hope).
(lisp-make-standard-cl-defn-regexp name-regexp type))
;; Problem function (with an ordinary (symbolic) name).
((and parent
(string-equal (car parent) "structure"))
;; desperate heuristic #1: look inside all defstruct forms.
(lisp-find-accessor-in-random-defstruct symbol))
;; Problem case without any applicable hacks.
(t
(lisp-make-standard-cl-defn-regexp name-regexp type))
;; [move this to lisp-locate-clisp somewhere? -- rgr, 4-Oct-02.]
(t
(message "[Pulling out all the stops.]")
;; Search for a "(defsomething (?name" form. This is way too broad
;; to use in general, but things are pretty desperate at this point.
(format lisp-match-deffoo-template name-regexp)))))
(defun lisp-make-cl-definition-regexp (definition)
(let ((name (ilisp-defn-spec-name-string definition)))
(cond ((string-match "^(\\([a-zA-Z0-9-_:]+\\)[ \t\n]+" name)
;; 'interesting' function naming cases, including setf & method
;; function specs. we try to dispatch on the car of the list, which
;; may include a package prefix.
(let* ((spec-type (downcase (match-string 1 name)))
(finder (intern (concat "lisp-make-cl-" spec-type
"-fspec-finder"))))
(if (and (not (fboundp finder))
(string-match ":+" spec-type))
;; try without the package prefix.
(setq finder
(intern (concat "lisp-make-cl-"
(substring spec-type (match-end 0))
"-fspec-finder"))))
(cond ((fboundp finder)
(funcall finder lisp-last-definition))
(t
(message "Oops; don't know how to find %S %s definitions."
spec-type (ilisp-defn-spec-type definition))
(sit-for 1)
nil))))
(t
(lisp-cl-symbolic-name-regexp lisp-last-definition)))))
(defun lisp-locate-clisp (symbol type first-p back-p)
"Try to find SYMBOL's TYPE definition in the current Common Lisp
buffer. Return true if sucessful, and move point to the match; else
return nil and move point to the end of the buffer. lisp-locate-clisp
acts as the top-level classifier of Common Lisp definition types,
dispatching to other functions to implement the appropriate search
tactics for the definition type, and using any hints or parentage data
we may find in the lisp-locate-definition global.
[This was changed to interpret non-symbol definition names, use hint
and parent heuristics, and centralize the regexp search. Somewhat
kludgily, it requires that lisp-last-definition be set to the
ilisp-defn-spec structure for the passed symbol and type. -- rgr,
25-Mar-03.]
[The FIRST-P and BACK-P arguments are ignored, since they are now
always t and nil, respectively. lisp-locate-clisp is one of several
possible values returned by \(ilisp-value 'ilisp-locator), so we need to
preserve the protocol; eventually, this should be fixed. -- rgr,
4-Oct-02.]"
(if lisp-find-definition-verbose-p
(message "[doing %S.]"
(list 'lisp-locate-clisp symbol type first-p back-p)))
;; [should check for lisp-last-definition validity here. -- rgr, 4-Oct-02.]
(if (equal type "any")
;; [may have to support this, for non-Lisp search. -- rgr, 7-Sep-02.]
(error "shouldn't have been given type %S, for %S." type symbol))
(let* ((case-fold-search t)
(regexp (lisp-make-cl-definition-regexp lisp-last-definition)))
(cond ((symbolp regexp)
;; lisp-make-cl-definition-regexp already did the searching for us.
regexp)
((not (stringp regexp))
(error "bug: %S should have returned a regexp string."
'lisp-make-cl-definition-regexp))
((re-search-forward regexp nil t)
(goto-char (match-beginning 0))
t)
(t
;; [use fallback strategy? -- rgr, 4-Oct-02.]
nil))))
(defun lisp-set-up-for-search-in-new-file (file)
;; Go to the file's buffer, reading it if necessary (and possible), and move
;; to the start after setting lisp-last-file and lisp-last-point. Returns t
;; if successful, and returns nil and prints a message if not.
(let ((target-buffer (get-file-buffer file)))
(cond ((or target-buffer
(file-readable-p file))
(set-buffer (or target-buffer
(find-file-noselect file)))
(setq lisp-last-file (buffer-file-name))
(setq lisp-last-point (point))
(goto-char (point-min))
t)
(t
(message "File %S doesn't exist!" file)
(sit-for 1)
nil))))
(defun lisp-locate-definition-in-file (locator defn-spec &optional pop)
;; [may no longer do POP correctly. -- rgr, 4-Sep-02.]
"Use LOCATOR to find the next DEFN-SPEC (an ilisp-defn-spec instance).
Search starts at POINT, optionally BACKWARDS and POP to buffer. Return T
if successful."
(if lisp-find-definition-verbose-p
(message "[doing %S.]"
(list 'lisp-locate-definition-in-file locator defn-spec pop)))
;; set this whether we succeed or not, because it will be used to generate a
;; message in certain failure cases. [and, kludgily, we refer to it freely
;; in locator functions, which allows us to avoid patching all locators to
;; pass the defn-spec instead of symbol & type. yech. -- rgr, 7-Sep-02.]
(setq lisp-last-definition defn-spec)
(let* ((symbol (ilisp-defn-spec-name defn-spec))
(name (ilisp-defn-spec-name-string defn-spec))
(type (ilisp-defn-spec-type defn-spec))
;; [we need this because lisp-set-up-for-search-in-new-file does
;; set-buffer for us. -- rgr, 4-Oct-02.]
(old-buffer (current-buffer))
;; go to the right place in the file buffer (if we can).
(result (lisp-set-up-for-search-in-new-file
(ilisp-defn-spec-file defn-spec))))
(cond ((null result)
nil)
((progn
(message "Searching %s for %s %s" (buffer-file-name) type name)
(setq result (funcall locator symbol type t nil))
;; (message "[locator returned %S.]" result)
result)
;; found it.
;; [this message would be clobbered by the "Mark set" message
;; anyway. -- rgr, 4-Oct-02.]
;; (message "Found %s %s definition" type name)
(push-mark lisp-last-point)
(switch-to-buffer (current-buffer))
(save-excursion
(or (bolp)
(beginning-of-defun))
;; reposition the start of the definition at the top of the
;; screen, regardless of where point ended up.
;; [if there are comments above this definition, and the window is
;; already positioned to show them, then this hides the comments
;; again. we need a way of telling reposition-window-lisp never
;; to hide comments. -- rgr, 25-Mar-03.]
(reposition-window-lisp))
t)
(t
;; not found in this buffer. restore point in searched buffer,
;; before returning to the old buffer. [shouldn't this be in an
;; unwind-protect cleanup? -- rgr, 4-Oct-02.]
(goto-char lisp-last-point)
(set-buffer old-buffer)
nil))))
;;; Getting definition specifications out of the *Edit-Definitions* buffer.
(defun lisp-parse-parent-hint (line-end)
;; The line-end is the part after "is inside +", and may contain the name
;; and/or type of the parent definition. This parses what
;; lisp-insert-defn-inside-defn-comment generates.
(cond ((string-match "the definition of :?\\([^ ]+\\) +\\(.+\\)" line-end)
(let ((type (downcase (match-string 1 line-end)))
(fn-spec (match-string 2 line-end)))
(list type (lisp-string-to-symbol fn-spec))))
((string-match "a \\(.+\\) definition" line-end)
(list nil (lisp-string-to-symbol (match-string 1 line-end))))
((string-match "some ?:\\([^ ]+\\) definition" line-end)
(list (downcase (match-string 1 line-end)) nil))
((string-match "another definition" line-end)
(list nil nil))
(t
(message "Can't parse %S as a 'parent hint.'" line-end)
(sit-for 1)
;; return the result for "another definition", so we still turn on all
;; the extra heuristics.
(list nil nil))))
(defun lisp-find-next-possibility (back-p)
"Return the next source file location in *Edit-Definitions* as an
ilisp-defn-spec structure, or nil if none. Returns the previous if
back-p is non-nil."
;; [bug: back-p doesn't fully work. -- rgr, 15-Aug-02.]
(let ((file t)
(parent-hint nil)
(result nil)
(original-buffer (current-buffer)))
;; [can't use save-excursion because we have to update point in the
;; definitions buffer. -- rgr, 6-Aug-02.]
(if lisp-find-definition-verbose-p
(message "[lfnp %swards in %s:]"
(if back-p "back" "for") original-buffer))
(unwind-protect
(progn
(set-buffer (or (get-buffer "*Edit-Definitions*")
(error "Bug: No *Edit-Definitions* buffer.")))
(if back-p
(forward-line -1))
(while (not (or result
(if back-p (bobp) (eobp))))
(if back-p
(forward-line -1))
(cond ((looking-at "\n"))
((looking-at "^;+ *\\(.*\\)")
(cond ((not back-p)
(message "%s" (match-string 1))
(sit-for 1))))
((looking-at "^!+ *\\(.*\\)")
(cond (back-p
;; [***bug***: we don't find these right when
;; working backwards. -- rgr, 5-Sep-02.]
)
(t
(let ((match (match-string 1)))
(message "%s" match)
(sit-for 1)
;; if there are several nested parent hints, take
;; only the first, as that will be the outermost.
(if (null parent-hint)
(setq parent-hint
(lisp-parse-parent-hint match)))))))
((looking-at "^:?\\([^ ]+\\) +\\(.+\\) in file \\(.+\\)")
;; normal definition match.
(let ((type (downcase (match-string 1)))
(fn-spec (match-string 2))
(file (match-string 3)))
(setq result
(make-ilisp-defn-spec
:name (lisp-string-to-symbol fn-spec)
:name-string fn-spec
:type type :file file :parent parent-hint)))))
(if (not back-p)
(forward-line)))
(if back-p
(forward-line)))
(set-buffer original-buffer))
result))
(defun lisp-next-definition (&optional back pop quiet-p)
"Go to the next definition from *Edit-Definitions*. Movement is BACK
with prefix and POPping. Return 'first if found first time, 'none if no
definition ever, and nil if no more definitions are found. So the
normal result is a series of first's, followed by a nil, or a 'none if a
definition couldn't be found in source. [The return value is
semi-historical (and may be broken). lisp-next-definition used to
return t if another definition was found in the same file, but this is
no longer possible, as lisp-next-definition iterates over definitions
instead of files. -- rgr, 25-Mar-03.]"
;; find the next possibility, which specifies a file.
(let ((possibility (lisp-find-next-possibility back)))
(if lisp-find-definition-verbose-p
(message "%S returned %S." 'lisp-find-next-possibility possibility))
(let ((result (cond ((null possibility) nil)
((lisp-locate-definition-in-file
lisp-last-locator possibility
(prog1 pop (setq pop nil)))
'first)
(t 'none))))
;; Generate a message in the "no more" cases; if something had been found,
;; then lisp-locate-definition-in-file will have issued a message to that
;; effect for us.
(if (and (not quiet-p)
(member result '(nil none)))
;; the outer save-excursion restores the current buffer; the inner one
;; preserves the point in the "*Edit-Definitions*" buffer.
(save-excursion
(set-buffer "*Edit-Definitions*")
(save-excursion
(goto-char (point-min))
(let ((message (if (looking-at "^;+ +\\(any +\\)?\\(.+\\):")
(match-string 2)
"definitions")))
(if (null result)
(message "No more %s." message)
(message "Can't find %s." message))))))
(if lisp-find-definition-verbose-p
(message "%S returns %S." 'lisp-next-definition result))
result)))
(defun next-definition-lisp (back &optional pop)
"Edit the next definition from *Edit-Definitions*.
Movement is BACK with prefix and optionally POPping or call
'tags-loop-continue' if using tags."
(interactive "P")
(cond (lisp-using-tags
;; [i don't think this tells us whether we've succeeded or not. --
;; rgr, 31-Jul-02.]
(tags-loop-continue)
'maybe)
(lisp-search
(error "lisp-search is %S, but this feature is broken." lisp-search))
(t
(lisp-next-definition back pop))))
;;; Putting definition specifications in the *Edit-Definitions* buffer.
(defvar lisp-edit-definitions-mode-map
(let ((map (make-sparse-keymap)))
;; [these are ok in compilation-mode-map, but don't help here.]
;; (define-key map " " 'scroll-up)
;; (define-key map "\^?" 'scroll-down)
(define-key map "\r" 'lisp-this-definition)
;; This is for compatibility with ACL eli.
(define-key map "." 'lisp-this-definition)
map)
"Mode map for *Edit-Definitions* buffers.")
(defun lisp-edit-definitions-mode ()
"Major mode for Lisp definition buffers.
To visit the source for a given definition,
move point to the appropriate line and type \\[compile-goto-error].
Runs `lisp-edit-definitions-mode-hook' with `run-hooks' (which see)."
(interactive)
(kill-all-local-variables)
(use-local-map lisp-edit-definitions-mode-map)
(setq major-mode 'lisp-edit-definitions-mode
mode-name "Edit Definitions")
;; [this would be really nice . . . -- rgr, 12-Feb-03.]
'(set (make-local-variable 'revert-buffer-function)
'lisp-edit-definitions-revert-buffer)
(run-hooks 'lisp-edit-definitions-mode-hook))
(defun lisp-this-definition ()
(interactive)
"Visit the source of the definition named on this line."
(beginning-of-line)
(lisp-next-definition))
(defun fix-source-filenames ()
"Apply the 'ilisp-source-directory-fixup-alist' to the current buffer,
normally *Edit-Definitions*. The aim is to change any distributed
source-file locations to point to local source file locations.
See 'ilisp-source-directory-fixup-alist'."
;; [now supports more than one hit per regexp per buffer. -- rgr, 31-Jul-02.]
(let ((alist (ilisp-value 'ilisp-source-directory-fixup-alist t)))
(save-excursion
(while alist
(let* ((cons (car alist))
(pattern (car cons)) (replacement (cdr cons)))
(goto-char (point-min))
(while (re-search-forward pattern nil t)
(if (stringp replacement)
;; [this is an enhancement with respect to the
;; ilisp-source-directory-fixup-alist documentation string. --
;; rgr, 27-Aug-02.]
(replace-match replacement t nil)
(funcall replacement))))
(setq alist (cdr alist))))))
(defun lisp-handle-comment-message (comment-strings)
;; note that we lose if any of these strings have newlines.
(while comment-strings
(let ((thing (car comment-strings)))
(insert ";; "
(if (stringp thing) thing (format "%S" thing))
"\n"))
(setq comment-strings (cdr comment-strings))))
(defun lisp-definition-spec-type-name-string (defn-spec)
(let ((type-name (ilisp-getf (cdr defn-spec) ':type)))
(and type-name
(capitalize (symbol-name type-name)))))
(defun lisp-insert-defn-inside-defn-comment (child parent &optional is-inside)
;; helper for lisp-handle-definition-message, below.
(let* ((child-name (ilisp-getf (cdr child) ':name))
(child-type (lisp-definition-spec-type-name-string child))
(parent-name (ilisp-getf (cdr parent) ':name))
(parent-type (lisp-definition-spec-type-name-string parent))
(both-defined-p (and parent-name parent-type)))
(insert (if both-defined-p ";; " "!! ")
(or child-type "???") (format " %S " child-name)
(or is-inside "is inside") " "
(cond (both-defined-p
(format "the definition of %s %S"
parent-type parent-name))
(parent-name
(format "a %S definition" parent-name))
(parent-type
(format "some %s definition" parent-type))
(t
"another definition."))
"\n")
both-defined-p))
(defun lisp-insert-defn-files (defn-spec file-tail)
;; helper for lisp-handle-definition-message, below.
(let* ((name (ilisp-getf (cdr defn-spec) ':name))
(type-string (lisp-definition-spec-type-name-string defn-spec))
(preamble (format "%s %S" type-string name)))
(or (and name type-string)
;; [debugging hack. -- rgr, 4-Sep-02.]
(error "Malformed definition message %S." defn-spec))
(if file-tail
(while file-tail
(insert preamble " in file " (car file-tail) "\n")
(setq file-tail (cdr file-tail)))
(insert ";; " preamble
" has no known source files.\n"))))
(defun lisp-handle-definition-message (defn-spec)
;; Helper for lisp-set-up-definitions-buffer, below. If we find a parent
;; definition, then an "X inside of Y" comment is generated. [Bug: this
;; currently fails if definitions are nested more than two deep. -- rgr,
;; 5-Sep-02.] Note that if the parent definition has both type and name, then
;; we put *that* in the buffer, since the parent will be easier to find.
(let ((comment (ilisp-getf (cdr defn-spec) ':comment))
(parent (ilisp-getf (cdr defn-spec) ':parent)))
(if comment
(lisp-handle-comment-message (cdr comment)))
(let ((use-parent-instead-p
(and parent
(lisp-insert-defn-inside-defn-comment defn-spec parent))))
(lisp-insert-defn-files (if use-parent-instead-p
parent
defn-spec)
(or (if parent
;; presumably, these are more
;; authoritative.
(ilisp-getf (cdr parent) ':files))
(ilisp-getf (cdr defn-spec) ':files))))))
(defun lisp-set-up-definitions-buffer (initial-message definition-specs
&optional buffer-name)
"Set up the *Edit-Definitions* buffer with INITIAL-MESSAGE to edit
DEFINITION-SPECS, which must be a new-style list of :DEFINITION spec
items."
;; [for some odd reason, initializing lisp-last-locator is the caller's
;; responsibility. -- rgr, 4-Sep-02.]
(setq lisp-using-tags nil)
(setq lisp-last-file nil)
(with-current-buffer (get-buffer-create (or buffer-name "*Edit-Definitions*"))
(erase-buffer)
(lisp-edit-definitions-mode)
(and initial-message
;; [putting an extra semi on this may make it easier to identify. --
;; rgr, 4-Sep-02.]
(insert ";;; " initial-message "\n\n"))
(let ((tail definition-specs))
(while tail
(let* ((entry (car tail))
(message-type (car entry)) (message-body (cdr entry)))
;; canonicalize case.
(if (and message-type
(symbolp message-type))
(setq message-type
(intern (downcase (symbol-name message-type)))))
;; now try to decode it.
(cond ((eq message-type ':comment)
(lisp-handle-comment-message message-body))
((eq message-type ':definition)
(lisp-handle-definition-message entry))
(t
(insert ";; [oops; unknown message type "
(format "%S" message-type)
"]\n"))))
(setq tail (cdr tail))))
(fix-source-filenames)
(goto-char (point-min))
(and initial-message
(forward-line 2))
(set-buffer-modified-p nil)))
;;; Getting definition specifications back from the inferior Lisp.
(defvar lisp-inferior-source-definitions-cache nil
"internal cache used by lisp-find-inferior-source-definitions, below.")
(defun lisp-parse-and-cache-source-definitions (type symbol files
&optional no-cache-p)
;; handles conversion from the old list-of-pathnames format. helper for
;; lisp-find-inferior-source-definitions, below.
(let* ((read-result (lisp-read-cl-syntax files))
(file-specs (car read-result)))
(if (or (cdr read-result)
(stringp file-specs))
(let ((name (car (lisp-read-cl-syntax (lisp-buffer-symbol symbol)))))
;; old style file-name string(s); there may be more than one of these.
;; convert to new format.
(setq file-specs (list (list ':definition ':name name
':type (intern type)
':files read-result)))))
(or no-cache-p
(setq lisp-inferior-source-definitions-cache
(cons (cons symbol type) file-specs)))
file-specs))
(defun lisp-find-inferior-source-definitions (symbol type &optional no-cache-p)
;; Utility function for querying the Lisp about source information.
;; Returns nil for most error cases.
(let ((symbol-name (lisp-symbol-name symbol))
(command (ilisp-value 'ilisp-find-source-command t))
(source nil))
(cond ((and (not no-cache-p)
lisp-inferior-source-definitions-cache
(equal (car lisp-inferior-source-definitions-cache)
(cons symbol type)))
(cdr lisp-inferior-source-definitions-cache))
((null command)
nil)
((not (comint-check-proc ilisp-buffer))
(error "The inferior lisp in %s is no longer running."