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