1 ;;; makefile-edit.el --- Makefile editing/scanning commands.
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
24 ;; Utilities for editing a Makefile for EDE Makefile management commands.
26 ;; Derived from project-am.el.
28 ;; Makefile editing and scanning commands
30 ;; Formatting of a makefile
32 ;; 1) Creating an automakefile, stick in a top level comment about
33 ;; being created by emacs
34 ;; 2) Leave order of variable contents alone, except for SOURCE
35 ;; SOURCE always keep in the order of .c, .h, the other stuff.
38 ;; makefile-fill-paragraph -- refill a macro w/ backslashes
39 ;; makefile-insert-macro -- insert "foo = "
44 (defun makefile-beginning-of-command ()
45 "Move to the beginning of the current command."
49 (makefile-line-continued-p))
52 (if (not (makefile-line-continued-p))
54 (while (and (makefile-line-continued-p)
59 (defun makefile-end-of-command ()
60 "Move to the end of the current command."
63 (while (and (makefile-line-continued-p)
68 (defun makefile-line-continued-p ()
69 "Return non-nil if the current line ends in continuation."
72 (= (preceding-char) ?\\)))
74 ;;; Programatic editing of a Makefile
76 (defun makefile-move-to-macro (macro &optional next)
77 "Move to the definition of MACRO. Return t if found.
78 If NEXT is non-nil, move to the next occurrence of MACRO."
79 (let ((oldpt (point)))
80 (when (not next) (goto-char (point-min)))
81 (if (re-search-forward (concat "^\\s-*" macro "\\s-*[+:?]?=") nil t)
86 (defun makefile-navigate-macro (stop-before)
87 "In a list of files, move forward until STOP-BEFORE is reached.
88 STOP-BEFORE is a regular expression matching a file name."
90 (makefile-beginning-of-command)
91 (let ((e (save-excursion
92 (makefile-end-of-command)
94 (if (re-search-forward stop-before nil t)
95 (goto-char (match-beginning 0))
98 (defun makefile-macro-file-list (macro)
99 "Return a list of all files in MACRO."
101 (goto-char (point-min))
103 (while (makefile-move-to-macro macro t)
104 (let ((e (save-excursion
105 (makefile-end-of-command)
107 (while (re-search-forward "\\s-**\\([-a-zA-Z0-9./_@$%(){}]+\\)\\s-*" e t)
108 (let ((var nil)(varexp nil)
109 (match (buffer-substring-no-properties
112 (if (not (setq var (makefile-extract-varname-from-text match)))
113 (setq lst (cons match lst))
114 (setq varexp (makefile-macro-file-list var))
116 (setq lst (cons V lst))))))))
119 (defun makefile-extract-varname-from-text (text)
120 "Extract the variable name from TEXT if it is a variable reference.
121 Return nil if it isn't a variable."
123 (when (string-match "\\$\\s(\\([A-Za-z0-9_]+\\)\\s)" text)
124 (match-string 1 text))))
127 (provide 'ede/makefile-edit)
129 ;;; ede/makefile-edit.el ends here