forked from jdtsmith/idlwave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idlw-scan.el
1757 lines (1607 loc) · 62.9 KB
/
idlw-scan.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: scan routine information provided with IDL, and among the
;; user's library, as well as open buffers
(require 'timer)
;; idlwave-routines format (whether system, library, or userlib)
;; ("ROUTINE" type class
;; (system) | (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) ...)) ...)
;;----------------------------------------------------
;; Storage variables
(defvar idlwave-routines nil
"Holds the combined procedure/function/method routine-info from all sources.")
(defvar idlwave-system-routines nil
"Holds the system routine-info obtained from the XML catalog.")
(defvar idlwave-buffer-routines nil
"Holds the routine-info obtained by scanning buffers.")
(defvar idlwave-compiled-routines nil
"Holds the routine-info obtained by asking the shell.")
(defvar idlwave-unresolved-routines nil
"Holds the unresolved routine-info obtained by asking the shell.")
(defvar idlwave-user-catalog-routines nil
"Holds the routine-info from the user scan.")
(defvar idlwave-library-catalog-routines nil
"Holds the routine-info from the .idlwave_catalog library files.")
(defvar idlwave-class-alist nil
"Holds the class names and brief info known to IDLWAVE.")
(defvar idlwave-class-info nil)
(defvar idlwave-library-catalog-libname nil
"Name of library catalog loaded from .idlwave_catalog files.")
(defvar idlwave-path-alist nil
"Alist with !PATH directories and zero or more flags if the dir has
been scanned in a user catalog ('user) or discovered in a library
catalog \('lib).")
(defvar idlwave-true-path-alist nil
"Like `idlwave-path-alist', but with true filenames.")
(defvar idlwave-class-history nil
"The history of classes selected with the minibuffer.")
(defvar idlwave-last-system-routine-info-cons-cell nil
"The last cons cell in the system routine info.")
;; XML information from idl_catalog.xml
(defvar idlwave-xml-routine-info-file nil)
(defvar idlwave-system-class-info nil)
(defvar idlwave-system-variables-alist nil
"Alist of system variables and the associated structure tags.
Gets set in cached XML rinfo.")
(defvar idlwave-executive-commands-alist nil
"Alist of system variables and their help files.")
(defvar idlwave-help-special-topic-words nil)
(defvar idlwave-catalog-process nil
"The background process currently updating the catalog.")
;; These are located in the idlwave-config-directory
(defvar idlwave-user-catalog-file "idlusercat.el")
(defvar idlwave-xml-system-rinfo-converted-file "idl_xml_rinfo.el")
(defvar idlwave-path-file "idlpath.el")
;;----------------------------------------------------
;; Global routine info inquiry/scanning
(defun idlwave-routines ()
"Provide a list of IDL routines.
This routine loads the builtin routines on the first call.
Later it only returns the value of the variable."
(if (and idlwave-catalog-process
(processp idlwave-catalog-process))
(progn
(cond
((equal (process-status idlwave-catalog-process) 'exit)
(message "updating........")
(setq idlwave-catalog-process nil)
(idlwave-update-routine-info '(4)))
((equal (process-status idlwave-catalog-process) 'run)
;; Keep it running...
)
(t
;; Something is wrong, get rid of the process
(message "Problem with catalog process") (beep)
(condition-case nil
(kill-process idlwave-catalog-process)
(error nil))
(setq idlwave-catalog-process nil)))))
(or idlwave-routines
(progn
(idlwave-update-routine-info)
;; return the current value
idlwave-routines)))
(defun idlwave-load-rinfo-next-step ()
(let ((inhibit-quit t)
(arr idlwave-load-rinfo-steps-done))
(if (catch 'exit
(when (not (aref arr 0))
(message "Loading system routine info in idle time...")
(idlwave-load-system-routine-info)
(message "Loading system routine info in idle time...done")
(aset arr 0 t)
(throw 'exit t))
(when (not (aref arr 1))
(message "Normalizing idlwave-system-routines in idle time...")
(idlwave-reset-sintern t)
(put 'idlwave-reset-sintern 'done-by-idle t)
(setq idlwave-system-routines
(idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
(message "Normalizing idlwave-system-routines in idle time...done")
(aset arr 1 t)
(throw 'exit t))
(when (not (aref arr 2))
(when (and (stringp idlwave-user-catalog-file)
(file-regular-p idlwave-user-catalog-file))
(message "Loading user catalog in idle time...")
(condition-case nil
(load-file idlwave-user-catalog-file)
(error (throw 'exit nil)))
;; Check for the old style catalog and warn
(if (and
(boundp 'idlwave-library-routines)
idlwave-library-routines)
(progn
(setq idlwave-library-routines nil)
(ding)
(message "Outdated user catalog: %s... recreate"
idlwave-user-catalog-file))
(message "Loading user catalog in idle time...done")))
(aset arr 2 t)
(throw 'exit t))
(when (not (aref arr 3))
(when idlwave-user-catalog-routines
(message "Normalizing user catalog routines in idle time...")
(setq idlwave-user-catalog-routines
(idlwave-sintern-rinfo-list
idlwave-user-catalog-routines 'sys))
(message
"Normalizing user catalog routines in idle time...done"))
(aset arr 3 t)
(throw 'exit t))
(when (not (aref arr 4))
(idlwave-scan-library-catalogs
"Loading and normalizing library catalogs in idle time...")
(aset arr 4 t)
(throw 'exit t))
(when (not (aref arr 5))
(message "Finishing initialization in idle time...")
(idlwave-routines)
(message "Finishing initialization in idle time...done")
(aset arr 5 t)
(throw 'exit nil)))
;; restart the timer
(if (sit-for 1)
(idlwave-load-rinfo-next-step)
(setq idlwave-load-rinfo-idle-timer
(run-with-idle-timer
idlwave-init-rinfo-when-idle-after
nil 'idlwave-load-rinfo-next-step))))))
(defun idlwave-load-all-rinfo (&optional force)
;; Load and case-treat the system, user catalog, and library routine
;; info files.
;; System
(when (or force (not (aref idlwave-load-rinfo-steps-done 0)))
(idlwave-load-system-routine-info))
(when (or force (not (aref idlwave-load-rinfo-steps-done 1)))
(message "Normalizing idlwave-system-routines...")
(setq idlwave-system-routines
(idlwave-sintern-rinfo-list idlwave-system-routines 'sys))
(message "Normalizing idlwave-system-routines...done"))
(when idlwave-system-routines
(setq idlwave-routines (copy-sequence idlwave-system-routines))
(setq idlwave-last-system-routine-info-cons-cell
(nthcdr (1- (length idlwave-routines)) idlwave-routines)))
;; User catalog
(when (and (stringp idlwave-user-catalog-file)
(file-regular-p idlwave-user-catalog-file))
(condition-case nil
(when (or force (not (aref idlwave-load-rinfo-steps-done 2)))
(load-file idlwave-user-catalog-file))
(error nil))
(when (and
(boundp 'idlwave-library-routines)
idlwave-library-routines)
(setq idlwave-library-routines nil)
(error "Outdated user catalog: %s... recreate"
idlwave-user-catalog-file))
(setq idlwave-true-path-alist nil)
(when (or force (not (aref idlwave-load-rinfo-steps-done 3)))
(message "Normalizing user catalog routines...")
(setq idlwave-user-catalog-routines
(idlwave-sintern-rinfo-list
idlwave-user-catalog-routines 'sys))
(message "Normalizing user catalog routines...done")))
;; Library catalog
(when (or force (not (aref idlwave-load-rinfo-steps-done 4)))
(idlwave-scan-library-catalogs
"Loading and normalizing library catalogs..."))
(run-hooks 'idlwave-after-load-rinfo-hook))
(defvar idlwave-load-rinfo-idle-timer)
(defvar idlwave-shell-path-query)
(defun idlwave-update-routine-info (&optional arg no-concatenate)
"Update the internal routine-info lists.
These lists are used by `idlwave-routine-info' (\\[idlwave-routine-info])
and by `idlwave-complete' (\\[idlwave-complete]) to provide information
about individual routines.
The information can come from 5 sources:
1. IDL programs in buffers in the current editing session.
2. Compiled modules in an IDL shell running under IDLWAVE.
3. The IDL system routines converted from XML catalog supplied with IDL.
4. Pre-scanned library files with .idlwave_catalog files.
5. A pre-scanned user catalog of local directories.
Scans all IDLWAVE-mode buffers of the current editing session (see
`idlwave-scan-all-buffers-for-routine-info').
When an IDL shell is running, this command also queries the IDL program
for currently compiled routines.
With prefix ARG, also reload the system and library lists.
With two prefix ARG's, also rescans the chosen user catalog tree.
With three prefix args, dispatch asynchronous process to do the update.
If NO-CONCATENATE is non-nil, don't pre-concatenate the routine info
lists, but instead wait for the shell query to complete and
asynchronously finish updating routine info. This is set
automatically when called interactively. When you need routine
information updated immediately, leave NO-CONCATENATE nil."
(interactive "P\np")
;; Stop any idle processing
(if (or (and (fboundp 'itimerp)
(itimerp idlwave-load-rinfo-idle-timer))
(and (fboundp 'timerp)
(timerp idlwave-load-rinfo-idle-timer)))
(cancel-timer idlwave-load-rinfo-idle-timer))
(cond
((equal arg '(64))
;; Start a background process which updates the catalog.
(idlwave-rescan-asynchronously))
((equal arg '(16))
;; Update the user catalog now, and wait for them.
(idlwave-create-user-catalog-file t))
(t
(let* ((load (or arg
idlwave-buffer-case-takes-precedence
(null idlwave-routines)))
;; The override-idle means, even if the idle timer has done some
;; preparing work, load and renormalize everything anyway.
(override-idle (or arg idlwave-buffer-case-takes-precedence)))
(setq idlwave-buffer-routines nil
idlwave-compiled-routines nil
idlwave-unresolved-routines nil)
;; Reset the appropriate hashes
(if (get 'idlwave-reset-sintern 'done-by-idle)
;; reset was already done in idle time, so skip this step now once
(put 'idlwave-reset-sintern 'done-by-idle nil)
(idlwave-reset-sintern (cond (load t)
((null idlwave-system-routines) t)
(t 'bufsh))))
(if idlwave-buffer-case-takes-precedence
;; We can safely scan the buffer stuff first
(progn
(idlwave-update-buffer-routine-info)
(and load (idlwave-load-all-rinfo override-idle)))
;; We first do the system info, and then the buffers
(and load (idlwave-load-all-rinfo override-idle))
(idlwave-update-buffer-routine-info))
;; Let's see if there is a shell
(let* ((shell-is-running (and (fboundp 'idlwave-shell-is-running)
(idlwave-shell-is-running)))
(ask-shell (and shell-is-running
idlwave-query-shell-for-routine-info)))
;; Load the library catalogs again, first re-scanning the path
(when arg
(if shell-is-running
(idlwave-shell-send-command idlwave-shell-path-query
'(progn
(idlwave-shell-get-path-info)
(idlwave-scan-library-catalogs))
'hide)
(idlwave-scan-library-catalogs)))
(if (or (not ask-shell)
(not no-concatenate))
;; 1. If we are not going to ask the shell, we need to do the
;; concatenation now.
;; 2. When this function is called non-interactively, it
;; means that someone needs routine info *now*. The
;; shell update causes the concatenation to be
;; *delayed*, so not in time for the current command.
;; Therefore, we do a concatenation now, even though
;; the shell might do it again.
(idlwave-concatenate-rinfo-lists nil 'run-hooks))
(when ask-shell
;; Ask the shell about the routines it knows of.
(message "Querying the shell")
(idlwave-shell-update-routine-info nil t)))))))
(defun idlwave-concatenate-rinfo-lists (&optional quiet run-hook)
"Put the different sources for routine information together."
;; The sequence here is important because earlier definitions shadow
;; later ones. We assume that if things in the buffers are newer
;; then in the shell of the system, they are meant to be different.
(if (consp idlwave-last-system-routine-info-cons-cell)
(setcdr idlwave-last-system-routine-info-cons-cell
(append idlwave-buffer-routines
idlwave-compiled-routines
idlwave-library-catalog-routines
idlwave-user-catalog-routines)))
(setq idlwave-class-alist nil)
;; Give a message with information about the number of routines we have.
(unless quiet
(message
"Routines Found: buffer(%d) compiled(%d) library(%d) user(%d) system(%d)"
(length idlwave-buffer-routines)
(length idlwave-compiled-routines)
(length idlwave-library-catalog-routines)
(length idlwave-user-catalog-routines)
(length idlwave-system-routines)))
(if run-hook
(run-hooks 'idlwave-update-rinfo-hook)))
;;----------------------------------------------------
;; Idle time routine updating
(defvar idlwave-after-load-rinfo-hook nil)
(defvar idlwave-update-rinfo-hook nil
"List of functions which should run after a global rinfo update.
Does not run after automatic updates of buffer or the shell.")
(defvar idlwave-class-reset nil) ; to reset buffer-local classes
(add-hook 'idlwave-load-hook
(lambda ()
(or idlwave-routines (idlwave-start-load-rinfo-timer))))
(add-hook 'idlwave-update-rinfo-hook
(lambda () (setq idlwave-class-reset t)))
(add-hook 'idlwave-after-load-rinfo-hook
(lambda () (setq idlwave-class-info nil)))
(defvar idlwave-load-rinfo-steps-done (make-vector 6 nil))
(defvar idlwave-load-rinfo-idle-timer nil)
(defun idlwave-start-load-rinfo-timer ()
(if (or (and (fboundp 'itimerp)
(itimerp idlwave-load-rinfo-idle-timer))
(and (fboundp 'timerp)
(timerp idlwave-load-rinfo-idle-timer)))
(cancel-timer idlwave-load-rinfo-idle-timer))
(setq idlwave-load-rinfo-steps-done (make-vector 6 nil))
(setq idlwave-load-rinfo-idle-timer nil)
(if (and idlwave-init-rinfo-when-idle-after
(numberp idlwave-init-rinfo-when-idle-after)
(not (equal 0 idlwave-init-rinfo-when-idle-after))
(not idlwave-routines))
(condition-case nil
(progn
(setq idlwave-load-rinfo-idle-timer
(run-with-idle-timer
idlwave-init-rinfo-when-idle-after
nil 'idlwave-load-rinfo-next-step)))
(error nil))))
(defvar idlwave-library-routines nil "Older library routine info.")
;;----------------------------------------------------
;; XML System Catalog
(defun idlwave-xml-system-routine-info-file()
"Return the file for the XML catalog file bundled with IDL."
(let* ((dir (file-name-as-directory
(expand-file-name "help/" (idlwave-sys-dir)))))
(if (and (not (file-exists-p (expand-file-name "idl_catalog.xml" dir)))
(file-directory-p (expand-file-name "online_help" dir)))
(setq dir (expand-file-name "online_help" dir)))
(expand-file-name "idl_catalog.xml" dir)))
(defun idlwave-convert-xml-system-routine-info ()
"Convert XML supplied IDL routine info into internal form.
Cache to disk for quick recovery."
(interactive)
(let* ((catalog-file (idlwave-xml-system-routine-info-file))
(elem-cnt 0)
props rinfo msg-cnt elem type nelem class-result alias
routines routine-aliases statement-aliases sysvar-aliases)
(if (not (file-exists-p catalog-file))
(error "No such XML routine info file: %s" catalog-file)
(if (not (file-readable-p catalog-file))
(error "Cannot read XML routine info file: %s" catalog-file)))
(message "Reading XML routine info...")
(require 'xml)
(setq rinfo
(let ((xml-validating-parser t))
(condition-case nil
(xml-parse-file catalog-file)
(error ;; Deal with XML.el bug
(setq xml-validating-parser nil)
(with-temp-buffer
(insert-file-contents catalog-file)
(while
(re-search-forward
"^\\s-*<!\\(ATTLIST\\|ELEMENT\\) * [A-Z]+_[A-Z]+.*>\\s-*[\r\n]"
nil t)
(replace-match ""))
(xml-parse-region (point-min) (point-max)))))))
(message "Reading XML routine info...done")
(setq rinfo (assq 'CATALOG rinfo))
(unless rinfo (error "Failed to parse XML routine info"))
;;(setq rinfo (car rinfo)) ; Skip the catalog stuff.
(setq rinfo (cddr rinfo))
(setq nelem (length rinfo)
msg-cnt (/ nelem 20))
(setq idlwave-xml-routine-info-file nil)
(message "Converting XML routine info...")
(setq idlwave-system-routines nil
idlwave-system-variables-alist nil
idlwave-system-class-info nil
idlwave-executive-commands-alist nil
idlwave-help-special-topic-words nil)
(while rinfo
(setq elem (car rinfo)
rinfo (cdr rinfo)
elem-cnt (1+ elem-cnt))
(when (listp elem)
(setq type (car elem)
props (car (cdr elem)))
(if (= (mod elem-cnt msg-cnt) 0)
(message "Converting XML routine info...%2d%%"
(/ (* elem-cnt 100) nelem)))
(cond
((eq type 'ROUTINE)
(if (and (setq alias (cdr (assq 'alias_to props)))
(not (string= "" alias)))
(push (cons (cdr (assq 'name props)) alias)
routine-aliases)
(setq routines (idlwave-xml-create-rinfo-list elem))
(if (listp (cdr routines))
(setq idlwave-system-routines
(nconc idlwave-system-routines routines))
;; a cons cell is an executive commands
(push routines idlwave-executive-commands-alist))))
((eq type 'CLASS)
(setq class-result (idlwave-xml-create-class-method-lists elem))
(push (car class-result) idlwave-system-class-info)
(setq idlwave-system-routines
(nconc idlwave-system-routines (cdr class-result))))
((eq type 'STATEMENT)
(push (cons (cdr (assq 'name props))
(cdr (assq 'link props)))
idlwave-help-special-topic-words)
;; Save the links to those which are statement aliases (not routines)
(if (and (setq alias (cdr (assq 'alias_to props)))
(not (string= "" alias)))
(unless (member alias statement-aliases)
(push alias statement-aliases))))
((eq type 'SYSVAR)
(if (and (setq alias (cdr (assq 'alias_to props)))
(not (string= "" alias)))
(push (cons (substring (cdr (assq 'name props)) 1)
(substring alias 1))
sysvar-aliases)
(push (idlwave-xml-create-sysvar-alist elem)
idlwave-system-variables-alist)))
(t))))
(idlwave-convert-xml-add-link-path-information)
(idlwave-convert-xml-clean-routine-aliases routine-aliases)
(idlwave-convert-xml-clean-statement-aliases statement-aliases)
(idlwave-convert-xml-clean-sysvar-aliases sysvar-aliases)
(setq idlwave-xml-routine-info-file catalog-file)
(idlwave-save-xml-routine-info)
(message "Converting XML routine info...done")))
(defun idlwave-save-xml-routine-info ()
(if idlwave-xml-routine-info-file
(with-temp-file idlwave-xml-system-rinfo-converted-file
(insert
(concat ";; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
;; IDLWAVE Routine Information File (IDLWAVE version " idlwave-mode-version ")
;; Automatically generated from source file:
;; " idlwave-xml-routine-info-file "
;; on " (current-time-string) "
;; " (format "%d routines, %d classes, %d sysvars, %d exec commands"
(length idlwave-system-routines)
(length idlwave-system-class-info)
(length idlwave-system-variables-alist)
(length idlwave-executive-commands-alist)) "
;; Do not edit."))
(insert (format "\n(setq idlwave-xml-routine-info-file \n \"%s\")"
idlwave-xml-routine-info-file))
(insert "\n(setq idlwave-system-routines\n '")
(prin1 idlwave-system-routines (current-buffer))
(insert ")")
(insert "\n(setq idlwave-system-variables-alist\n '")
(prin1 idlwave-system-variables-alist (current-buffer))
(insert ")")
(insert "\n(setq idlwave-system-class-info\n '")
(prin1 idlwave-system-class-info (current-buffer))
(insert ")")
(insert "\n(setq idlwave-executive-commands-alist\n '")
(prin1 idlwave-executive-commands-alist (current-buffer))
(insert ")")
(insert "\n(setq idlwave-help-special-topic-words\n '")
(prin1 idlwave-help-special-topic-words (current-buffer))
(insert ")"))))
(defun idlwave-rescan-xml-routine-info ()
"Rescan and reload IDL XML routine info."
(interactive)
(idlwave-convert-xml-system-routine-info)
(idlwave-update-routine-info))
(defun idlwave-load-system-routine-info ()
;; Load the system routine info from the cached routine info file,
;; which, if necessary, will be re-created from the XML file on
;; disk.
(unless (and (load idlwave-xml-system-rinfo-converted-file
'noerror 'nomessage)
(idlwave-xml-system-routine-info-up-to-date))
;; See if we can create it from XML source
(idlwave-convert-xml-system-routine-info)))
(defun idlwave-xml-system-routine-info-up-to-date()
(let* ((catalog-file (idlwave-xml-system-routine-info-file)))
(file-newer-than-file-p ;converted file is newer than catalog
idlwave-xml-system-rinfo-converted-file
catalog-file)))
(defun idlwave-shorten-syntax (syntax name &optional class)
;; From a list of syntax statements, shorten with %s and group with "or"
(let ((case-fold-search t))
(mapconcat
(lambda (x)
(while (string-match name x)
(setq x (replace-match "%s" t t x)))
(if class
(while (string-match class x)
(setq x (replace-match "%s" t t x))))
x)
(nreverse syntax)
" or ")))
(defun idlwave-xml-create-class-method-lists (xml-entry)
;; Create a class list entry from the xml parsed list., returning a
;; cons of form (class-entry method-entries).
(let* ((nameblock (nth 1 xml-entry))
(class (cdr (assq 'name nameblock)))
(link (cdr (assq 'link nameblock)))
(params (cddr xml-entry))
(case-fold-search t)
class-entry
method methods-entry extra-kwds
props get-props set-props init-props inherits
pelem ptype)
(while params
(setq pelem (car params))
(when (listp pelem)
(setq ptype (car pelem)
props (car (cdr pelem)))
(cond
((eq ptype 'SUPERCLASS)
(let ((pname (cdr (assq 'name props)))
(plink (cdr (assq 'link props))))
(unless (and (string= pname "None")
(string= plink "None"))
(push pname inherits))))
((eq ptype 'PROPERTY)
(let ((pname (cdr (assq 'name props)))
(plink (cdr (assq 'link props)))
(get (string= (cdr (assq 'get props)) "Yes"))
(set (string= (cdr (assq 'set props)) "Yes"))
(init (string= (cdr (assq 'init props)) "Yes")))
(if get (push (list pname plink) get-props))
(if set (push (list pname plink) set-props))
(if init (push (list pname plink) init-props))))
((eq ptype 'METHOD)
(setq method (cdr (assq 'name props)))
(setq extra-kwds ;;Assume all property keywords are gathered already
(cond
((string-match (concat class "::Init") method)
(put 'init-props 'matched t)
init-props)
((string-match (concat class "::GetProperty") method)
(put 'get-props 'matched t)
get-props)
((string-match (concat class "::SetProperty") method)
(put 'set-props 'matched t)
set-props)
(t nil)))
(setq methods-entry
(nconc (idlwave-xml-create-rinfo-list pelem class extra-kwds)
methods-entry)))
(t)))
(setq params (cdr params)))
;;(unless (get 'init-props 'matched)
;; (message "Failed to match Init in class %s" class))
;;(unless (get 'get-props 'matched)
;; (message "Failed to match GetProperty in class %s" class))
;;(unless (get 'set-props 'matched)
;; (message "Failed to match SetProperty in class %s" class))
(setq class-entry
(if inherits
(list class (append '(inherits) inherits) (list 'link link))
(list class (list 'link link))))
(cons class-entry methods-entry)))
(defun idlwave-xml-create-rinfo-list (xml-entry &optional class extra-kws)
;; Create correctly structured list elements from ROUTINE or METHOD
;; XML list structures. Return a list of list elements, with more
;; than one sub-list possible if a routine can serve as both
;; procedure and function (e.g. call_method).
(let* ((nameblock (nth 1 xml-entry))
(name (cdr (assq 'name nameblock)))
(link (cdr (assq 'link nameblock)))
(link-dir (file-name-directory link))
(params (cddr xml-entry))
(syntax-vec (make-vector 3 nil)) ; procedure, function, exec command
(case-fold-search t)
syntax kwd klink pref-list kwds pelem ptype props result type)
(if class ;; strip out class name from class method name string
(if (string-match (concat class "::") name)
(setq name (substring name (match-end 0)))))
(while params
(setq pelem (car params))
(when (listp pelem)
(setq ptype (car pelem)
props (car (cdr pelem)))
(cond
((eq ptype 'SYNTAX)
(setq syntax (cdr (assq 'name props))
type (cdr (assq 'type props)))
(unless (and (string-match "Keyword" syntax)
(string-match "^pro" type))
(if (string-match "->" syntax)
(setq syntax (replace-match "->" t nil syntax)))
(push syntax
(aref syntax-vec (cond
((or
(string-match "^exec" type)
(string= (substring name 0 1) ".")) 2)
((string-match "^pro" type) 0)
((string-match "^fun" type) 1)
(t 0))))))
((eq ptype 'KEYWORD)
(setq kwd (cdr (assq 'name props))
klink (cdr (assq 'link props)))
(if (and link-dir klink
(not (string= (file-name-directory klink) link-dir)))
(setq klink (concat link-dir klink)))
(if (string-match "\\s-*(.*?Only)" kwd)
(setq kwd (replace-match "" t t kwd)))
(if (string-match "^\\[XY\\(Z?\\)\\]" kwd)
(progn
(setq pref-list
(if (match-string 1 kwd) '("X" "Y" "Z") '("X" "Y"))
kwd (substring kwd (match-end 0)))
(loop for x in pref-list do
(push (list (concat x kwd) klink) kwds)))
(push (list kwd klink) kwds)))
(t))); Do nothing for the others
(setq params (cdr params)))
;; Debug
;; (if (and (null (aref syntax-vec 0))
;; (null (aref syntax-vec 1))
;; (null (aref syntax-vec 2)))
;; (with-current-buffer (get-buffer-create "IDL_XML_catalog_complaints")
;; (if class
;; (insert (format "Missing SYNTAX entry for %s::%s\n" class name))
;; (insert (message "Missing SYNTAX entry for %s\n" name)))))
;; Executive commands are treated specially
(if (aref syntax-vec 2)
(cons (substring name 1) link)
(if extra-kws (setq kwds (nconc kwds extra-kws)))
(setq kwds (idlwave-rinfo-group-keywords kwds link))
(loop for idx from 0 to 1 do
(if (aref syntax-vec idx)
(push (append (list name (if (eq idx 0) 'pro 'fun)
class '(system)
(idlwave-shorten-syntax
(aref syntax-vec idx) name class))
kwds) result)))
result)))
(defun idlwave-rinfo-group-keywords (kwds master-link)
;; Group keywords by link file, as a list with elements (linkfile (
;; ("KWD1" . link1) ("KWD2" . link2)) master-link specifies the link
;; for the parent routine.
(let (kwd link anchor linkfiles block master-elt)
(while kwds
(setq kwd (car kwds)
link (idlwave-split-link-target (nth 1 kwd))
anchor (cdr link)
link (car link)
kwd (car kwd))
(if (setq block (assoc link linkfiles))
(push (cons kwd anchor) (cdr block))
(push (list link (cons kwd anchor)) linkfiles))
(setq kwds (cdr kwds)))
;; Ensure the master link is there
(if (setq master-elt (assoc master-link linkfiles))
(if (eq (car linkfiles) master-elt)
linkfiles
(cons master-elt (delq master-elt linkfiles)))
(push (list master-link) linkfiles))))
(defun idlwave-convert-xml-clean-statement-aliases (aliases)
;; Clean up the syntax of routines which are actually aliases by
;; removing the "OR" from the statements
(let (syntax entry)
(loop for x in aliases do
(setq entry (assoc x idlwave-system-routines))
(when entry
(while (string-match " +or +" (setq syntax (nth 4 entry)))
(setf (nth 4 entry) (replace-match ", " t t syntax)))))))
(defun idlwave-alias-path (file alias-list content-path &optional class-dir)
"Search for the HTML help file FILE in the help content.
Uses alias information ALIAS-LIST from Alias.xml to link to the
help content, in top level CONTENT-PATH. CLASS-DIR, is set is
the directory of the class of a routine to try if it can't be
found through other means."
(let (alias linkfile)
(if (and file (> (length file) 0))
(cond
;; Directly on the alias list
((and
(setq alias (assoc-ignore-case file alias-list))
(file-exists-p (setq linkfile
(expand-file-name (cdr alias) content-path)))))
;; Sitting on content path already
((file-exists-p (setq linkfile (expand-file-name file content-path))))
;; Leading ../'s
((and (string-match "^\\(\.\./\\)+" file)
(file-exists-p
(setq linkfile
(expand-file-name
(replace-match "" t t file)
content-path)))))
;(message "Found extra ../: %s" file))
;; With the optional class directory passed in
((and class-dir
(file-exists-p
(setq linkfile
(expand-file-name (concat class-dir file)
content-path)))))
;(message "Found via class: %s" file))
;; Under Alphabetized Reference Directory
((file-exists-p
(setq linkfile
(expand-file-name
(concat "Reference Material"
"/" (upcase (substring file 0 1)) "/" file)
content-path))))
;(message "Found alphabetically under Reference Material: %s" file))
;; Dir from root name alias (e.g. CLASS_METHOD.html -> CLASS.html)
((let ((lfroot
(replace-regexp-in-string "_+[^_]*\.html" ".html" file)))
(and (not (string= file lfroot))
(setq alias (assoc-ignore-case lfroot alias-list))
(file-exists-p
(setq linkfile
(expand-file-name
(concat (file-name-directory (cdr alias)) file)
content-path))))))
;(message "Found using root name: %s" file))
;; Didn't find it... try a brute-force directory search
(t
(message "searching for %s" file)
(if (setq linkfile (idlwave-recursive-find-file content-path file))
(progn
(setq linkfile (file-relative-name linkfile content-path))
;; Save it as an alias in case it is requested again
(nconc alias-list (list (cons file linkfile))))
(prog1 nil (message "Could not locate %s" file))))))
linkfile))
(defun idlwave-convert-xml-add-link-path-information ()
;; Add path information missing from idl_catalog.xml since IDL 8
(let* ((help-path (expand-file-name "help/online_help/IDL/"
(idlwave-sys-dir)))
(content-path (expand-file-name "Content" help-path))
(alias-file (expand-file-name "Data/Alias.xml" help-path)))
(message "Linking help file info...")
(if (file-exists-p alias-file)
(let ((aliases (cdar (xml-parse-file alias-file))) elem alias-list)
;; Parse the aliases
(while aliases
(setq elem (car aliases)
aliases (cdr aliases))
(when (and (listp elem) (eq (car elem) 'Map))
(setq elem (cadr elem))
(let* ((link (cdr (assoc 'Link elem)))
(file (file-name-nondirectory link)))
(push (cons file link) alias-list))))
;; System class info
(mapc
(lambda (x)
(when (setq linkfile
(idlwave-alias-path
(nth 1 (assq 'link x)) alias-list content-path))
(setcar (cdr (assq 'link x)) linkfile)))
idlwave-system-class-info)
;; Main routines
(mapc
(lambda (x)
(let ((class (nth 2 x))
(kwd_blocks (nthcdr 5 x)) link linkfile class-entry)
(while kwd_blocks
(setq link (car kwd_blocks)
kwd_blocks (cdr kwd_blocks))
(when (setq linkfile
(idlwave-alias-path
(car link) alias-list content-path
(if (and class
(setq class-entry
(assoc class
idlwave-system-class-info)))
(file-name-directory
(nth 1 (assq 'link class-entry))))))
(setcar link linkfile)))))
idlwave-system-routines)
;; Executive commands/special topics
(mapc
(lambda (x)
(let ((alias (assoc-ignore-case (cdr x) alias-list)))
(if alias
(setcdr x (cdr alias)))))
(append idlwave-help-special-topic-words
idlwave-executive-commands-alist))))
(message "Linking help file info...done")))
(defun idlwave-convert-xml-clean-routine-aliases (aliases)
;; Duplicate and trim original routine aliases from rinfo list
;; This if for, e.g. OPENR/OPENW/OPENU
(let (alias remove-list new parts all-parts)
(loop for x in aliases do
(when (setq parts (split-string (cdr x) "/"))
(setq new (assoc (cdr x) all-parts))
(unless new
(setq new (cons (cdr x) parts))
(push new all-parts))
(setcdr new (delete (car x) (cdr new)))))
;; Add any missing aliases (separate by slashes)
(loop for x in all-parts do
(if (cdr x)
(push (cons (nth 1 x) (car x)) aliases)))
(loop for x in aliases do
(when (setq alias (assoc (cdr x) idlwave-system-routines))
(unless (memq alias remove-list) (push alias remove-list))
(setq alias (copy-sequence alias))
(setcar alias (car x))
(push alias idlwave-system-routines)))
(loop for x in remove-list do
(delq x idlwave-system-routines))))
(defun idlwave-convert-xml-clean-sysvar-aliases (aliases)
;; Duplicate and trim original routine aliases from rinfo list
;; This if for, e.g. !X, !Y, !Z.
(let (alias remove-list)
(loop for x in aliases do
(when (setq alias (assoc (cdr x) idlwave-system-variables-alist))
(unless (memq alias remove-list) (push alias remove-list))
(setq alias (copy-sequence alias))
(setcar alias (car x))
(push alias idlwave-system-variables-alist)))
(loop for x in remove-list do
(delq x idlwave-system-variables-alist))))
(defun idlwave-xml-create-sysvar-alist (xml-entry)
;; Create a sysvar list entry from the xml parsed list.
(let* ((nameblock (nth 1 xml-entry))
(name (cdr (assq 'name nameblock)))
(sysvar (substring name (progn (string-match "^ *!" name)
(match-end 0))))
(link (cdr (assq 'link nameblock)))
(params (cddr xml-entry))
(case-fold-search t)
pelem ptype props tags)
(while params
(setq pelem (car params))
(when (listp pelem)
(setq ptype (car pelem)
props (car (cdr pelem)))
(cond
((eq ptype 'FIELD)
(push (cons (cdr (assq 'name props))
(cdr
(idlwave-split-link-target (cdr (assq 'link props)))))
tags))))
(setq params (cdr params)))
(delq nil
(list sysvar (if tags (cons 'tags tags)) (list 'link link)))))
;;----------------------------------------------------
;; Buffer Scanning
(defvar idlwave-scanning-lib-dir)
(defvar idlwave-scanning-lib)
(defun idlwave-get-routine-info-from-buffers (buffers)
"Call `idlwave-get-buffer-routine-info' on idlwave-mode buffers in BUFFERS."
(let (buf routine-lists res)
(save-excursion
(while (setq buf (pop buffers))
(set-buffer buf)
(if (and (eq major-mode 'idlwave-mode)
buffer-file-name)
;; yes, this buffer has the right mode.
(progn (setq res (condition-case nil
(idlwave-get-buffer-routine-info)
(error nil)))
(push res routine-lists)))))
;; Concatenate the individual lists and return the result
(apply 'nconc routine-lists)))
(defun idlwave-get-buffer-routine-info ()
"Scan the current buffer for routine info. Return (PRO-LIST FUNC-LIST)."
(let* ((case-fold-search t)
routine-list string entry)
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward
"^[ \t]*\\(pro\\|function\\)[ \t]" nil t)
(setq string (buffer-substring-no-properties
(match-beginning 0)
(progn
(idlwave-end-of-statement)
(point))))
(setq entry (idlwave-parse-definition string))
(push entry routine-list))))
routine-list))
(defun idlwave-parse-definition (string)
"Parse a module definition."
(let ((case-fold-search t)
start name args type keywords class)
;; Remove comments
(while (string-match ";.*" string)
(setq string (replace-match "" t t string)))
;; Remove the continuation line stuff
(while (string-match "\\([^a-zA-Z0-9$_]\\)\\$[ \t]*\n" string)
(setq string (replace-match "\\1 " t nil string)))
(while (string-match "\n" string)
(setq string (replace-match " " t nil string)))
;; Match the name and type.
(when (string-match
"\\<\\(pro\\|function\\)\\>\\s-+\\(\\([a-zA-Z0-9$_]+\\)::\\)?\\([a-zA-Z0-9$_]+\\)" string)
(setq start (match-end 0))
(setq type (downcase (match-string 1 string)))
(if (match-beginning 3)
(setq class (match-string 3 string)))
(setq name (match-string 4 string)))
;; Match normal args and keyword args
(while (string-match
",\\s-*\\([a-zA-Z][a-zA-Z0-9$_]*\\|\\(_ref\\)?_extra\\)\\s-*\\(=\\)?"
string start)
(setq start (match-end 0))
(if (match-beginning 3)