1 ;;; autoconf-edit.el --- Keymap for autoconf
3 ;; Copyright (C) 1998, 1999, 2000 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; RCS: $Id: autoconf-edit.el,v 1.1 2007-11-26 15:22:05 michaels Exp $
9 ;; This software is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This software is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; Autoconf editing and modification support, and compatibility layer
27 ;; for Emacses w/out autoconf mode built in.
30 (if (locate-library "autoconf")
34 (error (require 'autoconf-mode "autoconf")))
36 (require 'autoconf-compat)
38 ;; This part is not in autoconf.el
39 (add-to-list 'auto-mode-alist '("\\<configure\\.in$" . autoconf-mode))
42 (defvar autoconf-new-automake-string
43 "dnl Process this file with autoconf to produce a configure script
46 AM_INIT_AUTOMAKE(%s, 0)
47 AM_CONFIG_HEADER(config.h)
49 dnl End the configure script.
50 AC_OUTPUT(Makefile, [date > stamp-h] )\n"
51 "This string is used to initialize a new configure.in.
52 The default is designed to be used with automake.
53 The first %s will be filled with the test file.
54 The second %s will be filled with the program name.")
56 (defun autoconf-new-program (rootdir program testfile)
57 "Initialize a new configure.in in ROOTDIR for PROGRAM using TESTFILE.
58 ROOTDIR is the root directory of a given autoconf controlled project.
59 PROGRAM is the program to be configured.
60 TESTFILE is the file used with AC_INIT.
61 configure the initial configure script using `autoconf-new-automake-string'"
62 (interactive "dRoot Dir: \nsProgram: \nsTest File: ")
65 (if (not (string-match "\\(/\\|\\\\)$" rootdir))
66 (setq rootdir (concat rootdir "/")))
67 (let ((cf (concat rootdir "configure.in")))
68 (if (and (file-exists-p cf)
69 (not (y-or-n-p (format "File %s exists. Start Over? " cf))))
72 ;; Note, we only ask about overwrite if a string/path is specified.
74 (insert (format autoconf-new-automake-string testfile program)))
76 (defvar autoconf-preferred-macro-order
93 "AC_PROG_GCC_TRADITIONAL"
110 ;; Typedefs, structures
115 ;; Compiler characteristics
124 "AM_INIT_GUILE_MODULE"
125 ;; AC_OUTPUT is always last
128 "List of macros in the order that they prefer to occur in.
129 This helps when inserting a macro which doesn't yet exist
130 by positioning it near other macros which may exist.
131 From the autoconf manual:
135 checks for header files
137 checks for structures
138 checks for compiler characteristics
139 checks for library functions
140 checks for system services
141 `AC_OUTPUT([FILE...])'")
143 (defvar autoconf-multiple-macros
151 "Macros which appear multiple times.")
153 (defvar autoconf-multiple-multiple-macros
154 '("AC_HEADERS" "AC_CHECK_FUNCS")
155 "Macros which appear multiple times, and perform multiple queries.")
157 (defun autoconf-in-macro (macro)
158 "Non-nil if point is in a macro of type MACRO."
161 (looking-at (concat "\\(A[CM]_" macro "\\|" macro "\\)"))))
163 (defun autoconf-find-last-macro (macro)
164 "Move to the last occurance of MACRO in FILE, and return that point.
165 The last macro is usually the one in which we would like to insert more
166 items such as CHECK_HEADERS."
168 (goto-char (point-max))
169 (if (re-search-backward (concat "^" (regexp-quote macro) "\\s-*\\((\\|$\\)") nil t)
176 (defun autoconf-position-for-macro (macro)
177 "Position the cursor where a new MACRO could be inserted.
178 This will appear at the BEGINNING of the macro MACRO should appear AFTER.
179 This is to make it compatible with `autoconf-find-last-macro'.
180 Assume that MACRO doesn't appear in the buffer yet, so search
181 the ordering list `autoconf-preferred-macro-order'."
182 ;; Search this list backwards.. heh heh heh
183 ;; This lets us do a reverse search easilly.
184 (let ((ml (member macro (reverse autoconf-preferred-macro-order))))
185 (if (not ml) (error "Don't know how to position for %s yet" macro))
187 (goto-char (point-max))
188 (while (and ml (not (autoconf-find-last-macro (car ml))))
190 (if (not ml) (error "Could not find context for positioning %s" macro))))
192 (defun autoconf-insert-macro-at-point (macro &optional param)
193 "Add MACRO at the current point with PARAM."
197 (insert "(" param ")")
198 (if (< (current-column) 3) (insert " dnl")))))
200 (defun autoconf-insert-new-macro (macro &optional param)
201 "Add a call to MACRO in the current autoconf file.
202 Deals with macro order. See `autoconf-preferred-macro-order' and
203 `autoconf-multi-macros'.
204 Optional argument PARAM is the parameter to pass to the macro as one string."
205 (cond ((member macro autoconf-multiple-macros)
206 ;; This occurs multiple times
207 (or (autoconf-find-last-macro macro)
208 (autoconf-position-for-macro macro))
212 (autoconf-insert-macro-at-point macro param))
213 ((member macro autoconf-multiple-multiple-macros)
215 (error "You must have a paramter for %s" macro))
216 (if (not (autoconf-find-last-macro macro))
218 ;; Doesn't exist yet....
219 (autoconf-position-for-macro macro)
223 (autoconf-insert-macro-at-point macro param))
224 ;; Does exist, can we fit onto the current line?
227 (if (> (+ (current-column) (length param)) fill-column)
232 (autoconf-insert-macro-at-point macro param))))
233 ((autoconf-find-last-macro macro)
234 ;; If it isn't one of the multi's, it's a singleton.
235 ;; If it exists, ignore it.
238 (autoconf-position-for-macro macro)
240 (if (looking-at "\\s-*(")
244 (autoconf-insert-macro-at-point macro param))))
246 (defun autoconf-find-query-for-header (header)
247 "Position the cursor where HEADER is queried."
248 (interactive "sHeader: ")
251 (goto-char (point-min))
256 (concat "\\b" (regexp-quote header) "\\b"))
259 (looking-at "AC_CHECK_HEADERS")))))
260 ;; We depend on the search failing to exit our loop on failure.
261 (error (setq found nil)))
262 (if (not found) (goto-char op))
265 (defun autoconf-add-query-for-header (header)
266 "Add in HEADER to be queried for in our autoconf file."
267 (interactive "sHeader: ")
268 (or (autoconf-find-query-for-header header)
269 (autoconf-insert-new-macro "AC_CHECK_HEADERS" header)))
272 (defun autoconf-find-query-for-func (func)
273 "Position the cursor where FUNC is queried."
274 (interactive "sFunction: ")
277 (goto-char (point-min))
282 (concat "\\b" (regexp-quote func) "\\b"))
285 (looking-at "AC_CHECK_FUNCS")))))
286 ;; We depend on the search failing to exit our loop on failure.
287 (error (setq found nil)))
288 (if (not found) (goto-char op))
291 (defun autoconf-add-query-for-func (func)
292 "Add in FUNC to be queried for in our autoconf file."
293 (interactive "sFunction: ")
294 (or (autoconf-find-query-for-func func)
295 (autoconf-insert-new-macro "AC_CHECK_FUNCS" func)))
297 (defvar autoconf-program-builtin
298 '(("AWK" . "AC_PROG_AWK")
299 ("CC" . "AC_PROG_CC")
300 ("CPP" . "AC_PROG_CPP")
301 ("CXX" . "AC_PROG_CXX")
302 ("CXXCPP" . "AC_PROG_CXXCPP")
303 ("F77" . "AC_PROG_F77")
304 ("GCC_TRADITIONAL" . "AC_PROG_GCC_TRADITIONAL")
305 ("INSTALL" . "AC_PROG_INSTALL")
306 ("LEX" . "AC_PROG_LEX")
307 ("LN_S" . "AC_PROG_LN_S")
308 ("RANLIB" . "AC_PROG_RANLIB")
309 ("YACC" . "AC_PROG_YACC")
311 "Association list of PROGRAM variables and their built-in MACRO.")
313 (defun autoconf-find-query-for-program (prog)
314 "Position the cursor where PROG is queried.
315 PROG is the VARIABLE to use in autoconf to identify the program.
316 PROG excludes the _PROG suffix. Thus if PROG were EMACS, then the
317 variable in configure.in would be EMACS_PROG."
320 (builtin (assoc prog autoconf-program-builtin)))
321 (goto-char (point-min))
325 (or (cdr-safe builtin)
326 (concat "AC_CHECK_PROG\\s-*(\\s-*" prog "_PROG"))
328 (error (setq found nil)))
329 (if (not found) (goto-char op))
332 (defun autoconf-add-query-for-program (prog &optional names)
333 "Add in PROG to be queried for in our autoconf file.
334 Optional NAMES is for non-built-in programs, and is the list
336 (interactive "sProgram: ")
337 (if (autoconf-find-query-for-program prog)
339 (let ((builtin (assoc prog autoconf-program-builtin)))
341 (autoconf-insert-new-macro (cdr builtin))
342 ;; Not built in, try the params item
343 (autoconf-insert-new-macro "AC_CHECK_PROGS" (concat prog "," names))
346 ;;; Scrappy little changes
348 (defvar autoconf-deleted-text nil
349 "Set to the last bit of text deleted during an edit.")
351 (defvar autoconf-inserted-text nil
352 "Set to the last bit of text inserted during an edit.")
354 (defmacro autoconf-edit-cycle (&rest body)
355 "Start an edit cycle, unsetting the modified flag if there is no change.
356 Optional argument BODY is the code to execute which edits the autoconf file."
357 `(let ((autoconf-deleted-text nil)
358 (autoconf-inserted-text nil)
359 (mod (buffer-modified-p)))
362 (string= autoconf-deleted-text autoconf-inserted-text))
363 (set-buffer-modified-p nil))))
365 (defun autoconf-delete-parameter (index)
366 "Delete the INDEXth parameter from the macro starting on the current line.
367 Leaves the cursor where a new parameter can be inserted.
371 (re-search-forward ", ?" nil nil (1- index))
372 (let ((end (save-excursion
373 (re-search-forward ",\\|)" (save-excursion
378 (setq autoconf-deleted-text (buffer-substring (point) end))
379 (delete-region (point) end)))
381 (defun autoconf-insert (text)
383 (setq autoconf-inserted-text text)
386 (defun autoconf-set-version (version)
387 "Set the version used with automake to VERSION."
388 (if (not (stringp version))
389 (signal 'wrong-type-argument '(stringp version)))
390 (if (not (autoconf-find-last-macro "AM_INIT_AUTOMAKE"))
391 (error "Cannot update version")
392 ;; Move to correct position.
394 (autoconf-delete-parameter 2)
395 (autoconf-insert version))))
397 (defun autoconf-set-output (outputlist)
398 "Set the files created in AC_OUTPUT to OUTPUTLIST.
399 OUTPUTLIST is a list of strings representing relative paths
400 to Makefiles, or other files using Autoconf substitution."
401 (if (not (autoconf-find-last-macro "AC_OUTPUT"))
402 (error "Cannot update version")
404 (autoconf-delete-parameter 1)
405 (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
407 (provide 'autoconf-edit)
409 ;;; autoconf-edit.el ends here