annotate lisp/packages/file-part.el @ 16:0293115a14e9 r19-15b91

Import from CVS: tag r19-15b91
author cvs
date Mon, 13 Aug 2007 08:49:20 +0200
parents 376386a54a3c
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; file-part.el --- treat a section of a buffer as a separate file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Keywords: extensions, tools
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Copyright (C) 1992-1993 Sun Microsystems.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
20 ;; along with XEmacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
22 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;; Written by Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 (provide 'file-part)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (define-error 'file-part-error "File part error" 'file-error)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 (defvar file-part-extent-alist nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 "Alist of file parts in the current buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 Each element of the alist maps an extent describing the file part
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 to the buffer containing the file part. DON'T MODIFY THIS.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (make-variable-buffer-local 'file-part-extent-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 (setq-default file-part-extent-alist nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 (defvar file-part-master-extent nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 "Extent this file part refers to in the master buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 NIL if this buffer is not a file part. The master buffer itself
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 can be found by calling `extent-buffer' on this extent.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 DON'T MODIFY THIS.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (make-variable-buffer-local 'file-part-master-extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (setq-default file-part-master-extent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (or (assq 'file-part-master-extent minor-mode-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (setq minor-mode-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (cons minor-mode-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 '((file-part-master-extent " File-part")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ; apply a function to each element of a list and return true if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ; any of the functions returns true.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (defun file-part-maptrue (fn list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (cond ((null list) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ((funcall fn (car list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (t (file-part-maptrue fn (cdr list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ; return a buffer to operate on. If NIL is specified, this is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ; current buffer. If a string is specified, this is the buffer with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ; that name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (defun file-part-buffer-from-arg (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (get-buffer (or arg (current-buffer))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (defun make-file-part (&optional start end name buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 "Make a file part on buffer BUFFER out of the region. Call it NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 This command creates a new buffer containing the contents of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 region and marks the buffer as referring to the specified buffer,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 called the `master buffer'. When the file-part buffer is saved,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 its changes are integrated back into the master buffer. When the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 master buffer is deleted, all file parts are deleted with it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 When called from a function, expects four arguments, START, END,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 NAME, and BUFFER, all of which are optional and default to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 beginning of BUFFER, the end of BUFFER, a name generated from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 BUFFER's name, and the current buffer, respectively."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (interactive "r\nsName of file part: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (setq buffer (file-part-buffer-from-arg buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (if (null start) (setq start (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (if (null end) (setq end (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (if (null name) (setq name (concat (buffer-name buffer) "-part")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (if (> start end) nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (set-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (make-local-variable 'write-contents-hooks)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (make-local-variable 'kill-buffer-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (make-local-variable 'revert-buffer-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (add-hook 'write-contents-hooks 'write-master-buffer-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (add-hook 'kill-buffer-hook 'kill-master-buffer-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (setq revert-buffer-function 'revert-master-buffer-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (if (file-part-maptrue (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (let ((b (extent-start-position (car x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (e (extent-end-position (car x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (numberp b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (numberp e)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (not (or (and (<= b start) (<= e start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (and (>= b end) (>= e end))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 file-part-extent-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (signal 'file-part-error (list "Overlapping file parts not allowed"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (let ((x (make-extent start end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (filebuf (generate-new-buffer name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (set-extent-property x 'read-only t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setq file-part-extent-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (cons (cons x filebuf) file-part-extent-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (switch-to-buffer filebuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (setq buffer-file-name (concat "File part on " (buffer-name buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (make-local-variable 'write-file-hooks)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (make-local-variable 'kill-buffer-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (make-local-variable 'revert-buffer-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (make-local-variable 'first-change-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (add-hook 'write-file-hooks 'write-file-part-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (add-hook 'kill-buffer-hook 'kill-file-part-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (setq revert-buffer-function 'revert-file-part-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (setq file-part-master-extent x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (insert-buffer-substring buffer start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ; do this after inserting the text so the master buffer isn't marked as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ; modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (add-hook 'first-change-hook 'file-part-first-change-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 filebuf))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (defun kill-file-part-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 "Hook to be called when a file-part buffer is killed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 Removes the file part from the master buffer's list of file parts."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (let ((x file-part-master-extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (buf (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (if x (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (set-buffer (extent-buffer x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (setq file-part-extent-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (delete (cons x buf) file-part-extent-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (delete-extent x)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (defun kill-all-file-parts (&optional bufname no-ask)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 "Kill all file parts on buffer BUFNAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 The argument may be a buffer or the name of a buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 If one or more of the file parts needs saving, prompts for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 confirmation unless optional second argument NO-ASK is non-nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 BUFFER defaults to the current buffer if not specified."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (interactive "b")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (setq bufname (file-part-buffer-from-arg bufname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (set-buffer bufname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (and (or no-ask
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (not (file-parts-modified-p bufname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (y-or-n-p "Buffer has modified file parts; kill anyway? "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (mapcar (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (set-buffer (cdr x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (kill-buffer (cdr x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 file-part-extent-alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (defun kill-master-buffer-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 "Hook to be called when a master buffer is killed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 Kills the associated file parts."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (kill-all-file-parts (current-buffer) t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (defun file-part-check-attached (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (cond ((null x) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ((extent-property x 'detached)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (kill-file-part-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (setq buffer-file-name nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setq file-part-master-extent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (message "File part has become detached.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (defun write-file-part-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 "Hook to be called when a file part is saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 Saves the file part into the master buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (let ((x file-part-master-extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (buf (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (len (- (point-max) (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (retval (not (null file-part-master-extent))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (and (file-part-check-attached x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (let ((b (extent-start-position x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (e (extent-end-position x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (set-buffer (extent-buffer x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (set-extent-property x 'read-only nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (goto-char b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (insert-buffer-substring buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (delete-region (+ len b) (+ len e))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (set-extent-property x 'read-only t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (set-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (message (format "Wrote file part %s on %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (buffer-name buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (buffer-name (extent-buffer x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 retval))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (defun write-master-buffer-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 "Hook to be called when a master buffer is saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 If there are modified file parts on the buffer, optionally
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 saves the file parts back into the buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (save-some-file-part-buffers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (defun save-some-file-part-buffers (&optional arg buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 "Save some modified file-part buffers on BUFFER. Asks user about each one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 Optional argument (the prefix) non-nil means save all with no questions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 BUFFER defaults to the current buffer if not specified."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (setq buffer (file-part-buffer-from-arg buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (let ((alist file-part-extent-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (name (buffer-name buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (while alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (let ((buf (cdr (car alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (and (buffer-modified-p buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (or arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (y-or-n-p (format "Save file part %s on %s? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (buffer-name buf) (buffer-name buffer))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (set-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (save-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (error nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (setq alist (cdr alist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (defun file-parts-modified-p (&optional buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 "Return true if BUFFER has any modified file parts on it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 BUFFER defaults to the current buffer if not specified."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (and buffer (set-buffer buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (file-part-maptrue (function (lambda (x) (buffer-modified-p (cdr x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 file-part-extent-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (defun revert-file-part-function (&optional check-auto noconfirm)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 "Hook to be called when a file part is reverted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 Reverts the file part from the master buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (let ((x file-part-master-extent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (and (file-part-check-attached x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (let ((master (extent-buffer x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (or noconfirm
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (yes-or-no-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 "Revert file part from master buffer %s? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (buffer-name master))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (let ((mod (buffer-modified-p master)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (insert-buffer-substring master
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (extent-start-position x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (extent-end-position x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (set-buffer master)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (set-buffer-modified-p mod)))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (defun revert-master-buffer-function (&optional check-auto noconfirm)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 "Hook to be called when a master-buffer is reverted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 Makes sure the user is aware that the file parts will become detached,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 then proceeds as normal."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (or noconfirm
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (null file-part-extent-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (message "Warning: file parts will become detached.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (sleep-for 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (let ((revert-buffer-function nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (revert-buffer (not check-auto) noconfirm)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (defun file-part-first-change-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 "Hook to be called when a file part is first modified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 Marks the master buffer as modified."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (let ((x file-part-master-extent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (and (file-part-check-attached x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (set-buffer (extent-buffer x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (set-buffer-modified-p t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273