]> code.delx.au - gnu-emacs-elpa/blob - packages/vlf/vlf-write.el
* packages/vlf: Perform search, occur and ediff operations over
[gnu-emacs-elpa] / packages / vlf / vlf-write.el
1 ;;; vlf-write.el --- Saving functionality for VLF -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Keywords: large files, saving
6 ;; Author: Andrey Kotlarski <m00naticus@gmail.com>
7 ;; URL: https://github.com/m00natic/vlfi
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25 ;; This package provides the `vlf-write' command which takes care of
26 ;; saving changes where only part of file is viewed and updated.
27
28 ;;; Code:
29
30 (require 'vlf-base)
31
32 (defun vlf-write ()
33 "Write current chunk to file. Always return true to disable save.
34 If changing size of chunk, shift remaining file content."
35 (interactive)
36 (when (and (buffer-modified-p)
37 (or (verify-visited-file-modtime (current-buffer))
38 (y-or-n-p "File has changed since visited or saved.\
39 Save anyway? ")))
40 (widen)
41 (run-hook-with-args 'vlf-before-batch-functions 'write)
42 (let ((hexl (derived-mode-p 'hexl-mode)))
43 (when hexl
44 (if (consp buffer-undo-list)
45 (setq buffer-undo-list nil))
46 (hexl-mode-exit))
47 (if (zerop vlf-file-size) ;new file
48 (progn (write-region nil nil buffer-file-name vlf-start-pos t)
49 (setq vlf-file-size (vlf-get-file-size
50 buffer-file-truename)
51 vlf-end-pos vlf-file-size)
52 (vlf-update-buffer-name))
53 (let* ((region-length (length (encode-coding-region
54 (point-min) (point-max)
55 buffer-file-coding-system t)))
56 (size-change (- vlf-end-pos vlf-start-pos
57 region-length)))
58 (if (zerop size-change)
59 (write-region nil nil buffer-file-name vlf-start-pos t)
60 (let ((tramp-verbose (if (boundp 'tramp-verbose)
61 (min tramp-verbose 2)))
62 (pos (point))
63 (font-lock font-lock-mode))
64 (font-lock-mode 0)
65 (if (< 0 size-change)
66 (vlf-file-shift-back size-change)
67 (vlf-file-shift-forward (- size-change)))
68 (if font-lock (font-lock-mode 1))
69 (vlf-move-to-chunk-2 vlf-start-pos
70 (if (< (- vlf-end-pos vlf-start-pos)
71 vlf-batch-size)
72 (+ vlf-start-pos vlf-batch-size)
73 vlf-end-pos))
74 (vlf-update-buffer-name)
75 (goto-char pos)))))
76 (if hexl (hexl-mode)))
77 (run-hook-with-args 'vlf-after-batch-functions 'write))
78 t)
79
80 (defun vlf-file-shift-back (size-change)
81 "Shift file contents SIZE-CHANGE bytes back."
82 (write-region nil nil buffer-file-name vlf-start-pos t)
83 (let ((read-start-pos vlf-end-pos)
84 (coding-system-for-write 'no-conversion)
85 (reporter (make-progress-reporter "Adjusting file content..."
86 vlf-end-pos
87 vlf-file-size)))
88 (vlf-with-undo-disabled
89 (while (vlf-shift-batch read-start-pos (- read-start-pos
90 size-change))
91 (setq read-start-pos (+ read-start-pos vlf-batch-size))
92 (progress-reporter-update reporter read-start-pos))
93 ;; pad end with space
94 (erase-buffer)
95 (vlf-verify-size t)
96 (insert-char 32 size-change))
97 (write-region nil nil buffer-file-name (- vlf-file-size
98 size-change) t)
99 (progress-reporter-done reporter)))
100
101 (defun vlf-shift-batch (read-pos write-pos)
102 "Read `vlf-batch-size' bytes from READ-POS and write them \
103 back at WRITE-POS. Return nil if EOF is reached, t otherwise."
104 (erase-buffer)
105 (vlf-verify-size t)
106 (let ((read-end (+ read-pos vlf-batch-size)))
107 (insert-file-contents-literally buffer-file-name nil
108 read-pos
109 (min vlf-file-size read-end))
110 (write-region nil nil buffer-file-name write-pos 0)
111 (< read-end vlf-file-size)))
112
113 (defun vlf-file-shift-forward (size-change)
114 "Shift file contents SIZE-CHANGE bytes forward.
115 Done by saving content up front and then writing previous batch."
116 (let ((read-size (max (/ vlf-batch-size 2) size-change))
117 (read-pos vlf-end-pos)
118 (write-pos vlf-start-pos)
119 (reporter (make-progress-reporter "Adjusting file content..."
120 vlf-start-pos
121 vlf-file-size)))
122 (vlf-with-undo-disabled
123 (when (vlf-shift-batches read-size read-pos write-pos t)
124 (setq write-pos (+ read-pos size-change)
125 read-pos (+ read-pos read-size))
126 (progress-reporter-update reporter write-pos)
127 (let ((coding-system-for-write 'no-conversion))
128 (while (vlf-shift-batches read-size read-pos write-pos nil)
129 (setq write-pos (+ read-pos size-change)
130 read-pos (+ read-pos read-size))
131 (progress-reporter-update reporter write-pos)))))
132 (progress-reporter-done reporter)))
133
134 (defun vlf-shift-batches (read-size read-pos write-pos hide-read)
135 "Append READ-SIZE bytes of file starting at READ-POS.
136 Then write initial buffer content to file at WRITE-POS.
137 If HIDE-READ is non nil, temporarily hide literal read content.
138 Return nil if EOF is reached, t otherwise."
139 (vlf-verify-size t)
140 (let ((read-more (< read-pos vlf-file-size))
141 (start-write-pos (point-min))
142 (end-write-pos (point-max)))
143 (when read-more
144 (goto-char end-write-pos)
145 (insert-file-contents-literally buffer-file-name nil read-pos
146 (min vlf-file-size
147 (+ read-pos read-size))))
148 ;; write
149 (if hide-read ; hide literal region if user has to choose encoding
150 (narrow-to-region start-write-pos end-write-pos))
151 (write-region start-write-pos end-write-pos
152 buffer-file-name write-pos 0)
153 (delete-region start-write-pos end-write-pos)
154 (if hide-read (widen))
155 read-more))
156
157 (provide 'vlf-write)
158
159 ;;; vlf-write.el ends here