Merge branch 'master' into comment-cache
[emacs.git] / lisp / cedet / ede / autoconf-edit.el
blobc9783cae8b6d68f630946cbe59a6a074c8b0a6ff
1 ;;; ede/autoconf-edit.el --- Keymap for autoconf
3 ;; Copyright (C) 1998-2000, 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project
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/>.
23 ;;; Commentary:
25 ;; Autoconf editing and modification support, and compatibility layer
26 ;; for Emacses w/out autoconf mode built in.
28 ;;; Code:
29 (require 'autoconf)
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.ac 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)
41 (if (bufferp rootdir)
42 (set-buffer rootdir)
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)
48 cf1 cf2)
49 ))))
50 (error "Quit"))
51 (find-file cf2)))
52 ;; Note, we only ask about overwrite if a string/path is specified.
53 (erase-buffer)
54 (ede-srecode-setup)
55 (ede-srecode-insert
56 "file:ede-empty"
57 "TEST_FILE" testfile
58 "PROGRAM" program)
61 (defvar autoconf-preferred-macro-order
62 '("AC_INIT"
63 "AC_CONFIG_SRCDIR"
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"
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...])'")
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"
137 "Macros which appear multiple times.")
139 (defvar autoconf-multiple-multiple-macros
140 '("AC_HEADERS" "AC_CHECK_FUNCS")
141 "Macros which appear multiple times, and perform multiple queries.")
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 "\\)"))))
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)))
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 ;; Look for occurrences of backslash newline
170 (while (string-match "\\s-*\\\\\\s-*\n\\s-*" param)
171 (setq param (replace-match " " t t param)))
172 param)
174 (defun autoconf-parameters-for-macro (macro &optional ignore-bol ignore-case)
175 "Retrieve the parameters to MACRO.
176 Returns a list of the arguments passed into MACRO as strings."
177 (let ((case-fold-search ignore-case))
178 (save-excursion
179 (when (autoconf-find-last-macro macro ignore-bol)
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))))))))
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 easily.
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))))
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")))))
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)
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))))
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))
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: ")
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))
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.ac 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))
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)
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 ))))
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)))
375 ,@body
376 (if (and (not mod)
377 (string= autoconf-deleted-text autoconf-inserted-text))
378 (set-buffer-modified-p nil))))
380 (defun autoconf-parameter-count ()
381 "Return the number of parameters to the function on the current line."
382 (save-excursion
383 (beginning-of-line)
384 (let* ((end-of-cmd
385 (save-excursion
386 (if (re-search-forward "(" (point-at-eol) t)
387 (progn
388 (forward-char -1)
389 (forward-sexp 1)
390 (point))
391 ;; Else, just return EOL.
392 (point-at-eol))))
393 (cnt 0))
394 (save-restriction
395 (narrow-to-region (point-at-bol) end-of-cmd)
396 (condition-case nil
397 (progn
398 (down-list 1)
399 (while (re-search-forward ", ?" end-of-cmd t)
400 (setq cnt (1+ cnt)))
401 (cond ((> cnt 1)
402 ;; If the # is > 1, then there is one fewer , than args.
403 (1+ cnt))
404 ((not (looking-at "\\s-*)"))
405 ;; If there are 0 args, then we have to see if there is one arg.
406 (1+ cnt))
408 ;; Else, just return the 0.
409 cnt)))
410 (error 0))))))
412 (defun autoconf-delete-parameter (index)
413 "Delete the INDEXth parameter from the macro starting on the current line.
414 Leaves the cursor where a new parameter can be inserted.
415 INDEX starts at 1."
416 (beginning-of-line)
417 (down-list 1)
418 (re-search-forward ", ?" nil nil (1- index))
419 (let ((end (save-excursion
420 (re-search-forward ",\\|)" (point-at-eol))
421 (forward-char -1)
422 (point))))
423 (setq autoconf-deleted-text (buffer-substring (point) end))
424 (delete-region (point) end)))
426 (defun autoconf-insert (text)
427 "Insert TEXT."
428 (setq autoconf-inserted-text text)
429 (insert text))
431 (defun autoconf-set-version (version)
432 "Set the version used with automake to VERSION."
433 (if (not (stringp version))
434 (signal 'wrong-type-argument '(stringp version)))
435 (if (and (autoconf-find-last-macro "AM_INIT_AUTOMAKE")
436 (>= (autoconf-parameter-count) 2))
437 ;; We can edit right here.
439 ;; Else, look for AC init instead.
440 (if (not (and (autoconf-find-last-macro "AC_INIT")
441 (>= (autoconf-parameter-count) 2)))
442 (error "Cannot update version")))
444 ;; Perform the edit.
445 (autoconf-edit-cycle
446 (autoconf-delete-parameter 2)
447 (autoconf-insert (concat "[" version "]"))))
449 (defun autoconf-set-output (outputlist)
450 "Set the files created in AC_OUTPUT to OUTPUTLIST.
451 OUTPUTLIST is a list of strings representing relative paths
452 to Makefiles, or other files using Autoconf substitution."
453 (if (not (autoconf-find-last-macro "AC_OUTPUT"))
454 (error "Cannot update version")
455 (autoconf-edit-cycle
456 (autoconf-delete-parameter 1)
457 (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
459 (provide 'ede/autoconf-edit)
461 ;;; ede/autoconf-edit.el ends here