1 ;;; ede/autoconf-edit.el --- Keymap for autoconf
3 ;; Copyright (C) 1998, 1999, 2000, 2009, 2010, 2011, 2012 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.
30 (declare-function ede-srecode-setup
"ede/srecode")
31 (declare-function ede-srecode-insert
"ede/srecode")
33 (defun autoconf-new-program (rootdir program testfile
)
34 "Initialize a new configure.in in ROOTDIR for PROGRAM using TESTFILE.
35 ROOTDIR is the root directory of a given autoconf controlled project.
36 PROGRAM is the program to be configured.
37 TESTFILE is the file used with AC_INIT.
38 configure the initial configure script using `autoconf-new-automake-string'"
39 (interactive "DRoot Dir: \nsProgram: \nsTest File: ")
40 (require 'ede
/srecode
)
43 (let ((cf1 (expand-file-name "configure.in" rootdir
))
44 (cf2 (expand-file-name "configure.ac" rootdir
)))
45 (if (and (or (file-exists-p cf1
) (file-exists-p cf2
))
46 (not (y-or-n-p (format "File %s exists. Start Over? "
47 (if (file-exists-p cf1
)
52 ;; Note, we only ask about overwrite if a string/path is specified.
61 (defvar autoconf-preferred-macro-order
78 "AC_PROG_GCC_TRADITIONAL"
95 ;; Typedefs, structures
100 ;; Compiler characteristics
109 "AM_INIT_GUILE_MODULE"
110 ;; AC_OUTPUT is always last
113 "List of macros in the order that they prefer to occur in.
114 This helps when inserting a macro which doesn't yet exist
115 by positioning it near other macros which may exist.
116 From the autoconf manual:
120 checks for header files
122 checks for structures
123 checks for compiler characteristics
124 checks for library functions
125 checks for system services
126 `AC_OUTPUT([FILE...])'")
128 (defvar autoconf-multiple-macros
136 "Macros which appear multiple times.")
138 (defvar autoconf-multiple-multiple-macros
139 '("AC_HEADERS" "AC_CHECK_FUNCS")
140 "Macros which appear multiple times, and perform multiple queries.")
142 (defun autoconf-in-macro (macro)
143 "Non-nil if point is in a macro of type MACRO."
146 (looking-at (concat "\\(A[CM]_" macro
"\\|" macro
"\\)"))))
148 (defun autoconf-find-last-macro (macro &optional ignore-bol
)
149 "Move to the last occurrence of MACRO in FILE, and return that point.
150 The last macro is usually the one in which we would like to insert more
151 items such as CHECK_HEADERS."
152 (let ((op (point)) (atbol (if ignore-bol
"" "^")))
153 (goto-char (point-max))
154 (if (re-search-backward (concat atbol
(regexp-quote macro
) "\\s-*\\((\\|$\\)") nil t
)
156 (unless ignore-bol
(beginning-of-line))
161 (defun autoconf-parameter-strip (param)
162 "Strip the parameter PARAM of whitespace and miscellaneous characters."
163 ;; force greedy match for \n.
164 (when (string-match "\\`\n*\\s-*\\[?\\s-*" param
)
165 (setq param
(substring param
(match-end 0))))
166 (when (string-match "\\s-*\\]?\\s-*\\'" param
)
167 (setq param
(substring param
0 (match-beginning 0))))
170 (defun autoconf-parameters-for-macro (macro &optional ignore-bol ignore-case
)
171 "Retrieve the parameters to MACRO.
172 Returns a list of the arguments passed into MACRO as strings."
173 (let ((case-fold-search ignore-case
))
175 (when (autoconf-find-last-macro macro ignore-bol
)
178 #'autoconf-parameter-strip
179 (when (looking-at "(")
180 (let* ((start (+ (point) 1))
184 (ans (buffer-substring-no-properties start end
)))
185 (split-string ans
"," t
))))))))
187 (defun autoconf-position-for-macro (macro)
188 "Position the cursor where a new MACRO could be inserted.
189 This will appear at the BEGINNING of the macro MACRO should appear AFTER.
190 This is to make it compatible with `autoconf-find-last-macro'.
191 Assume that MACRO doesn't appear in the buffer yet, so search
192 the ordering list `autoconf-preferred-macro-order'."
193 ;; Search this list backwards.. heh heh heh
194 ;; This lets us do a reverse search easilly.
195 (let ((ml (member macro
(reverse autoconf-preferred-macro-order
))))
196 (if (not ml
) (error "Don't know how to position for %s yet" macro
))
198 (goto-char (point-max))
199 (while (and ml
(not (autoconf-find-last-macro (car ml
))))
201 (if (not ml
) (error "Could not find context for positioning %s" macro
))))
203 (defun autoconf-insert-macro-at-point (macro &optional param
)
204 "Add MACRO at the current point with PARAM."
208 (insert "(" param
")")
209 (if (< (current-column) 3) (insert " dnl")))))
211 (defun autoconf-insert-new-macro (macro &optional param
)
212 "Add a call to MACRO in the current autoconf file.
213 Deals with macro order. See `autoconf-preferred-macro-order' and
214 `autoconf-multi-macros'.
215 Optional argument PARAM is the parameter to pass to the macro as one string."
216 (cond ((member macro autoconf-multiple-macros
)
217 ;; This occurs multiple times
218 (or (autoconf-find-last-macro macro
)
219 (autoconf-position-for-macro macro
))
223 (autoconf-insert-macro-at-point macro param
))
224 ((member macro autoconf-multiple-multiple-macros
)
226 (error "You must have a parameter for %s" macro
))
227 (if (not (autoconf-find-last-macro macro
))
229 ;; Doesn't exist yet....
230 (autoconf-position-for-macro macro
)
234 (autoconf-insert-macro-at-point macro param
))
235 ;; Does exist, can we fit onto the current line?
238 (if (> (+ (current-column) (length param
)) fill-column
)
243 (autoconf-insert-macro-at-point macro param
))))
244 ((autoconf-find-last-macro macro
)
245 ;; If it isn't one of the multi's, it's a singleton.
246 ;; If it exists, ignore it.
249 (autoconf-position-for-macro macro
)
251 (if (looking-at "\\s-*(")
255 (autoconf-insert-macro-at-point macro param
))))
257 (defun autoconf-find-query-for-header (header)
258 "Position the cursor where HEADER is queried."
259 (interactive "sHeader: ")
262 (goto-char (point-min))
267 (concat "\\b" (regexp-quote header
) "\\b"))
270 (looking-at "AC_CHECK_HEADERS")))))
271 ;; We depend on the search failing to exit our loop on failure.
272 (error (setq found nil
)))
273 (if (not found
) (goto-char op
))
276 (defun autoconf-add-query-for-header (header)
277 "Add in HEADER to be queried for in our autoconf file."
278 (interactive "sHeader: ")
279 (or (autoconf-find-query-for-header header
)
280 (autoconf-insert-new-macro "AC_CHECK_HEADERS" header
)))
283 (defun autoconf-find-query-for-func (func)
284 "Position the cursor where FUNC is queried."
285 (interactive "sFunction: ")
288 (goto-char (point-min))
293 (concat "\\b" (regexp-quote func
) "\\b"))
296 (looking-at "AC_CHECK_FUNCS")))))
297 ;; We depend on the search failing to exit our loop on failure.
298 (error (setq found nil
)))
299 (if (not found
) (goto-char op
))
302 (defun autoconf-add-query-for-func (func)
303 "Add in FUNC to be queried for in our autoconf file."
304 (interactive "sFunction: ")
305 (or (autoconf-find-query-for-func func
)
306 (autoconf-insert-new-macro "AC_CHECK_FUNCS" func
)))
308 (defvar autoconf-program-builtin
309 '(("AWK" .
"AC_PROG_AWK")
310 ("CC" .
"AC_PROG_CC")
311 ("CPP" .
"AC_PROG_CPP")
312 ("CXX" .
"AC_PROG_CXX")
313 ("CXXCPP" .
"AC_PROG_CXXCPP")
314 ("F77" .
"AC_PROG_F77")
315 ("GCC_TRADITIONAL" .
"AC_PROG_GCC_TRADITIONAL")
316 ("INSTALL" .
"AC_PROG_INSTALL")
317 ("LEX" .
"AC_PROG_LEX")
318 ("LN_S" .
"AC_PROG_LN_S")
319 ("RANLIB" .
"AC_PROG_RANLIB")
320 ("YACC" .
"AC_PROG_YACC")
322 "Association list of PROGRAM variables and their built-in MACRO.")
324 (defun autoconf-find-query-for-program (prog)
325 "Position the cursor where PROG is queried.
326 PROG is the VARIABLE to use in autoconf to identify the program.
327 PROG excludes the _PROG suffix. Thus if PROG were EMACS, then the
328 variable in configure.in would be EMACS_PROG."
331 (builtin (assoc prog autoconf-program-builtin
)))
332 (goto-char (point-min))
336 (or (cdr-safe builtin
)
337 (concat "AC_CHECK_PROG\\s-*(\\s-*" prog
"_PROG"))
339 (error (setq found nil
)))
340 (if (not found
) (goto-char op
))
343 (defun autoconf-add-query-for-program (prog &optional names
)
344 "Add in PROG to be queried for in our autoconf file.
345 Optional NAMES is for non-built-in programs, and is the list
347 (interactive "sProgram: ")
348 (if (autoconf-find-query-for-program prog
)
350 (let ((builtin (assoc prog autoconf-program-builtin
)))
352 (autoconf-insert-new-macro (cdr builtin
))
353 ;; Not built in, try the params item
354 (autoconf-insert-new-macro "AC_CHECK_PROGS" (concat prog
"," names
))
357 ;;; Scrappy little changes
359 (defvar autoconf-deleted-text nil
360 "Set to the last bit of text deleted during an edit.")
362 (defvar autoconf-inserted-text nil
363 "Set to the last bit of text inserted during an edit.")
365 (defmacro autoconf-edit-cycle
(&rest body
)
366 "Start an edit cycle, unsetting the modified flag if there is no change.
367 Optional argument BODY is the code to execute which edits the autoconf file."
368 `(let ((autoconf-deleted-text nil
)
369 (autoconf-inserted-text nil
)
370 (mod (buffer-modified-p)))
373 (string= autoconf-deleted-text autoconf-inserted-text
))
374 (set-buffer-modified-p nil
))))
376 (defun autoconf-delete-parameter (index)
377 "Delete the INDEXth parameter from the macro starting on the current line.
378 Leaves the cursor where a new parameter can be inserted.
382 (re-search-forward ", ?" nil nil
(1- index
))
383 (let ((end (save-excursion
384 (re-search-forward ",\\|)" (save-excursion
389 (setq autoconf-deleted-text
(buffer-substring (point) end
))
390 (delete-region (point) end
)))
392 (defun autoconf-insert (text)
394 (setq autoconf-inserted-text text
)
397 (defun autoconf-set-version (version)
398 "Set the version used with automake to VERSION."
399 (if (not (stringp version
))
400 (signal 'wrong-type-argument
'(stringp version
)))
401 (if (not (autoconf-find-last-macro "AM_INIT_AUTOMAKE"))
402 (error "Cannot update version")
403 ;; Move to correct position.
405 (autoconf-delete-parameter 2)
406 (autoconf-insert version
))))
408 (defun autoconf-set-output (outputlist)
409 "Set the files created in AC_OUTPUT to OUTPUTLIST.
410 OUTPUTLIST is a list of strings representing relative paths
411 to Makefiles, or other files using Autoconf substitution."
412 (if (not (autoconf-find-last-macro "AC_OUTPUT"))
413 (error "Cannot update version")
415 (autoconf-delete-parameter 1)
416 (autoconf-insert (mapconcat (lambda (a) a
) outputlist
" ")))))
418 (provide 'ede
/autoconf-edit
)
420 ;; arch-tag: 5932c433-4fd4-4d5e-ab35-8effd95a405f
421 ;;; ede/autoconf-edit.el ends here