-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
swoop-edit.el
169 lines (153 loc) · 6.49 KB
/
swoop-edit.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
;;; swoop-edit.el --- Peculiar buffer navigation -*- lexical-binding: t -*-
;; Copyright (C) 2014 by Shingo Fukuyama
;; 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 of
;; the License, 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.
;;; Commentary:
;;; Code:
(require 'swoop-lib)
(defvar swoop-edit-buffer "*Swoop Edit*")
(defvar swoop-edit-map
(let (($map (make-sparse-keymap)))
;; (define-key $map (kbd "C-x C-s") 'swoop-edit-apply-changes)
(define-key $map (kbd "C-x C-s") 'swoop-edit-finish)
(define-key $map (kbd "C-c C-c") 'swoop-edit-finish)
$map))
(define-key swoop-map (kbd "C-c C-e") 'swoop-edit)
(defun swoop-edit-finish ()
"Kill the edit buffer. The changes should have already been applied."
(interactive)
(select-window swoop--target-window)
(with-current-buffer swoop--target-buffer
(set-window-buffer nil swoop--target-buffer)
(goto-char swoop--target-last-position))
(kill-buffer swoop-edit-buffer))
(defun swoop-modify-buffer-content ($bufcont)
"Modify the original buffer content, but it causes slow rendering."
$bufcont)
(defsubst swoop-line-beg-point ($line &optional $buf)
(with-current-buffer (or $buf (current-buffer))
(save-excursion
(swoop-goto-line $line) (point))))
(defsubst swoop-set-marker ($line &optional $buf)
(with-current-buffer (or $buf (current-buffer))
(save-excursion
(swoop-goto-line $line)
(set-marker (make-marker) (point)))))
(defun swoop-edit ()
"Modify matched lines. Changes are automatically applying to target buffers."
(interactive)
(let (($bufcont (with-current-buffer swoop-buffer
(buffer-substring
(point-min) (point-max)))))
(run-with-timer
0 nil
(lambda ($bufcont $bufname)
(when (get-buffer swoop-edit-buffer)
(kill-buffer swoop-edit-buffer))
(funcall swoop-display-function swoop-edit-buffer)
(erase-buffer)
;; Header
(insert (propertize
(concat " " $bufname "\n")
'face
'swoop-line-buffer-name-face
'intangible t))
;; Body
(insert $bufcont)
(save-excursion
(goto-char (point-min))
(add-text-properties (point-min) (point-max)
'(read-only t rear-nonsticky t front-sticky t))
(let (($linum)
(inhibit-read-only t))
(goto-char (point-min))
(while (not (eobp))
(goto-char (or (next-single-property-change (point) 'swl) (point-max)))
(when (setq $linum (get-text-property (point) 'swl))
(let (($line-buf (get-text-property (point) 'swb)))
(insert (propertize
(format "%s:: " $linum)
'swp t
'face 'swoop-face-line-number
'intangible t
'rear-nonsticky t
'read-only t))
(put-text-property
(point-at-bol) (point-at-eol)
'swm (save-excursion
(with-current-buffer $line-buf
(swoop-goto-line $linum)
(set-marker
(make-marker)
(point)))))
(remove-text-properties (point) (point-at-eol) '(read-only t))
(set-text-properties (point-at-eol) (1+ (point-at-eol))
'(read-only t rear-nonsticky t)))))))
(swoop-overlay-word swoop-last-pattern (current-buffer))
(goto-char (point-min))
(forward-line 1)
(re-search-forward "^[[:space:]]*\\([0-9]+\\)::[[:space:]]" nil t)
(add-hook 'after-change-functions 'swoop-edit-sync nil t)
(use-local-map swoop-edit-map))
;; Args
$bufcont
swoop--target-buffer)
(exit-minibuffer)))
(defun swoop-edit-sync ($beg $end $length)
"Sync edit."
(save-excursion
(goto-char $beg)
(let* (($line-beg (point-at-bol))
($marker (get-text-property $line-beg 'swm))
($buf (marker-buffer $marker))
$col)
(with-current-buffer $buf
(when buffer-read-only
(if (y-or-n-p "Do you want to disable read-only-mode? ")
(read-only-mode -1)
(kill-buffer swoop-edit-buffer)
(error "Buffer \"%s\" is read only" (buffer-name)))))
(when (and (get-text-property $line-beg 'swp)
(not (get-text-property $end 'swp)))
(when (= $length 0)
(put-text-property $beg $end 'swm $marker)
(save-excursion
(and (re-search-forward "\n" $end t)
(delete-region (1- (point)) $end))))
(let* (($line (- (line-number-at-pos)
(line-number-at-pos (window-start))))
($readonly (with-current-buffer $buf buffer-read-only))
($win (or (get-buffer-window $buf)
(display-buffer $buf
'(nil (inhibit-same-window . t)
(inhibit-switch-frame . t)))))
($line-end (point-at-eol))
($text (save-excursion
(goto-char (next-single-property-change
$line-beg 'swp nil
$line-end))
(setq $col (- (point) $line-beg))
(buffer-substring-no-properties (point) $line-end))))
(with-selected-window $win
(goto-char $marker)
;; Unveil invisible block
(swoop-mapc $ov
(overlays-in (point-at-bol)
(point-at-eol))
(let (($type (overlay-get $ov 'invisible)))
(when $type
(overlay-put $ov 'invisible nil))))
(recenter $line)
(if $readonly
(message "Buffer `%s' is read only." $buf)
(delete-region (point-at-bol) (point-at-eol))
(insert $text))
(move-to-column $col)))))))
(provide 'swoop-edit)
;;; swoop-edit.el ends here