Initial Commit
[packages] / xemacs-packages / ede / ede-util.el
1 ;;; ede-util.el --- EDE utilities
2
3 ;;;  Copyright (C) 2000, 2005  Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7 ;; RCS: $Id: ede-util.el,v 1.1 2007-11-26 15:22:12 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 ;; Utilities that may not require project specific help, and oporate
27 ;; on generic EDE structures.  Provide user level commands for activities
28 ;; not directly related to source code organization or makefile generation.
29
30 (require 'ede)
31
32 ;;; Code:
33
34 ;;; Updating the version of a project.
35 ;;;###autoload
36 (defun ede-update-version (newversion)
37   "Update the current projects main version number.
38 Argument NEWVERSION is the version number to use in the current project."
39   (interactive (list (let* ((o (ede-toplevel))
40                             (v (oref o version)))
41                        (read-string (format "Update Version (was %s): " v)
42                                   v nil v))))
43   (let ((ede-object (ede-toplevel)))
44     ;; Don't update anything if there was no change.
45     (unless (string= (oref ede-object :version) newversion)
46       (oset ede-object :version newversion)
47       (project-update-version ede-object)
48       (ede-update-version-in-source ede-object newversion))))
49
50 (defmethod project-update-version ((ot ede-project))
51   "The :version of the project OT has been updated.
52 Handle saving, or other detail."
53   (error "project-update-version not supported by %s" (object-name ot)))
54
55 (defmethod ede-update-version-in-source ((this ede-project) version)
56   "Change occurrences of a version string in sources.
57 In project THIS, cycle over all targets to give them a chance to set
58 their sources to VERSION."
59   (ede-map-targets this (lambda (targ)
60                           (ede-update-version-in-source targ version))))
61
62 (defmethod ede-update-version-in-source ((this ede-target) version)
63   "In sources for THIS, change version numbers to VERSION."
64   (if (and (slot-boundp this 'versionsource)
65            (oref this versionsource))
66       (let ((vs (oref this versionsource)))
67         (while vs
68           (save-excursion
69             (set-buffer (find-file-noselect
70                          (ede-expand-filename this (car vs))))
71             (goto-char (point-min))
72             (let ((case-fold-search t))
73               (if (re-search-forward "version:\\s-*\\([^ \t\n]+\\)" nil t)
74                   (progn
75                     (save-match-data
76                       (ede-make-buffer-writable))
77                     (delete-region (match-beginning 1)
78                                    (match-end 1))
79                     (goto-char (match-beginning 1))
80                     (insert version)))))
81           (setq vs (cdr vs))))))
82
83 ;;; Writable files
84 ;;
85 ;; Utils for EDE when it needs to write a file that could be covered by a
86 ;; version control system.
87 (defun ede-make-buffer-writable (&optional buffer)
88   "Make sure that BUFFER is writable.
89 If BUFFER isn't specified, use the current buffer."
90   (save-excursion
91     (if buffer (set-buffer buffer))
92     (if buffer-read-only
93         (if (and vc-mode
94                  (y-or-n-p (format "Check out %s? " (buffer-file-name))))
95             (vc-toggle-read-only)
96           (if (not vc-mode)
97               (toggle-read-only -1))))))
98   
99 (provide 'ede-util)
100
101 ;;; ede-util.el ends here