1 ;;; ada-prj.el --- GUI editing of project files for the ada-mode
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Emmanuel Briot <briot@gnat.com>
7 ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
8 ;; Keywords: languages, ada, project file
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/>.
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
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.
42 ;; ----- Requirements -----------------------------------------------------
50 ;; ----- Buffer local variables -------------------------------------------
52 (defvar ada-prj-current-values nil
53 "Hold the current value of the fields, This is a property list.")
54 (make-variable-buffer-local 'ada-prj-current-values
)
56 (defvar ada-prj-default-values nil
57 "Hold the default value for the fields, This is a property list.")
58 (make-variable-buffer-local 'ada-prj-default-values
)
60 (defvar ada-prj-ada-buffer nil
61 "Indicates what Ada source file was being edited.")
63 (defvar ada-old-cross-prefix nil
64 "The cross-prefix associated with the currently loaded runtime library.")
67 ;; ----- Functions --------------------------------------------------------
70 "Open a new project file."
73 (if (and ada-prj-default-project-file
74 (not (string= ada-prj-default-project-file
"")))
75 ada-prj-default-project-file
77 (filename (read-file-name "Project file: "
82 (if (not (string= (file-name-extension filename t
) ".adp"))
83 (error "File name extension for project files must be .adp"))
85 (ada-customize nil filename
)))
87 (defun ada-prj-edit ()
88 "Editing the project file associated with the current Ada buffer.
89 If there is none, opens a new project file"
91 (if ada-prj-default-project-file
95 (defun ada-prj-initialize-values (symbol ada-buffer filename
)
96 "Set SYMBOL to the property list of the project file FILENAME.
97 If FILENAME is null, read the file associated with ADA-BUFFER. If no
98 project file is found, returns the default values."
99 ;; FIXME: rationalize arguments; make ada-buffer optional?
101 (not (string= filename
""))
102 (assoc filename ada-xref-project-files
))
103 (set symbol
(copy-sequence (cdr (assoc filename ada-xref-project-files
))))
105 ;; Set default values (except for the file name if this was given
107 (ada-xref-set-default-prj-values symbol ada-buffer
)
108 (if (and filename
(not (string= filename
"")))
109 (set symbol
(plist-put (eval symbol
) 'filename filename
)))
113 (defun ada-prj-save-specific-option (field)
114 "Return the string to print in the project file to save FIELD.
115 If the current value of FIELD is the default value, returns an empty string."
116 (if (string= (plist-get ada-prj-current-values field
)
117 (plist-get ada-prj-default-values field
))
119 (concat (symbol-name field
)
120 "=" (plist-get ada-prj-current-values field
) "\n")))
122 (defun ada-prj-save ()
123 "Save the edited project file."
125 (let ((file-name (plist-get ada-prj-current-values
'filename
))
130 ;; Save the fields that do not depend on the current buffer
131 ;; only if they are different from the default value
133 (ada-prj-save-specific-option 'comp_opt
)
134 (ada-prj-save-specific-option 'bind_opt
)
135 (ada-prj-save-specific-option 'link_opt
)
136 (ada-prj-save-specific-option 'gnatmake_opt
)
137 (ada-prj-save-specific-option 'gnatfind_opt
)
138 (ada-prj-save-specific-option 'cross_prefix
)
139 (ada-prj-save-specific-option 'remote_machine
)
140 (ada-prj-save-specific-option 'debug_cmd
)
142 ;; Always save the fields that depend on the current buffer
143 "main=" (plist-get ada-prj-current-values
'main
) "\n"
144 "main_unit=" (plist-get ada-prj-current-values
'main_unit
) "\n"
145 "build_dir=" (plist-get ada-prj-current-values
'build_dir
) "\n"
146 (ada-prj-set-list "check_cmd"
147 (plist-get ada-prj-current-values
'check_cmd
)) "\n"
148 (ada-prj-set-list "make_cmd"
149 (plist-get ada-prj-current-values
'make_cmd
)) "\n"
150 (ada-prj-set-list "comp_cmd"
151 (plist-get ada-prj-current-values
'comp_cmd
)) "\n"
152 (ada-prj-set-list "run_cmd"
153 (plist-get ada-prj-current-values
'run_cmd
)) "\n"
154 (ada-prj-set-list "src_dir"
155 (plist-get ada-prj-current-values
'src_dir
)
157 (ada-prj-set-list "obj_dir"
158 (plist-get ada-prj-current-values
'obj_dir
)
160 (ada-prj-set-list "debug_pre_cmd"
161 (plist-get ada-prj-current-values
'debug_pre_cmd
))
163 (ada-prj-set-list "debug_post_cmd"
164 (plist-get ada-prj-current-values
'debug_post_cmd
))
168 (find-file file-name
)
172 ;; kill the project buffer
175 ;; kill the editor buffer
176 (kill-buffer "*Edit Ada Mode Project*")
178 ;; automatically set the new project file as the active one
179 (set 'ada-prj-default-project-file file-name
)
181 ;; force Emacs to reread the project files
182 (ada-reread-prj-file file-name
)
186 (defun ada-prj-load-from-file (symbol)
187 "Load SYMBOL value from file. One item per line should be found in the file."
189 (let ((file (read-file-name "File name: " nil nil t
))
190 (buffer (current-buffer))
195 (goto-char (point-min))
197 (set 'line
(buffer-substring-no-properties
198 (point) (save-excursion (end-of-line) (point))))
199 (add-to-list 'list line
)
204 (set 'ada-prj-current-values
205 (plist-put ada-prj-current-values
207 (append (plist-get ada-prj-current-values symbol
)
210 (ada-prj-display-page 2)
213 (defun ada-prj-subdirs-of (dir)
214 "Return a list of all the subdirectories of DIR, recursively."
215 (let ((subdirs (directory-files dir t
"^[^.].*"))
216 (dirlist (list dir
)))
218 (if (file-directory-p (car subdirs
))
219 (let ((sub (ada-prj-subdirs-of (car subdirs
))))
221 (set 'dirlist
(append sub dirlist
)))))
222 (set 'subdirs
(cdr subdirs
)))
225 (defun ada-prj-load-directory (field &optional file-name
)
226 "Append to FIELD in the current project the subdirectories of FILE-NAME.
227 If FILE-NAME is nil, ask the user for the name."
229 ;; Do not use an external dialog for this, since it wouldn't allow
230 ;; the user to select a directory
231 (let ((use-dialog-box nil
))
233 (set 'file-name
(read-file-name "Root directory: " nil nil t
))))
235 (set 'ada-prj-current-values
236 (plist-put ada-prj-current-values
238 (append (plist-get ada-prj-current-values field
)
239 (reverse (ada-prj-subdirs-of
240 (expand-file-name file-name
))))))
241 (ada-prj-display-page 2))
243 (defun ada-prj-display-page (tab-num)
244 "Display page TAB-NUM in the notebook.
245 The current buffer must be the project editing buffer."
247 (let ((inhibit-read-only t
))
250 ;; Widget support in Emacs 21 requires that we clear the buffer first
251 (if (and (not (featurep 'xemacs
)) (>= emacs-major-version
21))
253 (setq widget-field-new nil
254 widget-field-list nil
)
255 (mapc (lambda (x) (delete-overlay x
)) (car (overlay-lists)))
256 (mapc (lambda (x) (delete-overlay x
)) (cdr (overlay-lists)))))
260 (widget-insert "\n Project configuration.\n
261 ___________ ____________ ____________ ____________ ____________\n / ")
262 (widget-create 'push-button
:notify
263 (lambda (&rest dummy
) (ada-prj-display-page 1)) "General")
264 (widget-insert " \\ / ")
265 (widget-create 'push-button
:notify
266 (lambda (&rest dummy
) (ada-prj-display-page 2)) "Paths")
267 (widget-insert " \\ / ")
268 (widget-create 'push-button
:notify
269 (lambda (&rest dummy
) (ada-prj-display-page 3)) "Switches")
270 (widget-insert " \\ / ")
271 (widget-create 'push-button
:notify
272 (lambda (&rest dummy
) (ada-prj-display-page 4)) "Ada Menu")
273 (widget-insert " \\ / ")
274 (widget-create 'push-button
:notify
275 (lambda (&rest dummy
) (ada-prj-display-page 5)) "Debugger")
276 (widget-insert " \\\n")
278 ;; Display the currently selected page
283 ;; First page (General)
286 (widget-insert "/ \\/______________\\/______________\\/______________\\/______________\\\n")
288 (widget-insert "Project file name:\n")
289 (widget-insert (plist-get ada-prj-current-values
'filename
))
290 (widget-insert "\n\n")
291 ; (ada-prj-field 'filename "Project file name"
292 ; "Enter the name and directory of the project
293 ; file. The name of the file should be the
294 ; name of the project itself. The extension
296 ; (ada-prj-field 'casing "Casing Exceptions Dictionnaries"
297 ; "List of files that contain casing exception
298 ; dictionnaries. All these files contain one
299 ; identifier per line, with a special casing.
300 ; The first file has the highest priority."
302 (ada-prj-field 'main
"Executable file name"
303 "Name of the executable generated when you
304 compile your application. This should include
305 the full directory name, using ${build_dir} if
307 (ada-prj-field 'main_unit
"File name of the main unit"
308 "Name of the file to pass to the gnatmake command,
309 and that will create the executable.
310 This should not include any directory specification.")
311 (ada-prj-field 'build_dir
"Build directory"
312 "Reference directory for relative paths in
313 src_dir and obj_dir below. This is also the directory
314 where the compilation is done.")
315 (ada-prj-field 'remote_machine
"Name of the remote machine (if any)"
316 "If you want to remotely compile, debug and
317 run your application, specify the name of a
318 remote machine here. This capability requires
319 the 'rsh' protocol on the remote machine.")
320 (ada-prj-field 'cross_prefix
"Prefix used in for the cross tool chain"
321 "When working on multiple cross targets, it is
322 most convenient to specify the prefix of the
323 tool chain here. For instance, on PowerPc
324 vxworks, you would enter 'powerpc-wrs-vxworks-'.
325 To use JGNAT, enter 'j'.")
330 ;; Second page (Paths)
333 (if (not (equal (plist-get ada-prj-current-values
'cross_prefix
)
334 ada-old-cross-prefix
))
336 (setq ada-old-cross-prefix
337 (plist-get ada-prj-current-values
'cross_prefix
))
338 (ada-initialize-runtime-library ada-old-cross-prefix
)))
341 (widget-insert "/_____________\\/ \\/______________\\/______________\\/______________\\\n")
342 (ada-prj-field 'src_dir
"Source directories"
343 "Enter the list of directories where your Ada
344 sources can be found. These directories will be
345 used for the cross-references and for the default
346 compilation commands.
347 Note that src_dir includes both the build directory
348 and the standard runtime."
350 (mapconcat (lambda(x)
352 ada-xref-runtime-library-specs-path
355 (widget-insert "\n\n")
357 (ada-prj-field 'obj_dir
"Object directories"
358 "Enter the list of directories where the GNAT
359 library files (ALI files) can be found. These
360 files are used for cross-references and by the
362 Note that obj_dir includes both the build directory
363 and the standard runtime."
365 (mapconcat (lambda(x)
367 ada-xref-runtime-library-ali-path
370 (widget-insert "\n\n")
374 ;; Third page (Switches)
377 (widget-insert "/_____________\\/______________\\/ \\/______________\\/______________\\\n")
378 (ada-prj-field 'comp_opt
"Switches for the compiler"
379 "These switches are used in the default
380 compilation commands, both for compiling a
381 single file and rebuilding the whole project")
382 (ada-prj-field 'bind_opt
"Switches for the binder"
383 "These switches are used in the default build
384 command and are passed to the binder")
385 (ada-prj-field 'link_opt
"Switches for the linker"
386 "These switches are used in the default build
387 command and are passed to the linker")
388 (ada-prj-field 'gnatmake_opt
"Switches for gnatmake"
389 "These switches are used in the default gnatmake
391 (ada-prj-field 'gnatfind_opt
"Switches for gnatfind"
392 "The command gnatfind is run every time the Ada/Goto/List_References menu.
393 You should for instance add -a if you are working in an environment
394 where most ALI files are write-protected, since otherwise they get
395 ignored by gnatfind and you don't see the references within.")
402 (widget-insert "/_____________\\/______________\\/______________\\/ \\/______________\\\n")
404 "All the fields below can use variable substitution The syntax is ${name},
405 where name is the name that appears after the Help buttons in this buffer. As
406 a special case, ${current} is replaced with the name of the file currently
407 edited, with directory name but no extension, whereas ${full_current} is
408 replaced with the name of the current file with directory name and
411 "The environment variables ADA_INCLUDE_PATH and ADA_OBJECTS_PATH are set to
412 ${src_dir} and ${obj_dir} before running the compilation commands, so that you
413 don't need to specify the -aI and -aO switches on the command line\n")
415 "You can reference any environment variable using the same ${...} syntax as
416 above, and put the name of the variable between the quotes.\n\n")
417 (ada-prj-field 'check_cmd
418 "Check syntax of a single file (menu Ada->Check File)"
419 "This command is run to check the syntax and semantics of a file.
420 The file name is added at the end of this command." t
)
421 (ada-prj-field 'comp_cmd
422 "Compiling a single file (menu Ada->Compile File)"
423 "This command is run when the recompilation
424 of a single file is needed. The file name is
425 added at the end of this command." t
)
426 (ada-prj-field 'make_cmd
"Rebuilding the whole project (menu Ada->Build)"
427 "This command is run when you want to rebuild
428 your whole application. It is never issues
429 automatically and you will need to ask for it.
430 If remote_machine has been set, this command
431 will be executed on the remote machine." t
)
432 (ada-prj-field 'run_cmd
"Running the application (menu Ada->Run)"
433 "This command specifies how to run the
434 application, including any switch you need to
435 specify. If remote_machine has been set, this
436 command will be executed on the remote host." t
)
443 (widget-insert "/_____________\\/______________\\/______________\\/______________\\/ \\\n")
444 (ada-prj-field 'debug_pre_cmd
"Commands to execute before launching the
446 "The following commands are executed one after the other before starting
447 the debugger. These can be used to set up your environment." t
)
449 (ada-prj-field 'debug_cmd
"Debugging the application"
450 "Specifies how to debug the application, possibly
451 remotely if remote_machine has been set. We
452 recommend the following debuggers:
455 > ddd --tty -fullname -toolbar")
457 (ada-prj-field 'debug_post_cmd
"Commands to execute in the debugger"
458 "The following commands are executed one in the debugger once it has been
459 started. These can be used to initialize the debugger, for instance to
460 connect to the target when working with cross-environments" t
)
466 (widget-insert "______________________________________________________________________\n\n ")
467 (widget-create 'push-button
468 :notify
(lambda (&rest ignore
)
469 (ada-xref-set-default-prj-values
470 'ada-prj-current-values ada-prj-ada-buffer
)
471 (ada-prj-display-page 1))
472 "Reset to Default Values")
474 (widget-create 'push-button
:notify
(lambda (&rest ignore
) (kill-buffer nil
))
477 (widget-create 'push-button
:notify
(lambda (&rest ignore
) (ada-prj-save))
479 (widget-insert "\n\n")
483 (beginning-of-buffer))
487 (defun ada-customize (&optional new-file filename
)
488 "Edit the project file associated with the current buffer.
489 If there is none or NEW-FILE is non-nil, make a new one.
490 If FILENAME is given, edit that file."
493 (let ((ada-buffer (current-buffer))
494 (inhibit-read-only t
))
496 ;; We can only edit interactively the standard ada-mode project files. If
497 ;; the user is using other formats for the project file (through hooks in
498 ;; `ada-load-project-hook', we simply edit the file
500 (if (and (not new-file
)
501 (or ada-prj-default-project-file filename
)
502 (string= (file-name-extension
503 (or filename ada-prj-default-project-file
))
506 (find-file ada-prj-default-project-file
)
507 (add-hook 'after-save-hook
'ada-reread-prj-file t t
)
511 (ada-reread-prj-file filename
)
512 (if (not (string= ada-prj-default-project-file
""))
513 (ada-reread-prj-file ada-prj-default-project-file
)
514 (ada-reread-prj-file)))
516 ;; Else start the interactive editor
517 (switch-to-buffer "*Edit Ada Mode Project*")
519 (ada-xref-set-default-prj-values 'ada-prj-default-values ada-buffer
)
520 (ada-prj-initialize-values 'ada-prj-current-values
522 ada-prj-default-project-file
)
524 (set (make-local-variable 'ada-prj-ada-buffer
) ada-buffer
)
526 (use-local-map (copy-keymap custom-mode-map
))
527 (local-set-key "\C-x\C-s" 'ada-prj-save
)
529 (make-local-variable 'widget-keymap
)
530 (define-key widget-keymap
"\C-x\C-s" 'ada-prj-save
)
532 (set (make-local-variable 'ada-old-cross-prefix
)
533 (ada-xref-get-project-field 'cross-prefix
))
535 (ada-prj-display-page 1)
538 ;; ---------------- Utilities --------------------------------
540 (defun ada-prj-set-list (string ada-list
&optional is-directory
)
541 "Prepend STRING to strings in ADA-LIST, return new-line separated string.
542 If IS-DIRECTORY is non-nil, each element of ADA-LIST is explicitly
543 converted to a directory name."
545 (mapconcat (lambda (x) (concat string
"="
547 (file-name-as-directory x
)
552 (defun ada-prj-field-modified (widget &rest dummy
)
553 "Callback for modification of WIDGET.
554 Remaining args DUMMY are ignored.
555 Save the change in `ada-prj-current-values' so that selecting
556 another page and coming back keeps the new value."
557 (set 'ada-prj-current-values
558 (plist-put ada-prj-current-values
559 (widget-get widget
':prj-field
)
560 (widget-value widget
))))
562 (defun ada-prj-display-help (widget widget-modified event
)
563 "Callback for help button in WIDGET.
564 Parameters WIDGET-MODIFIED, EVENT match :notify for the widget."
565 (let ((text (widget-get widget
'prj-help
)))
567 ;; If we have a mouse-event, popup a menu
568 (widget-choose "Help"
569 (mapcar (lambda (a) (cons a t
))
570 (split-string text
"\n"))
572 ;; Else display the help string just before the next group of
574 (momentary-string-display
575 (concat "*****Help*****\n" text
"\n**************\n")
576 (save-excursion (forward-line) (beginning-of-line) (point)))
579 (defun ada-prj-show-value (widget widget-modified event
)
580 "Show the current field value in WIDGET.
581 Parameters WIDGET-MODIFIED, EVENT match :notify for the widget."
582 (let* ((field (widget-get widget
':prj-field
))
583 (value (plist-get ada-prj-current-values field
))
584 (inhibit-read-only t
)
587 ;; If the other widget is already visible, delete it
588 (if (widget-get widget
'prj-other-widget
)
590 (widget-delete (widget-get widget
'prj-other-widget
))
591 (widget-put widget
'prj-other-widget nil
)
592 (widget-put widget
':prj-field field
)
593 (widget-default-value-set widget
"Show Value")
598 (mouse-set-point event
)
601 (setq w
(widget-create 'editable-list
602 :entry-format
"%i%d %v"
603 :notify
'ada-prj-field-modified
604 :help-echo
(widget-get widget
'prj-help
)
606 (list 'editable-field
:keymap widget-keymap
)))
607 (widget-put widget
'prj-other-widget w
)
608 (widget-put w
':prj-field field
)
609 (widget-put widget
':prj-field field
)
610 (widget-default-value-set widget
"Hide Value")
616 (defun ada-prj-field (field text help-text
&optional is-list is-paths after-text
)
617 "Create a widget to edit FIELD in the current buffer.
618 TEXT is a short explanation of what the field means, whereas HELP-TEXT
619 is the text displayed when the user pressed the help button.
620 If IS-LIST is non-nil, the field contains a list. Otherwise, it contains
622 if IS-PATHS is true, some special buttons are added to load paths,...
623 AFTER-TEXT is inserted just after the widget."
624 (let ((value (plist-get ada-prj-current-values field
))
625 (inhibit-read-only t
)
629 (if is-list
'() "")))
632 (move-to-column 54 t
)
633 (widget-put (widget-create 'push-button
634 :notify
'ada-prj-display-help
638 (widget-insert (concat " (" (symbol-name field
) ")\n"))
641 (widget-create 'push-button
643 (list 'lambda
'(&rest dummy
) '(interactive)
644 (list 'ada-prj-load-from-file
645 (list 'quote field
)))
648 (widget-create 'push-button
650 (list 'lambda
'(&rest dummy
) '(interactive)
651 (list 'ada-prj-load-directory
652 (list 'quote field
)))
653 "Load Recursive Directory")
654 (widget-insert "\n ${build_dir}\n")))
658 (if (< (length value
) 15)
659 (widget-create 'editable-list
660 :entry-format
"%i%d %v"
661 :notify
'ada-prj-field-modified
664 (list 'editable-field
:keymap widget-keymap
))
666 (let ((w (widget-create 'push-button
667 :notify
'ada-prj-show-value
670 (widget-put w
'prj-help help-text
)
671 (widget-put w
'prj-other-widget nil
)
674 (widget-create 'editable-field
676 :notify
'ada-prj-field-modified
678 :keymap widget-keymap
680 (widget-put widget
':prj-field field
)
682 (widget-insert after-text
))
689 ;; arch-tag: 65978c77-816e-49c6-896e-6905605d1b4c
690 ;;; ada-prj.el ends here