summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/fupd.el
blob: bb8b3af0fa4779590067a41da91a15471bb72887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
;;; fupd.el --- Helper functions for updating files
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: Tue Feb 28 17:21:20 2006
;; Version: 0.1
;; Last-Updated: Tue Feb 20 21:09:20 2007 (3600 +0100)
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Helper functions for updating files.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(defun fupd-has-contents (file content)
  "Check if file FILE contains CONTENT.
Return a vector with these elements:
- elt 0: t if file contains CONTENT and buffer is not modified.
- elt 1: t if file contains CONTENT.
- elt 2: file buffer if file exists.
- elt 3: nil unless file already was in a buffer."
  (let (ok same buffer old-buffer)
    (when (file-exists-p file)
      (setq buffer (get-file-buffer file))
      (setq old-buffer (when buffer t))
      (unless buffer
        (setq buffer (find-file-noselect file)))
      (with-current-buffer buffer
        (setq same (string=
                    content
                    (buffer-substring-no-properties
                     (point-min) (point-max)))))
      (setq ok (and same
                    (not (buffer-modified-p buffer)))))
    (vector ok same buffer old-buffer)))

(defun fupd-ok (ret-val)
  "Return t if RET-VAL indicate file is uptodate.
RET-VAL should be the return value from `fupd-has-contents'."
  (elt ret-val 0))

(defun fupd-kill-new-buffer (ret-val)
  "Kill new buffer indicated by RET-VAL.
RET-VAL should be the return value from `fupd-has-contents'."
  (unless (elt ret-val 3)
    (let ((buffer (elt ret-val 2)))
      (when (bufferp buffer)
        ;;(message "fupd-kill-new-buffer: %s" (buffer-file-name buffer))(sit-for 4)
        (kill-buffer buffer)))))

;;(fupd-has-contents buffer-file-name (buffer-string))
;;(fupd-update-file buffer-file-name (buffer-string))
(defun fupd-update-file (file content)
  "Update file FILE with content CONTENT.
Do nothing if the file already has that content.  If the file was
not in a buffer before kill the file's buffer afterwards.

Return t if the file was updated, otherwise nil."
  (let* ((osbo (fupd-has-contents file content))
         (ok   (elt osbo 0))
         (same (elt osbo 1))
         (buff (elt osbo 2))
         (oldb (elt osbo 3))
         wrote
         )
    (unless ok
      (if buff
          (with-current-buffer buff
            (unless same
              (erase-buffer)
              (insert content))
            (save-buffer)
            (setq wrote t)
            (unless oldb
              (kill-buffer (current-buffer))))
        (with-temp-buffer
          (insert content)
          (write-file file))))
    wrote))

;; (defun fupd-copy-file (from-file to-file)
;;   (let (
;;         (from-buff (find-buffer-visiting from-file))
;;         (to-buff (find-buffer-visiting to-file))
;;         (from-attr (file-attributes from-file))
;;         (to-attr (file-attributes to-file))
;;         (from-size (nth 7 from-attr))
;;         (to-size (nth 7 to-attr))
;;         (from-mod (nth 5 from-attr))
;;         (to-mode (nth 5 to-attr))
;;         )
;;   ))

(provide 'fupd)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; fupd.el ends here