Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / ede / makefile-edit.el
blob4c4398eb06215aa81f09ea5ef5e306ffb58947ac
1 ;;; makefile-edit.el --- Makefile editing/scanning commands.
3 ;; Copyright (C) 2009-2014 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/>.
22 ;;; Commentary:
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.
37 ;;; Things to do
38 ;; makefile-fill-paragraph -- refill a macro w/ backslashes
39 ;; makefile-insert-macro -- insert "foo = "
42 ;;; Code:
44 (defun makefile-beginning-of-command ()
45 "Move to the beginning of the current command."
46 (interactive)
47 (if (save-excursion
48 (forward-line -1)
49 (makefile-line-continued-p))
50 (forward-line -1))
51 (beginning-of-line)
52 (if (not (makefile-line-continued-p))
53 nil
54 (while (and (makefile-line-continued-p)
55 (not (bobp)))
56 (forward-line -1))
57 (forward-line 1)))
59 (defun makefile-end-of-command ()
60 "Move to the end of the current command."
61 (interactive)
62 (end-of-line)
63 (while (and (makefile-line-continued-p)
64 (not (eobp)))
65 (forward-line 1)
66 (end-of-line)))
68 (defun makefile-line-continued-p ()
69 "Return non-nil if the current line ends in continuation."
70 (save-excursion
71 (end-of-line)
72 (= (preceding-char) ?\\)))
74 ;;; Programmatic 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)
83 (goto-char oldpt)
84 nil)))
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."
89 (save-excursion
90 (makefile-beginning-of-command)
91 (let ((e (save-excursion
92 (makefile-end-of-command)
93 (point))))
94 (if (re-search-forward stop-before nil t)
95 (goto-char (match-beginning 0))
96 (goto-char e)))))
98 (defun makefile-macro-file-list (macro)
99 "Return a list of all files in MACRO."
100 (save-excursion
101 (goto-char (point-min))
102 (let ((lst nil)
103 (case-fold-search nil))
104 (while (makefile-move-to-macro macro t)
105 (let ((e (save-excursion
106 (makefile-end-of-command)
107 (point))))
108 (while (re-search-forward "\\s-**\\([-a-zA-Z0-9./_@$%(){}]+\\)\\s-*" e t)
109 (let ((var nil)(varexp nil)
110 (match (buffer-substring-no-properties
111 (match-beginning 1)
112 (match-end 1))))
113 (if (not (setq var (makefile-extract-varname-from-text match)))
114 (setq lst (cons match lst))
115 (setq varexp (makefile-macro-file-list var))
116 (dolist (V varexp)
117 (setq lst (cons V lst))))))))
118 (nreverse lst))))
120 (defun makefile-extract-varname-from-text (text)
121 "Extract the variable name from TEXT if it is a variable reference.
122 Return nil if it isn't a variable."
123 (save-match-data
124 (when (string-match "\\$\\s(\\([A-Za-z0-9_]+\\)\\s)" text)
125 (match-string 1 text))))
128 (provide 'ede/makefile-edit)
130 ;;; ede/makefile-edit.el ends here