]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/util/fupd.el
bb8b3af0fa4779590067a41da91a15471bb72887
[.emacs.d.git] / emacs / nxhtml / util / fupd.el
1 ;;; fupd.el --- Helper functions for updating files
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Tue Feb 28 17:21:20 2006
5 ;; Version: 0.1
6 ;; Last-Updated: Tue Feb 20 21:09:20 2007 (3600 +0100)
7 ;; Keywords:
8 ;; Compatibility:
9 ;;
10 ;; Features that might be required by this library:
11 ;;
12 ;;   None
13 ;;
14 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 ;;
16 ;;; Commentary:
17 ;;
18 ;; Helper functions for updating files.
19 ;;
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;;
22 ;;; Change log:
23 ;;
24 ;;
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;
27 ;; This program is free software; you can redistribute it and/or modify
28 ;; it under the terms of the GNU General Public License as published by
29 ;; the Free Software Foundation; either version 2, or (at your option)
30 ;; any later version.
31 ;;
32 ;; This program is distributed in the hope that it will be useful,
33 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
34 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35 ;; GNU General Public License for more details.
36 ;;
37 ;; You should have received a copy of the GNU General Public License
38 ;; along with this program; see the file COPYING.  If not, write to the
39 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
40 ;; Boston, MA 02111-1307, USA.
41 ;;
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;;
44 ;;; Code:
45
46 (defun fupd-has-contents (file content)
47   "Check if file FILE contains CONTENT.
48 Return a vector with these elements:
49 - elt 0: t if file contains CONTENT and buffer is not modified.
50 - elt 1: t if file contains CONTENT.
51 - elt 2: file buffer if file exists.
52 - elt 3: nil unless file already was in a buffer."
53   (let (ok same buffer old-buffer)
54     (when (file-exists-p file)
55       (setq buffer (get-file-buffer file))
56       (setq old-buffer (when buffer t))
57       (unless buffer
58         (setq buffer (find-file-noselect file)))
59       (with-current-buffer buffer
60         (setq same (string=
61                     content
62                     (buffer-substring-no-properties
63                      (point-min) (point-max)))))
64       (setq ok (and same
65                     (not (buffer-modified-p buffer)))))
66     (vector ok same buffer old-buffer)))
67
68 (defun fupd-ok (ret-val)
69   "Return t if RET-VAL indicate file is uptodate.
70 RET-VAL should be the return value from `fupd-has-contents'."
71   (elt ret-val 0))
72
73 (defun fupd-kill-new-buffer (ret-val)
74   "Kill new buffer indicated by RET-VAL.
75 RET-VAL should be the return value from `fupd-has-contents'."
76   (unless (elt ret-val 3)
77     (let ((buffer (elt ret-val 2)))
78       (when (bufferp buffer)
79         ;;(message "fupd-kill-new-buffer: %s" (buffer-file-name buffer))(sit-for 4)
80         (kill-buffer buffer)))))
81
82 ;;(fupd-has-contents buffer-file-name (buffer-string))
83 ;;(fupd-update-file buffer-file-name (buffer-string))
84 (defun fupd-update-file (file content)
85   "Update file FILE with content CONTENT.
86 Do nothing if the file already has that content.  If the file was
87 not in a buffer before kill the file's buffer afterwards.
88
89 Return t if the file was updated, otherwise nil."
90   (let* ((osbo (fupd-has-contents file content))
91          (ok   (elt osbo 0))
92          (same (elt osbo 1))
93          (buff (elt osbo 2))
94          (oldb (elt osbo 3))
95          wrote
96          )
97     (unless ok
98       (if buff
99           (with-current-buffer buff
100             (unless same
101               (erase-buffer)
102               (insert content))
103             (save-buffer)
104             (setq wrote t)
105             (unless oldb
106               (kill-buffer (current-buffer))))
107         (with-temp-buffer
108           (insert content)
109           (write-file file))))
110     wrote))
111
112 ;; (defun fupd-copy-file (from-file to-file)
113 ;;   (let (
114 ;;         (from-buff (find-buffer-visiting from-file))
115 ;;         (to-buff (find-buffer-visiting to-file))
116 ;;         (from-attr (file-attributes from-file))
117 ;;         (to-attr (file-attributes to-file))
118 ;;         (from-size (nth 7 from-attr))
119 ;;         (to-size (nth 7 to-attr))
120 ;;         (from-mod (nth 5 from-attr))
121 ;;         (to-mode (nth 5 to-attr))
122 ;;         )
123 ;;   ))
124
125 (provide 'fupd)
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
127 ;;; fupd.el ends here