-
Notifications
You must be signed in to change notification settings - Fork 25
/
vlf-write.el
202 lines (186 loc) · 9.13 KB
/
vlf-write.el
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;; vlf-write.el --- Saving functionality for VLF -*- lexical-binding: t -*-
;; Copyright (C) 2014 Free Software Foundation, Inc.
;; Keywords: large files, saving
;; Author: Andrey Kotlarski <[email protected]>
;; URL: https://github.com/m00natic/vlfi
;; This file 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 3, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This package provides the `vlf-write' command which takes care of
;; saving changes where only part of file is viewed and updated.
;;; Code:
(require 'vlf-base)
(defcustom vlf-save-in-place 'ask
"Should VLF save in place when additional adjustment of file content\
is needed."
:group 'vlf :type '(choice (const :tag "Always when applicable" t)
(const :tag "Ask when applicable" 'ask)
(const :tag "Never" nil)))
(defun vlf-write ()
"Write current chunk to file. Always return true to disable save.
If changing size of chunk, shift remaining file content."
(interactive)
(when (and (buffer-modified-p)
(or (verify-visited-file-modtime (current-buffer))
(y-or-n-p "File has changed since visited or saved.\
Save anyway? ")))
(widen)
(run-hook-with-args 'vlf-before-batch-functions 'write)
(let ((hexl (derived-mode-p 'hexl-mode)))
(when hexl
(if (consp buffer-undo-list)
(setq buffer-undo-list nil))
(vlf-tune-dehexlify))
(if (zerop vlf-file-size) ;new file
(progn (vlf-tune-write nil nil vlf-start-pos t
(vlf-tune-encode-length (point-min)
(point-max)))
(if hexl (vlf-tune-hexlify))
(setq vlf-file-size (vlf-get-file-size
buffer-file-truename)
vlf-end-pos vlf-file-size))
(let* ((region-length (vlf-tune-encode-length (point-min)
(point-max)))
(size-change (- vlf-end-pos vlf-start-pos
region-length)))
(if (zerop size-change)
(progn (vlf-tune-write nil nil vlf-start-pos t
(- vlf-end-pos vlf-start-pos))
(if hexl (vlf-tune-hexlify)))
(let ((pos (point))
(font-lock font-lock-mode)
(batch-size vlf-batch-size)
time)
(font-lock-mode 0)
(if (or (file-remote-p buffer-file-name)
(if (eq vlf-save-in-place 'ask)
(y-or-n-p "File content needs be adjusted\
till end. Use temporary copy of the whole file (slower but safer)? ")
(not vlf-save-in-place)))
(let ((file-tmp (make-temp-file "vlf")))
(setq time (float-time))
(copy-file buffer-file-name file-tmp t t t t)
(if (< 0 size-change)
(vlf-file-shift-back size-change region-length
file-tmp)
(vlf-file-shift-forward (- size-change)
region-length file-tmp))
(rename-file file-tmp buffer-file-name t))
(setq time (float-time))
(if (< 0 size-change)
(vlf-file-shift-back size-change region-length)
(vlf-file-shift-forward (- size-change)
region-length)))
(if font-lock (font-lock-mode 1))
(setq vlf-batch-size batch-size)
(vlf-move-to-chunk-2 vlf-start-pos
(if (< (- vlf-end-pos vlf-start-pos)
vlf-batch-size)
(+ vlf-start-pos vlf-batch-size)
vlf-end-pos))
(goto-char pos)
(message "Save took %f seconds" (- (float-time) time)))))))
(run-hook-with-args 'vlf-after-batch-functions 'write))
t)
(defun vlf-file-shift-back (size-change write-size &optional file)
"Shift file contents SIZE-CHANGE bytes back.
WRITE-SIZE is byte length of saved chunk.
FILE if given is filename to be used, otherwise `buffer-file-name'."
(vlf-tune-write nil nil vlf-start-pos (if file nil t) write-size file)
(let ((read-start-pos vlf-end-pos)
(coding-system-for-write 'no-conversion)
(reporter (make-progress-reporter "Adjusting file content..."
vlf-end-pos
vlf-file-size)))
(vlf-with-undo-disabled
(while (vlf-shift-batch read-start-pos (- read-start-pos
size-change)
file)
(setq read-start-pos (+ read-start-pos vlf-batch-size))
(progress-reporter-update reporter read-start-pos))
;; pad end with space
(erase-buffer)
(vlf-verify-size t file)
(insert-char 32 size-change))
(vlf-tune-write nil nil (- vlf-file-size size-change)
(if file nil t) size-change file)
(progress-reporter-done reporter)))
(defun vlf-shift-batch (read-pos write-pos file)
"Read `vlf-batch-size' bytes from READ-POS and write them \
back at WRITE-POS using FILE.
Return nil if EOF is reached, t otherwise."
(erase-buffer)
(vlf-verify-size t file)
(vlf-tune-batch '(:raw :write) nil file) ;insert speed over temp write file may defer wildly
(let ((read-end (min (+ read-pos vlf-batch-size) vlf-file-size))) ;compared to the original file
(vlf-tune-insert-file-contents-literally read-pos read-end file)
(vlf-tune-write nil nil write-pos 0 (- read-end read-pos) file)
(< read-end vlf-file-size)))
(defun vlf-file-shift-forward (size-change write-size &optional file)
"Shift file contents SIZE-CHANGE bytes forward.
WRITE-SIZE is byte length of saved chunk.
FILE if given is filename to be used, otherwise `buffer-file-name'.
Done by saving content up front and then writing previous batch."
(vlf-tune-batch '(:raw :write) nil file)
(let ((read-size (max vlf-batch-size size-change))
(read-pos vlf-end-pos)
(write-pos vlf-start-pos)
(reporter (make-progress-reporter "Adjusting file content..."
vlf-start-pos
vlf-file-size)))
(vlf-with-undo-disabled
(when (vlf-shift-batches read-size read-pos write-pos
write-size t file)
(vlf-tune-batch '(:raw :write) nil file)
(setq write-pos (+ read-pos size-change)
read-pos (+ read-pos read-size)
write-size read-size
read-size (max vlf-batch-size size-change))
(progress-reporter-update reporter write-pos)
(let ((coding-system-for-write 'no-conversion))
(while (vlf-shift-batches read-size read-pos write-pos
write-size nil file)
(vlf-tune-batch '(:raw :write) nil file)
(setq write-pos (+ read-pos size-change)
read-pos (+ read-pos read-size)
write-size read-size
read-size (max vlf-batch-size size-change))
(progress-reporter-update reporter write-pos)))))
(progress-reporter-done reporter)))
(defun vlf-shift-batches (read-size read-pos write-pos write-size
hide-read file)
"Append READ-SIZE bytes of file starting at READ-POS.
Then write initial buffer content to file at WRITE-POS.
WRITE-SIZE is byte length of saved chunk.
If HIDE-READ is non nil, temporarily hide literal read content.
FILE if given is filename to be used, otherwise `buffer-file-name'.
Return nil if EOF is reached, t otherwise."
(vlf-verify-size t file)
(let ((read-more (< read-pos vlf-file-size))
(start-write-pos (point-min))
(end-write-pos (point-max)))
(when read-more
(goto-char end-write-pos)
(vlf-tune-insert-file-contents-literally
read-pos (min vlf-file-size (+ read-pos read-size)) file))
;; write
(if hide-read ; hide literal region if user has to choose encoding
(narrow-to-region start-write-pos end-write-pos))
(vlf-tune-write start-write-pos end-write-pos write-pos
(or (and (not read-more) (not file)) 0)
write-size file)
(delete-region start-write-pos end-write-pos)
(if hide-read (widen))
read-more))
(provide 'vlf-write)
;;; vlf-write.el ends here