Initial Commit
[packages] / xemacs-packages / ede / autoconf-edit.el
1 ;;; autoconf-edit.el --- Keymap for autoconf
2
3 ;;  Copyright (C) 1998, 1999, 2000  Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project
7 ;; RCS: $Id: autoconf-edit.el,v 1.1 2007-11-26 15:22:05 michaels Exp $
8
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)
12 ;; any later version.
13
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.
18
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.
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 (if (locate-library "autoconf")
31
32     (condition-case nil
33         (require 'autoconf)
34       (error (require 'autoconf-mode "autoconf")))
35
36   (require 'autoconf-compat)
37
38   ;; This part is not in autoconf.el
39   (add-to-list 'auto-mode-alist '("\\<configure\\.in$" . autoconf-mode))
40   )
41
42 (defvar autoconf-new-automake-string
43   "dnl Process this file with autoconf to produce a configure script
44
45 AC_INIT(%s)
46 AM_INIT_AUTOMAKE(%s, 0)
47 AM_CONFIG_HEADER(config.h)
48
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.")
55
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: ")
63   (if (bufferp rootdir)
64       (set-buffer rootdir)
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))))
70           (error "Quit"))
71       (find-file cf)))
72   ;; Note, we only ask about overwrite if a string/path is specified.
73   (erase-buffer)
74   (insert (format autoconf-new-automake-string testfile program)))
75
76 (defvar autoconf-preferred-macro-order
77   '("AC_INIT"
78     "AM_INIT_AUTOMAKE"
79     "AM_CONFIG_HEADER"
80     ;; Arg parsing
81     "AC_ARG_ENABLE"
82     "AC_ARG_WITH"
83     ;; Programs
84     "AC_PROG_MAKE_SET"
85     "AC_PROG_AWK"
86     "AC_PROG_CC"
87     "AC_PROG_CC_C_O"
88     "AC_PROG_CPP"
89     "AC_PROG_CXX"
90     "AC_PROG_CXXCPP"
91     "AC_ISC_POSIX"
92     "AC_PROG_F77"
93     "AC_PROG_GCC_TRADITIONAL"
94     "AC_PROG_INSTALL"
95     "AC_PROG_LEX"
96     "AC_PROG_LN_S"
97     "AC_PROG_RANLIB"
98     "AC_PROG_YACC"
99     "AC_CHECK_PROG"
100     "AC_CHECK_PROGS"
101     "AM_PROG_LIBTOOL"
102     ;; Libraries
103     "AC_CHECK_LIB"
104     "AC_PATH_XTRA"
105     ;; Headers
106     "AC_HEADER_STDC"
107     "AC_HEADER_SYS_WAIT"
108     "AC_HEADER_TIME"
109     "AC_HEADERS"
110     ;; Typedefs, structures
111     "AC_TYPE_PID_T"
112     "AC_TYPE_SIGNAL"
113     "AC_TYPE_UID_T"
114     "AC_STRUCT_TM"
115     ;; Compiler characteristics
116     "AC_CHECK_SIZEOF"
117     "AC_C_CONST"
118     ;; Library functions
119     "AC_CHECK_FUNCS"
120     "AC_TRY_LINK"
121     ;; System Services
122     ;; Other
123     "AM_PATH_LISPDIR"
124     "AM_INIT_GUILE_MODULE"
125     ;; AC_OUTPUT is always last
126     "AC_OUTPUT"
127     )
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:
132      `AC_INIT(FILE)'
133      checks for programs
134      checks for libraries
135      checks for header files
136      checks for typedefs
137      checks for structures
138      checks for compiler characteristics
139      checks for library functions
140      checks for system services
141      `AC_OUTPUT([FILE...])'")
142
143 (defvar autoconf-multiple-macros
144   '("AC_ARG_ENABLE"
145     "AC_ARG_WITH"
146     "AC_CHECK_PROGS"
147     "AC_CHECK_LIB"
148     "AC_CHECK_SIZEOF"
149     "AC_TRY_LINK"
150     )
151   "Macros which appear multiple times.")
152
153 (defvar autoconf-multiple-multiple-macros
154   '("AC_HEADERS" "AC_CHECK_FUNCS")
155   "Macros which appear multiple times, and perform multiple queries.")
156
157 (defun autoconf-in-macro (macro)
158   "Non-nil if point is in a macro of type MACRO."
159   (save-excursion
160     (beginning-of-line)
161     (looking-at (concat "\\(A[CM]_" macro "\\|" macro "\\)"))))
162
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."
167   (let ((op (point)))
168     (goto-char (point-max))
169     (if (re-search-backward (concat "^" (regexp-quote macro) "\\s-*\\((\\|$\\)") nil t)
170         (progn
171           (beginning-of-line)
172           (point))
173       (goto-char op)
174       nil)))
175
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))
186     (setq ml (cdr ml))
187     (goto-char (point-max))
188     (while (and ml (not (autoconf-find-last-macro (car ml))))
189       (setq ml (cdr ml)))
190     (if (not ml) (error "Could not find context for positioning %s" macro))))
191
192 (defun autoconf-insert-macro-at-point (macro &optional param)
193   "Add MACRO at the current point with PARAM."
194   (insert macro)
195   (if param
196       (progn
197         (insert "(" param ")")
198         (if (< (current-column) 3) (insert " dnl")))))
199     
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))
209          (forward-sexp 2)
210          (end-of-line)
211          (insert "\n")
212          (autoconf-insert-macro-at-point macro param))
213         ((member macro autoconf-multiple-multiple-macros)
214          (if (not param)
215              (error "You must have a paramter for %s" macro))
216          (if (not (autoconf-find-last-macro macro))
217              (progn
218                ;; Doesn't exist yet....
219                (autoconf-position-for-macro macro)
220                (forward-sexp 2)
221                (end-of-line)
222                (insert "\n")
223                (autoconf-insert-macro-at-point macro param))
224            ;; Does exist, can we fit onto the current line?
225            (forward-sexp 2)
226            (down-list -1)
227            (if (> (+ (current-column) (length param))  fill-column)
228                (insert " " param)
229              (up-list 1)
230              (end-of-line)
231              (insert "\n")
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.
236          nil)
237         (t
238          (autoconf-position-for-macro macro)
239          (forward-sexp 1)
240          (if (looking-at "\\s-*(")
241              (forward-sexp 1))
242          (end-of-line)
243          (insert "\n")
244          (autoconf-insert-macro-at-point macro param))))
245
246 (defun autoconf-find-query-for-header (header)
247   "Position the cursor where HEADER is queried."
248   (interactive "sHeader: ")
249   (let ((op (point))
250         (found t))
251     (goto-char (point-min))
252     (condition-case nil
253         (while (not
254                 (progn
255                   (re-search-forward
256                    (concat "\\b" (regexp-quote header) "\\b"))
257                   (save-excursion
258                     (beginning-of-line)
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))
263     found))
264
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)))
270
271
272 (defun autoconf-find-query-for-func (func)
273   "Position the cursor where FUNC is queried."
274   (interactive "sFunction: ")
275   (let ((op (point))
276         (found t))
277     (goto-char (point-min))
278     (condition-case nil
279         (while (not
280                 (progn
281                   (re-search-forward
282                    (concat "\\b" (regexp-quote func) "\\b"))
283                   (save-excursion
284                     (beginning-of-line)
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))
289     found))
290
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)))
296
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")
310     )
311   "Association list of PROGRAM variables and their built-in MACRO.")
312
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."
318   (let ((op (point))
319         (found t)
320         (builtin (assoc prog autoconf-program-builtin)))
321     (goto-char (point-min))
322     (condition-case nil
323         (re-search-forward
324          (concat "^"
325                  (or (cdr-safe builtin)
326                      (concat "AC_CHECK_PROG\\s-*(\\s-*" prog "_PROG"))
327                  "\\>"))
328       (error (setq found nil)))
329     (if (not found) (goto-char op))
330     found))
331
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
335 of possible names."
336   (interactive "sProgram: ")
337   (if (autoconf-find-query-for-program prog)
338       nil
339     (let ((builtin (assoc prog autoconf-program-builtin)))
340       (if 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))
344         ))))
345
346 ;;; Scrappy little changes
347 ;;
348 (defvar autoconf-deleted-text nil
349   "Set to the last bit of text deleted during an edit.")
350
351 (defvar autoconf-inserted-text nil
352   "Set to the last bit of text inserted during an edit.")
353
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)))
360      ,@body
361      (if (and (not mod)
362               (string= autoconf-deleted-text autoconf-inserted-text))
363          (set-buffer-modified-p nil))))
364
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.
368 INDEX starts at 1."
369   (beginning-of-line)
370   (down-list 1)
371   (re-search-forward ", ?" nil nil (1- index))
372   (let ((end (save-excursion
373                (re-search-forward ",\\|)" (save-excursion
374                                             (end-of-line)
375                                             (point)))
376                (forward-char -1)
377                (point))))
378     (setq autoconf-deleted-text (buffer-substring (point) end))
379     (delete-region (point) end)))
380
381 (defun autoconf-insert (text)
382   "Insert TEXT."
383   (setq autoconf-inserted-text text)
384   (insert text))
385
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.
393     (autoconf-edit-cycle
394      (autoconf-delete-parameter 2)
395      (autoconf-insert version))))
396
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")
403     (autoconf-edit-cycle
404      (autoconf-delete-parameter 1)
405      (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
406
407 (provide 'autoconf-edit)
408
409 ;;; autoconf-edit.el ends here