1 ;;; fupd.el --- Helper functions for updating files
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Tue Feb 28 17:21:20 2006
6 ;; Last-Updated: Tue Feb 20 21:09:20 2007 (3600 +0100)
10 ;; Features that might be required by this library:
14 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 ;; Helper functions for updating files.
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)
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.
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.
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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))
58 (setq buffer (find-file-noselect file)))
59 (with-current-buffer buffer
62 (buffer-substring-no-properties
63 (point-min) (point-max)))))
65 (not (buffer-modified-p buffer)))))
66 (vector ok same buffer old-buffer)))
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'."
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)))))
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.
89 Return t if the file was updated, otherwise nil."
90 (let* ((osbo (fupd-has-contents file content))
99 (with-current-buffer buff
106 (kill-buffer (current-buffer))))
112 ;; (defun fupd-copy-file (from-file to-file)
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))
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
127 ;;; fupd.el ends here