Some fixes to follow coding conventions.
[emacs.git] / lisp / progmodes / ada-prj.el
blobd6ded072a0d015b204b696df9d79393b71ff69c7
1 ;;; ada-prj.el --- easy editing of project files for the ada-mode
3 ;; Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5 ;; Author: Emmanuel Briot <briot@gnat.com>
6 ;; Ada Core Technologies's version: $Revision: 1.6 $
7 ;; Keywords: languages, ada, project file
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; This package provides a set of functions to easily edit the project
29 ;;; files used by the ada-mode.
30 ;;; The only function publicly available here is `ada-customize'.
31 ;;; See the documentation of the Ada mode for more information on the project
32 ;;; files.
33 ;;; Internally, a project file is represented as a property list, with each
34 ;;; field of the project file matching one property of the list.
36 ;;; Code:
39 ;; ----- Requirements -----------------------------------------------------
41 (require 'cus-edit)
43 ;; ----- Buffer local variables -------------------------------------------
45 (defvar ada-prj-current-values nil
46 "Hold the current value of the fields, This is a property list.")
47 (make-variable-buffer-local 'ada-prj-current-values)
49 (defvar ada-prj-default-values nil
50 "Hold the default value for the fields, This is a property list.")
51 (make-variable-buffer-local 'ada-prj-default-values)
53 (defvar ada-prj-ada-buffer nil
54 "Indicates what Ada source file was being edited.")
57 ;; ----- Functions --------------------------------------------------------
59 (defun ada-prj-new ()
60 "Open a new project file"
61 (interactive)
62 (let* ((prj
63 (if (my-local-variable-if-set-p 'ada-prj-prj-file (current-buffer))
64 ada-prj-prj-file
65 "default.adp"))
66 (filename (read-file-name "Project file: "
67 (if prj "" nil)
68 nil
69 nil
70 prj)))
71 (if (not (string= (file-name-extension filename t) ".adp"))
72 (error "File name extension for project files must be .adp"))
74 (ada-customize nil filename)))
76 (defun ada-prj-edit ()
77 "Editing the project file associated with the current Ada buffer.
78 If there is none, opens a new project file"
79 (interactive)
80 (let ((file (ada-prj-find-prj-file)))
81 (if file
82 (progn
83 (ada-reread-prj-file file)
84 (ada-customize))
85 (ada-prj-new))))
87 (defun ada-prj-add-ada-menu ()
88 "Add a new submenu to the Ada menu.
89 The items are added to the menu NAME in map MAP. NAME should be the same
90 name as was passed to `ada-create-menu'."
91 (if ada-xemacs
92 (progn
93 (funcall (symbol-function 'add-menu-button)
94 '("Ada" "Project")
95 ["Edit" ada-prj-edit t] "Associate")
96 (funcall (symbol-function 'add-menu-button)
97 '("Ada" "Project")
98 ["New..." ada-prj-new t] "Associate"))
99 (define-key (lookup-key ada-mode-map [menu-bar Ada Project])
100 [Edit] '("Edit current" . ada-prj-edit))
101 (define-key (lookup-key ada-mode-map [menu-bar Ada Project])
102 [New] '("New" . ada-prj-new))))
104 (defun ada-prj-add-keymap ()
105 "Add new keybindings for ada-prj."
106 (define-key ada-mode-map "\C-cu" 'ada-prj-edit))
108 (defun ada-prj-initialize-values (symbol ada-buffer &optional filename)
109 "Set SYMBOL to the property list of the project file FILENAME.
110 If FILENAME is null, read the file associated with ADA-BUFFER. If no
111 project file is found, returns the default values."
113 (let ((prj filename))
115 (if filename
116 ;; If filename is given, reread if first if needed
117 (if (file-exists-p filename)
118 (ada-reread-prj-file))
120 ;; Else use the one from the current buffer
121 (save-excursion
122 (set-buffer ada-buffer)
123 (set 'prj ada-prj-prj-file)))
126 (if (and prj
127 (not (string= prj ""))
128 (assoc prj ada-xref-project-files))
129 (set symbol (copy-sequence (cdr (assoc prj ada-xref-project-files))))
131 ;; Set default values (except for the file name if this was given
132 ;; in the buffer
133 (ada-xref-set-default-prj-values symbol ada-buffer)
134 (if (and prj (not (string= prj "")))
135 (set symbol (plist-put (eval symbol) 'filename prj)))
139 (defun ada-prj-save-specific-option (field)
140 "Returns the string to print in the project file to save FIELD.
141 If the current value of FIELD is the default value, returns an empty string."
142 (if (string= (plist-get ada-prj-current-values field)
143 (plist-get ada-prj-default-values field))
145 (concat (symbol-name field)
146 "=" (plist-get ada-prj-current-values field) "\n")))
148 (defun ada-prj-save ()
149 "Save the edited project file."
150 (interactive)
151 (let ((file-name (plist-get ada-prj-current-values 'filename))
152 output)
153 (set 'output
154 (concat
156 ;; Save the fields that do not depend on the current buffer
157 ;; only if they are different from the default value
159 (ada-prj-save-specific-option 'comp_opt)
160 (ada-prj-save-specific-option 'bind_opt)
161 (ada-prj-save-specific-option 'link_opt)
162 (ada-prj-save-specific-option 'gnatmake_opt)
163 (ada-prj-save-specific-option 'cross_prefix)
164 (ada-prj-save-specific-option 'remote_machine)
165 (ada-prj-save-specific-option 'comp_cmd)
166 (ada-prj-save-specific-option 'check_cmd)
167 (ada-prj-save-specific-option 'make_cmd)
168 (ada-prj-save-specific-option 'run_cmd)
169 (ada-prj-save-specific-option 'debug_cmd)
171 ;; Always save the fields that depend on the current buffer
172 (concat "main=" (plist-get ada-prj-current-values 'main) "\n")
173 (concat "main_unit=" (plist-get ada-prj-current-values 'main_unit) "\n")
174 (concat "build_dir=" (plist-get ada-prj-current-values 'build_dir) "\n")
176 (ada-prj-set-list "casing"
177 (plist-get ada-prj-current-values 'casing)) "\n"
178 (ada-prj-set-list "src_dir"
179 (plist-get ada-prj-current-values 'src_dir)) "\n"
180 (ada-prj-set-list "obj_dir"
181 (plist-get ada-prj-current-values 'obj_dir)) "\n"
184 (find-file file-name)
185 (erase-buffer)
186 (insert output)
187 (save-buffer)
188 ;; kill the project buffer
189 (kill-buffer nil)
191 ;; kill the editor buffer
192 (kill-buffer "*Customize Ada Mode*")
194 ;; automatically associates the current buffer with the
195 ;; new project file
196 (set (make-local-variable 'ada-prj-prj-file) file-name)
198 ;; force Emacs to reread the project files
199 (ada-reread-prj-file file-name)
203 (defun ada-prj-load-from-file (symbol)
204 "Load SYMBOL value from file. One item per line should be found in the file."
205 (save-excursion
206 (let ((file (read-file-name "File name: " nil nil t))
207 (buffer (current-buffer))
208 line
209 list)
210 (find-file file)
211 (widen)
212 (goto-char (point-min))
213 (while (not (eobp))
214 (set 'line (buffer-substring-no-properties
215 (point) (save-excursion (end-of-line) (point))))
216 (add-to-list 'list line)
217 (forward-line 1)
219 (kill-buffer nil)
220 (set-buffer buffer)
221 (set 'ada-prj-current-values
222 (plist-put ada-prj-current-values
223 symbol
224 (append (plist-get ada-prj-current-values symbol)
225 (reverse list))))
227 (ada-prj-display-page 2)
230 (defun ada-prj-subdirs-of (dir)
231 "Returns a list of all the subdirectories of dir, recursively."
232 (let ((subdirs (directory-files dir t "^[^.].*"))
233 (dirlist (list dir)))
234 (while subdirs
235 (if (file-directory-p (car subdirs))
236 (let ((sub (ada-prj-subdirs-of (car subdirs))))
237 (if sub
238 (set 'dirlist (append sub dirlist)))))
239 (set 'subdirs (cdr subdirs)))
240 dirlist))
242 (defun ada-prj-load-directory (field &optional file-name)
243 "Append the content of FILE-NAME to FIELD in the current project file.
244 If FILE-NAME is nil, ask the user for the name."
245 (unless file-name
246 (set 'file-name (read-file-name "Root directory: " nil nil t)))
248 (set 'ada-prj-current-values
249 (plist-put ada-prj-current-values
250 field
251 (append (plist-get ada-prj-current-values field)
252 (reverse (ada-prj-subdirs-of
253 (expand-file-name file-name))))))
254 (ada-prj-display-page 2))
256 (defun ada-prj-display-page (tab-num)
257 "Display one of the pages available in the notebook. TAB-NUM should have
258 a value between 1 and the maximum number of pages.
259 The current buffer must be the project editing buffer."
261 (let ((inhibit-read-only t))
262 (erase-buffer))
264 ;; Display the tabs
266 (widget-insert "\n Project and Editor configuration.\n
267 ___________ ____________ ____________ ____________\n / ")
268 (widget-create 'push-button :notify
269 (lambda (&rest dummy) (ada-prj-display-page 1)) "General")
270 (widget-insert " \\ / ")
271 (widget-create 'push-button :notify
272 (lambda (&rest dummy) (ada-prj-display-page 2)) "Paths")
273 (widget-insert " \\ / ")
274 (widget-create 'push-button :notify
275 (lambda (&rest dummy) (ada-prj-display-page 3)) "Switches")
276 (widget-insert " \\ / ")
277 (widget-create 'push-button :notify
278 (lambda (&rest dummy) (ada-prj-display-page 4)) "Ada Menu")
279 (widget-insert " \\\n")
281 ;; Display the currently selected page
283 (cond
286 ;; First page (General)
288 ((= tab-num 1)
289 (widget-insert "_/ \\/______________\\/______________\\/______________\\_____\n\n")
291 (widget-insert "Project file name:\n")
292 (widget-insert (plist-get ada-prj-current-values 'filename))
293 (widget-insert "\n\n")
294 ; (ada-prj-field 'filename "Project file name"
295 ; "Enter the name and directory of the project
296 ; file. The name of the file should be the
297 ; name of the project itself. The extension
298 ; must be .adp")
299 ; (ada-prj-field 'casing "Casing Exceptions Dictionnaries"
300 ; "List of files that contain casing exception
301 ; dictionnaries. All these files contain one
302 ; identifier per line, with a special casing.
303 ; The first file has the highest priority."
304 ; t)
305 (ada-prj-field 'main "Executable file name"
306 "Name of the executable generated when you
307 compile your application. This should include
308 the full directory name, using ${build_dir} if
309 you wish.")
310 (ada-prj-field 'main_unit "File name of the main unit"
311 "Name of the file to pass to the gnatmake command,
312 and that will create the executable.
313 This should not include any directory specification.")
314 (ada-prj-field 'build_dir "Build directory"
315 "Reference directory for relative paths in
316 src_dir and obj_dir below. This is also the directory
317 where the compilation is done.")
318 (ada-prj-field 'remote_machine "Name of the remote machine (if any)"
319 "If you want to remotely compile, debug and
320 run your application, specify the name of a
321 remote machine here. This capability requires
322 the 'rsh' protocol on the remote machine.")
323 (ada-prj-field 'cross_prefix "Prefix used in for the cross tool chain"
324 "When working on multiple cross targets, it is
325 most convenient to specify the prefix of the
326 tool chain here. For instance, on PowerPc
327 vxworks, you would enter 'powerpc-wrs-vxworks-'.
328 To use JGNAT, enter 'j'.")
333 ;; Second page (Paths)
335 ((= tab-num 2)
336 (widget-insert "_/_____________\\/ \\/______________\\/______________\\_____\n\n")
337 (ada-prj-field 'src_dir "Source directories"
338 "Enter the list of directories where your Ada
339 sources can be found. These directories will be
340 used for the cross-references and for the default
341 compilation commands.
342 Note that src_dir includes both the build directory
343 and the standard runtime."
345 (mapconcat (lambda(x)
346 (concat " " x))
347 ada-xref-runtime-library-specs-path
348 "\n")
350 (widget-insert "\n\n")
352 (ada-prj-field 'obj_dir "Object directories"
353 "Enter the list of directories where the GNAT
354 library files (ALI files) can be found. These
355 files are used for cross-references and by the
356 gnatmake command.
357 Note that obj_dir includes both the build directory
358 and the standard runtime."
360 (mapconcat (lambda(x)
361 (concat " " x))
362 ada-xref-runtime-library-ali-path
363 "\n")
365 (widget-insert "\n\n")
369 ;; Third page (Switches)
371 ((= tab-num 3)
372 (widget-insert "_/_____________\\/______________\\/ \\/______________\\_____\n\n")
373 (ada-prj-field 'comp_opt "Switches for the compiler"
374 "These switches are used in the default
375 compilation commands, both for compiling a
376 single file and rebuilding the whole project")
377 (ada-prj-field 'bind_opt "Switches for the binder"
378 "These switches are used in the default build
379 command and are passed to the binder")
380 (ada-prj-field 'link_opt "Switches for the linker"
381 "These switches are used in the default build
382 command and are passed to the linker")
383 (ada-prj-field 'gnatmake_opt "Switches for gnatmake"
384 "These switches are used in the default gnatmake
385 command.")
389 ;; Fourth page
391 ((= tab-num 4)
392 (widget-insert "_/_____________\\/______________\\/______________\\/ \\_____\n\n")
393 (widget-insert "All the fields below can use variable substitution\n")
394 (widget-insert "The syntax is ${name}, where name is the name that\n")
395 (widget-insert "appears after the Help buttons in this buffer.\n")
396 (widget-insert "As a special case, ${current} is replaced with the name\n")
397 (widget-insert "of the file currently edited, with directory name but\n")
398 (widget-insert "no extension.\n\n")
399 (widget-insert
400 "The environment variables ADA_INCLUDE_PATH and ADA_OBJECTS_PATH\n")
401 (widget-insert
402 "are set to ${src_dir} and ${obj_dir} before running the compilation\n")
403 (widget-insert
404 "commands, so that you don't need to specify the -aI and -aO\n")
405 (widget-insert
406 "switches on the command line\n\n")
408 (ada-prj-field 'check_cmd
409 "Check syntax of a single file (menu Ada->Check File)"
410 "This command is run to check the syntax and semantics of a file.
411 The file name is added at the end of this command.")
412 (ada-prj-field 'comp_cmd
413 "Compiling a single file (menu Ada->Compile File)"
414 "This command is run when the recompilation
415 of a single file is needed. The file name is
416 added at the end of this command.")
417 (ada-prj-field 'make_cmd "Rebuilding the whole project (menu Ada->Build)"
418 "This command is run when you want to rebuild
419 your whole application. It is never issues
420 automatically and you will need to ask for it.
421 If remote_machine has been set, this command
422 will be executed on the remote machine.")
423 (ada-prj-field 'run_cmd "Running the application (menu Ada->Run)"
424 "This command specifies how to run the
425 application, including any switch you need to
426 specify. If remote_machine has been set, this
427 command will be executed on the remote host.")
428 (ada-prj-field 'debug_cmd "Debugging the application"
429 "Specifies how to debug the application, possibly
430 remotely if remote_machine has been set. We
431 recommend the following debuggers:
432 > gdb
433 > gdbtk
434 > ddd --tty -fullname -toolbar")
439 (widget-insert "______________________________________________________________________\n\n ")
440 (widget-create 'push-button
441 :notify (lambda (&rest ignore)
442 (ada-xref-set-default-prj-values
443 'ada-prj-current-values ada-prj-ada-buffer)
444 (ada-prj-display-page 1))
445 "Reset to Default Values")
446 (widget-insert " ")
447 (widget-create 'push-button :notify (lambda (&rest ignore) (kill-buffer nil))
448 "Cancel")
449 (widget-insert " ")
450 (widget-create 'push-button :notify (lambda (&rest ignore) (ada-prj-save))
451 "Save")
452 (widget-insert "\n\n")
454 (widget-setup)
455 (beginning-of-buffer)
459 (defun ada-customize (&optional new-file filename)
460 "Edit the project file associated with the current buffer.
461 If there is none or NEW-FILE is non-nil, make a new one.
462 If FILENAME is given, edit that file."
463 (interactive)
465 (let ((ada-buffer (current-buffer))
466 (inhibit-read-only t))
468 (ada-require-project-file)
470 (switch-to-buffer "*Customize Ada Mode*")
471 (kill-all-local-variables)
473 (ada-xref-set-default-prj-values 'ada-prj-default-values ada-buffer)
474 (ada-prj-initialize-values 'ada-prj-current-values ada-buffer filename)
476 (set (make-local-variable 'ada-prj-ada-buffer) ada-buffer)
478 (use-local-map (copy-keymap custom-mode-map))
479 (local-set-key "\C-x\C-s" 'ada-prj-save)
481 (make-local-variable 'widget-keymap)
482 (define-key widget-keymap "\C-x\C-s" 'ada-prj-save)
484 (ada-prj-display-page 1)
487 ;; ---------------- Utilities --------------------------------
489 (defun ada-prj-set-list (string ada-dir-list)
490 "Join the strings in ADA-DIR-LIST into a single string. Each name is put
491 on a separate line that begins with STRING."
492 (mapconcat (lambda (x) (concat string "=" (file-name-as-directory x)))
493 ada-dir-list "\n"))
496 (defun ada-prj-get-prj-dir (&optional ada-file)
497 "Returns the directory/name of the project file for ADA-FILE.
498 If ADA-FILE is nil, returns the project file for the current buffer."
499 (unless ada-file
500 (setq ada-file (buffer-file-name)))
502 (save-excursion
503 (set-buffer (get-file-buffer ada-file))
505 (let ((prj-file (ada-prj-find-prj-file t)))
506 (if (or (not prj-file)
507 (not (file-exists-p prj-file))
509 (setq prj-file
510 (concat (file-name-sans-extension ada-file)
511 ada-project-file-extension)))
512 prj-file)
515 (defun ada-prj-field-modified (widget &rest dummy)
516 "Callback called each time the value of WIDGET is modified. Save the
517 change in ada-prj-current-values so that selecting another page and coming
518 back keeps the new value."
519 (set 'ada-prj-current-values
520 (plist-put ada-prj-current-values
521 (widget-get widget 'prj-field)
522 (widget-value widget))))
524 (defun ada-prj-display-help (widget widget-modified event)
525 "An help button in WIDGET was clicked on. The parameters are so that
526 this function can be used as :notify for the widget."
527 (let ((text (widget-get widget 'prj-help)))
528 (if event
529 ;; If we have a mouse-event, popup a menu
530 (widget-choose "Help"
531 (mapcar (lambda (a) (cons a t))
532 (split-string text "\n"))
533 event)
534 ;; Else display the help string just before the next group of
535 ;; variables
536 (momentary-string-display
537 (concat "*****Help*****\n" text "\n**************\n")
538 (save-excursion (forward-line) (beginning-of-line) (point)))
541 (defun ada-prj-show-value (widget widget-modified event)
542 (let ((value (plist-get ada-prj-current-values
543 (widget-get widget 'prj-field)))
544 (inhibit-read-only t))
546 ;; If the other widget is already visible, delete it
547 (if (widget-get widget 'prj-other-widget)
548 (progn
549 (widget-delete (widget-get widget 'prj-other-widget))
550 (widget-put widget 'prj-other-widget nil)
551 (widget-default-value-set widget "Show Value")
554 ;; Else create it
555 (save-excursion
556 (mouse-set-point event)
557 (forward-line 1)
558 (beginning-of-line)
559 (widget-put widget 'prj-other-widget
560 (widget-create 'editable-list
561 :entry-format "%i%d %v"
562 :notify 'ada-prj-field-modified
563 :help-echo (widget-get widget 'prj-help)
564 :value value
565 (list 'editable-field
566 :keymap widget-keymap)))
567 (widget-default-value-set widget "Hide Value")
570 (widget-setup)
573 (defun ada-prj-field (field text help-text &optional is-list is-paths after-text)
574 "Create a widget to edit FIELD in the current buffer.
575 TEXT is a short explanation of what the field means, whereas HELP-TEXT
576 is the text displayed when the user pressed the help button.
577 If IS-LIST is non-nil, the field contains a list. Otherwise, it contains
578 a single string.
579 if IS-PATHS is true, some special buttons are added to load paths,...
580 AFTER-TEXT is inserted just after the widget."
581 (let ((value (plist-get ada-prj-current-values field))
582 (inhibit-read-only t)
583 widget)
584 (unless value
585 (set 'value
586 (if is-list '() "")))
587 (widget-insert text)
588 (widget-insert ":")
589 (move-to-column 54 t)
590 (widget-put (widget-create 'push-button
591 :notify 'ada-prj-display-help
592 "Help")
593 'prj-help
594 help-text)
595 (widget-insert (concat " (" (symbol-name field) ")\n"))
596 (if is-paths
597 (progn
598 (widget-create 'push-button
599 :notify
600 (list 'lambda '(&rest dummy) '(interactive)
601 (list 'ada-prj-load-from-file
602 (list 'quote field)))
603 "Load From File")
604 (widget-insert " ")
605 (widget-create 'push-button
606 :notify
607 (list 'lambda '(&rest dummy) '(interactive)
608 (list 'ada-prj-load-directory
609 (list 'quote field)))
610 "Load Recursive Directory")
611 (widget-insert "\n ${build_dir}\n")))
612 (set 'widget
613 (if is-list
614 (if (< (length value) 15)
615 (widget-create 'editable-list
616 :entry-format "%i%d %v"
617 :notify 'ada-prj-field-modified
618 :help-echo help-text
619 :value value
620 (list 'editable-field :keymap widget-keymap))
621 (let ((w (widget-create 'push-button
622 :notify 'ada-prj-show-value
623 "Show value")))
624 (widget-insert "\n")
625 (widget-put w 'prj-field field)
626 (widget-put w 'prj-help help-text)
627 (widget-put w 'prj-other-widget nil)
630 (widget-create 'editable-field
631 :format "%v"
632 :notify 'ada-prj-field-modified
633 :help-echo help-text
634 :keymap widget-keymap
635 value)))
636 (widget-put widget 'prj-field field)
637 (if after-text
638 (widget-insert after-text))
639 (widget-insert "\n")
643 ;; Set the keymap once and for all, so that the keys set by the user in his
644 ;; config file are not overwritten every time we open a new file.
645 (ada-prj-add-keymap)
646 (ada-prj-add-ada-menu)
648 (provide 'ada-prj)
650 ;;; ada-prj.el ends here