-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathada-mode.el
5494 lines (4785 loc) · 183 KB
/
ada-mode.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-mode.el --- major-mode for editing Ada sources
;; Copyright (C) 1994-1995, 1997-2019 Free Software Foundation, Inc.
;; Author: Rolf Ebert <[email protected]>
;; Markus Heritsch <[email protected]>
;; Emmanuel Briot <[email protected]>
;; Maintainer: Stephen Leake <[email protected]>
;; Keywords: languages ada
;; Version: 4.0
;; 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 mode is a major mode for editing Ada code. This is a major
;; rewrite of the file packaged with Emacs-20. The Ada mode is
;; composed of four Lisp files: ada-mode.el, ada-xref.el, ada-prj.el
;; and ada-stmt.el. Only this file (ada-mode.el) is completely
;; independent from the GNU Ada compiler GNAT, distributed by Ada
;; Core Technologies. All the other files rely heavily on features
;; provided only by GNAT.
;;; Usage:
;; Emacs should enter Ada mode automatically when you load an Ada file.
;; By default, the valid extensions for Ada files are .ads, .adb or .ada
;; If the ada-mode does not start automatically, then simply type the
;; following command :
;; M-x ada-mode
;;
;; By default, ada-mode is configured to take full advantage of the GNAT
;; compiler (the menus will include the cross-referencing features,...).
;; If you are using another compiler, you might want to set the following
;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
;; won't work) :
;; (setq ada-which-compiler 'generic)
;;
;; This mode requires find-file.el to be present on your system.
;;; History:
;; The first Ada mode for GNU Emacs was written by V. Broman in
;; 1985. He based his work on the already existing Modula-2 mode.
;; This was distributed as ada.el in versions of Emacs prior to 19.29.
;;
;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
;; several files with support for dired commands and other nice
;; things. It is currently available from the PAL
;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
;;
;; The probably very first Ada mode (called electric-ada.el) was
;; written by Steven D. Litvintchouk and Steven M. Rosen for the
;; Gosling Emacs. L. Slater based his development on ada.el and
;; electric-ada.el.
;;
;; A complete rewrite by M. Heritsch and R. Ebert has been done.
;; Some ideas from the Ada mode mailing list have been
;; added. Some of the functionality of L. Slater's mode has not
;; (yet) been recoded in this new mode. Perhaps you prefer sticking
;; to his version.
;;
;; A complete rewrite for Emacs-20 / GNAT-3.11 has been done by Ada Core
;; Technologies.
;;; Credits:
;; Many thanks to John McCabe <[email protected]> for sending so
;; many patches included in this package.
;; Christian Egli <[email protected]>:
;; ada-imenu-generic-expression
;; Many thanks also to the following persons that have contributed
;; to the ada-mode
;; Philippe Waroquiers (PW) <[email protected]> in particular,
;; [email protected] (John Woodruff)
;; [email protected] (Jesper Joergensen)
;; [email protected] (Scott Evans)
;; [email protected] (Cyrille Comar)
;; [email protected] (Stephen Leake)
;; and others for their valuable hints.
;;; Code:
;; Note: Every function in this package is compiler-independent.
;; The names start with ada-
;; The variables that the user can edit can all be modified through
;; the customize mode. They are sorted in alphabetical order in this
;; file.
;; Supported packages.
;; This package supports a number of other Emacs modes. These other modes
;; should be loaded before the ada-mode, which will then setup some variables
;; to improve the support for Ada code.
;; Here is the list of these modes:
;; `which-function-mode': Display in the mode line the name of the subprogram
;; the cursor is in.
;; `outline-mode': Provides the capability to collapse or expand the code
;; for specific language constructs, for instance if you want to hide the
;; code corresponding to a subprogram
;; `align': This mode is now provided with Emacs 21, but can also be
;; installed manually for older versions of Emacs. It provides the
;; capability to automatically realign the selected region (for instance
;; all ':=', ':' and '--' will be aligned on top of each other.
;; `imenu': Provides a menu with the list of entities defined in the current
;; buffer, and an easy way to jump to any of them
;; `speedbar': Provides a separate file browser, and the capability for each
;; file to see the list of entities defined in it and to jump to them
;; easily
;; `abbrev-mode': Provides the capability to define abbreviations, which
;; are automatically expanded when you type them. See the Emacs manual.
(require 'find-file nil t)
(require 'align nil t)
(require 'which-func nil t)
(require 'compile nil t)
(defvar ispell-check-comments)
(defvar skeleton-further-elements)
(define-error 'ada-mode-errors nil)
(defun ada-mode-version ()
"Return Ada mode version."
(interactive)
(let ((version-string "4.00"))
(if (called-interactively-p 'interactive)
(message version-string)
version-string)))
(defvar ada-mode-hook nil
"List of functions to call when Ada mode is invoked.
This hook is automatically executed after the `ada-mode' is
fully loaded.
This is a good place to add Ada environment specific bindings.")
(defgroup ada nil
"Major mode for editing and compiling Ada source in Emacs."
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
:link '(custom-manual "(ada-mode) Top")
:link '(emacs-commentary-link :tag "Commentary" "ada-mode.el")
:group 'languages)
(defcustom ada-auto-case t
"Non-nil means automatically change case of preceding word while typing.
Casing is done according to `ada-case-keyword', `ada-case-identifier'
and `ada-case-attribute'."
:type 'boolean :group 'ada)
(defcustom ada-broken-decl-indent 0
"Number of columns to indent a broken declaration.
An example is :
declare
A,
>>>>>B : Integer;"
:type 'integer :group 'ada)
(defcustom ada-broken-indent 2
"Number of columns to indent the continuation of a broken line.
An example is :
My_Var : My_Type := (Field1 =>
>>>>>>>>>Value);"
:type 'integer :group 'ada)
(defcustom ada-continuation-indent ada-broken-indent
"Number of columns to indent the continuation of broken lines in parenthesis.
An example is :
Func (Param1,
>>>>>Param2);"
:type 'integer :group 'ada)
(defcustom ada-case-attribute 'ada-capitalize-word
"Function to call to adjust the case of Ada attributes.
It may be `downcase-word', `upcase-word', `ada-loose-case-word',
`ada-capitalize-word' or `ada-no-auto-case'."
:type '(choice (const downcase-word)
(const upcase-word)
(const ada-capitalize-word)
(const ada-loose-case-word)
(const ada-no-auto-case))
:group 'ada)
(defcustom ada-case-exception-file
(list (convert-standard-filename' "~/.emacs_case_exceptions"))
"List of special casing exceptions dictionaries for identifiers.
The first file is the one where new exceptions will be saved by Emacs
when you call `ada-create-case-exception'.
These files should contain one word per line, that gives the casing
to be used for that word in Ada files. If the line starts with the
character *, then the exception will be used for substrings that either
start at the beginning of a word or after a _ character, and end either
at the end of the word or at a _ character. Each line can be terminated
by a comment."
:type '(repeat (file))
:group 'ada)
(defcustom ada-case-keyword 'downcase-word
"Function to call to adjust the case of an Ada keywords.
It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
`ada-capitalize-word'."
:type '(choice (const downcase-word)
(const upcase-word)
(const ada-capitalize-word)
(const ada-loose-case-word)
(const ada-no-auto-case))
:group 'ada)
(defcustom ada-case-identifier 'ada-loose-case-word
"Function to call to adjust the case of an Ada identifier.
It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
`ada-capitalize-word'."
:type '(choice (const downcase-word)
(const upcase-word)
(const ada-capitalize-word)
(const ada-loose-case-word)
(const ada-no-auto-case))
:group 'ada)
(defcustom ada-clean-buffer-before-saving t
"Non-nil means remove trailing spaces and untabify the buffer before saving."
:type 'boolean :group 'ada)
(make-obsolete-variable 'ada-clean-buffer-before-saving
"it has no effect - use `write-file-functions' hook."
"23.2")
(defcustom ada-indent 3
"Size of Ada indentation.
An example is :
procedure Foo is
begin
>>>>>>>>>>null;"
:type 'integer :group 'ada)
(defcustom ada-indent-after-return t
"Non-nil means automatically indent after RET or LFD."
:type 'boolean :group 'ada)
(defcustom ada-indent-align-comments t
"Non-nil means align comments on previous line comments, if any.
If nil, indentation is calculated as usual.
Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
For instance:
A := 1; -- A multi-line comment
-- aligned if `ada-indent-align-comments' is t"
:type 'boolean :group 'ada)
(defcustom ada-indent-comment-as-code t
"Non-nil means indent comment lines as code.
A nil value means do not auto-indent comments."
:type 'boolean :group 'ada)
(defcustom ada-indent-handle-comment-special nil
"Non-nil if comment lines should be handled specially inside parenthesis.
By default, if the line that contains the open parenthesis has some
text following it, then the following lines will be indented in the
same column as this text. This will not be true if the first line is
a comment and `ada-indent-handle-comment-special' is t.
type A is
( Value_1, -- common behavior, when not a comment
Value_2);
type A is
( -- `ada-indent-handle-comment-special' is nil
Value_1,
Value_2);
type A is
( -- `ada-indent-handle-comment-special' is non-nil
Value_1,
Value_2);"
:type 'boolean :group 'ada)
(defcustom ada-indent-is-separate t
"Non-nil means indent `is separate' or `is abstract' if on a single line."
:type 'boolean :group 'ada)
(defcustom ada-indent-record-rel-type 3
"Indentation for `record' relative to `type' or `use'.
An example is:
type A is
>>>>>>>>>>>record"
:type 'integer :group 'ada)
(defcustom ada-indent-renames ada-broken-indent
"Indentation for renames relative to the matching function statement.
If `ada-indent-return' is null or negative, the indentation is done relative to
the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
An example is:
function A (B : Integer)
return C;
>>>renames Foo;"
:type 'integer :group 'ada)
(defcustom ada-indent-return 0
"Indentation for `return' relative to the matching `function' statement.
If `ada-indent-return' is null or negative, the indentation is done relative to
the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
An example is:
function A (B : Integer)
>>>>>return C;"
:type 'integer :group 'ada)
(defcustom ada-indent-to-open-paren t
"Non-nil means indent according to the innermost open parenthesis."
:type 'boolean :group 'ada)
(defcustom ada-fill-comment-prefix "-- "
"Text inserted in the first columns when filling a comment paragraph.
Note: if you modify this variable, you will have to invoke `ada-mode'
again to take account of the new value."
:type 'string :group 'ada)
(defcustom ada-fill-comment-postfix " --"
"Text inserted at the end of each line when filling a comment paragraph.
Used by `ada-fill-comment-paragraph-postfix'."
:type 'string :group 'ada)
(defcustom ada-label-indent -4
"Number of columns to indent a label.
An example is:
procedure Foo is
begin
>>>>Label:
This is also used for <<..>> labels"
:type 'integer :group 'ada)
(defcustom ada-language-version 'ada95
"Ada language version; one of `ada83', `ada95', `ada2005'."
:type '(choice (const ada83) (const ada95) (const ada2005)) :group 'ada)
(defcustom ada-move-to-declaration nil
"Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to `begin'."
:type 'boolean :group 'ada)
(defcustom ada-popup-key '[down-mouse-3]
"Key used for binding the contextual menu.
If nil, no contextual menu is available."
:type '(restricted-sexp :match-alternatives (stringp vectorp))
:group 'ada)
(defcustom ada-search-directories
(append '(".")
(split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
'("/usr/adainclude" "/usr/local/adainclude"
"/opt/gnu/adainclude"))
"Default list of directories to search for Ada files.
See the description for the `ff-search-directories' variable. This variable
is the initial value of `ada-search-directories-internal'."
:type '(repeat (choice :tag "Directory"
(const :tag "default" nil)
(directory :format "%v")))
:group 'ada)
(defvar ada-search-directories-internal ada-search-directories
"Internal version of `ada-search-directories'.
Its value is the concatenation of the search path as read in the project file
and the standard runtime location, and the value of the user-defined
`ada-search-directories'.")
(defcustom ada-stmt-end-indent 0
"Number of columns to indent the end of a statement on a separate line.
An example is:
if A = B
>>>>then"
:type 'integer :group 'ada)
(defcustom ada-tab-policy 'indent-auto
"Control the behavior of the TAB key.
Must be one of :
`indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
`indent-auto' : use indentation functions in this file.
`always-tab' : do `indent-relative'."
:type '(choice (const indent-auto)
(const indent-rigidly)
(const always-tab))
:group 'ada)
(defcustom ada-use-indent ada-broken-indent
"Indentation for the lines in a `use' statement.
An example is:
use Ada.Text_IO,
>>>>Ada.Numerics;"
:type 'integer :group 'ada)
(defcustom ada-when-indent 3
"Indentation for `when' relative to `exception' or `case'.
An example is:
case A is
>>>>when B =>"
:type 'integer :group 'ada)
(defcustom ada-with-indent ada-broken-indent
"Indentation for the lines in a `with' statement.
An example is:
with Ada.Text_IO,
>>>>Ada.Numerics;"
:type 'integer :group 'ada)
(defcustom ada-which-compiler 'gnat
"Name of the compiler to use.
This will determine what features are made available through the Ada mode.
The possible choices are:
`gnat': Use Ada Core Technologies' GNAT compiler. Add some cross-referencing
features.
`generic': Use a generic compiler."
:type '(choice (const gnat)
(const generic))
:group 'ada)
;;; ---- end of user configurable variables
(defvar ada-body-suffixes '(".adb")
"List of possible suffixes for Ada body files.
The extensions should include a `.' if needed.")
(defvar ada-spec-suffixes '(".ads")
"List of possible suffixes for Ada spec files.
The extensions should include a `.' if needed.")
(defvar ada-mode-menu (make-sparse-keymap "Ada")
"Menu for Ada mode.")
(defvar ada-mode-map (make-sparse-keymap)
"Local keymap used for Ada mode.")
(defvar ada-mode-extra-map (make-sparse-keymap)
"Keymap used for non-standard keybindings.")
;; default is C-c C-q because it's free in ada-mode-map
(defvar ada-mode-extra-prefix "\C-c\C-q"
"Prefix key to access `ada-mode-extra-map' functions.")
(define-abbrev-table 'ada-mode-abbrev-table ()
"Local abbrev table for Ada mode.")
(eval-when-compile
;; These values are used in eval-when-compile expressions.
(defconst ada-83-string-keywords
'("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
"body" "case" "constant" "declare" "delay" "delta" "digits" "do"
"else" "elsif" "end" "entry" "exception" "exit" "for" "function"
"generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
"not" "null" "of" "or" "others" "out" "package" "pragma" "private"
"procedure" "raise" "range" "record" "rem" "renames" "return"
"reverse" "select" "separate" "subtype" "task" "terminate" "then"
"type" "use" "when" "while" "with" "xor")
"List of Ada 83 keywords.
Used to define `ada-*-keywords'.")
(defconst ada-95-string-keywords
'("abstract" "aliased" "protected" "requeue" "tagged" "until")
"List of keywords new in Ada 95.
Used to define `ada-*-keywords'.")
(defconst ada-2005-string-keywords
'("interface" "overriding" "synchronized")
"List of keywords new in Ada 2005.
Used to define `ada-*-keywords.'"))
(defvar ada-ret-binding nil
"Variable to save key binding of RET when casing is activated.")
(defvar ada-case-exception '()
"Alist of words (entities) that have special casing.")
(defvar ada-case-exception-substring '()
"Alist of substrings (entities) that have special casing.
The substrings are detected for word constituent when the word
is not itself in `ada-case-exception', and only for substrings that
either are at the beginning or end of the word, or start after `_'.")
(defvar ada-lfd-binding nil
"Variable to save key binding of LFD when casing is activated.")
(defvar ada-other-file-alist nil
"Variable used by `find-file' to find the name of the other package.
See `ff-other-file-alist'.")
(defvar ada-align-list
'(("[^:]\\(\\s-*\\):[^:]" 1 t)
("[^=]\\(\\s-+\\)=[^=]" 1 t)
("\\(\\s-*\\)use\\s-" 1)
("\\(\\s-*\\)--" 1))
"Ada support for align.el <= 2.2.
This variable provides regular expressions on which to align different lines.
See `align-mode-alist' for more information.")
(defvar ada-align-modes
'((ada-declaration
(regexp . "[^:]\\(\\s-*\\):[^:]")
(valid . (lambda() (not (ada-in-comment-p))))
(modes . '(ada-mode)))
(ada-assignment
(regexp . "[^=]\\(\\s-+\\)=[^=]")
(valid . (lambda() (not (ada-in-comment-p))))
(modes . '(ada-mode)))
(ada-comment
(regexp . "\\(\\s-*\\)--")
(modes . '(ada-mode)))
(ada-use
(regexp . "\\(\\s-*\\)use\\s-")
(valid . (lambda() (not (ada-in-comment-p))))
(modes . '(ada-mode)))
)
"Ada support for align.el >= 2.8.
This variable defines several rules to use to align different lines.")
(defconst ada-align-region-separate
(eval-when-compile
(concat
"^\\s-*\\($\\|\\("
"begin\\|"
"declare\\|"
"else\\|"
"end\\|"
"exception\\|"
"for\\|"
"function\\|"
"generic\\|"
"if\\|"
"is\\|"
"procedure\\|"
"record\\|"
"return\\|"
"type\\|"
"when"
"\\)\\>\\)"))
"See the variable `align-region-separate' for more information.")
;;; ---- Below are the regexp used in this package for parsing
(defconst ada-83-keywords
(eval-when-compile
(concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
"Regular expression matching Ada83 keywords.")
(defconst ada-95-keywords
(eval-when-compile
(concat "\\<" (regexp-opt
(append
ada-95-string-keywords
ada-83-string-keywords) t) "\\>"))
"Regular expression matching Ada95 keywords.")
(defconst ada-2005-keywords
(eval-when-compile
(concat "\\<" (regexp-opt
(append
ada-2005-string-keywords
ada-83-string-keywords
ada-95-string-keywords) t) "\\>"))
"Regular expression matching Ada2005 keywords.")
(defvar ada-keywords ada-2005-keywords
"Regular expression matching Ada keywords.")
;; FIXME: make this customizable
(defconst ada-ident-re
"[[:alpha:]]\\(?:[_[:alnum:]]\\)*"
;; [:alnum:] matches any multibyte word constituent, as well as
;; Latin-1 letters and numbers. This allows __ and trailing _;
;; someone (emacs bug#1919) proposed [^\W_] to fix that, but \W does
;; _not_ mean "not word constituent" inside a character alternative.
"Regexp matching an Ada identifier.")
(defconst ada-goto-label-re
(concat "<<" ada-ident-re ">>")
"Regexp matching a goto label.")
(defconst ada-block-label-re
(concat ada-ident-re "[ \t\n]*:[^=]")
"Regexp matching a block label.
Note that this also matches a variable declaration.")
(defconst ada-label-re
(concat "\\(?:" ada-block-label-re "\\)\\|\\(?:" ada-goto-label-re "\\)")
"Regexp matching a goto or block label.")
;; "with" needs to be included in the regexp, to match generic subprogram parameters
;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
(defvar ada-procedure-start-regexp
(concat
"^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
;; subprogram name: operator ("[+/=*]")
"\\("
"\\(\"[^\"]+\"\\)"
;; subprogram name: name
"\\|"
"\\(\\(\\sw\\|[_.]\\)+\\)"
"\\)")
"Regexp matching Ada subprogram start.
The actual start is at (match-beginning 4). The name is in (match-string 5).")
(defconst ada-name-regexp
"\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
"Regexp matching a fully qualified name (including attribute).")
(defconst ada-package-start-regexp
(concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp)
"Regexp matching start of package.
The package name is in (match-string 4).")
(defconst ada-compile-goto-error-file-linenr-re
"\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
"Regexp matching filename:linenr[:column].")
;;; ---- regexps for indentation functions
(defvar ada-block-start-re
(eval-when-compile
(concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
"exception" "generic" "loop" "or"
"private" "select" ))
"\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
"Regexp for keywords starting Ada blocks.")
(defvar ada-end-stmt-re
(eval-when-compile
(concat "\\("
";" "\\|"
"=>[ \t]*$" "\\|"
"=>[ \t]*--.*$" "\\|"
"^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
"\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
"loop" "private" "record" "select"
"then abort" "then") t) "\\>" "\\|"
"^[ \t]*" (regexp-opt '("function" "package" "procedure")
t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
"^[ \t]*exception\\>"
"\\)") )
"Regexp of possible ends for a non-broken statement.
A new statement starts after these.")
(defvar ada-matching-start-re
(eval-when-compile
(concat "\\<"
(regexp-opt
'("end" "loop" "select" "begin" "case" "do" "declare"
"if" "task" "package" "procedure" "function" "record" "protected") t)
"\\>"))
"Regexp used in `ada-goto-matching-start'.")
(defvar ada-loop-start-re
"\\<\\(for\\|while\\|loop\\)\\>"
"Regexp for the start of a loop.")
(defvar ada-subprog-start-re
(eval-when-compile
(concat "\\<" (regexp-opt '("accept" "entry" "function" "overriding" "package" "procedure"
"protected" "task") t) "\\>"))
"Regexp for the start of a subprogram.")
(defvar ada-contextual-menu-on-identifier nil
"Set to true when the right mouse button was clicked on an identifier.")
(defvar ada-contextual-menu-last-point nil
"Position of point just before displaying the menu.
This is a list (point buffer).
Since `ada-popup-menu' moves the point where the user clicked, the region
is modified. Therefore no command from the menu knows what the user selected
before displaying the contextual menu.
To get the original region, restore the point to this position before
calling `region-end' and `region-beginning'.
Modify this variable if you want to restore the point to another position.")
(easy-menu-define ada-contextual-menu nil
"Menu to use when the user presses the right mouse button.
The variable `ada-contextual-menu-on-identifier' will be set to t before
displaying the menu if point was on an identifier."
'("Ada"
["Goto Declaration/Body" ada-point-and-xref
:included ada-contextual-menu-on-identifier]
["Goto Body" ada-point-and-xref-body
:included ada-contextual-menu-on-identifier]
["Goto Previous Reference" ada-xref-goto-previous-reference]
["List References" ada-find-references
:included ada-contextual-menu-on-identifier]
["List Local References" ada-find-local-references
:included ada-contextual-menu-on-identifier]
["-" nil nil]
["Other File" ff-find-other-file]
["Goto Parent Unit" ada-goto-parent]))
;;------------------------------------------------------------------
;; Support for imenu (see imenu.el)
;;------------------------------------------------------------------
(defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
(defconst ada-imenu-subprogram-menu-re
(concat "^[ \t]*\\(overriding[ \t]*\\)?\\(procedure\\|function\\)[ \t\n]+"
"\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
ada-imenu-comment-re
"\\)[ \t\n]*"
"\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
(defvar ada-imenu-generic-expression
(list
(list nil ada-imenu-subprogram-menu-re 3)
(list "*Specs*"
(concat
"^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
"\\("
"\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
ada-imenu-comment-re "\\)";; parameter list or simple space
"\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
"\\)?;") 2)
'("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
'("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
'("*Protected*"
"^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
'("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
"Imenu generic expression for Ada mode.
See `imenu-generic-expression'. This variable will create several submenus for
each type of entity that can be found in an Ada file.")
;;------------------------------------------------------------
;; Support for compile.el
;;------------------------------------------------------------
(defun ada-compile-mouse-goto-error ()
"Mouse interface for `ada-compile-goto-error'."
(interactive)
(mouse-set-point last-input-event)
(ada-compile-goto-error (point))
)
(defun ada-compile-goto-error (pos)
"Replace `compile-goto-error' from compile.el.
If POS is on a file and line location, go to this position. It adds
to compile.el the capacity to go to a reference in an error message.
For instance, on these lines:
foo.adb:61:11: [...] in call to size declared at foo.ads:11
foo.adb:61:11: [...] in call to local declared at line 20
the 4 file locations can be clicked on and jumped to."
(interactive "d")
(goto-char pos)
(skip-chars-backward "-a-zA-Z0-9_:./\\\\")
(cond
;; special case: looking at a filename:line not at the beginning of a line
;; or a simple line reference "at line ..."
((and (not (bolp))
(or (looking-at ada-compile-goto-error-file-linenr-re)
(and
(save-excursion
(beginning-of-line)
(looking-at ada-compile-goto-error-file-linenr-re))
(save-excursion
(if (looking-at "\\([0-9]+\\)") (backward-word-strictly 1))
(looking-at "line \\([0-9]+\\)"))))
)
(let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
(file (if (match-beginning 2) (match-string 1)
(save-excursion (beginning-of-line)
(looking-at ada-compile-goto-error-file-linenr-re)
(match-string 1))))
(error-pos (point-marker))
source)
;; set source marker
(save-excursion
(compilation-find-file (point-marker) (match-string 1) "./")
(set-buffer file)
(when (stringp line)
(goto-char (point-min))
(forward-line (1- (string-to-number line))))
(setq source (point-marker)))
(compilation-goto-locus error-pos source nil)
))
;; otherwise, default behavior
(t
(compile-goto-error))
)
(recenter))
;;-------------------------------------------------------------------------
;; Grammar related function
;; The functions below work with the syntax class of the characters in an Ada
;; buffer. Two syntax tables are created, depending on whether we want '_'
;; to be considered as part of a word or not.
;; Some characters may have multiple meanings depending on the context:
;; - ' is either the beginning of a constant character or an attribute
;; - # is either part of a based literal or a gnatprep statement.
;; - " starts a string, but not if inside a constant character.
;; - ( and ) should be ignored if inside a constant character.
;; Thus their syntax property is changed automatically, and we can still use
;; the standard Emacs functions for sexp (see `ada-in-string-p')
;;
;; On Emacs, this is done through the `syntax-table' text property. The
;; corresponding action is applied automatically each time the buffer
;; changes via syntax-propertize-function.
;;
;; on XEmacs, the `syntax-table' property does not exist and we have to use a
;; slow advice to `parse-partial-sexp' to do the same thing.
;; When executing parse-partial-sexp, we simply modify the strings before and
;; after, so that the special constants '"', '(' and ')' do not interact
;; with parse-partial-sexp.
;; Note: this code is slow and needs to be rewritten as soon as something
;; better is available on XEmacs.
;;-------------------------------------------------------------------------
(defvar ada-mode-syntax-table
(let ((st (make-syntax-table)))
;; Define string brackets (`%' is alternative string bracket, but
;; almost never used as such and throws font-lock and indentation
;; off the track.)
(modify-syntax-entry ?% "$" st)
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?\; "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?\| "." st)
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?* "." st)
(modify-syntax-entry ?/ "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?$ "." st)
(modify-syntax-entry ?\[ "." st)
(modify-syntax-entry ?\] "." st)
(modify-syntax-entry ?\{ "." st)
(modify-syntax-entry ?\} "." st)
(modify-syntax-entry ?. "." st)
(modify-syntax-entry ?\\ "." st)
(modify-syntax-entry ?\' "." st)
;; A single hyphen is punctuation, but a double hyphen starts a comment.
(modify-syntax-entry ?- ". 12" st)
;; See the comment above on grammar related function for the special
;; setup for '#'.
(modify-syntax-entry ?# (if (featurep 'xemacs) "<" "$") st)
;; And \f and \n end a comment.
(modify-syntax-entry ?\f "> " st)
(modify-syntax-entry ?\n "> " st)
;; Define what belongs in Ada symbols.
(modify-syntax-entry ?_ "_" st)
;; Define parentheses to match.
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
st)
"Syntax table to be used for editing Ada source code.")
(defvar ada-mode-symbol-syntax-table
(let ((st (make-syntax-table ada-mode-syntax-table)))
(modify-syntax-entry ?_ "w" st)
st)
"Syntax table for Ada, where `_' is a word constituent.")
;; Support of special characters in XEmacs (see the comments at the beginning
;; of the section on Grammar related functions).
(if (featurep 'xemacs)
(defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
"Handles special character constants and gnatprep statements."
(let (change)
(if (< to from)
(let ((tmp from))
(setq from to to tmp)))
(save-excursion
(goto-char from)
(while (re-search-forward "'\\([(\")#]\\)'" to t)
(setq change (cons (list (match-beginning 1)
1
(match-string 1))
change))
(replace-match "'A'"))
(goto-char from)
(while (re-search-forward "\\(#[[:xdigit:]]*#\\)" to t)
(setq change (cons (list (match-beginning 1)
(length (match-string 1))
(match-string 1))
change))
(replace-match (make-string (length (match-string 1)) ?@))))
ad-do-it
(save-excursion
(while change
(goto-char (caar change))
(delete-char (cadar change))
(insert (caddar change))
(setq change (cdr change)))))))
(unless (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
;; Before `syntax-propertize', we had to use font-lock to apply syntax-table
;; properties, and in some cases we even had to do it manually (in
;; `ada-after-change-function'). `ada-handle-syntax-table-properties'
;; decides which method to use.
(defun ada-set-syntax-table-properties ()
"Assign `syntax-table' properties in accessible part of buffer.
In particular, character constants are said to be strings, #...#
are treated as numbers instead of gnatprep comments."
(let ((modified (buffer-modified-p))
(buffer-undo-list t)
(inhibit-read-only t)
(inhibit-point-motion-hooks t)
(inhibit-modification-hooks t))
(remove-text-properties (point-min) (point-max) '(syntax-table nil))
(goto-char (point-min))
(while (re-search-forward
;; The following regexp was adapted from
;; `ada-font-lock-syntactic-keywords'.
"^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
nil t)
(if (match-beginning 1)
(put-text-property
(match-beginning 1) (match-end 1) 'syntax-table '(11 . ?\n))
(put-text-property
(match-beginning 2) (match-end 2) 'syntax-table '(7 . ?'))
(put-text-property
(match-beginning 3) (match-end 3) 'syntax-table '(7 . ?'))))
(unless modified
(restore-buffer-modified-p nil))))
(defun ada-after-change-function (beg end _old-len)
"Called when the region between BEG and END was changed in the buffer.
OLD-LEN indicates what the length of the replaced text was."
(save-excursion
(save-restriction
(let ((from (progn (goto-char beg) (line-beginning-position)))
(to (progn (goto-char end) (line-end-position))))
(narrow-to-region from to)
(save-match-data
(ada-set-syntax-table-properties))))))
(defun ada-initialize-syntax-table-properties ()
"Assign `syntax-table' properties in current buffer."
(save-excursion
(save-restriction
(widen)
(save-match-data
(ada-set-syntax-table-properties))))
(add-hook 'after-change-functions 'ada-after-change-function nil t))
(defun ada-handle-syntax-table-properties ()
"Handle `syntax-table' properties."
(if font-lock-mode
;; `font-lock-mode' will take care of `syntax-table' properties.
(remove-hook 'after-change-functions 'ada-after-change-function t)
;; Take care of `syntax-table' properties manually.
(ada-initialize-syntax-table-properties)))
) ;;(not (fboundp 'syntax-propertize))
;;------------------------------------------------------------------
;; Testing the grammatical context
;;------------------------------------------------------------------
(defsubst ada-in-comment-p (&optional parse-result)
"Return t if inside a comment.
If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
(nth 4 (or parse-result
(parse-partial-sexp
(line-beginning-position) (point)))))
(defsubst ada-in-string-p (&optional parse-result)