autoconf-edit.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. ;;; ede/autoconf-edit.el --- Keymap for autoconf
  2. ;; Copyright (C) 1998-2000, 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: project
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Autoconf editing and modification support, and compatibility layer
  19. ;; for Emacses w/out autoconf mode built in.
  20. ;;; Code:
  21. (require 'autoconf)
  22. (declare-function ede-srecode-setup "ede/srecode")
  23. (declare-function ede-srecode-insert "ede/srecode")
  24. (defun autoconf-new-program (rootdir program testfile)
  25. "Initialize a new configure.in in ROOTDIR for PROGRAM using TESTFILE.
  26. ROOTDIR is the root directory of a given autoconf controlled project.
  27. PROGRAM is the program to be configured.
  28. TESTFILE is the file used with AC_INIT.
  29. configure the initial configure script using `autoconf-new-automake-string'"
  30. (interactive "DRoot Dir: \nsProgram: \nsTest File: ")
  31. (require 'ede/srecode)
  32. (if (bufferp rootdir)
  33. (set-buffer rootdir)
  34. (let ((cf1 (expand-file-name "configure.in" rootdir))
  35. (cf2 (expand-file-name "configure.ac" rootdir)))
  36. (if (and (or (file-exists-p cf1) (file-exists-p cf2))
  37. (not (y-or-n-p (format "File %s exists. Start Over? "
  38. (if (file-exists-p cf1)
  39. cf1 cf2)
  40. ))))
  41. (error "Quit"))
  42. (find-file cf2)))
  43. ;; Note, we only ask about overwrite if a string/path is specified.
  44. (erase-buffer)
  45. (ede-srecode-setup)
  46. (ede-srecode-insert
  47. "file:ede-empty"
  48. "TEST_FILE" testfile
  49. "PROGRAM" program)
  50. )
  51. (defvar autoconf-preferred-macro-order
  52. '("AC_INIT"
  53. "AM_INIT_AUTOMAKE"
  54. "AM_CONFIG_HEADER"
  55. ;; Arg parsing
  56. "AC_ARG_ENABLE"
  57. "AC_ARG_WITH"
  58. ;; Programs
  59. "AC_PROG_MAKE_SET"
  60. "AC_PROG_AWK"
  61. "AC_PROG_CC"
  62. "AC_PROG_CC_C_O"
  63. "AC_PROG_CPP"
  64. "AC_PROG_CXX"
  65. "AC_PROG_CXXCPP"
  66. "AC_ISC_POSIX"
  67. "AC_PROG_F77"
  68. "AC_PROG_GCC_TRADITIONAL"
  69. "AC_PROG_INSTALL"
  70. "AC_PROG_LEX"
  71. "AC_PROG_LN_S"
  72. "AC_PROG_RANLIB"
  73. "AC_PROG_YACC"
  74. "AC_CHECK_PROG"
  75. "AC_CHECK_PROGS"
  76. "AC_PROG_LIBTOOL"
  77. ;; Libraries
  78. "AC_CHECK_LIB"
  79. "AC_PATH_XTRA"
  80. ;; Headers
  81. "AC_HEADER_STDC"
  82. "AC_HEADER_SYS_WAIT"
  83. "AC_HEADER_TIME"
  84. "AC_HEADERS"
  85. ;; Typedefs, structures
  86. "AC_TYPE_PID_T"
  87. "AC_TYPE_SIGNAL"
  88. "AC_TYPE_UID_T"
  89. "AC_STRUCT_TM"
  90. ;; Compiler characteristics
  91. "AC_CHECK_SIZEOF"
  92. "AC_C_CONST"
  93. ;; Library functions
  94. "AC_CHECK_FUNCS"
  95. "AC_TRY_LINK"
  96. ;; System Services
  97. ;; Other
  98. "AM_PATH_LISPDIR"
  99. "AM_INIT_GUILE_MODULE"
  100. ;; AC_OUTPUT is always last
  101. "AC_OUTPUT"
  102. )
  103. "List of macros in the order that they prefer to occur in.
  104. This helps when inserting a macro which doesn't yet exist
  105. by positioning it near other macros which may exist.
  106. From the autoconf manual:
  107. `AC_INIT(FILE)'
  108. checks for programs
  109. checks for libraries
  110. checks for header files
  111. checks for typedefs
  112. checks for structures
  113. checks for compiler characteristics
  114. checks for library functions
  115. checks for system services
  116. `AC_OUTPUT([FILE...])'")
  117. (defvar autoconf-multiple-macros
  118. '("AC_ARG_ENABLE"
  119. "AC_ARG_WITH"
  120. "AC_CHECK_PROGS"
  121. "AC_CHECK_LIB"
  122. "AC_CHECK_SIZEOF"
  123. "AC_TRY_LINK"
  124. )
  125. "Macros which appear multiple times.")
  126. (defvar autoconf-multiple-multiple-macros
  127. '("AC_HEADERS" "AC_CHECK_FUNCS")
  128. "Macros which appear multiple times, and perform multiple queries.")
  129. (defun autoconf-in-macro (macro)
  130. "Non-nil if point is in a macro of type MACRO."
  131. (save-excursion
  132. (beginning-of-line)
  133. (looking-at (concat "\\(A[CM]_" macro "\\|" macro "\\)"))))
  134. (defun autoconf-find-last-macro (macro &optional ignore-bol)
  135. "Move to the last occurrence of MACRO in FILE, and return that point.
  136. The last macro is usually the one in which we would like to insert more
  137. items such as CHECK_HEADERS."
  138. (let ((op (point)) (atbol (if ignore-bol "" "^")))
  139. (goto-char (point-max))
  140. (if (re-search-backward (concat atbol (regexp-quote macro) "\\s-*\\((\\|$\\)") nil t)
  141. (progn
  142. (unless ignore-bol (beginning-of-line))
  143. (point))
  144. (goto-char op)
  145. nil)))
  146. (defun autoconf-parameter-strip (param)
  147. "Strip the parameter PARAM of whitespace and miscellaneous characters."
  148. ;; force greedy match for \n.
  149. (when (string-match "\\`\n*\\s-*\\[?\\s-*" param)
  150. (setq param (substring param (match-end 0))))
  151. (when (string-match "\\s-*\\]?\\s-*\\'" param)
  152. (setq param (substring param 0 (match-beginning 0))))
  153. param)
  154. (defun autoconf-parameters-for-macro (macro &optional ignore-bol ignore-case)
  155. "Retrieve the parameters to MACRO.
  156. Returns a list of the arguments passed into MACRO as strings."
  157. (let ((case-fold-search ignore-case))
  158. (save-excursion
  159. (when (autoconf-find-last-macro macro ignore-bol)
  160. (forward-sexp 1)
  161. (mapcar
  162. #'autoconf-parameter-strip
  163. (when (looking-at "(")
  164. (let* ((start (+ (point) 1))
  165. (end (save-excursion
  166. (forward-sexp 1)
  167. (- (point) 1)))
  168. (ans (buffer-substring-no-properties start end)))
  169. (split-string ans "," t))))))))
  170. (defun autoconf-position-for-macro (macro)
  171. "Position the cursor where a new MACRO could be inserted.
  172. This will appear at the BEGINNING of the macro MACRO should appear AFTER.
  173. This is to make it compatible with `autoconf-find-last-macro'.
  174. Assume that MACRO doesn't appear in the buffer yet, so search
  175. the ordering list `autoconf-preferred-macro-order'."
  176. ;; Search this list backwards.. heh heh heh
  177. ;; This lets us do a reverse search easily.
  178. (let ((ml (member macro (reverse autoconf-preferred-macro-order))))
  179. (if (not ml) (error "Don't know how to position for %s yet" macro))
  180. (setq ml (cdr ml))
  181. (goto-char (point-max))
  182. (while (and ml (not (autoconf-find-last-macro (car ml))))
  183. (setq ml (cdr ml)))
  184. (if (not ml) (error "Could not find context for positioning %s" macro))))
  185. (defun autoconf-insert-macro-at-point (macro &optional param)
  186. "Add MACRO at the current point with PARAM."
  187. (insert macro)
  188. (if param
  189. (progn
  190. (insert "(" param ")")
  191. (if (< (current-column) 3) (insert " dnl")))))
  192. (defun autoconf-insert-new-macro (macro &optional param)
  193. "Add a call to MACRO in the current autoconf file.
  194. Deals with macro order. See `autoconf-preferred-macro-order' and
  195. `autoconf-multi-macros'.
  196. Optional argument PARAM is the parameter to pass to the macro as one string."
  197. (cond ((member macro autoconf-multiple-macros)
  198. ;; This occurs multiple times
  199. (or (autoconf-find-last-macro macro)
  200. (autoconf-position-for-macro macro))
  201. (forward-sexp 2)
  202. (end-of-line)
  203. (insert "\n")
  204. (autoconf-insert-macro-at-point macro param))
  205. ((member macro autoconf-multiple-multiple-macros)
  206. (if (not param)
  207. (error "You must have a parameter for %s" macro))
  208. (if (not (autoconf-find-last-macro macro))
  209. (progn
  210. ;; Doesn't exist yet....
  211. (autoconf-position-for-macro macro)
  212. (forward-sexp 2)
  213. (end-of-line)
  214. (insert "\n")
  215. (autoconf-insert-macro-at-point macro param))
  216. ;; Does exist, can we fit onto the current line?
  217. (forward-sexp 2)
  218. (down-list -1)
  219. (if (> (+ (current-column) (length param)) fill-column)
  220. (insert " " param)
  221. (up-list 1)
  222. (end-of-line)
  223. (insert "\n")
  224. (autoconf-insert-macro-at-point macro param))))
  225. ((autoconf-find-last-macro macro)
  226. ;; If it isn't one of the multi's, it's a singleton.
  227. ;; If it exists, ignore it.
  228. nil)
  229. (t
  230. (autoconf-position-for-macro macro)
  231. (forward-sexp 1)
  232. (if (looking-at "\\s-*(")
  233. (forward-sexp 1))
  234. (end-of-line)
  235. (insert "\n")
  236. (autoconf-insert-macro-at-point macro param))))
  237. (defun autoconf-find-query-for-header (header)
  238. "Position the cursor where HEADER is queried."
  239. (interactive "sHeader: ")
  240. (let ((op (point))
  241. (found t))
  242. (goto-char (point-min))
  243. (condition-case nil
  244. (while (not
  245. (progn
  246. (re-search-forward
  247. (concat "\\b" (regexp-quote header) "\\b"))
  248. (save-excursion
  249. (beginning-of-line)
  250. (looking-at "AC_CHECK_HEADERS")))))
  251. ;; We depend on the search failing to exit our loop on failure.
  252. (error (setq found nil)))
  253. (if (not found) (goto-char op))
  254. found))
  255. (defun autoconf-add-query-for-header (header)
  256. "Add in HEADER to be queried for in our autoconf file."
  257. (interactive "sHeader: ")
  258. (or (autoconf-find-query-for-header header)
  259. (autoconf-insert-new-macro "AC_CHECK_HEADERS" header)))
  260. (defun autoconf-find-query-for-func (func)
  261. "Position the cursor where FUNC is queried."
  262. (interactive "sFunction: ")
  263. (let ((op (point))
  264. (found t))
  265. (goto-char (point-min))
  266. (condition-case nil
  267. (while (not
  268. (progn
  269. (re-search-forward
  270. (concat "\\b" (regexp-quote func) "\\b"))
  271. (save-excursion
  272. (beginning-of-line)
  273. (looking-at "AC_CHECK_FUNCS")))))
  274. ;; We depend on the search failing to exit our loop on failure.
  275. (error (setq found nil)))
  276. (if (not found) (goto-char op))
  277. found))
  278. (defun autoconf-add-query-for-func (func)
  279. "Add in FUNC to be queried for in our autoconf file."
  280. (interactive "sFunction: ")
  281. (or (autoconf-find-query-for-func func)
  282. (autoconf-insert-new-macro "AC_CHECK_FUNCS" func)))
  283. (defvar autoconf-program-builtin
  284. '(("AWK" . "AC_PROG_AWK")
  285. ("CC" . "AC_PROG_CC")
  286. ("CPP" . "AC_PROG_CPP")
  287. ("CXX" . "AC_PROG_CXX")
  288. ("CXXCPP" . "AC_PROG_CXXCPP")
  289. ("F77" . "AC_PROG_F77")
  290. ("GCC_TRADITIONAL" . "AC_PROG_GCC_TRADITIONAL")
  291. ("INSTALL" . "AC_PROG_INSTALL")
  292. ("LEX" . "AC_PROG_LEX")
  293. ("LN_S" . "AC_PROG_LN_S")
  294. ("RANLIB" . "AC_PROG_RANLIB")
  295. ("YACC" . "AC_PROG_YACC")
  296. )
  297. "Association list of PROGRAM variables and their built-in MACRO.")
  298. (defun autoconf-find-query-for-program (prog)
  299. "Position the cursor where PROG is queried.
  300. PROG is the VARIABLE to use in autoconf to identify the program.
  301. PROG excludes the _PROG suffix. Thus if PROG were EMACS, then the
  302. variable in configure.in would be EMACS_PROG."
  303. (let ((op (point))
  304. (found t)
  305. (builtin (assoc prog autoconf-program-builtin)))
  306. (goto-char (point-min))
  307. (condition-case nil
  308. (re-search-forward
  309. (concat "^"
  310. (or (cdr-safe builtin)
  311. (concat "AC_CHECK_PROG\\s-*(\\s-*" prog "_PROG"))
  312. "\\>"))
  313. (error (setq found nil)))
  314. (if (not found) (goto-char op))
  315. found))
  316. (defun autoconf-add-query-for-program (prog &optional names)
  317. "Add in PROG to be queried for in our autoconf file.
  318. Optional NAMES is for non-built-in programs, and is the list
  319. of possible names."
  320. (interactive "sProgram: ")
  321. (if (autoconf-find-query-for-program prog)
  322. nil
  323. (let ((builtin (assoc prog autoconf-program-builtin)))
  324. (if builtin
  325. (autoconf-insert-new-macro (cdr builtin))
  326. ;; Not built in, try the params item
  327. (autoconf-insert-new-macro "AC_CHECK_PROGS" (concat prog "," names))
  328. ))))
  329. ;;; Scrappy little changes
  330. ;;
  331. (defvar autoconf-deleted-text nil
  332. "Set to the last bit of text deleted during an edit.")
  333. (defvar autoconf-inserted-text nil
  334. "Set to the last bit of text inserted during an edit.")
  335. (defmacro autoconf-edit-cycle (&rest body)
  336. "Start an edit cycle, unsetting the modified flag if there is no change.
  337. Optional argument BODY is the code to execute which edits the autoconf file."
  338. `(let ((autoconf-deleted-text nil)
  339. (autoconf-inserted-text nil)
  340. (mod (buffer-modified-p)))
  341. ,@body
  342. (if (and (not mod)
  343. (string= autoconf-deleted-text autoconf-inserted-text))
  344. (set-buffer-modified-p nil))))
  345. (defun autoconf-delete-parameter (index)
  346. "Delete the INDEXth parameter from the macro starting on the current line.
  347. Leaves the cursor where a new parameter can be inserted.
  348. INDEX starts at 1."
  349. (beginning-of-line)
  350. (down-list 1)
  351. (re-search-forward ", ?" nil nil (1- index))
  352. (let ((end (save-excursion
  353. (re-search-forward ",\\|)" (point-at-eol))
  354. (forward-char -1)
  355. (point))))
  356. (setq autoconf-deleted-text (buffer-substring (point) end))
  357. (delete-region (point) end)))
  358. (defun autoconf-insert (text)
  359. "Insert TEXT."
  360. (setq autoconf-inserted-text text)
  361. (insert text))
  362. (defun autoconf-set-version (version)
  363. "Set the version used with automake to VERSION."
  364. (if (not (stringp version))
  365. (signal 'wrong-type-argument '(stringp version)))
  366. (if (not (autoconf-find-last-macro "AM_INIT_AUTOMAKE"))
  367. (error "Cannot update version")
  368. ;; Move to correct position.
  369. (autoconf-edit-cycle
  370. (autoconf-delete-parameter 2)
  371. (autoconf-insert version))))
  372. (defun autoconf-set-output (outputlist)
  373. "Set the files created in AC_OUTPUT to OUTPUTLIST.
  374. OUTPUTLIST is a list of strings representing relative paths
  375. to Makefiles, or other files using Autoconf substitution."
  376. (if (not (autoconf-find-last-macro "AC_OUTPUT"))
  377. (error "Cannot update version")
  378. (autoconf-edit-cycle
  379. (autoconf-delete-parameter 1)
  380. (autoconf-insert (mapconcat (lambda (a) a) outputlist " ")))))
  381. (provide 'ede/autoconf-edit)
  382. ;;; ede/autoconf-edit.el ends here