1 ;;; ede/autoconf-edit.el --- Keymap for autoconf
3 ;; Copyright (C) 1998, 1999, 2000, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; Autoconf editing and modification support, and compatibility layer
26 ;; for Emacses w/out autoconf mode built in.
31 (defvar autoconf-new-automake-string
32 "dnl Process this file with autoconf to produce a configure script
35 AM_INIT_AUTOMAKE([%s], 0)
36 AM_CONFIG_HEADER(config.h)
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.")
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: ")
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)
63 ;; Note, we only ask about overwrite if a string/path is specified.
65 (insert (format autoconf-new-automake-string testfile program)))
67 (defvar autoconf-preferred-macro-order
84 "AC_PROG_GCC_TRADITIONAL"
101 ;; Typedefs, structures
106 ;; Compiler characteristics
115 "AM_INIT_GUILE_MODULE"
116 ;; AC_OUTPUT is always last
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:
126 checks for header files
128 checks for structures
129 checks for compiler characteristics
130 checks for library functions
131 checks for system services
132 `AC_OUTPUT([FILE...])'")
134 (defvar autoconf-multiple-macros
142 "Macros which appear multiple times.")
144 (defvar autoconf-multiple-multiple-macros
145 '("AC_HEADERS" "AC_CHECK_FUNCS")
146 "Macros which appear multiple times, and perform multiple queries.")
148 (defun autoconf-in-macro (macro)
149 "Non-nil if point is in a macro of type MACRO."
152 (looking-at (concat "\\(A[CM]_" macro "\\|" macro "\\)"))))
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."
159 (goto-char (point-max))
160 (if (re-search-backward (concat "^" (regexp-quote macro) "\\s-*\\((\\|$\\)") nil t)
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))))
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."
179 (when (autoconf-find-last-macro macro)
182 #'autoconf-parameter-strip
183 (when (looking-at "(")
184 (let* ((start (+ (point) 1))
188 (ans (buffer-substring-no-properties start end)))
189 (split-string ans "," t)))))))
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))
202 (goto-char (point-max))
203 (while (and ml (not (autoconf-find-last-macro (car ml))))
205 (if (not ml) (error "Could not find context for positioning %s" macro))))
207 (defun autoconf-insert-macro-at-point (macro &optional param)
208 "Add MACRO at the current point with PARAM."
212 (insert "(" param ")")
213 (if (< (current-column) 3) (insert " dnl")))))
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))
227 (autoconf-insert-macro-at-point macro param))
228 ((member macro autoconf-multiple-multiple-macros)
230 (error "You must have a parameter for %s" macro))
231 (if (not (autoconf-find-last-macro macro))
233 ;; Doesn't exist yet....
234 (autoconf-position-for-macro macro)
238 (autoconf-insert-macro-at-point macro param))
239 ;; Does exist, can we fit onto the current line?
242 (if (> (+ (current-column) (length param)) fill-column)
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.
253 (autoconf-position-for-macro macro)
255 (if (looking-at "\\s-*(")
259 (autoconf-insert-macro-at-point macro param))))
261 (defun autoconf-find-query-for-header (header)
262 "Position the cursor where HEADER is queried."
263 (interactive "sHeader: ")
266 (goto-char (point-min))
271 (concat "\\b" (regexp-quote header) "\\b"))
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))
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)))
287 (defun autoconf-find-query-for-func (func)
288 "Position the cursor where FUNC is queried."
289 (interactive "sFunction: ")
292 (goto-char (point-min))
297 (concat "\\b" (regexp-quote func) "\\b"))
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))
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)))
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")
326 "Association list of PROGRAM variables and their built-in MACRO.")
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."
335 (builtin (assoc prog autoconf-program-builtin)))
336 (goto-char (point-min))
340 (or (cdr-safe builtin)
341 (concat "AC_CHECK_PROG\\s-*(\\s-*" prog "_PROG"))
343 (error (setq found nil)))
344 (if (not found) (goto-char op))
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
351 (interactive "sProgram: ")
352 (if (autoconf-find-query-for-program prog)
354 (let ((builtin (assoc prog autoconf-program-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))
361 ;;; Scrappy little changes
363 (defvar autoconf-deleted-text nil
364 "Set to the last bit of text deleted during an edit.")
366 (defvar autoconf-inserted-text nil
367 "Set to the last bit of text inserted during an edit.")
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)))
377 (string= autoconf-deleted-text autoconf-inserted-text))
378 (set-buffer-modified-p nil))))
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.
386 (re-search-forward ", ?" nil nil (1- index))
387 (let ((end (save-excursion
388 (re-search-forward ",\\|)" (save-excursion
393 (setq autoconf-deleted-text (buffer-substring (point) end))
394 (delete-region (point) end)))
396 (defun autoconf-insert (text)
398 (setq autoconf-inserted-text text)
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.
409 (autoconf-delete-parameter 2)
410 (autoconf-insert version))))
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")
419 (autoconf-delete-parameter 1)
420 (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
422 (provide 'ede/autoconf-edit)
424 ;; arch-tag: 5932c433-4fd4-4d5e-ab35-8effd95a405f
425 ;;; ede/autoconf-edit.el ends here