]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/gpr-mode.el
release ada-mode 5.1.0, wisi 1.0.1
[gnu-emacs-elpa] / packages / ada-mode / gpr-mode.el
1 ;;; gpr-mode --- major-mode for editing GNAT project files
2
3 ;; Copyright (C) 2004, 2007, 2008, 2012-2014 Free Software Foundation, Inc.
4
5 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
6 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; gpr-mode is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; gpr-mode is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24 ;;
25 ;;; Commentary:
26 ;;
27 ;;; History:
28 ;;
29 ;; The first gpr-mode was written by Rolf Ebert
30 ;; <rolf.ebert_nosp...@gmx.net> in 2004.
31 ;;
32 ;; Stephen Leake <stephen_leake@member.fsf.org> rewrote it in 2013 to
33 ;; use the wisi indentation engine.
34 ;;
35 ;;;;; Code:
36
37 ;; we reuse several ada-mode functions
38 (require 'ada-mode)
39
40 (defvar gpr-mode-map
41 (let ((map (make-sparse-keymap)))
42 ;; C-c <letter> are reserved for users
43
44 ;; global-map has C-x ` 'next-error
45 (define-key map [return] 'ada-indent-newline-indent)
46 (define-key map "\C-c`" 'ada-show-secondary-error)
47 ;; comment-dwim is in global map on M-;
48 (define-key map "\C-c\C-c" 'compile)
49 (define-key map "\C-c\C-e" 'gpr-expand)
50 (define-key map "\C-c\C-f" 'gpr-show-parse-error)
51 (define-key map "\C-c\C-i" 'gpr-indent-statement)
52 ;; FIXME (later): implement?
53 ;; (define-key map "\C-c\C-n" 'ada-next-statement-keyword)
54 ;; (define-key map "\C-c\C-p" 'ada-prev-statement-keyword)
55 (define-key map "\C-c\C-o" 'ff-find-other-file)
56 (define-key map "\C-c\C-S-p" 'gpr-set-as-project)
57 (define-key map "\C-c\C-t" 'ada-case-read-all-exceptions)
58 (define-key map "\C-c\C-w" 'ada-case-adjust-at-point)
59 (define-key map "\C-c\C-y" 'ada-case-create-exception)
60 (define-key map "\C-c\C-\M-y" (lambda () (ada-case-create-exception nil nil t)))
61 map
62 ) "Local keymap used for GPR mode.")
63
64 (defvar gpr-mode-menu (make-sparse-keymap "gpr"))
65 (easy-menu-define gpr-mode-menu gpr-mode-map "Menu keymap for gpr mode"
66 '("gpr"
67 ("Help"
68 ["gpr Mode" (info "gpr-mode") t]
69 ["GNAT Reference Manual" (info "gnat_rm") t]
70 ["GNAT User Guide" (info "gnat_ugn") t]
71 ["Key bindings" describe-bindings t]
72 )
73
74 ["Customize" (customize-group 'ada)];; we reuse the Ada indentation options
75 ["------" nil nil]
76 ["Find and select project ..." ada-build-prompt-select-prj-file t]
77 ["Select project ..." ada-prj-select t]
78 ["Set as current project" gpr-set-as-project t]
79 ["Show current project" ada-prj-show t]
80 ["Next compilation error" next-error t]
81 ["Show secondary error" ada-show-secondary-error t]
82 ["Show last parse error" gpr-show-parse-error t]
83 ["Other file" ff-find-other-file t]
84 ("Edit"
85 ["Indent Line or selection" indent-for-tab-command t]
86 ["Indent current statement" gpr-indent-statement t]
87 ["Indent Lines in File" (indent-region (point-min) (point-max)) t]
88 ["Expand skeleton" gpr-expand t]
89 ["Comment/uncomment selection" comment-dwim t]
90 ["Fill Comment Paragraph" fill-paragraph t]
91
92 ["Fill Comment Paragraph Justify" ada-fill-comment-paragraph-justify t]
93 ["Fill Comment Paragraph Postfix" ada-fill-comment-paragraph-postfix t]
94 )
95 ))
96
97 (defvar gpr-show-parse-error nil
98 ;; Supplied by indentation engine parser
99 "Function to show last error reported by indentation parser."
100 )
101
102 (defun gpr-show-parse-error ()
103 (interactive)
104 (when gpr-show-parse-error
105 (funcall gpr-show-parse-error)))
106
107 (defvar gpr-expand nil
108 ;; skeleton function
109 "Function to call to expand tokens (ie insert skeletons).")
110
111 (defun gpr-expand ()
112 "Expand previous word into a statement skeleton."
113 (interactive)
114 (when gpr-expand
115 (funcall gpr-expand)))
116
117 (defvar gpr-indent-statement nil
118 ;; indentation function
119 "Function to indent the statement/declaration point is in or after.
120 Function is called with no arguments.")
121
122 (defun gpr-indent-statement ()
123 "Indent current statement."
124 (interactive)
125 (when gpr-indent-statement
126 (funcall gpr-indent-statement)))
127
128 (defvar gpr-font-lock-keywords
129 (progn
130 (list
131 ;;
132 ;; keyword plus name.
133 (list (concat
134 "\\<\\("
135 "package\\|"
136 "project\\|"
137 "for"
138 "\\)\\>[ \t]*"
139 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
140 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
141 ;;
142 ;; Main keywords
143 (list (concat "\\<"
144 (regexp-opt
145 '("abstract" "aggregate" "case" "configuration" "external" "is" "library" "null" "others"
146 "renames" "standard" "type" "use" "when" "with") t)
147 "\\>")
148 '(1 font-lock-keyword-face))
149 ;;
150 ;; Anything following end and not already fontified is a body name.
151 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
152 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
153 ;;
154 ))
155 "Expressions to highlight in gpr mode.")
156
157 (defun gpr-ff-special-with ()
158 (ada-require-project-file)
159 (let ((project-path (match-string 1)))
160 ;; project-path may be any of "foo", "foo.gpr", "../foo.gpr"
161 ;;
162 ;; The result of ff-special-constructs is used by
163 ;; ff-find-the-other-file with ff-search-directories and nil
164 ;; suffix list, so it must contain the relative path and the
165 ;; suffix
166 (if (file-name-extension project-path)
167 project-path
168 (concat project-path ".gpr"))
169 ))
170
171 (defun gpr-set-ff-special-constructs ()
172 "Add gpr-specific pairs to `ff-special-constructs'."
173 (set (make-local-variable 'ff-special-constructs) nil)
174 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair))
175 ;; Each car is a regexp; if it matches at point, the cdr is
176 ;; invoked. Each cdr should return the absolute file name to
177 ;; go to.
178 (list
179 ;; A "with" clause; allow "foo_bar.gpr" and "../foo"
180 (cons "^with[ \t]+\"\\(\\(?:\\(?:\\sw\\|\\s.\\)\\|\\s_\\)+\\)\";"
181 'gpr-ff-special-with)
182 )))
183
184 (defvar gpr-which-function nil
185 ;; supplied by the indentation engine
186 "Function called with no parameters; it should return the name
187 of the package or project point is in or just after, or nil.")
188
189 (defun gpr-which-function ()
190 "See `gpr-which-function' variable."
191 (when gpr-which-function
192 (funcall gpr-which-function)))
193
194 (defun gpr-add-log-current-function ()
195 "For `add-log-current-defun-function'. Returns enclosing package or project name."
196 ;; add-log-current-defun is typically called with point at the start
197 ;; of an ediff change section, which is before the start of the
198 ;; declaration of a new item. So go to the end of the current line
199 ;; first
200 (save-excursion
201 (end-of-line 1)
202 (gpr-which-function)))
203
204 (defun gpr-set-as-project (&optional file)
205 "Set FILE (default current buffer file) as Emacs project file."
206 (interactive)
207 (ada-parse-prj-file (or file (buffer-file-name)))
208 (ada-select-prj-file (or file (buffer-file-name))))
209
210 ;;;;
211 ;;;###autoload
212 (defun gpr-mode ()
213 "The major mode for editing GNAT project files."
214
215 (interactive)
216 (kill-all-local-variables)
217 (setq major-mode 'gpr-mode)
218 (setq mode-name "GNAT Project")
219 (use-local-map gpr-mode-map)
220 (set-syntax-table ada-mode-syntax-table)
221 (set (make-local-variable 'syntax-begin-function) nil)
222 (set 'case-fold-search t); gpr is case insensitive; the syntax parsing requires this setting
223 (set (make-local-variable 'comment-start) "--")
224 (set (make-local-variable 'comment-end) "")
225 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
226 (set (make-local-variable 'comment-multi-line) nil)
227
228 (set (make-local-variable 'require-final-newline) t)
229
230 (set (make-local-variable 'font-lock-defaults)
231 '(gpr-font-lock-keywords
232 nil t
233 ((?\_ . "w"))))
234
235 (gpr-set-ff-special-constructs)
236 (setq ff-search-directories 'ada-project-search-path)
237
238 (set (make-local-variable 'add-log-current-defun-function)
239 'gpr-add-log-current-function)
240
241 ;; used by autofill to break a comment line and continue it on
242 ;; another line. The reason we need this one is that the default
243 ;; behavior does not work correctly with the definition of
244 ;; paragraph-start above when the comment is right after a
245 ;; multi-line subprogram declaration (the comments are aligned under
246 ;; the latest parameter, not under the declaration start).
247 ;; FIXME: need test - should be in gpr-wisi? why doesn't ada-mode do this?
248 (set (make-local-variable 'comment-line-break-function)
249 (lambda (&optional soft) (let ((fill-prefix nil))
250 (indent-new-comment-line soft))))
251
252 (run-hooks 'gpr-mode-hook)
253
254 )
255
256 ;;;###autoload
257 (add-to-list 'auto-mode-alist '("\\.gpr\\'" . gpr-mode)) ; GNAT project files
258
259 (provide 'gpr-mode)
260
261 (unless (featurep 'gpr-indent-engine)
262 (require 'gpr-wisi))
263
264 (unless (featurep 'gpr-skeletons)
265 (require 'gpr-skel))
266
267 ;;; end of file