Codebase list ilisp / upstream/5.12.0+cvs.2004.12.26 ilisp-src.el
upstream/5.12.0+cvs.2004.12.26

Tree @upstream/5.12.0+cvs.2004.12.26 (Download .tar.gz)

ilisp-src.el @upstream/5.12.0+cvs.2004.12.26raw · history · blame

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
;;; -*- 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.
;;;
;;; $Id: ilisp-src.el,v 1.13 2003/12/21 03:16:39 rgrjr Exp $

(require 'cl)

;;; 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.]
  (save-excursion
    (set-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)
  (save-excursion
    (set-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."
		   ilisp-buffer))
	  ((let ((command (format command symbol-name
				  (or (lisp-symbol-package symbol)
				      (lisp-buffer-package))
				  type)))
	     ;; (message "[sending %S.]" command)
	     (setq source
		   (ilisp-send
		     command
		     (concat "Finding " type " "
			     (lisp-buffer-symbol symbol) " definitions")
		     'source)))
	    (let* ((result (lisp-last-line source))
		   (return-value (and (stringp (car result))
				      (downcase (car result))))
		   (files (cdr result))
		   (source-ok (and (not (ilisp-value 'comint-errorp t))
				   (string-equal "t" return-value))))
	      (cond ((not source-ok)
		      ;; normally, this will be an error message.
		      (message "%s" return-value)
		      (sit-for 2)
		      nil)
		    (t
		      (lisp-parse-and-cache-source-definitions type symbol
							       files))))))))

;;; The edit-definitions-lisp command and friends.

(defun lisp-make-edit-files-definitions (type name edit-files)
  ;; Compatibility.
  (let ((files-origin "lisp-directory files"))
    (if (eq edit-files t)
	(let ((buffers (buffer-list)))
	  (setq edit-files nil)
	  (setq files-origin "all Lisp buffers")
	  (save-excursion 
	    (while buffers
	      (set-buffer (car buffers)) 
	      (if (and (memq major-mode lisp-source-modes)
		       (buffer-file-name))
		  (setq edit-files (cons (buffer-file-name) edit-files)))
	      (setq buffers (cdr buffers))))
	  (setq edit-files (nreverse edit-files))))
    (if (null edit-files)
	(error "No files to edit; see the lisp-directory command."))
    (list (list ':definition ':type (intern type) ':name (intern name)
		':comment (list (format "Searching through %s to find %s %S"
					files-origin type name))
		':files edit-files))))

(defun lisp-edit-definitions-normal (symbol type &optional stay locator)
  (let* ((file-specs (lisp-find-inferior-source-definitions symbol type))
	 ;; case-fold-search is always buffer-local when set, so binding this
	 ;; here may not mean much.  -- rgr, 14-Sep-02.
	 (case-fold-search t)
	 (try-tags-p lisp-fall-back-on-find-tags)
	 (found-p nil)
	 (name (lisp-buffer-symbol symbol)))
    ;; (message "[got files %S.]" file-specs)
    (cond (file-specs
	    ;; We've gotten something useful from the inferior lisp.
	    (setq lisp-last-locator (or locator (ilisp-value 'ilisp-locator)))
	    (lisp-set-up-definitions-buffer
	      (format "%s %s definitions in %s:" type name ilisp-buffer)
	      file-specs)
	    (setq found-p (next-definition-lisp nil t))))
    (cond ((and (not found-p)
		nil	;; [disabled until fixed.  -- rgr, 17-Dec-03.]
		lisp-edit-files)
	    ;; No luck from the inferior lisp; search the available buffers.
	    ;; [note that this does not work now, since it tries to pass type
	    ;; "any" to lisp-locate-clisp, which no longer accepts this.  --
	    ;; rgr, 14-Sep-02.]
	    (setq lisp-last-locator (or locator (ilisp-value 'ilisp-locator)))
	    (lisp-set-up-definitions-buffer
	      (format "%s %s definitions in %s:" type name 'lisp-edit-files)
	      (lisp-make-edit-files-definitions type name lisp-edit-files))
	    (setq found-p (next-definition-lisp nil t))))
    ;; Now check to see if tags searching is needed/wanted.
    (cond (found-p)
	  ((and (or tags-file-name tags-table-list)
		(if (eq try-tags-p ':ask)
		    (yes-or-no-p (format "Can't find %s %s; use tags? "
					 (or type "any definitions of")
					 name))
		    try-tags-p))
	    ;; Use tags.  This will generally lose on packages.
	    (let ((symbol-name (lisp-symbol-name symbol)))
	      (setq lisp-using-tags t)
	      (if (string-match "Lucid" emacs-version)
		  (find-tag symbol-name stay)
		  (find-tag symbol-name nil stay))))
	  (t
	    (message "Can't find %s %s."
		     (or type "any definitions of")
		     name)
	    nil))))

(defun lisp-find-inferior-definition-types (symbol)
  ;; Returns a list of unique definition types (as lowercase strings) known to
  ;; the inferior Lisp for symbol.
  (let ((tail (lisp-find-inferior-source-definitions symbol "any"))
	(type nil)
	(result nil))
    (while tail
      (let ((entry (car tail)))
	(if (and (consp entry)
		 (memq (car entry) '(:DEFINITION :definition))
		 (symbolp (setq type (ilisp-getf (cdr entry) ':type))))
	    (let ((type-string (downcase (symbol-name type))))
	      (if (string-match "^:" type-string)
		  (setq type-string (substring type-string 1)))
	      (or (member type-string result)
		  (setq result (cons type-string result))))))
      (setq tail (cdr tail)))
    (sort result (function string-lessp))))

(defun lisp-ask-for-definition-to-edit (current-prefix-arg)
  ;; Used in the edit-definitions-lisp 'interactive' form.  May also ask for a
  ;; definition type as well, if not one of the continuation cases, and if the
  ;; inferior lisp knows how to find more than one such definition (e.g. a
  ;; function with the same name as a global variable).
  (let* ((numeric-arg (and current-prefix-arg
			   (prefix-numeric-value current-prefix-arg)))
	 (function (lisp-function-name)))
    (cond ((or (null numeric-arg)
	       (not (get-buffer "*Edit-Definitions*")))
	    (let* ((defn-spec
		     (let ((result (ilisp-read
				     (format "Edit Definition [%s]: "
					     (lisp-buffer-symbol function)))))
		       (if (equal result "")
			   function
			   (lisp-string-to-symbol result))))
		   (name (lisp-symbol-name defn-spec))
		   (possible-types
		     (cond ((string-match "[()]" name)
			     ;; compound definition names happen only for
			     ;; functions, so there is no point in asking.
			     ;; [***bug***: this is not true in general for
			     ;; user-defined types.  -- rgr, 15-Aug-02.]
			     ;; besides, it is not helpful to offer a choice
			     ;; between "generic-function" and "function" when
			     ;; the user gives a GF name.
			     '("function"))
			   ((not (comint-check-proc ilisp-buffer))
			     ;; oops; this probably won't do us much good . . .
			     (mapcar (function car)
				     (ilisp-value 'ilisp-source-types t)))
			   (t
			     (lisp-find-inferior-definition-types defn-spec)))))
	      (list defn-spec
		    (if (cdr possible-types)
			(ilisp-completing-read
			  "Type [any]: "
			  (cons '("any")
				(mapcar (function list) possible-types))
			  "any")
			;; [specifying "any" uses the cache.  -- rgr, 4-Sep-02.]
			;; (car possible-types)
                      (if (ilisp-value 'ilisp-find-source-command t)
                          ;; inferior lisp will tell us
                          "any"
                        (let ((types (ilisp-value 'ilisp-source-types t)))
                          (ilisp-completing-read
                           (format "Type [%s]: " (caar types))
                           types (caar types))))))))
	  ;; Non-interactive cases (reusing the last definition name).
	  ((zerop numeric-arg)
	    (list nil 'visit))
	  ((minusp numeric-arg)
	    ;; continue looking backward
	    (list nil 'back)) 
	  (t
	    ;; continue looking forward
	    (list nil nil)))))

(defun edit-definitions-lisp (definition-name
			      &optional type stay search locator)
  "Visit all definitions of DEFINITION-NAME of type TYPE.  The
DEFINITION-NAME is normally a symbol, but in Common Lisp can also be a
list like \"(SETF FOO)\" or \"(method frob (random-thing))\".  The type
defaults to \"any\", which really means to look for *all* definitions of
DEFINITION-NAME.  If specified interactively, emacs prompts for a type
only if the Lisp knows about more than one definition type \(but the
default is still \"any\"\).

   When invoked interactively with a numeric arg, \\[edit-definitions-lisp]
continues looking for the last definition specified: a positive arg
means forward, while a negative arg means backward.  Possible locations
for the last requested definition are kept in the *Edit-Definitions*
buffer; forward and backward means relative to point in this buffer, and
a zero arg means to switch to this buffer.  If you want to jump to an
arbitrary possibility, move the point just before it and do
\"\\[universal-argument] \\[edit-definitions-lisp]\".  [bug: the
keybinding is not available unless you switch back to an ilisp buffer
first.  -- rgr, 4-Sep-02.]

   If the optional STAY argument is true, put the source file in the
same window.  If SEARCH, do not look for DEFINITION-NAME in the inferior
Lisp.  The optional LOCATOR can be a function of args \(definition-name
type first-p back-p\); normally this is specified by the inferior Lisp,
which knows the syntax of its source files.

   In order to find the definition, we look for it in the following way:

   0.  If SEARCH is true, we do the equivalent of \\[tags-search] and
skip the other steps.  [This is really a distinct protocol, and perhaps
ought to be made separate.  -- rgr, 31-Jul-02.]

   1.  First we ask the inferior Lisp if it knows about the definition's
source file.  If it does, then we look for the definition in that
file(s), using ilisp's built-in definition-finding heuristics.

   2.  If the Lisp doesn't know (or we were told not to ask), and
lisp-edit-files is not nil, we search the files in lisp-edit-files,
possibly set up by the \\[lisp-directory] command, or the buffers in one
of lisp-source-modes if lisp-edit-files is T.
[Bug:  This doesn't work.  -- rgr, 17-Dec-03.]

   3.  If still not found, and at least one tag table is defined, we may
use \\[find-tag] to keep searching.  This is controlled by the
lisp-fall-back-on-find-tags variable \(q.v.), which defaults to :ask."
  (interactive (lisp-ask-for-definition-to-edit current-prefix-arg))
  (or type
      (setq type "any"))
  (and (stringp definition-name)
       (setq definition-name (lisp-string-to-symbol definition-name)))
  (cond (search 
	  ;; Search through all files listed in tags table.  [really, this is a
	  ;; different protocol; should be handled by another command.  -- rgr,
	  ;; 31-Jul-02.]
	  (let ((case-fold-search t))
	    (setq tags-loop-scan (list locator
				       (list 'quote definition-name) 
				       type t nil))
	    (setq tags-loop-operate nil)
	    (setq lisp-using-tags t)
	    (tags-loop-continue t)))
	(definition-name
	  (lisp-edit-definitions-normal definition-name type stay locator))
	((eq type 'visit)
	  (switch-to-buffer "*Edit-Definitions*"))
	(t
	  ;; continuing to edit the same thing.
	  (next-definition-lisp (eq type 'back)))))

;;;; End of new M-. code.

;; [some of the rest of this could profit from better integration with the new
;; M-. infrastructure.  in particular, search-lisp and replace-lisp use the
;; *Edit-Definitions* buffer incompatibly.  -- rgr, 25-Mar-03.]

;;;%%Utilities

(defun lisp-setup-edit-definitions (message edit-files)
  "Set up *Edit-Definitions* with MESSAGE.
If EDIT-FILES is T, insert all buffer filenames that are in one of
lisp-source-modes into the current buffer.  If it is a list of files
set up by lisp-directory, insert those in the buffer.  If it is a
string put that in the buffer."

  ;; Note
  ;; 19990804 Marco Antoniotti
  ;; Are we sure we want to set 'lisp-using-tags' to nil?
  (setq lisp-using-tags nil
	lisp-search (not (stringp edit-files)))
  (set-buffer (get-buffer-create "*Edit-Definitions*"))
  (erase-buffer)
  (insert message)
  (insert "\n\n")
  (if edit-files
      (progn
	(if (eq edit-files t)
	    (let ((buffers (buffer-list)))
	      (while buffers
		(let ((buffer (car buffers)))
		  (if (save-excursion 
			(set-buffer buffer) 
			(and (memq major-mode lisp-source-modes)
			     (buffer-file-name buffer)))
		      (progn (insert ?\") (insert (buffer-file-name buffer))
			     (insert "\"\n"))))
		(setq buffers (cdr buffers))))
	    (if (stringp edit-files)
		(progn (insert edit-files)
		       	;; Remove garbage collection messages
		       (replace-regexp "^;[^\n]*\n" "")
		       (fix-source-filenames))
		(let ((files edit-files))
		  (while files
		    (insert ?\")
		    (insert (car files))
		    (insert "\"\n")
		    (setq files (cdr files))))))
	(goto-char (point-min))
	(forward-line 2)
	(set-buffer-modified-p nil))
      (error 
       (substitute-command-keys
	"Use \\[lisp-directory] to define source files."))))
	  
;;;
(defun lisp-next-file (back)
  "Return the next filename in *Edit-Definitions*, or nil if none."
  (let ((file t) 
	result)
    (set-buffer (get-buffer-create "*Edit-Definitions*"))
    (if back 
	(progn (forward-line -1)
	       (if (looking-at "\n")
		   (progn 
		     (forward-line 1)
		     (end-of-line)
		     (setq file nil)))))
  (if file
      (progn
	(skip-chars-forward "^\"")
	(if (eobp)
	    (progn (bury-buffer (current-buffer))
		   (setq result nil))
	    (let* ((start (progn (forward-char 1) (point))))
	      (skip-chars-forward "^\"") 
	      (setq file
		    (prog1 (buffer-substring-no-properties start (point))
		      (end-of-line)))
	      (bury-buffer (current-buffer))))))
  (if (not (eq file 't)) file)))

;;;%%Searching
(defun lisp-locate-search (pattern type first back)
  "Find PATTERN in the current buffer."
  (if back
      (search-backward pattern nil t)
      (search-forward pattern nil t)))

;;;
(defun lisp-locate-regexp (regexp type first back)
  "Find REGEXP in the current buffer."
  (if back
      (re-search-backward regexp nil t)
      (re-search-forward regexp nil t)))

;;;

(defvar lisp-last-pattern nil "Last search regexp.")

(defun search-lisp (pattern regexp)
  "Search for PATTERN through the files or buffers.
Search for file in 'lisp-edit-files' if it is a list or the
current buffers in one of 'lisp-source-modes' otherwise.  If
lisp-edit-files is nil, no search will be done.  If called with a
prefix, search for regexp.  If there is a tags file, call 'tags-search'
instead."
  (interactive
   (list (read-string (if current-prefix-arg 
			  "Search for regexp: "
			  "Search for: ") lisp-last-pattern)
	 current-prefix-arg))
  (if tags-file-name
      (progn (setq lisp-using-tags t)
	     (tags-search (if regexp pattern (regexp-quote pattern))))
      (setq lisp-last-pattern pattern
	    lisp-last-definition (cons pattern t)
	    lisp-last-file nil
	    lisp-last-locator (if regexp
				  'lisp-locate-regexp
				  'lisp-locate-search))
      (lisp-setup-edit-definitions (format "Searching for %s:" pattern) 
				   lisp-edit-files)
      (next-definition-lisp nil nil)))

;;;%%Replacing
(defvar lisp-last-replace nil "Last replace regexp.")

(defun replace-lisp (old new regexp)
  "Query replace OLD by NEW through the files or the current buffers.
The query is done in 'lisp-edit-files' if it is a list and the current
buffers in one of 'lisp-source-modes' otherwise.  If 'lisp-edit-files'
is NIL, no search will be done.  If called with a prefix, replace
regexps.  If there is a tags file, then call tags-query-replace
instead."
  (interactive
   (let ((old (read-string (if current-prefix-arg
			       "Replace regexp: "
			       "Replace: ") lisp-last-pattern)))
     (list old
	   (read-string (if current-prefix-arg
			    (format "Replace regexp %s by: " old)
			    (format "Replace %s by: " old))
			lisp-last-replace)
	   current-prefix-arg)))
  (cond (tags-file-name
	 (setq lisp-using-tags t)
	 (tags-query-replace (if regexp old (regexp-quote old))
			     new))
	(t
	 (setq lisp-last-pattern old
	       lisp-last-replace new)
	 (lisp-setup-edit-definitions 
	  (format "Replacing %s by %s:\n\n" old new)
	  lisp-edit-files)
	 (let ((file nil))
	   (while (setq file (lisp-next-file nil))
	     (lisp-find-file file)
	     (let ((point (point)))
	       (goto-char (point-min))
	       (if (if regexp 
		       (re-search-forward old nil t)
		       (search-forward old nil t))
		   (progn (beginning-of-line)
			  (if regexp
			      (query-replace-regexp old new)
			      (query-replace old new)))
		   (goto-char point))))))))

;;;%%Edit-callers
(defvar lisp-callers nil 
  "T if we found callers through inferior LISP.")

;;;
(defun who-calls-lisp (function &optional no-show)
  "Put the functions that call FUNCTION into the buffer *All-Callers*.
Show the buffer *All-Callers* unless NO-SHOW is T.  Return T if successful."
  (interactive 
   (let* ((function (lisp-defun-name))
	  (symbol (lisp-buffer-symbol function)))
     (if (lisp-minus-prefix)
	 (list function)
	 (list (ilisp-read-symbol 
		(format "Who Calls [%s]: " symbol)
		function
		t t)))))
  (let* ((name (lisp-buffer-symbol function))
	 (command (ilisp-value 'ilisp-callers-command t))
	 (callers
	  (if command
	      (ilisp-send
	       (format command
		       (lisp-symbol-name function)
		       (lisp-symbol-package function))
	       (concat "Finding callers of " name)
	       'callers)))
	 (last-line (if callers (lisp-last-line callers)))
	 (case-fold-search t))
    (set-buffer (get-buffer-create "*All-Callers*"))
    (erase-buffer)
    (insert (format "All callers of function %s:\n\n" name))
    (if (and command (not (ilisp-value 'comint-errorp t)))
	(if (string-match "nil" (car last-line))
	    (error "%s has no callers" name)
	    (message "")
	    (insert (cdr last-line))
	    (goto-char (point-min))
	    ;; Remove garbage collection messages
	    (replace-regexp "^;[^\n]*\n" "")
	    (goto-char (point-min))
	    (forward-line 2)
	    (if (not no-show) 
		(if (ilisp-temp-buffer-show-function)
		    (funcall (ilisp-temp-buffer-show-function)
			     (get-buffer "*All-Callers*"))
		    (view-buffer "*All-Callers*")))
	    t)
	(insert "Using the current source files to find callers.")
	nil)))

;;;
(defun next-caller-lisp (back &optional pop)
  "Edit the next caller from *All-Callers*.
With prefix, edit the previous caller.  If it can't get caller
information from the inferior LISP, this will search using the current
source files.  See lisp-directory."

  (interactive "P")
  (if (not lisp-callers)
      (next-definition-lisp back pop)
      (set-buffer (get-buffer-create "*All-Callers*"))
      (if back (forward-line -1))
      (skip-chars-forward " \t\n")
      (if (eobp)
	  (progn
	    (bury-buffer (current-buffer))
	    (error "No more callers"))
	  (let* ((start (point))
		 (caller-function
		  (progn
		    (skip-chars-forward "^ \t\n")
		    (buffer-substring-no-properties start (point)))))
	    (bury-buffer (current-buffer))
	    (edit-definitions-lisp (lisp-string-to-symbol caller-function) 
				  (car (car (ilisp-value 'ilisp-source-types)))
				  (not pop))))))

;;;
(defun edit-callers-lisp (function)
  "Edit the callers of FUNCTION.
With a minus prefix use the symbol at the start of the current defun."
  (interactive
   (let* ((function (lisp-defun-name)))
     (if (lisp-minus-prefix)
	 (list function)
	 (list (ilisp-read-symbol 
		(format "Edit callers of [%s]: "
			(lisp-buffer-symbol function))
		function
		t)))))
  (if (save-excursion (setq lisp-callers (who-calls-lisp function t)))
      (progn 
	(setq lisp-last-locator (ilisp-value 'ilisp-calls-locator))
	(next-caller-lisp nil t))
      (edit-definitions-lisp function "calls" nil t 
			    (ilisp-value 'ilisp-calls-locator))))

;;;%Locators
(defun lisp-re (back format &rest args)
  "Search BACK if T using FORMAT applied to ARGS."
  (let ((regexp (apply 'format format args)))
    (if back
	(re-search-backward regexp nil t)
	(re-search-forward regexp nil t))))

;;;
(defun lisp-locate-ilisp (symbol type first back)
  "Find SYMBOL's TYPE definition in the current file.  Return T if successful.
A definition is of the form

    (def<something><whitespace>(?name<whitespace>

This is used as the default value of ilisp-locator; the specific dialect
will usually override this with something more clever."
  (lisp-re back
	   "^[ \t\n]*(def[^ \t\n]*[ \t\n]+(?%s[ \t\n(]+" 
	   (regexp-quote (lisp-symbol-name symbol))))

;;;
(defun lisp-locate-calls (symbol type first back)
  "Locate calls to SYMBOL."
  (lisp-re back "\\(#'\\|(\\|'\\)%s\\([ \t\n]+\\|)\\)"
	   (regexp-quote (lisp-buffer-symbol symbol))))


;;;%% Locators for Scheme

;;; Matthias Koeppe <mail.math.uni-magdeburg.de>
;;;
;;; The standard locators would fail on "(define (thunk) ....)"  and
;;; report "(define (procedure ...) ....)" as a call to procedure.

(defun ilisp-locate-scheme-definition (symbol type first back)
  "Find SYMBOL's TYPE definition in the current file. Return T if successful.
This is the Scheme counterpart of `lisp-locate-clisp'."
  (lisp-re back
	   "[ \t\n]*(def[^ \t\n]*[ \t\n]+(*%s\[ \t\n()]"
	   (regexp-quote (lisp-symbol-name symbol))))

(defun ilisp-locate-scheme-calls (symbol type first back)
  "Locate calls to SYMBOL.
This is the Scheme counterpart of `lisp-locate-calls'."
  (let ((call-regexp 
	 (format "[( \t\n]+%s[ \t\n()]+"
		 (regexp-quote 
		  ;; Scheme has no package prefixes, so we use
		  ;; lisp-symbol-name instead of lisp-buffer-symbol.
		  (lisp-symbol-name symbol))))
	(def-regexp "[ \t\n]*(def[^ \t\n]*[ \t\n]+(*")
	(result 'unknown))
    (while (eq result 'unknown)
      (cond 
       ((if back
	    (re-search-backward call-regexp nil t)
	  (re-search-forward call-regexp nil t))
	(if (not (save-excursion	; check whether definition
		   (goto-char (match-beginning 0))
		   (backward-sexp) (backward-char)
		   (looking-at def-regexp)))
	    (setq result t)))
       (t (setq result nil))))
    result))	    


;;; end of file -- ilisp-src.el --