forked from jdtsmith/idlwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidlw-routine.el
1480 lines (1355 loc) · 51.6 KB
/
idlw-routine.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
;; IDLWAVE Routine Information code and variables
;; The list format for all routine info user catalog, library
;; catalogs, etc.:
;;
;; ("ROUTINE" type class
;; (system nil nil nil) | (lib pro_file dir "LIBNAME") | (user pro_file dir "USERLIB") |
;; (buffer pro_file dir) | (compiled pro_file dir)
;; "calling_string" ("LINKFILE" (("KWD1" . anchorlink1) ...))
;; ("LINKFILE2" (("KWD2" . ancorlink2) ...)) ...)
;;
;; DIR will be supplied dynamically while loading library catalogs,
;; and is sinterned (hashed to an internal symbol) to save space, as
;; is LIBNAME. PRO_FILE can be a complete filepath, in which case DIR
;; is unnecessary. HELPFILE can be nil, as can LINKFILE, etc., if no
;; HTML help is available for that routine. Since keywords can be
;; referenced in multiples files (e.g. Graphics Keywords), there are
;; multiple keyword link lists.
;;----------------------------------------------------
;; Convenience Routines for routine info lists
(defun idlwave-routine-routine-name (x)
(car x))
(defun idlwave-routine-class-name (x)
(nth 2 x))
(defun idlwave-routine-first-link-file (x)
(car (nth 5 x)))
;;----------------------------------------------------
;; Routine Info
(defun idlwave-routine-info (&optional arg external)
"Display a routine's calling sequence and list of keywords.
When point is on the name a function or procedure, or in the argument
list of a function or procedure, this command displays a help buffer with
the information. When called with prefix arg, enforce class query.
When point is on an object operator `->', display the class stored in
this arrow, if any (see `idlwave-store-inquired-class'). With a prefix
arg, the class property is cleared out."
(interactive "P")
(idlwave-routines)
(if (string-match "->" (buffer-substring
(max (point-min) (1- (point)))
(min (+ 2 (point)) (point-max))))
;; Cursor is on an arrow
(if (get-text-property (point) 'idlwave-class)
;; arrow has class property
(if arg
;; Remove property
(save-excursion
(backward-char 1)
(when (looking-at ".?\\(->\\)")
(remove-text-properties (match-beginning 1) (match-end 1)
'(idlwave-class nil face nil))
(message "Class property removed from arrow")))
;; Echo class property
(message "Arrow has text property identifying object to be class %s"
(get-text-property (point) 'idlwave-class)))
;; No property found
(message "Arrow has no class text property"))
;; Not on an arrow...
(let* ((idlwave-query-class nil)
(idlwave-force-class-query (equal arg '(4)))
(module (idlwave-what-module)))
(if (car module)
(apply 'idlwave-display-calling-sequence
(idlwave-fix-module-if-obj_new module))
(error "Don't know which calling sequence to show")))))
;;----------------------------------------------------
;; Selecting/matching routines
(defun idlwave-rinfo-assoc (name type class list)
"Like `idlwave-rinfo-assq', but sintern strings first."
(idlwave-rinfo-assq
(idlwave-sintern-routine-or-method name class)
type (idlwave-sintern-class class) list))
(defun idlwave-rinfo-assq (name type class list)
;; Works like assq, but also checks type and class
(catch 'exit
(let (match)
(while (setq match (assq name list))
(and (or (eq type t)
(eq (nth 1 match) type))
(eq (nth 2 match) class)
(throw 'exit match))
(setq list (cdr (memq match list)))))))
(defun idlwave-best-rinfo-assq (name type class list &optional with-file
keep-system)
"Like `idlwave-rinfo-assq', but get all twins and sort, then return first.
If WITH-FILE is passed, find the best rinfo entry with a file
included. If KEEP-SYSTEM is set, don't prune system for compiled
syslib files."
(let ((twins (idlwave-routine-twins
(idlwave-rinfo-assq-any-class name type class list)
list))
syslibp)
(when (> (length twins) 1)
(setq twins (sort twins 'idlwave-routine-entry-compare-twins))
(if (and (null keep-system)
(eq 'system (car (nth 3 (car twins))))
(setq syslibp (idlwave-any-syslib (cdr twins)))
(not (equal 1 syslibp)))
;; Its a compiled syslib, so we need to remove the system entry
(setq twins (cdr twins)))
(if with-file
(setq twins (delq nil
(mapcar (lambda (x)
(if (nth 1 (nth 3 x)) x))
twins)))))
(car twins)))
(defun idlwave-best-rinfo-assoc (name type class list &optional with-file
keep-system)
"Like `idlwave-best-rinfo-assq', but sintern strings first."
(idlwave-best-rinfo-assq
(idlwave-sintern-routine-or-method name class)
type (idlwave-sintern-class class) list with-file keep-system))
(defun idlwave-rinfo-assq-any-class (name type class list)
;; Return the first matching method on the inheritance list
(let* ((classes (cons class (idlwave-all-class-inherits class)))
class rtn)
(while classes
(if (setq rtn (idlwave-rinfo-assq name type (pop classes) list))
(setq classes nil)))
rtn))
;;----------------------------------------------------
;; Routine Shadows
;; Routine shadows aka twins: same routine name, different routines on
;; path (or in IDL distributed system)
(defun idlwave-routine-twins (entry &optional list)
"Return all twin entries of ENTRY in LIST.
LIST defaults to `idlwave-routines'.
Twin entries are those which have the same name, type, and class.
ENTRY will also be returned, as the first item of this list."
(let* ((name (car entry))
(type (nth 1 entry))
(class (nth 2 entry))
(candidates (idlwave-all-assq name (or list (idlwave-routines))))
twins candidate)
(while (setq candidate (pop candidates))
(if (and (not (eq candidate entry))
(eq type (nth 1 candidate))
(eq class (nth 2 candidate)))
(push candidate twins)))
(if (setq candidate (idlwave-rinfo-assq name type class
idlwave-unresolved-routines))
(push candidate twins))
(cons entry (nreverse twins))))
;; Bound in idlwave-study-twins,idlwave-routine-entry-compare-twins.
(defvar idlwave-class)
(defun idlwave-study-twins (entries)
"Return dangerous twins of first entry in ENTRIES.
Dangerous twins are routines with same name, but in different files on
the load path. If a file is in the system library and has an entry in
the `idlwave-system-routines' list, we omit the latter as
non-dangerous because many IDL routines are implemented as library
routines, and may have been scanned."
(let* ((entry (car entries))
(name (car entry)) ;
(type (nth 1 entry)) ; Must be bound for
(idlwave-class (nth 2 entry)) ; idlwave-routine-twin-compare
(cnt 0)
source type type-cons file alist syslibp key)
(while (setq entry (pop entries))
(incf cnt)
(setq source (nth 3 entry)
type (car source)
type-cons (cons type (nth 3 source))
file (idlwave-routine-source-file source))
;; Make KEY to index entry properly
(setq key (cond ((eq type 'system) type)
(file (file-truename file))
(t 'unresolved)))
;; Check for an entry in the system library
(if (and file
(not syslibp)
(idlwave-syslib-p file))
(setq syslibp t))
;; If there's more than one matching entry for the same file, just
;; append the type-cons to the type list.
(if (setq entry (assoc key alist))
(push type-cons (nth 2 entry))
(push (list key file (list type-cons)) alist)))
(setq alist (nreverse alist))
(when syslibp
;; File is in system *library* - remove any 'system entry
(setq alist (delq (assq 'system alist) alist)))
;; If 'system remains and we've scanned the syslib, it's a builtin
;; (rather than a !DIR/lib/.pro file bundled as source).
(when (and (idlwave-syslib-scanned-p)
(setq entry (assoc 'system alist)))
(setcar entry 'builtin))
(sort alist 'idlwave-routine-twin-compare)))
(defun idlwave-routine-entry-compare (a b)
"Compare two routine info entries for sorting.
This is the general case. It first compares class, names, and type.
If it turns out that A and B are twins (same name, class, and type),
calls another routine which compares twins on the basis of their file
names and path locations."
(let ((name (car a)) (type (nth 1 a)) (class (nth 2 a)))
(cond
((not (equal (idlwave-downcase-safe class)
(idlwave-downcase-safe (nth 2 b))))
;; Class decides
(cond ((null (nth 2 b)) nil)
((null class) t)
(t (string< (downcase class) (downcase (nth 2 b))))))
((not (equal (downcase name) (downcase (car b))))
;; Name decides
(string< (downcase name) (downcase (car b))))
((not (eq type (nth 1 b)))
;; Type decides
(< (if (eq type 'fun) 1 0) (if (eq (nth 1 b) 'fun) 1 0)))
(t
;; A and B are twins - so the decision is more complicated.
;; Call twin-compare with the proper arguments.
(idlwave-routine-entry-compare-twins a b)))))
(defun idlwave-routine-entry-compare-twins (a b)
"Compare two routine entries, under the assumption that they are twins.
This basically calls `idlwave-routine-twin-compare' with the correct args."
(let* ((name (car a))
(type (nth 1 a))
(idlwave-class (nth 2 a)) ; needed outside
(asrc (nth 3 a))
(atype (car asrc))
(bsrc (nth 3 b))
(btype (car bsrc))
(afile (idlwave-routine-source-file asrc))
(bfile (idlwave-routine-source-file bsrc)))
(idlwave-routine-twin-compare
(if (stringp afile)
(list (file-truename afile) afile (list atype))
(list atype afile (list atype)))
(if (stringp bfile)
(list (file-truename bfile) bfile (list btype))
(list btype bfile (list btype))))
))
(defun idlwave-routine-twin-compare (a b)
"Compare two routine twin entries for sorting.
In here, A and B are not normal routine info entries, but special
lists (KEY FILENAME (TYPES...)).
This expects NAME TYPE CLASS to be bound to the right values."
(let* (;; Dis-assemble entries
(akey (car a)) (bkey (car b))
(afile (nth 1 a)) (bfile (nth 1 b))
(atypes (nth 2 a)) (btypes (nth 2 b))
;; System routines?
(asysp (memq akey '(builtin system)))
(bsysp (memq bkey '(builtin system)))
;; Compiled routines?
(acompp (memq 'compiled atypes))
(bcompp (memq 'compiled btypes))
;; Unresolved?
(aunresp (or (eq akey 'unresolved)
(and acompp (not afile))))
(bunresp (or (eq bkey 'unresolved)
(and bcompp (not bfile))))
;; Buffer info available?
(abufp (memq 'buffer atypes))
(bbufp (memq 'buffer btypes))
;; On search path?
(tpath-alist (idlwave-true-path-alist))
(apathp (and (stringp akey)
(assoc (file-name-directory akey) tpath-alist)))
(bpathp (and (stringp bkey)
(assoc (file-name-directory bkey) tpath-alist)))
;; How early on search path? High number means early since we
;; measure the tail of the path list
(anpath (length (memq apathp tpath-alist)))
(bnpath (length (memq bpathp tpath-alist)))
;; Look at file names
(aname (if (stringp afile) (downcase (file-name-nondirectory afile)) ""))
(bname (if (stringp bfile) (downcase (file-name-nondirectory bfile)) ""))
(fname-re (if idlwave-class (format "\\`%s__\\(%s\\|define\\)\\.pro\\'"
(regexp-quote
(downcase idlwave-class))
(regexp-quote (downcase name)))
(format "\\`%s\\.pro" (regexp-quote (downcase name)))))
;; Is file name derived from the routine name?
;; Method file or class definition file?
(anamep (string-match fname-re aname))
(adefp (and idlwave-class anamep
(string= "define" (match-string 1 aname))))
(bnamep (string-match fname-re bname))
(bdefp (and idlwave-class bnamep
(string= "define" (match-string 1 bname)))))
;; Now: follow JD's ideas about sorting. Looks really simple now,
;; doesn't it? The difficult stuff is hidden above...
(cond
((idlwave-xor asysp bsysp) asysp) ; System entries first
((idlwave-xor aunresp bunresp) bunresp) ; Unresolved last
((and idlwave-sort-prefer-buffer-info
(idlwave-xor abufp bbufp)) abufp) ; Buffers before non-buffers
((idlwave-xor acompp bcompp) acompp) ; Compiled entries
((idlwave-xor apathp bpathp) apathp) ; Library before non-library
((idlwave-xor anamep bnamep) anamep) ; Correct file names first
((and idlwave-class anamep bnamep ; both file names match ->
(idlwave-xor adefp bdefp)) bdefp) ; __define after __method
((> anpath bnpath) t) ; Who is first on path?
(t nil)))) ; Default
(defun idlwave-list-buffer-load-path-shadows (&optional arg)
"List the load path shadows of all routines defined in current buffer."
(interactive "P")
(idlwave-routines)
(if (eq major-mode 'idlwave-mode)
(idlwave-list-load-path-shadows
nil (idlwave-update-current-buffer-info 'save-buffer)
"in current buffer")
(error "Current buffer is not in idlwave-mode")))
(defun idlwave-list-shell-load-path-shadows (&optional arg)
"List the load path shadows of all routines compiled under the shell.
This is very useful for checking an IDL application. Just compile the
application, do RESOLVE_ALL, and `C-c C-i' to compile all referenced
routines and update IDLWAVE internal info. Then check for shadowing
with this command."
(interactive "P")
(cond
((or (not (fboundp 'idlwave-shell-is-running))
(not (idlwave-shell-is-running)))
(error "Shell is not running"))
((null idlwave-compiled-routines)
(error "No compiled routines. Maybe you need to update with `C-c C-i'"))
(t
(idlwave-list-load-path-shadows nil idlwave-compiled-routines
"in the shell"))))
(defun idlwave-list-all-load-path-shadows (&optional arg)
"List the load path shadows of all routines known to IDLWAVE."
(interactive "P")
(idlwave-list-load-path-shadows nil nil "globally"))
(defvar idlwave-sort-prefer-buffer-info t
"Internal variable used to influence `idlwave-routine-twin-compare'.")
(defun idlwave-list-load-path-shadows (arg &optional special-routines loc)
"List the routines which are defined multiple times.
Search the information IDLWAVE has about IDL routines for multiple
definitions.
When SPECIAL-ROUTINES in non-nil, only look for shadows of these routines.
When IDL hits a routine call which is not defined, it will search on
the load path in order to find a definition. The output of this command
can be used to detect possible name clashes during this process."
(idlwave-routines) ; Make sure everything is loaded.
(unless (or idlwave-user-catalog-routines idlwave-library-catalog-routines)
(or (y-or-n-p
"You don't have any user or library catalogs. Continue anyway? ")
(error "Abort")))
(let* ((routines (append idlwave-system-routines
idlwave-compiled-routines
idlwave-library-catalog-routines
idlwave-user-catalog-routines
idlwave-buffer-routines
nil))
(km-prop (if (featurep 'xemacs) 'keymap 'local-map))
(keymap (make-sparse-keymap))
(props (list 'mouse-face 'highlight
km-prop keymap
'help-echo "Mouse2: Find source"))
(nroutines (length (or special-routines routines)))
(step (max 1 (/ nroutines 100)))
(n 0)
(cnt 0)
(idlwave-sort-prefer-buffer-info nil)
routine twins dtwins twin done props1 lroutines)
(if special-routines
;; Just looking for shadows of a few special routines
(setq lroutines routines
routines special-routines))
(message "Sorting routines...")
(setq routines (sort routines
(lambda (a b)
(string< (downcase (idlwave-make-full-name
(nth 2 a) (car a)))
(downcase (idlwave-make-full-name
(nth 2 b) (car b)))))))
(message "Sorting routines...done")
(define-key keymap (if (featurep 'xemacs) [(button2)] [(mouse-2)])
(lambda (ev)
(interactive "e")
(mouse-set-point ev)
(apply 'idlwave-do-find-module
(get-text-property (point) 'find-args))))
(define-key keymap [(return)]
(lambda ()
(interactive)
(apply 'idlwave-do-find-module
(get-text-property (point) 'find-args))))
(message "Compiling list...( 0%%)")
(with-current-buffer (get-buffer-create "*Shadows*")
(setq buffer-read-only nil)
(erase-buffer)
(while (setq routine (pop routines))
(if (= (mod (setq n (1+ n)) step) 0)
(message "Compiling list...(%2d%%)" (/ (* n 100) nroutines)))
;; Get a list of all twins
(setq twins (idlwave-routine-twins routine (or lroutines routines)))
(if (memq routine done)
(setq dtwins nil)
(setq dtwins (idlwave-study-twins twins)))
;; Mark all twins as dealt with
(setq done (append twins done))
(when (or (> (length dtwins) 1)
(> (idlwave-count-memq 'lib (nth 2 (car dtwins))) 1)
(> (idlwave-count-memq 'user (nth 2 (car dtwins))) 1)
(> (idlwave-count-memq 'buffer (nth 2 (car dtwins))) 1))
(incf cnt)
(insert (format "\n%s%s"
(idlwave-make-full-name (nth 2 routine)
(car routine))
(if (eq (nth 1 routine) 'fun) "()" "")))
(while (setq twin (pop dtwins))
(setq props1 (append (list 'find-args
(list (nth 0 routine)
(nth 1 routine)
(nth 2 routine)))
props))
(idlwave-insert-source-location "\n - " twin props1))))
(goto-char (point-min))
(setq buffer-read-only t))
(setq loc (or loc ""))
(if (> cnt 0)
(progn
(display-buffer (get-buffer "*Shadows*"))
(message "%d case%s of shadowing found %s"
cnt (if (= cnt 1) "" "s") loc))
(message "No shadowing conflicts found %s" loc))))
;;----------------------------------------------------
;; Routine data structure tools
(defun idlwave-routine-source-file (source)
(if (nth 2 source)
(expand-file-name (nth 1 source) (nth 2 source))
(nth 1 source)))
(defun idlwave-any-syslib (entries)
"Does the entry list ENTRIES contain a syslib entry?
If yes, return the index (>=1)."
(let (file (cnt 0))
(catch 'exit
(while entries
(incf cnt)
(setq file (idlwave-routine-source-file (nth 3 (car entries))))
(if (and file (idlwave-syslib-p file))
(throw 'exit cnt)
(setq entries (cdr entries))))
nil)))
(defun idlwave-all-method-classes (method &optional type)
"Return all classes which have a method METHOD.
TYPE is 'fun or 'pro.
When TYPE is not specified, both procedures and functions will be considered."
(if (null method)
(mapcar 'car (idlwave-class-alist))
(let (rtn)
(mapc (lambda (x)
(and (nth 2 x)
(or (not type)
(eq type (nth 1 x)))
(push (nth 2 x) rtn)))
(idlwave-all-assq method (idlwave-routines)))
(idlwave-uniquify rtn))))
(defun idlwave-all-method-keyword-classes (method keyword &optional type)
"Return all classes which have a method METHOD with keyword KEYWORD.
TYPE is 'fun or 'pro.
When TYPE is not specified, both procedures and functions will be considered."
(if (or (null method)
(null keyword))
nil
(let (rtn)
(mapc (lambda (x)
(and (nth 2 x) ; non-nil class
(or (not type) ; correct or unspecified type
(eq type (nth 1 x)))
(assoc keyword (idlwave-entry-keywords x))
(push (nth 2 x) rtn)))
(idlwave-all-assq method (idlwave-routines)))
(idlwave-uniquify rtn))))
(defun idlwave-make-full-name (class &optional name)
(when (and (listp class) (not (null class)))
;; a routine info or idlwave-what-module entry
(setq name (car class)
class (nth 2 class)))
;; Make a fully qualified module name including the class name
(concat (if class (format "%s::" class) "") name))
(defun idlwave-determine-class (cw-list type)
;; Determine the class of a routine call. CW-LIST is the `cw-list'
;; structure as returned by idlwave-where. The second element in
;; this structure is the class. When nil, we return nil. When t,
;; try to get the class from text properties at the method call
;; arrow. When the object is "self", we use the class of the
;; current (enclosing) routine. otherwise prompt the user for a
;; class name. Also stores the selected class as a text property at
;; the arrow. TYPE is 'fun or 'pro.
(let* ((class (nth 2 cw-list))
(apos (nth 3 cw-list))
(nassoc (assoc (if (stringp (car cw-list))
(upcase (car cw-list))
(car cw-list))
idlwave-query-class))
(dassoc (assq (if (car cw-list) 'keyword-default 'method-default)
idlwave-query-class))
(query (cond (nassoc (cdr nassoc))
(dassoc (cdr dassoc))
(t t)))
(arrow (and apos (string= (buffer-substring apos (+ 2 apos)) "->")))
(is-self
(and arrow
(save-excursion (goto-char apos)
(forward-word -1)
(let ((case-fold-search t))
(looking-at "self\\>")))))
(force-query idlwave-force-class-query)
store special-class class-alist)
(cond
((null class) nil)
((eq t class)
;; There is an object which would like to know its class
(if (and arrow (get-text-property apos 'idlwave-class)
idlwave-store-inquired-class
(not force-query))
(setq class (get-text-property apos 'idlwave-class)
class (idlwave-sintern-class class)))
(if (and (eq t class) is-self)
(setq class (or (nth 2 (idlwave-current-routine)) class)))
;; Before prompting, try any special class determination routines
(when (and (eq t class)
idlwave-determine-class-special
(not force-query))
(setq special-class
(idlwave-call-special idlwave-determine-class-special apos))
(if special-class
(setq class (idlwave-sintern-class special-class)
store idlwave-store-inquired-class)))
;; Prompt for a class, if we need to
(when (and (eq class t)
(or force-query query))
(setq class-alist
(mapcar 'list (idlwave-all-method-classes (car cw-list) type)))
(setq class
(idlwave-sintern-class
(cond
((and (= (length class-alist) 0) (not force-query))
(error "No classes available with method %s" (car cw-list)))
((and (= (length class-alist) 1) (not force-query))
(car (car class-alist)))
(t
(setq store idlwave-store-inquired-class)
(idlwave-completing-read
(format "Class%s: " (if (stringp (car cw-list))
(format " for %s method %s"
type (car cw-list))
""))
class-alist nil nil nil 'idlwave-class-history))))))
;; Store it, if requested
(when (and class (not (eq t class)))
;; We have a real class here
(when (and store arrow)
(condition-case ()
(add-text-properties
apos (+ apos 2)
`(idlwave-class ,class face ,idlwave-class-arrow-face
rear-nonsticky t))
(error nil)))
(setf (nth 2 cw-list) class))
;; Return the class
class)
;; Default as fallback
(t class))))
;;----------------------------------------------------
;; Context (buffer-local)
(defvar idlwave-determine-class-special nil
"List of special functions for determining class.
Must accept two arguments: `apos' and `info'.")
(defun idlwave-where ()
"Find out where we are.
The return value is a list with the following stuff:
\(PRO-LIST FUNC-LIST COMPLETE-WHAT CW-LIST LAST-CHAR)
PRO-LIST (PRO POINT CLASS ARROW)
FUNC-LIST (FUNC POINT CLASS ARROW)
COMPLETE-WHAT a symbol indicating what kind of completion makes sense here
CW-LIST (PRO-OR-FUNC POINT CLASS ARROW) Like PRO-LIST, for what can
be completed here.
LAST-CHAR last relevant character before point (non-white non-comment,
not part of current identifier or leading slash).
In the lists, we have these meanings:
PRO: Procedure name
FUNC: Function name
POINT: Where is this
CLASS: What class has the routine (nil=no, t=is method, but class unknown)
ARROW: Location of the arrow for method calls"
(idlwave-routines)
(let* (;(bos (save-excursion (idlwave-beginning-of-statement) (point)))
(bos (save-excursion (idlwave-start-of-substatement 'pre) (point)))
(func-entry (idlwave-what-function bos))
(func (car func-entry))
(func-class (nth 1 func-entry))
(func-arrow (nth 2 func-entry))
(func-point (or (nth 3 func-entry) 0))
(func-level (or (nth 4 func-entry) 0))
(pro-entry (idlwave-what-procedure bos))
(pro (car pro-entry))
(pro-class (nth 1 pro-entry))
(pro-arrow (nth 2 pro-entry))
(pro-point (or (nth 3 pro-entry) 0))
(last-char (idlwave-last-valid-char))
(case-fold-search t)
(match-string (buffer-substring bos (point)))
cw cw-mod cw-arrow cw-class cw-point)
(if (< func-point pro-point) (setq func nil))
(cond
;; Class name in routine definition
((string-match "\\`[ \t]*\\(pro\\|function\\)[ \t]+[a-zA-Z0-9_]*\\'"
match-string)
(setq cw 'class))
;; Don't complete pro/function
((string-match "\\`[ \t]*\\(pro\\|function\\)\\>"
match-string)
nil)
;; Procedure call
((string-match
"\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)?\\'"
(if (> pro-point 0)
(buffer-substring pro-point (point))
match-string))
(setq cw 'procedure
cw-class pro-class
cw-point pro-point
cw-arrow pro-arrow))
;; Complete class inside obj_new statement
((string-match "OBJ_NEW([ \t]*['\"]\\([a-zA-Z0-9$_]*\\)?\\'"
match-string)
(setq cw 'class))
;; Or in an INHERITS statement
((string-match "\\<inherits\\s-+\\([a-zA-Z0-9$_]*\\)?\\'"
match-string)
(setq cw 'class))
;; Function keyword inside function
((and func
(> func-point pro-point)
(= func-level 1)
(memq last-char '(?\( ?,)))
(setq cw 'function-keyword
cw-mod func
cw-point func-point
cw-class func-class
cw-arrow func-arrow))
;; Procedure keyword otherwise
((and pro (eq last-char ?,))
(setq cw 'procedure-keyword
cw-mod pro
cw-point pro-point
cw-class pro-class
cw-arrow pro-arrow))
; ((member last-char '(?\' ?\) ?\] ?!))
; ;; after these chars, a function makes no sense
; ;; FIXME: I am sure there can be more in this list
; ;; FIXME: Do we want to do this at all?
; nil)
;; Everywhere else we try a function.
(t
(setq cw 'function)
(save-excursion
(if (re-search-backward "->[ \t]*\\(\\$[ \t]*\\(;.*\\)?\n\\s-*\\)?\\(\\([$a-zA-Z0-9_]+\\)::\\)?[$a-zA-Z0-9_]*\\=" bos t)
(setq cw-arrow (copy-marker (match-beginning 0))
cw-class (if (match-end 4)
(idlwave-sintern-class (match-string 4))
t))))))
(list (list pro pro-point pro-class pro-arrow)
(list func func-point func-class func-arrow)
cw
(list cw-mod cw-point cw-class cw-arrow)
last-char)))
(defun idlwave-what-function (&optional bound)
;; Find out if point is within the argument list of a function.
;; The return value is ("function-name" class arrow-start (point) level).
;; Level is 1 on the top level parentheses, higher further down.
;; If the optional BOUND is an integer, bound backwards directed
;; searches to this point.
(catch 'exit
(let (pos
func-point
(cnt 0)
func arrow-start class)
(idlwave-with-special-syntax
(save-restriction
(save-excursion
(narrow-to-region (max 1 (or bound 0)) (point-max))
;; move back out of the current parenthesis
(while (condition-case nil
(progn (up-list -1) t)
(error nil))
(setq pos (point))
(incf cnt)
(when (and (= (following-char) ?\()
(re-search-backward
"\\(::\\|\\<\\)\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\="
bound t))
(setq func (match-string 2)
func-point (goto-char (match-beginning 2))
pos func-point)
(if (re-search-backward
"->[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\=" bound t)
(setq arrow-start (copy-marker (match-beginning 0))
class (or (match-string 2) t)))
(throw
'exit
(list
(idlwave-sintern-routine-or-method func class)
(idlwave-sintern-class class)
arrow-start func-point cnt)))
(goto-char pos))
(throw 'exit nil)))))))
(defun idlwave-what-procedure (&optional bound)
;; Find out if point is within the argument list of a procedure.
;; The return value is ("procedure-name" class arrow-pos (point)).
;; If the optional BOUND is an integer, bound backwards directed
;; searches to this point.
(let ((pos (point)) pro-point
pro class arrow-start string)
(save-excursion
;;(idlwave-beginning-of-statement)
(idlwave-start-of-substatement 'pre)
(setq string (buffer-substring (point) pos))
(if (string-match
"\\`[ \t]*\\([a-zA-Z][a-zA-Z0-9$_]*\\)[ \t]*\\(,\\|\\'\\)" string)
(setq pro (match-string 1 string)
pro-point (+ (point) (match-beginning 1)))
(if (and (idlwave-skip-object)
(setq string (buffer-substring (point) pos))
(string-match
"\\`[ \t]*\\(->\\)[ \t]*\\(\\([a-zA-Z][a-zA-Z0-9$_]*\\)::\\)?\\([a-zA-Z][a-zA-Z0-9$_]*\\)?[ \t]*\\(,\\|\\(\\$\\s *\\(;.*\\)?\\)?$\\)"
string))
(setq pro (if (match-beginning 4)
(match-string 4 string))
pro-point (if (match-beginning 4)
(+ (point) (match-beginning 4))
pos)
arrow-start (copy-marker (+ (point) (match-beginning 1)))
class (or (match-string 3 string) t)))))
(list (idlwave-sintern-routine-or-method pro class)
(idlwave-sintern-class class)
arrow-start
pro-point)))
(defun idlwave-skip-object ()
;; If there is an object at point, move over it and return t.
(let ((pos (point)))
(if (catch 'exit
(save-excursion
(skip-chars-forward " ") ; white space
(skip-chars-forward "*") ; de-reference
(cond
((looking-at idlwave-identifier)
(goto-char (match-end 0)))
((eq (following-char) ?\()
nil)
(t (throw 'exit nil)))
(catch 'endwhile
(while t
(cond ((eq (following-char) ?.)
(forward-char 1)
(if (not (looking-at idlwave-identifier))
(throw 'exit nil))
(goto-char (match-end 0)))
((memq (following-char) '(?\( ?\[))
(condition-case nil
(forward-list 1)
(error (throw 'exit nil))))
(t (throw 'endwhile t)))))
(if (looking-at "[ \t]*->")
(throw 'exit (setq pos (match-beginning 0)))
(throw 'exit nil))))
(goto-char pos)
nil)))
(defun idlwave-last-valid-char ()
"Return the last character before point which is not white or a comment
and also not part of the current identifier. Since we do this in
order to identify places where keywords are, we consider the initial
`/' of a keyword as part of the identifier.
This function is not general, can only be used for completion stuff."
(catch 'exit
(save-excursion
;; skip the current identifier
(skip-chars-backward "a-zA-Z0-9_$")
;; also skip a leading slash which might be belong to the keyword
(if (eq (preceding-char) ?/)
(backward-char 1))
;; FIXME: does not check if this is a valid identifier
(while t
(skip-chars-backward " \t")
(cond
((memq (preceding-char) '(?\; ?\$)) (throw 'exit nil))
((eq (preceding-char) ?\n)
(beginning-of-line 0)
(if (looking-at "\\([^\n]*\\)\\$[ \t]*\\(;[^\n]*\\)?\n")
;; continuation line
(goto-char (match-end 1))
(throw 'exit nil)))
(t (throw 'exit (preceding-char))))))))
(defun idlwave-this-word (&optional class)
;; Grab the word around point. CLASS is for the `skip-chars=...' functions
(setq class (or class "a-zA-Z0-9$_."))
(save-excursion
(buffer-substring
(progn (skip-chars-backward class) (point))
(progn (skip-chars-forward class) (point)))))
(defun idlwave-what-module ()
"Return a default module for stuff near point.
Used by `idlwave-routine-info' and `idlwave-find-module'.
A module specification has the simple format (name 'type class)"
(idlwave-routines)
(if (let ((case-fold-search t))
(save-excursion
(idlwave-beginning-of-statement)
(looking-at "[ \t]*\\(pro\\|function\\)[ \t]+\\(\\([a-zA-Z0-9_$]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)\\([, \t\n]\\|$\\)")))
;; This is a function or procedure definition statement
;; We return the defined routine as module.
(list
(idlwave-sintern-routine-or-method (match-string-no-properties 4)
(match-string-no-properties 2))
(if (equal (downcase (match-string 1)) "pro") 'pro 'fun)
(idlwave-sintern-class (match-string 3)))
;; Not a definition statement - analyze precise position.
(let* ((where (idlwave-where))
(cw (nth 2 where))
(pro (car (nth 0 where)))
(func (car (nth 1 where)))
(this-word (idlwave-this-word "a-zA-Z0-9$_"))
(next-char (save-excursion (skip-chars-forward "a-zA-Z0-9$_")
(following-char)))
)
(cond
((and (eq cw 'procedure)
(not (equal this-word "")))
(setq this-word (idlwave-sintern-routine-or-method
this-word (nth 2 (nth 3 where))))
(list this-word 'pro
(idlwave-determine-class
(cons this-word (cdr (nth 3 where)))
'pro)))
((and (eq cw 'function)
(not (equal this-word ""))
(or (eq next-char ?\() ; exclude arrays, vars.
(looking-at "[a-zA-Z0-9_]*[ \t]*(")))
(setq this-word (idlwave-sintern-routine-or-method
this-word (nth 2 (nth 3 where))))
(list this-word 'fun
(idlwave-determine-class
(cons this-word (cdr (nth 3 where)))
'fun)))
((and (memq cw '(function-keyword procedure-keyword))
(not (equal this-word ""))
(eq next-char ?\()) ; A function!
(setq this-word (idlwave-sintern-routine this-word))
(list this-word 'fun nil))
(func
(list func 'fun (idlwave-determine-class (nth 1 where) 'fun)))
(pro
(list pro 'pro (idlwave-determine-class (nth 0 where) 'pro)))
(t nil)))))
(defun idlwave-what-module-find-class ()
"Call `idlwave-what-module' and find the inherited class if necessary."
(let* ((module (idlwave-what-module))
(class (nth 2 module)))
(if (and (= (length module) 3)
(stringp class))
(list (car module)
(nth 1 module)
(apply 'idlwave-find-inherited-class module))
module)))
(defun idlwave-find-inherited-class (name type class)
"Find the class which defines TYPE NAME and is CLASS or inherited by CLASS."
(let ((entry (idlwave-best-rinfo-assoc name type class (idlwave-routines))))
(if entry
(nth 2 entry)
class)))
(defun idlwave-fix-module-if-obj_new (module)
"Check if MODULE points to obj_new.
If yes, and if the cursor is in the keyword region, change to the
appropriate Init method."
(let* ((name (car module))
(pos (point))
(case-fold-search t)
string)
(if (and (stringp name)
(equal (downcase name) "obj_new")
(save-excursion
(idlwave-beginning-of-statement)
(setq string (buffer-substring (point) pos))
(string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
string)))
(let ((name "Init")
(class (match-string 1 string)))
(setq module (list (idlwave-sintern-method "Init")
'fun
(idlwave-sintern-class class)))))
module))
(defun idlwave-fix-keywords (name type class keywords
&optional super-classes system)
"Update a list of keywords.
Translate OBJ_NEW, adding all super-class keywords, or all keywords
from all classes if CLASS equals t. If SYSTEM is non-nil, don't
demand _EXTRA in the keyword list."
(let ((case-fold-search t))
;; If this is the OBJ_NEW function, try to figure out the class and use
;; the keywords from the corresponding INIT method.
(if (and (equal (upcase name) "OBJ_NEW")
(or (eq major-mode 'idlwave-mode)
(eq major-mode 'idlwave-shell-mode)))
(let* ((bos (save-excursion (idlwave-beginning-of-statement) (point)))
(string (buffer-substring bos (point)))
(case-fold-search t)
class)
(and (string-match "obj_new([^'\"]*['\"]\\([a-zA-Z0-9_]+\\)"
string)
(setq class (idlwave-sintern-class (match-string 1 string)))
(setq idlwave-current-obj_new-class class)
(setq keywords
(append keywords
(idlwave-entry-keywords
(idlwave-rinfo-assq
(idlwave-sintern-method "INIT")
'fun
class
(idlwave-routines)) 'do-link))))))