]> code.delx.au - gnu-emacs/blob - lisp/cedet/ede/autoconf-edit.el
03df3f2f3d53ba5f5c6014b2073ac36c5c3bfe69
[gnu-emacs] / lisp / cedet / ede / autoconf-edit.el
1 ;;; ede/autoconf-edit.el --- Keymap for autoconf
2
3 ;; Copyright (C) 1998, 1999, 2000, 2009 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs 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 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Autoconf editing and modification support, and compatibility layer
26 ;; for Emacses w/out autoconf mode built in.
27
28 ;;; Code:
29 (require 'autoconf)
30
31 (defvar autoconf-new-automake-string
32 "dnl Process this file with autoconf to produce a configure script
33
34 AC_INIT(%s)
35 AM_INIT_AUTOMAKE([%s], 0)
36 AM_CONFIG_HEADER(config.h)
37
38 dnl End the configure script.
39 AC_OUTPUT(Makefile, [date > stamp-h] )\n"
40 "This string is used to initialize a new configure.in.
41 The default is designed to be used with automake.
42 The first %s will be filled with the test file.
43 The second %s will be filled with the program name.")
44
45 (defun autoconf-new-program (rootdir program testfile)
46 "Initialize a new configure.in in ROOTDIR for PROGRAM using TESTFILE.
47 ROOTDIR is the root directory of a given autoconf controlled project.
48 PROGRAM is the program to be configured.
49 TESTFILE is the file used with AC_INIT.
50 configure the initial configure script using `autoconf-new-automake-string'"
51 (interactive "DRoot Dir: \nsProgram: \nsTest File: ")
52 (if (bufferp rootdir)
53 (set-buffer rootdir)
54 (let ((cf1 (expand-file-name "configure.in" rootdir))
55 (cf2 (expand-file-name "configure.ac" rootdir)))
56 (if (and (or (file-exists-p cf1) (file-exists-p cf2))
57 (not (y-or-n-p (format "File %s exists. Start Over? "
58 (if (file-exists-p cf1)
59 cf1 cf2)
60 ))))
61 (error "Quit"))
62 (find-file cf2)))
63 ;; Note, we only ask about overwrite if a string/path is specified.
64 (erase-buffer)
65 (insert (format autoconf-new-automake-string testfile program)))
66
67 (defvar autoconf-preferred-macro-order
68 '("AC_INIT"
69 "AM_INIT_AUTOMAKE"
70 "AM_CONFIG_HEADER"
71 ;; Arg parsing
72 "AC_ARG_ENABLE"
73 "AC_ARG_WITH"
74 ;; Programs
75 "AC_PROG_MAKE_SET"
76 "AC_PROG_AWK"
77 "AC_PROG_CC"
78 "AC_PROG_CC_C_O"
79 "AC_PROG_CPP"
80 "AC_PROG_CXX"
81 "AC_PROG_CXXCPP"
82 "AC_ISC_POSIX"
83 "AC_PROG_F77"
84 "AC_PROG_GCC_TRADITIONAL"
85 "AC_PROG_INSTALL"
86 "AC_PROG_LEX"
87 "AC_PROG_LN_S"
88 "AC_PROG_RANLIB"
89 "AC_PROG_YACC"
90 "AC_CHECK_PROG"
91 "AC_CHECK_PROGS"
92 "AC_PROG_LIBTOOL"
93 ;; Libraries
94 "AC_CHECK_LIB"
95 "AC_PATH_XTRA"
96 ;; Headers
97 "AC_HEADER_STDC"
98 "AC_HEADER_SYS_WAIT"
99 "AC_HEADER_TIME"
100 "AC_HEADERS"
101 ;; Typedefs, structures
102 "AC_TYPE_PID_T"
103 "AC_TYPE_SIGNAL"
104 "AC_TYPE_UID_T"
105 "AC_STRUCT_TM"
106 ;; Compiler characteristics
107 "AC_CHECK_SIZEOF"
108 "AC_C_CONST"
109 ;; Library functions
110 "AC_CHECK_FUNCS"
111 "AC_TRY_LINK"
112 ;; System Services
113 ;; Other
114 "AM_PATH_LISPDIR"
115 "AM_INIT_GUILE_MODULE"
116 ;; AC_OUTPUT is always last
117 "AC_OUTPUT"
118 )
119 "List of macros in the order that they prefer to occur in.
120 This helps when inserting a macro which doesn't yet exist
121 by positioning it near other macros which may exist.
122 From the autoconf manual:
123 `AC_INIT(FILE)'
124 checks for programs
125 checks for libraries
126 checks for header files
127 checks for typedefs
128 checks for structures
129 checks for compiler characteristics
130 checks for library functions
131 checks for system services
132 `AC_OUTPUT([FILE...])'")
133
134 (defvar autoconf-multiple-macros
135 '("AC_ARG_ENABLE"
136 "AC_ARG_WITH"
137 "AC_CHECK_PROGS"
138 "AC_CHECK_LIB"
139 "AC_CHECK_SIZEOF"
140 "AC_TRY_LINK"
141 )
142 "Macros which appear multiple times.")
143
144 (defvar autoconf-multiple-multiple-macros
145 '("AC_HEADERS" "AC_CHECK_FUNCS")
146 "Macros which appear multiple times, and perform multiple queries.")
147
148 (defun autoconf-in-macro (macro)
149 "Non-nil if point is in a macro of type MACRO."
150 (save-excursion
151 (beginning-of-line)
152 (looking-at (concat "\\(A[CM]_" macro "\\|" macro "\\)"))))
153
154 (defun autoconf-find-last-macro (macro)
155 "Move to the last occurrence of MACRO in FILE, and return that point.
156 The last macro is usually the one in which we would like to insert more
157 items such as CHECK_HEADERS."
158 (let ((op (point)))
159 (goto-char (point-max))
160 (if (re-search-backward (concat "^" (regexp-quote macro) "\\s-*\\((\\|$\\)") nil t)
161 (progn
162 (beginning-of-line)
163 (point))
164 (goto-char op)
165 nil)))
166
167 (defun autoconf-parameter-strip (param)
168 "Strip the parameter PARAM of whitespace and miscellaneous characters."
169 (when (string-match "^\\s-*\\[?\\s-*" param)
170 (setq param (substring param (match-end 0))))
171 (when (string-match "\\s-*\\]?\\s-*$" param)
172 (setq param (substring param 0 (match-beginning 0))))
173 param)
174
175 (defun autoconf-parameters-for-macro (macro)
176 "Retrieve the parameters to MACRO.
177 Returns a list of the arguments passed into MACRO as strings."
178 (save-excursion
179 (when (autoconf-find-last-macro macro)
180 (forward-sexp 1)
181 (mapcar
182 #'autoconf-parameter-strip
183 (when (looking-at "(")
184 (let* ((start (+ (point) 1))
185 (end (save-excursion
186 (forward-sexp 1)
187 (- (point) 1)))
188 (ans (buffer-substring-no-properties start end)))
189 (split-string ans "," t)))))))
190
191 (defun autoconf-position-for-macro (macro)
192 "Position the cursor where a new MACRO could be inserted.
193 This will appear at the BEGINNING of the macro MACRO should appear AFTER.
194 This is to make it compatible with `autoconf-find-last-macro'.
195 Assume that MACRO doesn't appear in the buffer yet, so search
196 the ordering list `autoconf-preferred-macro-order'."
197 ;; Search this list backwards.. heh heh heh
198 ;; This lets us do a reverse search easilly.
199 (let ((ml (member macro (reverse autoconf-preferred-macro-order))))
200 (if (not ml) (error "Don't know how to position for %s yet" macro))
201 (setq ml (cdr ml))
202 (goto-char (point-max))
203 (while (and ml (not (autoconf-find-last-macro (car ml))))
204 (setq ml (cdr ml)))
205 (if (not ml) (error "Could not find context for positioning %s" macro))))
206
207 (defun autoconf-insert-macro-at-point (macro &optional param)
208 "Add MACRO at the current point with PARAM."
209 (insert macro)
210 (if param
211 (progn
212 (insert "(" param ")")
213 (if (< (current-column) 3) (insert " dnl")))))
214
215 (defun autoconf-insert-new-macro (macro &optional param)
216 "Add a call to MACRO in the current autoconf file.
217 Deals with macro order. See `autoconf-preferred-macro-order' and
218 `autoconf-multi-macros'.
219 Optional argument PARAM is the parameter to pass to the macro as one string."
220 (cond ((member macro autoconf-multiple-macros)
221 ;; This occurs multiple times
222 (or (autoconf-find-last-macro macro)
223 (autoconf-position-for-macro macro))
224 (forward-sexp 2)
225 (end-of-line)
226 (insert "\n")
227 (autoconf-insert-macro-at-point macro param))
228 ((member macro autoconf-multiple-multiple-macros)
229 (if (not param)
230 (error "You must have a parameter for %s" macro))
231 (if (not (autoconf-find-last-macro macro))
232 (progn
233 ;; Doesn't exist yet....
234 (autoconf-position-for-macro macro)
235 (forward-sexp 2)
236 (end-of-line)
237 (insert "\n")
238 (autoconf-insert-macro-at-point macro param))
239 ;; Does exist, can we fit onto the current line?
240 (forward-sexp 2)
241 (down-list -1)
242 (if (> (+ (current-column) (length param)) fill-column)
243 (insert " " param)
244 (up-list 1)
245 (end-of-line)
246 (insert "\n")
247 (autoconf-insert-macro-at-point macro param))))
248 ((autoconf-find-last-macro macro)
249 ;; If it isn't one of the multi's, it's a singleton.
250 ;; If it exists, ignore it.
251 nil)
252 (t
253 (autoconf-position-for-macro macro)
254 (forward-sexp 1)
255 (if (looking-at "\\s-*(")
256 (forward-sexp 1))
257 (end-of-line)
258 (insert "\n")
259 (autoconf-insert-macro-at-point macro param))))
260
261 (defun autoconf-find-query-for-header (header)
262 "Position the cursor where HEADER is queried."
263 (interactive "sHeader: ")
264 (let ((op (point))
265 (found t))
266 (goto-char (point-min))
267 (condition-case nil
268 (while (not
269 (progn
270 (re-search-forward
271 (concat "\\b" (regexp-quote header) "\\b"))
272 (save-excursion
273 (beginning-of-line)
274 (looking-at "AC_CHECK_HEADERS")))))
275 ;; We depend on the search failing to exit our loop on failure.
276 (error (setq found nil)))
277 (if (not found) (goto-char op))
278 found))
279
280 (defun autoconf-add-query-for-header (header)
281 "Add in HEADER to be queried for in our autoconf file."
282 (interactive "sHeader: ")
283 (or (autoconf-find-query-for-header header)
284 (autoconf-insert-new-macro "AC_CHECK_HEADERS" header)))
285
286
287 (defun autoconf-find-query-for-func (func)
288 "Position the cursor where FUNC is queried."
289 (interactive "sFunction: ")
290 (let ((op (point))
291 (found t))
292 (goto-char (point-min))
293 (condition-case nil
294 (while (not
295 (progn
296 (re-search-forward
297 (concat "\\b" (regexp-quote func) "\\b"))
298 (save-excursion
299 (beginning-of-line)
300 (looking-at "AC_CHECK_FUNCS")))))
301 ;; We depend on the search failing to exit our loop on failure.
302 (error (setq found nil)))
303 (if (not found) (goto-char op))
304 found))
305
306 (defun autoconf-add-query-for-func (func)
307 "Add in FUNC to be queried for in our autoconf file."
308 (interactive "sFunction: ")
309 (or (autoconf-find-query-for-func func)
310 (autoconf-insert-new-macro "AC_CHECK_FUNCS" func)))
311
312 (defvar autoconf-program-builtin
313 '(("AWK" . "AC_PROG_AWK")
314 ("CC" . "AC_PROG_CC")
315 ("CPP" . "AC_PROG_CPP")
316 ("CXX" . "AC_PROG_CXX")
317 ("CXXCPP" . "AC_PROG_CXXCPP")
318 ("F77" . "AC_PROG_F77")
319 ("GCC_TRADITIONAL" . "AC_PROG_GCC_TRADITIONAL")
320 ("INSTALL" . "AC_PROG_INSTALL")
321 ("LEX" . "AC_PROG_LEX")
322 ("LN_S" . "AC_PROG_LN_S")
323 ("RANLIB" . "AC_PROG_RANLIB")
324 ("YACC" . "AC_PROG_YACC")
325 )
326 "Association list of PROGRAM variables and their built-in MACRO.")
327
328 (defun autoconf-find-query-for-program (prog)
329 "Position the cursor where PROG is queried.
330 PROG is the VARIABLE to use in autoconf to identify the program.
331 PROG excludes the _PROG suffix. Thus if PROG were EMACS, then the
332 variable in configure.in would be EMACS_PROG."
333 (let ((op (point))
334 (found t)
335 (builtin (assoc prog autoconf-program-builtin)))
336 (goto-char (point-min))
337 (condition-case nil
338 (re-search-forward
339 (concat "^"
340 (or (cdr-safe builtin)
341 (concat "AC_CHECK_PROG\\s-*(\\s-*" prog "_PROG"))
342 "\\>"))
343 (error (setq found nil)))
344 (if (not found) (goto-char op))
345 found))
346
347 (defun autoconf-add-query-for-program (prog &optional names)
348 "Add in PROG to be queried for in our autoconf file.
349 Optional NAMES is for non-built-in programs, and is the list
350 of possible names."
351 (interactive "sProgram: ")
352 (if (autoconf-find-query-for-program prog)
353 nil
354 (let ((builtin (assoc prog autoconf-program-builtin)))
355 (if builtin
356 (autoconf-insert-new-macro (cdr builtin))
357 ;; Not built in, try the params item
358 (autoconf-insert-new-macro "AC_CHECK_PROGS" (concat prog "," names))
359 ))))
360
361 ;;; Scrappy little changes
362 ;;
363 (defvar autoconf-deleted-text nil
364 "Set to the last bit of text deleted during an edit.")
365
366 (defvar autoconf-inserted-text nil
367 "Set to the last bit of text inserted during an edit.")
368
369 (defmacro autoconf-edit-cycle (&rest body)
370 "Start an edit cycle, unsetting the modified flag if there is no change.
371 Optional argument BODY is the code to execute which edits the autoconf file."
372 `(let ((autoconf-deleted-text nil)
373 (autoconf-inserted-text nil)
374 (mod (buffer-modified-p)))
375 ,@body
376 (if (and (not mod)
377 (string= autoconf-deleted-text autoconf-inserted-text))
378 (set-buffer-modified-p nil))))
379
380 (defun autoconf-delete-parameter (index)
381 "Delete the INDEXth parameter from the macro starting on the current line.
382 Leaves the cursor where a new parameter can be inserted.
383 INDEX starts at 1."
384 (beginning-of-line)
385 (down-list 1)
386 (re-search-forward ", ?" nil nil (1- index))
387 (let ((end (save-excursion
388 (re-search-forward ",\\|)" (save-excursion
389 (end-of-line)
390 (point)))
391 (forward-char -1)
392 (point))))
393 (setq autoconf-deleted-text (buffer-substring (point) end))
394 (delete-region (point) end)))
395
396 (defun autoconf-insert (text)
397 "Insert TEXT."
398 (setq autoconf-inserted-text text)
399 (insert text))
400
401 (defun autoconf-set-version (version)
402 "Set the version used with automake to VERSION."
403 (if (not (stringp version))
404 (signal 'wrong-type-argument '(stringp version)))
405 (if (not (autoconf-find-last-macro "AM_INIT_AUTOMAKE"))
406 (error "Cannot update version")
407 ;; Move to correct position.
408 (autoconf-edit-cycle
409 (autoconf-delete-parameter 2)
410 (autoconf-insert version))))
411
412 (defun autoconf-set-output (outputlist)
413 "Set the files created in AC_OUTPUT to OUTPUTLIST.
414 OUTPUTLIST is a list of strings representing relative paths
415 to Makefiles, or other files using Autoconf substitution."
416 (if (not (autoconf-find-last-macro "AC_OUTPUT"))
417 (error "Cannot update version")
418 (autoconf-edit-cycle
419 (autoconf-delete-parameter 1)
420 (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
421
422 (provide 'ede/autoconf-edit)
423
424 ;; arch-tag: 5932c433-4fd4-4d5e-ab35-8effd95a405f
425 ;;; ede/autoconf-edit.el ends here