annotate lisp/packages/file-part.el @ 2:ac2d302a0011 r19-15b2

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