Spelling fixes.
[bpt/emacs.git] / lisp / cedet / ede / makefile-edit.el
1 ;;; makefile-edit.el --- Makefile editing/scanning commands.
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Utilities for editing a Makefile for EDE Makefile management commands.
25 ;;
26 ;; Derived from project-am.el.
27 ;;
28 ;; Makefile editing and scanning commands
29 ;;
30 ;; Formatting of a makefile
31 ;;
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.
36
37 ;;; Things to do
38 ;; makefile-fill-paragraph -- refill a macro w/ backslashes
39 ;; makefile-insert-macro -- insert "foo = "
40
41
42 ;;; Code:
43
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)))
58
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)))
67
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) ?\\)))
73
74 ;;; Programmatic editing of a Makefile
75 ;;
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)
82 t
83 (goto-char oldpt)
84 nil)))
85
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)))))
97
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 (while (makefile-move-to-macro macro t)
104 (let ((e (save-excursion
105 (makefile-end-of-command)
106 (point))))
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
110 (match-beginning 1)
111 (match-end 1))))
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))
115 (dolist (V varexp)
116 (setq lst (cons V lst))))))))
117 (nreverse lst))))
118
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."
122 (save-match-data
123 (when (string-match "\\$\\s(\\([A-Za-z0-9_]+\\)\\s)" text)
124 (match-string 1 text))))
125
126
127 (provide 'ede/makefile-edit)
128
129 ;;; ede/makefile-edit.el ends here