-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathada-xref.el
2359 lines (1977 loc) · 82.2 KB
/
ada-xref.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
;; ada-xref.el --- for lookup and completion in Ada mode
;; Copyright (C) 1994-2019 Free Software Foundation, Inc.
;; Author: Markus Heritsch <[email protected]>
;; Rolf Ebert <[email protected]>
;; Emmanuel Briot <[email protected]>
;; Maintainer: Stephen Leake <[email protected]>
;; Keywords: languages ada xref
;; Package: ada-mode
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This Package provides a set of functions to use the output of the
;; cross reference capabilities of the GNAT Ada compiler
;; for lookup and completion in Ada mode.
;;
;; If a file *.`adp' exists in the ada-file directory, then it is
;; read for configuration information. It is read only the first
;; time a cross-reference is asked for, and is not read later.
;;; Code:
;; ----- Requirements -----------------------------------------------------
(require 'compile)
(require 'comint)
(require 'find-file)
(require 'ada-mode)
(eval-when-compile (require 'cl-lib))
;; ------ User variables
(defcustom ada-xref-other-buffer t
"If nil, always display the cross-references in the same buffer.
Otherwise create either a new buffer or a new frame."
:type 'boolean :group 'ada)
(defcustom ada-xref-create-ali nil
"If non-nil, run gcc whenever the cross-references are not up-to-date.
If nil, the cross-reference mode never runs gcc."
:type 'boolean :group 'ada)
(defcustom ada-xref-confirm-compile nil
"If non-nil, ask for confirmation before compiling or running the application."
:type 'boolean :group 'ada)
(defcustom ada-krunch-args "0"
"Maximum number of characters for filenames created by `gnatkr'.
Set to 0, if you don't use crunched filenames. This should be a string."
:type 'string :group 'ada)
(defcustom ada-gnat-cmd "gnat"
"Default GNAT project file parser.
Will be run with args \"list -v -Pfile.gpr\".
Default is standard GNAT distribution; alternate \"gnatpath\"
is faster, available from Ada mode web site."
:type 'string :group 'ada)
(defcustom ada-gnatls-args '("-v")
"Arguments to pass to `gnatls' to find location of the runtime.
Typical use is to pass `--RTS=soft-floats' on some systems that support it.
You can also add `-I-' if you do not want the current directory to be included.
Otherwise, going from specs to bodies and back will first look for files in the
current directory. This only has an impact if you are not using project files,
but only ADA_INCLUDE_PATH."
:type '(repeat string) :group 'ada)
(defcustom ada-prj-default-comp-opt "-gnatq -gnatQ"
"Default compilation options."
:type 'string :group 'ada)
(defcustom ada-prj-default-bind-opt ""
"Default binder options."
:type 'string :group 'ada)
(defcustom ada-prj-default-link-opt ""
"Default linker options."
:type 'string :group 'ada)
(defcustom ada-prj-default-gnatmake-opt "-g"
"Default options for `gnatmake'."
:type 'string :group 'ada)
(defcustom ada-prj-default-gpr-file ""
"Default GNAT project file.
If non-empty, this file is parsed to set the source and object directories for
the Ada mode project."
:type 'string :group 'ada)
(defcustom ada-prj-ada-project-path-sep
(cond ((boundp 'path-separator) path-separator) ; 20.3+
((memq system-type '(windows-nt ms-dos)) ";")
(t ":"))
"Default separator for ada_project_path project variable."
:type 'string :group 'ada)
(defcustom ada-prj-gnatfind-switches "-rf"
"Default switches to use for `gnatfind'.
You should modify this variable, for instance to add `-a', if you are working
in an environment where most ALI files are write-protected.
The command `gnatfind' is used every time you choose the menu
\"Show all references\"."
:type 'string :group 'ada)
(defcustom ada-prj-default-check-cmd
(concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}"
" -cargs ${comp_opt}")
"Default command to be used to compile a single file.
Emacs will substitute the current filename for ${full_current}, or add
the filename at the end. This is the same syntax as in the project file."
:type 'string :group 'ada)
(defcustom ada-prj-default-comp-cmd
(concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
" ${comp_opt}")
"Default command to be used to compile a single file.
Emacs will substitute the current filename for ${full_current}, or add
the filename at the end. This is the same syntax as in the project file."
:type 'string :group 'ada)
(defcustom ada-prj-default-debugger "${cross_prefix}gdb"
"Default name of the debugger."
:type 'string :group 'ada)
(defcustom ada-prj-default-make-cmd
(concat "${cross_prefix}gnatmake -o ${main} ${main} ${gnatmake_opt} "
"-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
"Default command to be used to compile the application.
This is the same syntax as in the project file."
:type 'string :group 'ada)
(defcustom ada-prj-default-project-file ""
"Name of the current project file.
Emacs will not try to use the search algorithm to find the project file if
this string is not empty. It is set whenever a project file is found."
:type '(file :must-match t) :group 'ada)
(defcustom ada-gnatstub-opts "-q -I${src_dir}"
"Options to pass to `gnatsub' to generate the body of a package.
This has the same syntax as in the project file (with variable substitution)."
:type 'string :group 'ada)
(defcustom ada-always-ask-project nil
"If nil, use default values when no project file was found.
Otherwise, ask the user for the name of the project file to use."
:type 'boolean :group 'ada)
(defconst ada-on-ms-windows (memq system-type '(windows-nt))
"True if we are running on Windows.")
(defcustom ada-tight-gvd-integration nil
"If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
If GVD is not the debugger used, nothing happens."
:type 'boolean :group 'ada)
(defcustom ada-xref-search-with-egrep t
"If non-nil, use grep -E to find the possible declarations for an entity.
This alternate method is used when the exact location was not found in the
information provided by GNAT. However, it might be expensive if you have a lot
of sources, since it will search in all the files in your project."
:type 'boolean :group 'ada)
(defvar ada-load-project-hook nil
"Hook that is run when loading a project file.
Each function in this hook takes one argument FILENAME, that is the name of
the project file to load.
This hook should be used to support new formats for the project files.
If the function can load the file with the given filename, it should create a
buffer that contains a conversion of the file to the standard format of the
project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\"
lines.) It should return nil if it doesn't know how to convert that project
file.")
;; ------- Nothing to be modified by the user below this
(defvar ada-last-prj-file ""
"Name of the last project file entered by the user.")
(defconst ada-prj-file-extension ".adp"
"The extension used for project files.")
(defvar ada-xref-runtime-library-specs-path '()
"Directories where the specs for the standard library is found.
This is used for cross-references.")
(defvar ada-xref-runtime-library-ali-path '()
"Directories where the ali for the standard library is found.
This is used for cross-references.")
(defvar ada-xref-pos-ring '()
"List of positions selected by the cross-references functions.
Used to go back to these positions.")
(defvar ada-cd-command
(if (string-match "cmdproxy.exe" shell-file-name)
"cd /d"
"cd")
"Command to use to change to a specific directory.
On Windows systems using `cmdproxy.exe' as the shell,
we need to use `/d' or the drive is never changed.")
(defvar ada-command-separator (if ada-on-ms-windows " && " "\n")
"Separator to use between multiple commands to `compile' or `start-process'.
`cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use
\"&&\" for now.")
(defconst ada-xref-pos-ring-max 16
"Number of positions kept in the list `ada-xref-pos-ring'.")
(defvar ada-operator-re
"\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
"Regexp to match for operators.")
(defvar ada-xref-project-files '()
"Associative list of project files with properties.
It has the format: (project project ...)
A project has the format: (project-file . project-plist)
\(See `apropos plist' for operations on property lists).
See `ada-default-prj-properties' for the list of valid properties.
The current project is retrieved with `ada-xref-current-project'.
Properties are retrieved with `ada-xref-get-project-field', set with
`ada-xref-set-project-field'. If project properties are accessed with no
project file, a (nil . default-properties) entry is created.")
;; ----- Identlist manipulation -------------------------------------------
;; An identlist is a vector that is used internally to reference an identifier
;; To facilitate its use, we provide the following macros
(defmacro ada-make-identlist () (make-vector 8 nil))
(defmacro ada-name-of (identlist) (list 'aref identlist 0))
(defmacro ada-line-of (identlist) (list 'aref identlist 1))
(defmacro ada-column-of (identlist) (list 'aref identlist 2))
(defmacro ada-file-of (identlist) (list 'aref identlist 3))
(defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
(defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
(defmacro ada-references-of (identlist) (list 'aref identlist 6))
(defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
(defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
(defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
(defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
(defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
(defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
(defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
(defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
(defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
(defsubst ada-get-ali-buffer (file)
"Read the ali file FILE into a new buffer, and return the buffer's name."
(find-file-noselect (ada-get-ali-file-name file)))
;; -----------------------------------------------------------------------
(defun ada-quote-cmd (cmd)
"Duplicate all `\\' characters in CMD so that it can be passed to `compile'."
(mapconcat 'identity (split-string cmd "\\\\") "\\\\"))
(defun ada-find-executable (exec-name)
"Find the full path to the executable file EXEC-NAME.
If not found, throw an error.
On Windows systems, this will properly handle .exe extension as well."
(let ((result (or (ada-find-file-in-dir exec-name exec-path)
(ada-find-file-in-dir (concat exec-name ".exe") exec-path))))
(if result
result
(error "`%s' not found in path" exec-name))))
(defun ada-initialize-runtime-library (cross-prefix)
"Initialize the variables for the runtime library location.
CROSS-PREFIX is the prefix to use for the `gnatls' command."
(let ((gnatls
(condition-case nil
;; if gnatls not found, just give up (may not be using GNAT)
(ada-find-executable (concat cross-prefix "gnatls"))
(error nil))))
(if gnatls
(save-excursion
(setq ada-xref-runtime-library-specs-path '()
ada-xref-runtime-library-ali-path '())
(set-buffer (get-buffer-create "*gnatls*"))
(widen)
(erase-buffer)
;; Even if we get an error, delete the *gnatls* buffer
(unwind-protect
(let ((status (apply 'call-process gnatls (append '(nil t nil) ada-gnatls-args))))
(goto-char (point-min))
;; Since we didn't provide all the inputs gnatls expects, it returns status 4
(if (/= 4 status)
(error (buffer-substring (point) (line-end-position))))
;; Source path
(search-forward "Source Search Path:")
(forward-line 1)
(while (not (looking-at "^$"))
(back-to-indentation)
(add-to-list 'ada-xref-runtime-library-specs-path
(if (looking-at "<Current_Directory>")
"."
(buffer-substring-no-properties
(point)
(point-at-eol))))
(forward-line 1))
;; Object path
(search-forward "Object Search Path:")
(forward-line 1)
(while (not (looking-at "^$"))
(back-to-indentation)
(add-to-list 'ada-xref-runtime-library-ali-path
(if (looking-at "<Current_Directory>")
"."
(buffer-substring-no-properties
(point)
(point-at-eol))))
(forward-line 1))
)
(kill-buffer nil))))
(setq ada-xref-runtime-library-specs-path
(reverse ada-xref-runtime-library-specs-path))
(setq ada-xref-runtime-library-ali-path
(reverse ada-xref-runtime-library-ali-path))
))
(defun ada-gnat-parse-gpr (plist gpr-file)
"Set gpr_file, src_dir and obj_dir properties in PLIST by parsing GPR-FILE.
Return new value of PLIST.
GPR_FILE must be full path to file, normalized.
src_dir, obj_dir will include compiler runtime.
Assumes environment variable ADA_PROJECT_PATH is set properly."
(with-current-buffer (get-buffer-create "*gnatls*")
(erase-buffer)
;; this can take a long time; let the user know what's up
(message "Parsing %s ..." gpr-file)
;; Even if we get an error, delete the *gnatls* buffer
(unwind-protect
(let* ((cross-prefix (plist-get plist 'cross_prefix))
(gnat (concat cross-prefix ada-gnat-cmd))
;; Putting quotes around gpr-file confuses gnatpath on Lynx; not clear why
(gpr-opt (concat "-P" gpr-file))
(src-dir '())
(obj-dir '())
(status (call-process gnat nil t nil "list" "-v" gpr-opt)))
(goto-char (point-min))
(if (/= 0 status)
(error (buffer-substring (point) (line-end-position))))
;; Source path
(search-forward "Source Search Path:")
(forward-line 1) ; first directory in list
(while (not (looking-at "^$")) ; terminate on blank line
(back-to-indentation) ; skip whitespace
(cl-pushnew (if (looking-at "<Current_Directory>")
default-directory
(expand-file-name
(buffer-substring-no-properties
(point) (line-end-position))))
src-dir :test #'equal)
(forward-line 1))
;; Object path
(search-forward "Object Search Path:")
(forward-line 1)
(while (not (looking-at "^$"))
(back-to-indentation)
(cl-pushnew (if (looking-at "<Current_Directory>")
default-directory
(expand-file-name
(buffer-substring-no-properties
(point) (line-end-position))))
obj-dir :test #'equal)
(forward-line 1))
;; Set properties
(setq plist (plist-put plist 'gpr_file gpr-file))
(setq plist (plist-put plist 'src_dir src-dir))
(plist-put plist 'obj_dir obj-dir)
)
(kill-buffer nil)
(message "Parsing %s ... done" gpr-file)
)
))
(defun ada-treat-cmd-string (cmd-string)
"Replace variable references ${var} in CMD-STRING with the appropriate value.
Also replace standard environment variables $var.
Assumes project exists.
As a special case, ${current} is replaced with the name of the current
file, minus extension but with directory, and ${full_current} is
replaced by the name including the extension."
(while (string-match "\\(-[^-$IO]*[IO]\\)?\\${\\([^}]+\\)}" cmd-string)
(let (value
(name (match-string 2 cmd-string)))
(cond
((string= name "current")
(setq value (file-name-sans-extension (buffer-file-name))))
((string= name "full_current")
(setq value (buffer-file-name)))
(t
(save-match-data
(setq value (ada-xref-get-project-field (intern name))))))
;; Check if there is an environment variable with the same name
(if (null value)
(if (not (setq value (getenv name)))
(message "%s" (concat "No project or environment variable " name " found"))))
(cond
((null value)
(setq cmd-string (replace-match "" t t cmd-string)))
((stringp value)
(setq cmd-string (replace-match value t t cmd-string)))
((listp value)
(let ((prefix (match-string 1 cmd-string)))
(setq cmd-string (replace-match
(mapconcat (lambda(x) (concat prefix x)) value " ")
t t cmd-string)))))
))
(substitute-in-file-name cmd-string))
(defun ada-xref-get-project-field (field)
"Extract the value of FIELD from the current project file.
Project variables are substituted.
Note that for src_dir and obj_dir, you should rather use
`ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field'
which will in addition return the default paths."
(let* ((project-plist (cdr (ada-xref-current-project)))
(value (plist-get project-plist field)))
(cond
((eq field 'gnatmake_opt)
(let ((gpr-file (plist-get project-plist 'gpr_file)))
(if (not (string= gpr-file ""))
(setq value (concat "-P\"" gpr-file "\" " value)))))
;; FIXME: check for src_dir, obj_dir here, rather than requiring user to do it
(t
nil))
;; Substitute the ${...} constructs in all the strings, including
;; inside lists
(cond
((stringp value)
(ada-treat-cmd-string value))
((null value)
nil)
((listp value)
(mapcar (lambda(x) (if x (ada-treat-cmd-string x) x)) value))
(t
value)
)
))
(defun ada-xref-get-src-dir-field ()
"Return the full value for src_dir, including the default directories.
All the directories are returned as absolute directories."
(let ((build-dir (ada-xref-get-project-field 'build_dir)))
(append
;; Add ${build_dir} in front of the path
(list build-dir)
(ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
build-dir)
;; Add the standard runtime at the end
ada-xref-runtime-library-specs-path)))
(defun ada-xref-get-obj-dir-field ()
"Return the full value for obj_dir, including the default directories.
All the directories are returned as absolute directories."
(let ((build-dir (ada-xref-get-project-field 'build_dir)))
(append
;; Add ${build_dir} in front of the path
(list build-dir)
(ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir)
build-dir)
;; Add the standard runtime at the end
ada-xref-runtime-library-ali-path)))
(defun ada-xref-set-project-field (field value)
"Set FIELD to VALUE in current project. Assumes project exists."
;; same algorithm to find project-plist as ada-xref-current-project
(let* ((file-name (ada-xref-current-project-file))
(project-plist (cdr (assoc file-name ada-xref-project-files))))
(setq project-plist (plist-put project-plist field value))
(setcdr (assoc file-name ada-xref-project-files) project-plist)))
(defun ada-xref-update-project-menu ()
"Update the menu Ada->Project, with the list of available project files."
;; Create the standard items.
(let ((submenu
`("Project"
["Load..." ada-set-default-project-file t]
["New..." ada-prj-new t]
["Edit..." ada-prj-edit t]
"---"
;; Add the project files
,@(mapcar
(lambda (x)
(let* ((name (or (car x) "<default>"))
(command `(lambda ()
"Select the current project file."
(interactive)
(ada-select-prj-file ,name))))
(vector
(file-name-nondirectory name)
command
:button (cons
:toggle
(equal ada-prj-default-project-file
(car x))
))))
(or ada-xref-project-files '(nil))))))
(easy-menu-add-item ada-mode-menu '() submenu)))
;;-------------------------------------------------------------
;;-- Searching a file anywhere on the source path.
;;--
;;-- The following functions provide support for finding a file anywhere
;;-- on the source path, without providing an explicit directory.
;;-- They also provide file name completion in the minibuffer.
;;--
;;-- Public subprograms: ada-find-file
;;--
;;-------------------------------------------------------------
(defun ada-do-file-completion (string predicate flag)
"Completion function when reading a file from the minibuffer.
Completion is attempted in all the directories in the source path,
as defined in the project file."
;; FIXME: doc arguments
;; This function is not itself interactive, but it is called as part
;; of the prompt of interactive functions, so we require a project
;; file.
(ada-require-project-file)
(let (list
(dirs (ada-xref-get-src-dir-field)))
(while dirs
(if (file-directory-p (car dirs))
(setq list (append list (file-name-all-completions string (car dirs)))))
(setq dirs (cdr dirs)))
(cond ((equal flag 'lambda)
(assoc string list))
(flag
list)
(t
(try-completion string
(mapcar (lambda (x) (cons x 1)) list)
predicate)))))
;;;###autoload
(defun ada-find-file (filename)
"Open FILENAME, from anywhere in the source path.
Completion is available."
(interactive
(list (completing-read "File: " 'ada-do-file-completion)))
(let ((file (ada-find-src-file-in-dir filename)))
(if file
(find-file file)
(error "%s not found in src_dir" filename))))
;; ----- Utilities -------------------------------------------------
(defun ada-require-project-file ()
"If the current project does not exist, load or create a default one.
Should only be called from interactive functions."
(if (string= "" ada-prj-default-project-file)
(ada-reread-prj-file (ada-prj-find-prj-file t))))
(defun ada-xref-current-project-file ()
"Return the current project file name; never nil.
Call `ada-require-project-file' first if a project must exist."
(if (not (string= "" ada-prj-default-project-file))
ada-prj-default-project-file
(ada-prj-find-prj-file t)))
(defun ada-xref-current-project ()
"Return the current project.
Call `ada-require-project-file' first to ensure a project exists."
(let ((file-name (ada-xref-current-project-file)))
(assoc file-name ada-xref-project-files)))
(defun ada-show-current-project ()
"Display current project file name in message buffer."
(interactive)
(message (ada-xref-current-project-file)))
(defun ada-show-current-main ()
"Display current main file name in message buffer."
(interactive)
(message "ada-mode main: %s" (ada-xref-get-project-field 'main)))
(defun ada-xref-push-pos (filename position)
"Push (FILENAME, POSITION) on the position ring for cross-references."
(setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
(if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
(setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
(defun ada-xref-goto-previous-reference ()
"Go to the previous cross-reference we were on."
(interactive)
(if ada-xref-pos-ring
(let ((pos (car ada-xref-pos-ring)))
(setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
(find-file (car (cdr pos)))
(goto-char (car pos)))))
(defun ada-set-default-project-file (file)
"Set FILE as the current project file."
(interactive "fProject file:")
(ada-parse-prj-file file)
(ada-select-prj-file file))
;; ------ Handling the project file -----------------------------
(defun ada-prj-find-prj-file (&optional no-user-question)
"Find the project file associated with the current buffer.
If the buffer is not in Ada mode, or not associated with a file,
return `ada-prj-default-project-file'. Otherwise, search for a file with
the same base name as the Ada file, but extension given by
`ada-prj-file-extension' (default .adp). If not found, search for *.adp
in the current directory; if several are found, and NO-USER-QUESTION
is non-nil, prompt the user to select one. If none are found, return
\"default.adp\"."
(let (selected)
(if (not (and (derived-mode-p 'ada-mode)
buffer-file-name))
;; Not in an Ada buffer, or current buffer not associated
;; with a file (for instance an emerge buffer)
(setq selected nil)
;; other cases: use a more complex algorithm
(let* ((current-file (buffer-file-name))
(first-choice (concat
(file-name-sans-extension current-file)
ada-prj-file-extension))
(dir (file-name-directory current-file))
(prj-files (directory-files
dir t
(concat ".*" (regexp-quote
ada-prj-file-extension) "$")))
(choice nil))
(cond
((file-exists-p first-choice)
;; filename.adp
(setq selected first-choice))
((= (length prj-files) 1)
;; Exactly one project file was found in the current directory
(setq selected (car prj-files)))
((and (> (length prj-files) 1) (not no-user-question))
;; multiple project files in current directory, ask the user
(save-window-excursion
(with-output-to-temp-buffer "*choice list*"
(princ "There are more than one possible project file.\n")
(princ "Which one should we use ?\n\n")
(princ " no. file name \n")
(princ " --- ------------------------\n")
(let ((counter 1))
(while (<= counter (length prj-files))
(princ (format " %2d) %s\n"
counter
(nth (1- counter) prj-files)))
(setq counter (1+ counter))
))) ; end of with-output-to ...
(setq choice nil)
(while (or
(not choice)
(not (integerp choice))
(< choice 1)
(> choice (length prj-files)))
(setq choice (string-to-number
(read-from-minibuffer "Enter No. of your choice: "))))
(setq selected (nth (1- choice) prj-files))))
((= (length prj-files) 0)
;; No project file in the current directory; ask user
(unless (or no-user-question (not ada-always-ask-project))
(setq ada-last-prj-file
(read-file-name
(concat "project file [" ada-last-prj-file "]:")
nil ada-last-prj-file))
(unless (string= ada-last-prj-file "")
(setq selected ada-last-prj-file))))
)))
(or selected "default.adp")
))
(defun ada-default-prj-properties ()
"Return the default project properties list with the current buffer as main."
(let ((file (buffer-file-name nil)))
(list
;; variable name alphabetical order
'ada_project_path (or (getenv "ADA_PROJECT_PATH") "")
'ada_project_path_sep ada-prj-ada-project-path-sep
'bind_opt ada-prj-default-bind-opt
'build_dir default-directory
'casing (if (listp ada-case-exception-file)
ada-case-exception-file
(list ada-case-exception-file))
'check_cmd (list ada-prj-default-check-cmd) ;; FIXME: should not a list
'comp_cmd (list ada-prj-default-comp-cmd) ;; FIXME: should not a list
'comp_opt ada-prj-default-comp-opt
'cross_prefix ""
'debug_cmd (concat ada-prj-default-debugger
" ${main}" (if ada-on-ms-windows ".exe")) ;; FIXME: don't need .exe?
'debug_post_cmd (list nil)
'debug_pre_cmd (list (concat ada-cd-command " ${build_dir}"))
'gnatmake_opt ada-prj-default-gnatmake-opt
'gnatfind_opt ada-prj-gnatfind-switches
'gpr_file ada-prj-default-gpr-file
'link_opt ada-prj-default-link-opt
'main (if file
(file-name-nondirectory
(file-name-sans-extension file))
"")
'make_cmd (list ada-prj-default-make-cmd) ;; FIXME: should not a list
'obj_dir (list ".")
'remote_machine ""
'run_cmd (list (concat "./${main}" (if ada-on-ms-windows ".exe")))
;; FIXME: should not a list
;; FIXME: don't need .exe?
'src_dir (list ".")
)))
(defun ada-parse-prj-file (prj-file)
"Read PRJ-FILE, set project properties in `ada-xref-project-files'."
(let ((project (ada-default-prj-properties)))
(setq prj-file (expand-file-name prj-file))
(if (string= (file-name-extension prj-file) "gpr")
(setq project (ada-gnat-parse-gpr project prj-file))
(setq project (ada-parse-prj-file-1 prj-file project))
)
;; Store the project properties
(if (assoc prj-file ada-xref-project-files)
(setcdr (assoc prj-file ada-xref-project-files) project)
(add-to-list 'ada-xref-project-files (cons prj-file project)))
(ada-xref-update-project-menu)
))
(defun ada-parse-prj-file-1 (prj-file project)
"Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
Return new value of PROJECT."
(let ((ada-buffer (current-buffer))
;; fields that are lists or otherwise require special processing
ada_project_path casing comp_cmd check_cmd
debug_pre_cmd debug_post_cmd gpr_file make_cmd obj_dir src_dir run_cmd)
;; Give users a chance to use compiler-specific project file formats
(let ((buffer (run-hook-with-args-until-success
'ada-load-project-hook prj-file)))
(unless buffer
;; we load the project file with no warnings; if it does not
;; exist, we stay in the Ada buffer; no project variable
;; settings will be found. That works for the default
;; "default.adp", which does not exist as a file.
(setq buffer (find-file-noselect prj-file nil)))
(set-buffer buffer))
(widen)
(goto-char (point-min))
;; process each line
(while (not (eobp))
;; ignore lines that don't have the format "name=value", put
;; 'name', 'value' in match-string.
(if (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
(cond
;; FIXME: strip trailing spaces
;; variable name alphabetical order
((string= (match-string 1) "ada_project_path")
(cl-pushnew (expand-file-name
(substitute-in-file-name (match-string 2)))
ada_project_path :test #'equal))
((string= (match-string 1) "build_dir")
(setq project
(plist-put project 'build_dir
(file-name-as-directory (match-string 2)))))
((string= (match-string 1) "casing")
(cl-pushnew (expand-file-name (substitute-in-file-name (match-string 2)))
casing :test #'equal))
((string= (match-string 1) "check_cmd")
(cl-pushnew (match-string 2) check_cmd :test #'equal))
((string= (match-string 1) "comp_cmd")
(cl-pushnew (match-string 2) comp_cmd :test #'equal))
((string= (match-string 1) "debug_post_cmd")
(cl-pushnew (match-string 2) debug_post_cmd :test #'equal))
((string= (match-string 1) "debug_pre_cmd")
(cl-pushnew (match-string 2) debug_pre_cmd :test #'equal))
((string= (match-string 1) "gpr_file")
;; expand now; path is relative to Emacs project file
(setq gpr_file (expand-file-name (match-string 2))))
((string= (match-string 1) "make_cmd")
(cl-pushnew (match-string 2) make_cmd :test #'equal))
((string= (match-string 1) "obj_dir")
(cl-pushnew (file-name-as-directory
(expand-file-name (match-string 2)))
obj_dir :test #'equal))
((string= (match-string 1) "run_cmd")
(cl-pushnew (match-string 2) run_cmd :test #'equal))
((string= (match-string 1) "src_dir")
(cl-pushnew (file-name-as-directory
(expand-file-name (match-string 2)))
src_dir :test #'equal))
(t
;; any other field in the file is just copied
(setq project (plist-put project
(intern (match-string 1))
(match-string 2))))))
(forward-line 1))
;; done reading file
;; back to the user buffer
(set-buffer ada-buffer)
;; process accumulated lists
(if ada_project_path
(let ((sep (plist-get project 'ada_project_path_sep)))
(setq ada_project_path (reverse ada_project_path))
(setq ada_project_path (mapconcat 'identity ada_project_path sep))
(setq project (plist-put project 'ada_project_path ada_project_path))
;; env var needed now for ada-gnat-parse-gpr
(setenv "ADA_PROJECT_PATH" ada_project_path)))
(if debug_post_cmd (setq project (plist-put project 'debug_post_cmd (reverse debug_post_cmd))))
(if debug_pre_cmd (setq project (plist-put project 'debug_pre_cmd (reverse debug_pre_cmd))))
(if casing (setq project (plist-put project 'casing (reverse casing))))
(if check_cmd (setq project (plist-put project 'check_cmd (reverse check_cmd))))
(if comp_cmd (setq project (plist-put project 'comp_cmd (reverse comp_cmd))))
(if make_cmd (setq project (plist-put project 'make_cmd (reverse make_cmd))))
(if run_cmd (setq project (plist-put project 'run_cmd (reverse run_cmd))))
(if gpr_file
(progn
(setq project (ada-gnat-parse-gpr project gpr_file))
;; append Ada source and object directories to others from Emacs project file
(setq src_dir (append (plist-get project 'src_dir) src_dir))
(setq obj_dir (append (plist-get project 'obj_dir) obj_dir))
(setq ada-xref-runtime-library-specs-path '()
ada-xref-runtime-library-ali-path '()))
)
;; FIXME: gnatpath.exe doesn't output the runtime libraries, so always call ada-initialize-runtime-library
;; if using a gpr_file, the runtime library directories are
;; included in src_dir and obj_dir; otherwise they are in the
;; 'runtime-library' variables.
;; FIXME: always append to src_dir, obj_dir
(ada-initialize-runtime-library (or (ada-xref-get-project-field 'cross_prefix) ""))
;;)
(if obj_dir (setq project (plist-put project 'obj_dir (reverse obj_dir))))
(if src_dir (setq project (plist-put project 'src_dir (reverse src_dir))))
project
))
(defun ada-select-prj-file (file)
"Select FILE as the current project file."
(interactive)
(setq ada-prj-default-project-file (expand-file-name file))
(let ((casing (ada-xref-get-project-field 'casing)))
(if casing
(progn
;; FIXME: use ada-get-absolute-dir here
(setq ada-case-exception-file casing)
(ada-case-read-exceptions))))
(let ((ada_project_path (ada-xref-get-project-field 'ada_project_path)))
(if ada_project_path
;; FIXME: use ada-get-absolute-dir, mapconcat here
(setenv "ADA_PROJECT_PATH" ada_project_path)))
(setq compilation-search-path (ada-xref-get-src-dir-field))
(setq ada-search-directories-internal
;; FIXME: why do we need directory-file-name here?
(append (mapcar 'directory-file-name compilation-search-path)
ada-search-directories))
;; return t, for decent display in message buffer when called interactively
t)
(defun ada-find-references (&optional pos arg local-only)
"Find all references to the entity under POS.
Calls gnatfind to find the references.
If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved.
If LOCAL-ONLY is non-nil, only declarations in the current file are returned."
(interactive "d\nP")
(ada-require-project-file)
(let* ((identlist (ada-read-identifier pos))
(alifile (ada-get-ali-file-name (ada-file-of identlist)))
(process-environment (ada-set-environment)))
(set-buffer (get-file-buffer (ada-file-of identlist)))
;; if the file is more recent than the executable
(if (or (buffer-modified-p (current-buffer))
(file-newer-than-file-p (ada-file-of identlist) alifile))
(ada-find-any-references (ada-name-of identlist)
(ada-file-of identlist)
nil nil local-only arg)
(ada-find-any-references (ada-name-of identlist)
(ada-file-of identlist)
(ada-line-of identlist)
(ada-column-of identlist) local-only arg)))
)
(defun ada-find-local-references (&optional pos arg)
"Find all references to the entity under POS.
Calls `gnatfind' to find the references.
If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved."
(interactive "d\nP")
(ada-find-references pos arg t))
(defconst ada-gnatfind-buffer-name "*gnatfind*")
(defun ada-find-any-references
(entity &optional file line column local-only append)
"Search for references to any entity whose name is ENTITY.
ENTITY was first found the location given by FILE, LINE and COLUMN.
If LOCAL-ONLY is non-nil, then list only the references in FILE,
which is much faster.
If APPEND is non-nil, then append the output of the command to the
existing buffer `*gnatfind*', if there is one."
(interactive "sEntity name: ")
(ada-require-project-file)