Merge branch 'master' into comment-cache
[emacs.git] / lisp / progmodes / ada-prj.el
blobf1b908750443f3cb807b654ded1309da27d3c591
1 ;;; ada-prj.el --- GUI editing of project files for the ada-mode
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Emmanuel Briot <briot@gnat.com>
6 ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
7 ;; Keywords: languages, ada, project file
8 ;; Package: ada-mode
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides a set of functions to easily edit the project
28 ;; files used by the ada-mode.
29 ;; The only function publicly available here is `ada-customize'.
30 ;; See the documentation of the Ada mode for more information on the project
31 ;; files.
32 ;; Internally, a project file is represented as a property list, with each
33 ;; field of the project file matching one property of the list.
35 ;;; Code:
38 ;; ----- Requirements -----------------------------------------------------
40 (require 'cus-edit)
41 (require 'ada-xref)
43 (eval-when-compile
44 (require 'ada-mode))
45 (eval-when-compile (require 'cl-lib))
47 ;; ----- Buffer local variables -------------------------------------------
49 (defvar ada-prj-current-values nil
50 "Hold the current value of the fields, This is a property list.")
51 (make-variable-buffer-local 'ada-prj-current-values)
53 (defvar ada-prj-default-values nil
54 "Hold the default value for the fields, This is a property list.")
55 (make-variable-buffer-local 'ada-prj-default-values)
57 (defvar ada-prj-ada-buffer nil
58 "Indicates what Ada source file was being edited.")
60 (defvar ada-old-cross-prefix nil
61 "The cross-prefix associated with the currently loaded runtime library.")
64 ;; ----- Functions --------------------------------------------------------
66 (defun ada-prj-new ()
67 "Open a new project file."
68 (interactive)
69 (let* ((prj
70 (if (and ada-prj-default-project-file
71 (not (string= ada-prj-default-project-file "")))
72 ada-prj-default-project-file
73 "default.adp"))
74 (filename (read-file-name "Project file: "
75 (if prj "" nil)
76 nil
77 nil
78 prj)))
79 (if (not (string= (file-name-extension filename t) ".adp"))
80 (error "File name extension for project files must be .adp"))
82 (ada-customize nil filename)))
84 (defun ada-prj-edit ()
85 "Editing the project file associated with the current Ada buffer.
86 If there is none, opens a new project file."
87 (interactive)
88 (if ada-prj-default-project-file
89 (ada-customize)
90 (ada-prj-new)))
92 (defun ada-prj-initialize-values (symbol _ada-buffer filename)
93 "Set SYMBOL to the property list of the project file FILENAME.
94 If FILENAME is null, read the file associated with ADA-BUFFER.
95 If no project file is found, return the default values."
96 ;; FIXME: rationalize arguments; make ada-buffer optional?
97 (if (and filename
98 (not (string= filename ""))
99 (assoc filename ada-xref-project-files))
100 (set symbol (copy-sequence (cdr (assoc filename ada-xref-project-files))))
102 ;; Set default values (except for the file name if this was given
103 ;; in the buffer
104 (set symbol (ada-default-prj-properties))
105 (if (and filename (not (string= filename "")))
106 (set symbol (plist-put (eval symbol) 'filename filename)))
110 (defun ada-prj-save-specific-option (field)
111 "Return the string to print in the project file to save FIELD.
112 If the current value of FIELD is the default value, return an empty string."
113 (if (string= (plist-get ada-prj-current-values field)
114 (plist-get ada-prj-default-values field))
116 (concat (symbol-name field)
117 "=" (plist-get ada-prj-current-values field) "\n")))
119 (defun ada-prj-save ()
120 "Save the edited project file."
121 (interactive)
122 (let ((file-name (or (plist-get ada-prj-current-values 'filename)
123 (read-file-name "Save project as: ")))
124 output)
125 (setq output
126 (concat
128 ;; Save the fields that do not depend on the current buffer
129 ;; only if they are different from the default value
131 (ada-prj-save-specific-option 'comp_opt)
132 (ada-prj-save-specific-option 'bind_opt)
133 (ada-prj-save-specific-option 'link_opt)
134 (ada-prj-save-specific-option 'gnatmake_opt)
135 (ada-prj-save-specific-option 'gnatfind_opt)
136 (ada-prj-save-specific-option 'cross_prefix)
137 (ada-prj-save-specific-option 'remote_machine)
138 (ada-prj-save-specific-option 'debug_cmd)
140 ;; Always save the fields that depend on the current buffer
141 "main=" (plist-get ada-prj-current-values 'main) "\n"
142 "build_dir=" (plist-get ada-prj-current-values 'build_dir) "\n"
143 (ada-prj-set-list "check_cmd"
144 (plist-get ada-prj-current-values 'check_cmd)) "\n"
145 (ada-prj-set-list "make_cmd"
146 (plist-get ada-prj-current-values 'make_cmd)) "\n"
147 (ada-prj-set-list "comp_cmd"
148 (plist-get ada-prj-current-values 'comp_cmd)) "\n"
149 (ada-prj-set-list "run_cmd"
150 (plist-get ada-prj-current-values 'run_cmd)) "\n"
151 (ada-prj-set-list "src_dir"
152 (plist-get ada-prj-current-values 'src_dir)
153 t) "\n"
154 (ada-prj-set-list "obj_dir"
155 (plist-get ada-prj-current-values 'obj_dir)
156 t) "\n"
157 (ada-prj-set-list "debug_pre_cmd"
158 (plist-get ada-prj-current-values 'debug_pre_cmd))
159 "\n"
160 (ada-prj-set-list "debug_post_cmd"
161 (plist-get ada-prj-current-values 'debug_post_cmd))
162 "\n"
165 (find-file file-name)
166 (erase-buffer)
167 (insert output)
168 (save-buffer)
169 ;; kill the project buffer
170 (kill-buffer nil)
172 ;; kill the editor buffer
173 (kill-buffer "*Edit Ada Mode Project*")
175 ;; automatically set the new project file as the active one
176 (setq ada-prj-default-project-file file-name)
178 ;; force Emacs to reread the project files
179 (ada-reread-prj-file file-name)
183 (defun ada-prj-load-from-file (symbol)
184 "Load SYMBOL value from file.
185 One item per line should be found in the file."
186 (save-excursion
187 (let ((file (read-file-name "File name: " nil nil t))
188 (buffer (current-buffer))
189 line
190 list)
191 (find-file file)
192 (widen)
193 (goto-char (point-min))
194 (while (not (eobp))
195 (setq line (buffer-substring-no-properties (point) (point-at-eol)))
196 (cl-pushnew line list :test #'equal)
197 (forward-line 1))
198 (kill-buffer nil)
199 (set-buffer buffer)
200 (setq ada-prj-current-values
201 (plist-put ada-prj-current-values
202 symbol
203 (append (plist-get ada-prj-current-values symbol)
204 (reverse list)))))
205 (ada-prj-display-page 2)))
207 (defun ada-prj-subdirs-of (dir)
208 "Return a list of all the subdirectories of DIR, recursively."
209 (let ((subdirs (directory-files dir t "^[^.].*"))
210 (dirlist (list dir)))
211 (while subdirs
212 (if (file-directory-p (car subdirs))
213 (let ((sub (ada-prj-subdirs-of (car subdirs))))
214 (if sub
215 (setq dirlist (append sub dirlist)))))
216 (setq subdirs (cdr subdirs)))
217 dirlist))
219 (defun ada-prj-load-directory (field &optional file-name)
220 "Append to FIELD in the current project the subdirectories of FILE-NAME.
221 If FILE-NAME is nil, ask the user for the name."
223 ;; Do not use an external dialog for this, since it wouldn't allow
224 ;; the user to select a directory
225 (let ((use-dialog-box nil))
226 (unless file-name
227 (setq file-name (read-directory-name "Root directory: " nil nil t))))
229 (setq ada-prj-current-values
230 (plist-put ada-prj-current-values
231 field
232 (append (plist-get ada-prj-current-values field)
233 (reverse (ada-prj-subdirs-of
234 (expand-file-name file-name))))))
235 (ada-prj-display-page 2))
237 (defun ada-prj-display-page (tab-num)
238 "Display page TAB-NUM in the notebook.
239 The current buffer must be the project editing buffer."
241 (let ((inhibit-read-only t))
242 (erase-buffer))
244 ;; Widget support in Emacs 21 requires that we clear the buffer first
245 (if (and (not (featurep 'xemacs)) (>= emacs-major-version 21))
246 (progn
247 (setq widget-field-new nil
248 widget-field-list nil)
249 (mapc (lambda (x) (delete-overlay x)) (car (overlay-lists)))
250 (mapc (lambda (x) (delete-overlay x)) (cdr (overlay-lists)))))
252 ;; Display the tabs
254 (widget-insert "\n Project configuration.\n
255 ___________ ____________ ____________ ____________ ____________\n / ")
256 (widget-create 'push-button :notify
257 (lambda (&rest _dummy) (ada-prj-display-page 1)) "General")
258 (widget-insert " \\ / ")
259 (widget-create 'push-button :notify
260 (lambda (&rest _dummy) (ada-prj-display-page 2)) "Paths")
261 (widget-insert " \\ / ")
262 (widget-create 'push-button :notify
263 (lambda (&rest _dummy) (ada-prj-display-page 3)) "Switches")
264 (widget-insert " \\ / ")
265 (widget-create 'push-button :notify
266 (lambda (&rest _dummy) (ada-prj-display-page 4)) "Ada Menu")
267 (widget-insert " \\ / ")
268 (widget-create 'push-button :notify
269 (lambda (&rest _dummy) (ada-prj-display-page 5)) "Debugger")
270 (widget-insert " \\\n")
272 ;; Display the currently selected page
274 (cond
277 ;; First page (General)
279 ((= tab-num 1)
280 (widget-insert "/ \\/______________\\/______________\\/______________\\/______________\\\n")
282 (widget-insert "Project file name:\n")
283 (widget-insert (plist-get ada-prj-current-values 'filename))
284 (widget-insert "\n\n")
285 (ada-prj-field 'casing "Casing Exceptions"
286 "List of files that contain casing exception
287 dictionaries. All these files contain one
288 identifier per line, with a special casing.
289 The first file has the highest priority."
290 t nil
291 (mapconcat (lambda(x)
292 (concat " " x))
293 (ada-xref-get-project-field 'casing)
294 "\n")
296 (ada-prj-field 'main "Executable file name"
297 "Name of the executable generated when you
298 compile your application. This should include
299 the full directory name, using ${build_dir} if
300 you wish.")
301 (ada-prj-field 'build_dir "Build directory"
302 "Reference directory for relative paths in
303 src_dir and obj_dir below. This is also the directory
304 where the compilation is done.")
305 (ada-prj-field 'remote_machine "Name of the remote machine (if any)"
306 "If you want to remotely compile, debug and
307 run your application, specify the name of a
308 remote machine here. This capability requires
309 the `rsh' protocol on the remote machine.")
310 (ada-prj-field 'cross_prefix "Prefix used in for the cross tool chain"
311 "When working on multiple cross targets, it is
312 most convenient to specify the prefix of the
313 tool chain here. For instance, on PowerPc
314 vxworks, you would enter `powerpc-wrs-vxworks-'.
315 To use JGNAT, enter `j'.")
320 ;; Second page (Paths)
322 ((= tab-num 2)
323 (if (not (equal (plist-get ada-prj-current-values 'cross_prefix)
324 ada-old-cross-prefix))
325 (progn
326 (setq ada-old-cross-prefix
327 (plist-get ada-prj-current-values 'cross_prefix))
328 (ada-initialize-runtime-library ada-old-cross-prefix)))
331 (widget-insert "/_____________\\/ \\/______________\\/______________\\/______________\\\n")
332 (ada-prj-field 'src_dir "Source directories"
333 "Enter the list of directories where your Ada
334 sources can be found. These directories will be
335 used for the cross-references and for the default
336 compilation commands.
337 Note that src_dir includes both the build directory
338 and the standard runtime."
340 (mapconcat (lambda(x)
341 (concat " " x))
342 ada-xref-runtime-library-specs-path
343 "\n")
345 (widget-insert "\n\n")
347 (ada-prj-field 'obj_dir "Object directories"
348 "Enter the list of directories where the GNAT
349 library files (ALI files) can be found. These
350 files are used for cross-references and by the
351 gnatmake command.
352 Note that obj_dir includes both the build directory
353 and the standard runtime."
355 (mapconcat (lambda(x)
356 (concat " " x))
357 ada-xref-runtime-library-ali-path
358 "\n")
360 (widget-insert "\n\n")
364 ;; Third page (Switches)
366 ((= tab-num 3)
367 (widget-insert "/_____________\\/______________\\/ \\/______________\\/______________\\\n")
368 (ada-prj-field 'comp_opt "Switches for the compiler"
369 "These switches are used in the default
370 compilation commands, both for compiling a
371 single file and rebuilding the whole project")
372 (ada-prj-field 'bind_opt "Switches for the binder"
373 "These switches are used in the default build
374 command and are passed to the binder")
375 (ada-prj-field 'link_opt "Switches for the linker"
376 "These switches are used in the default build
377 command and are passed to the linker")
378 (ada-prj-field 'gnatmake_opt "Switches for gnatmake"
379 "These switches are used in the default gnatmake
380 command.")
381 (ada-prj-field 'gnatfind_opt "Switches for gnatfind"
382 "The command gnatfind is run every time the Ada/Goto/List_References menu.
383 You should for instance add -a if you are working in an environment
384 where most ALI files are write-protected, since otherwise they get
385 ignored by gnatfind and you don't see the references within.")
389 ;; Fourth page
391 ((= tab-num 4)
392 (widget-insert "/_____________\\/______________\\/______________\\/ \\/______________\\\n")
393 (widget-insert
394 "All the fields below can use variable substitution. The syntax is ${name},
395 where name is the name that appears after the Help buttons in this buffer. As
396 a special case, ${current} is replaced with the name of the file currently
397 edited, with directory name but no extension, whereas ${full_current} is
398 replaced with the name of the current file with directory name and
399 extension.\n")
400 (widget-insert
401 "The environment variables ADA_INCLUDE_PATH and ADA_OBJECTS_PATH are set to
402 ${src_dir} and ${obj_dir} before running the compilation commands, so that you
403 don't need to specify the -aI and -aO switches on the command line\n")
404 (widget-insert
405 "You can reference any environment variable using the same ${...} syntax as
406 above, and put the name of the variable between the quotes.\n\n")
407 (ada-prj-field 'check_cmd
408 "Check syntax of a single file (menu Ada->Check File)"
409 "This command is run to check the syntax and semantics of a file.
410 The file name is added at the end of this command." t)
411 (ada-prj-field 'comp_cmd
412 "Compiling a single file (menu Ada->Compile File)"
413 "This command is run when the recompilation
414 of a single file is needed. The file name is
415 added at the end of this command." t)
416 (ada-prj-field 'make_cmd "Rebuilding the whole project (menu Ada->Build)"
417 "This command is run when you want to rebuild
418 your whole application. It is never issues
419 automatically and you will need to ask for it.
420 If remote_machine has been set, this command
421 will be executed on the remote machine." t)
422 (ada-prj-field 'run_cmd "Running the application (menu Ada->Run)"
423 "This command specifies how to run the
424 application, including any switch you need to
425 specify. If remote_machine has been set, this
426 command will be executed on the remote host." t)
430 ;; Fifth page
432 ((= tab-num 5)
433 (widget-insert "/_____________\\/______________\\/______________\\/______________\\/ \\\n")
434 (ada-prj-field 'debug_pre_cmd "Commands to execute before launching the
435 debugger"
436 "The following commands are executed one after the other before starting
437 the debugger. These can be used to set up your environment." t)
439 (ada-prj-field 'debug_cmd "Debugging the application"
440 "Specifies how to debug the application, possibly
441 remotely if remote_machine has been set. We
442 recommend the following debuggers:
443 > gdb
444 > gvd --tty
445 > ddd --tty -fullname -toolbar")
447 (ada-prj-field 'debug_post_cmd "Commands to execute in the debugger"
448 "The following commands are executed one in the debugger once it has been
449 started. These can be used to initialize the debugger, for instance to
450 connect to the target when working with cross-environments" t)
456 (widget-insert "______________________________________________________________________\n\n ")
457 (widget-create 'push-button
458 :notify (lambda (&rest _ignore)
459 (setq ada-prj-current-values (ada-default-prj-properties))
460 (ada-prj-display-page 1))
461 "Reset to Default Values")
462 (widget-insert " ")
463 (widget-create 'push-button :notify (lambda (&rest _ignore) (kill-buffer nil))
464 "Cancel")
465 (widget-insert " ")
466 (widget-create 'push-button :notify (lambda (&rest _ignore) (ada-prj-save))
467 "Save")
468 (widget-insert "\n\n")
470 (widget-setup)
471 (with-no-warnings
472 (beginning-of-buffer))
476 (defun ada-customize (&optional new-file filename)
477 "Edit the project file associated with the current buffer.
478 If there is none or NEW-FILE is non-nil, make a new one.
479 If FILENAME is given, edit that file."
480 (interactive)
482 (let ((ada-buffer (current-buffer))
483 (inhibit-read-only t))
485 ;; We can only edit interactively the standard ada-mode project files. If
486 ;; the user is using other formats for the project file (through hooks in
487 ;; `ada-load-project-hook', we simply edit the file
489 (if (and (not new-file)
490 (or ada-prj-default-project-file filename)
491 (string= (file-name-extension
492 (or filename ada-prj-default-project-file))
493 "gpr"))
494 (progn
495 (find-file ada-prj-default-project-file)
496 (add-hook 'after-save-hook 'ada-reread-prj-file t t)
499 (if filename
500 (ada-reread-prj-file filename)
501 (if (not (string= ada-prj-default-project-file ""))
502 (ada-reread-prj-file ada-prj-default-project-file)
503 (ada-reread-prj-file)))
505 (switch-to-buffer "*Edit Ada Mode Project*")
507 (ada-prj-initialize-values 'ada-prj-current-values
508 ada-buffer
509 ada-prj-default-project-file)
511 (set (make-local-variable 'ada-prj-ada-buffer) ada-buffer)
513 (use-local-map
514 (let ((map (make-sparse-keymap)))
515 (set-keymap-parent map custom-mode-map)
516 (define-key map "\C-x\C-s" 'ada-prj-save)
517 map))
519 ;; FIXME: Not sure if this works!!
520 (set (make-local-variable 'widget-keymap)
521 (let ((map (make-sparse-keymap)))
522 (set-keymap-parent map widget-keymap)
523 (define-key map "\C-x\C-s" 'ada-prj-save)
524 map))
526 (set (make-local-variable 'ada-old-cross-prefix)
527 (ada-xref-get-project-field 'cross-prefix))
529 (ada-prj-display-page 1)
532 ;; ---------------- Utilities --------------------------------
534 (defun ada-prj-set-list (string ada-list &optional is-directory)
535 "Prepend STRING to strings in ADA-LIST, return new-line separated string.
536 If IS-DIRECTORY is non-nil, each element of ADA-LIST is explicitly
537 converted to a directory name."
539 (mapconcat (lambda (x) (concat string "="
540 (if is-directory
541 (file-name-as-directory x)
542 x)))
543 ada-list "\n"))
546 (defun ada-prj-field-modified (widget &rest _dummy)
547 "Callback for modification of WIDGET.
548 Remaining args DUMMY are ignored.
549 Save the change in `ada-prj-current-values' so that selecting
550 another page and coming back keeps the new value."
551 (setq ada-prj-current-values
552 (plist-put ada-prj-current-values
553 (widget-get widget ':prj-field)
554 (widget-value widget))))
556 (defun ada-prj-display-help (widget _widget-modified event)
557 "Callback for help button in WIDGET.
558 Parameters WIDGET-MODIFIED, EVENT match :notify for the widget."
559 (let ((text (widget-get widget 'prj-help)))
560 (if event
561 ;; If we have a mouse-event, popup a menu
562 (widget-choose "Help"
563 (mapcar (lambda (a) (cons a t))
564 (split-string text "\n"))
565 event)
566 ;; Else display the help string just before the next group of
567 ;; variables
568 (momentary-string-display
569 (concat "*****Help*****\n" text "\n**************\n")
570 (point-at-bol 2)))))
572 (defun ada-prj-show-value (widget _widget-modified event)
573 "Show the current field value in WIDGET.
574 Parameters WIDGET-MODIFIED, EVENT match :notify for the widget."
575 (let* ((field (widget-get widget ':prj-field))
576 (value (plist-get ada-prj-current-values field))
577 (inhibit-read-only t)
580 ;; If the other widget is already visible, delete it
581 (if (widget-get widget 'prj-other-widget)
582 (progn
583 (widget-delete (widget-get widget 'prj-other-widget))
584 (widget-put widget 'prj-other-widget nil)
585 (widget-put widget ':prj-field field)
586 (widget-default-value-set widget "Show Value")
589 ;; Else create it
590 (save-excursion
591 (mouse-set-point event)
592 (forward-line 1)
593 (beginning-of-line)
594 (setq w (widget-create 'editable-list
595 :entry-format "%i%d %v"
596 :notify 'ada-prj-field-modified
597 :help-echo (widget-get widget 'prj-help)
598 :value value
599 (list 'editable-field :keymap widget-keymap)))
600 (widget-put widget 'prj-other-widget w)
601 (widget-put w ':prj-field field)
602 (widget-put widget ':prj-field field)
603 (widget-default-value-set widget "Hide Value")
606 (widget-setup)
609 (defun ada-prj-field (field text help-text &optional is-list is-paths after-text)
610 "Create a widget to edit FIELD in the current buffer.
611 TEXT is a short explanation of what the field means, whereas HELP-TEXT
612 is the text displayed when the user pressed the help button.
613 If IS-LIST is non-nil, the field contains a list. Otherwise, it contains
614 a single string.
615 If IS-PATHS is true, some special buttons are added to load paths,...
616 AFTER-TEXT is inserted just after the widget."
617 (let ((value (plist-get ada-prj-current-values field))
618 (inhibit-read-only t)
619 widget)
620 (unless value
621 (setq value
622 (if is-list '() "")))
623 (widget-insert text)
624 (widget-insert ":")
625 (move-to-column 54 t)
626 (widget-put (widget-create 'push-button
627 :notify 'ada-prj-display-help
628 "Help")
629 'prj-help
630 help-text)
631 (widget-insert (concat " (" (symbol-name field) ")\n"))
632 (if is-paths
633 (progn
634 (widget-create 'push-button
635 :notify
636 (list 'lambda '(&rest dummy) '(interactive)
637 (list 'ada-prj-load-from-file
638 (list 'quote field)))
639 "Load From File")
640 (widget-insert " ")
641 (widget-create 'push-button
642 :notify
643 (list 'lambda '(&rest dummy) '(interactive)
644 (list 'ada-prj-load-directory
645 (list 'quote field)))
646 "Load Recursive Directory")
647 (widget-insert "\n ${build_dir}\n")))
649 (setq widget
650 (if is-list
651 (if (< (length value) 15)
652 (widget-create 'editable-list
653 :entry-format "%i%d %v"
654 :notify 'ada-prj-field-modified
655 :help-echo help-text
656 :value value
657 (list 'editable-field :keymap widget-keymap))
659 (let ((w (widget-create 'push-button
660 :notify 'ada-prj-show-value
661 "Show value")))
662 (widget-insert "\n")
663 (widget-put w 'prj-help help-text)
664 (widget-put w 'prj-other-widget nil)
667 (widget-create 'editable-field
668 :format "%v"
669 :notify 'ada-prj-field-modified
670 :help-echo help-text
671 :keymap widget-keymap
672 value)))
673 (widget-put widget ':prj-field field)
674 (if after-text
675 (widget-insert after-text))
676 (widget-insert "\n")
680 (provide 'ada-prj)
682 ;;; ada-prj.el ends here