Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 179 lines (146 sloc) 6.465 kb
2bf67ff6 »
2012-08-09 Merge changes in master and
1 ;;; iedit-rect.el --- visible rectangle editing support based on Iedit.
2
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
4
19f35aa1 »
2013-10-21 Change the default key binding for `iedit-rectangle-mode' to c-x-r-ret
5 ;; Time-stamp: <2013-10-21 16:15:25 Victor Ren>
2bf67ff6 »
2012-08-09 Merge changes in master and
6 ;; Author: Victor Ren <victorhge@gmail.com>
7 ;; Keywords: occurrence region simultaneous rectangle refactoring
8 ;; Version: 0.97
9 ;; X-URL: http://www.emacswiki.org/emacs/Iedit
10 ;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
11
12 ;; This file is not part of GNU Emacs, but it is distributed under
13 ;; the same terms as GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; This package also provides rectangle support with *visible rectangle*
a089103f »
2012-08-28 Fix typoes
31 ;; highlighting, which is similar with cua-mode rectangle support. But it is
6f80f881 »
2012-08-24 Differentiate lib keymap and occurrence keymap
32 ;; lighter weight and uses iedit mechanisms.
2bf67ff6 »
2012-08-09 Merge changes in master and
33
34 ;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially
35 ;; tested on Gnu Emacs 22. If you have any compatible problem, please let me
36 ;; know.
37
38 ;;; todo:
39 ;; - Add restrict function back
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
84896f8b »
2012-08-13 *** empty log message ***
44 (require 'rect) ;; kill-rectangle
2bf67ff6 »
2012-08-09 Merge changes in master and
45 (require 'iedit-lib)
46
47 (defvar iedit-rectangle-mode nil) ;; Name of the minor mode
48
49 (make-variable-buffer-local 'iedit-rectangle-mode)
50 (or (assq 'iedit-rectangle-mode minor-mode-alist)
51 (nconc minor-mode-alist
52 (list '(iedit-rectangle-mode iedit-rectangle-mode))))
53
54
55 ;;; Default key bindings:
19f35aa1 »
2013-10-21 Change the default key binding for `iedit-rectangle-mode' to c-x-r-ret
56 (define-key ctl-x-r-map [return] 'iedit-rectangle-mode)
2bf67ff6 »
2012-08-09 Merge changes in master and
57
58 (defvar iedit-rectangle nil
59 "This buffer local variable which is the rectangle geometry if
60 current mode is iedit-rect. Otherwise it is nil.
61 \(car iedit-rectangle) is the top-left corner and
62 \(cadr iedit-rectangle) is the bottom-right corner" )
63
64 (make-variable-buffer-local 'iedit-rectangle)
65
66 ;;; Define Iedit rect mode map
67 (defvar iedit-rect-keymap
68 (let ((map (make-sparse-keymap)))
6f80f881 »
2012-08-24 Differentiate lib keymap and occurrence keymap
69 (set-keymap-parent map iedit-occurrence-keymap-default)
2bf67ff6 »
2012-08-09 Merge changes in master and
70 (define-key map (kbd "M-K") 'iedit-kill-rectangle)
71 map)
541eab7b »
2012-08-28 Add document for iedit-rectangle-mode
72 "Keymap used within overlays in Iedit-rect mode.")
2bf67ff6 »
2012-08-09 Merge changes in master and
73
74 (or (assq 'iedit-rectangle-mode minor-mode-map-alist)
75 (setq minor-mode-map-alist
6f80f881 »
2012-08-24 Differentiate lib keymap and occurrence keymap
76 (cons (cons 'iedit-rectangle-mode iedit-lib-keymap) minor-mode-map-alist)))
2bf67ff6 »
2012-08-09 Merge changes in master and
77
78
79 ;; Avoid to restore Iedit-rect mode when restoring desktop
80 (add-to-list 'desktop-minor-mode-handlers
81 '(iedit-rectangle-mode . nil))
82
83 ;;;###autoload
4efe5116 »
2012-10-10 allow iedit-rectangle-mode to take beg end programatically
84 (defun iedit-rectangle-mode (&optional beg end)
541eab7b »
2012-08-28 Add document for iedit-rectangle-mode
85 "Toggle Iedit-rect mode.
86
87 When Iedit-rect mode is on, a rectangle is started with visible
a089103f »
2012-08-28 Fix typoes
88 rectangle highlighting. Rectangle editing support is based on
541eab7b »
2012-08-28 Add document for iedit-rectangle-mode
89 Iedit mechanism.
90
91 Commands:
92 \\{iedit-rect-keymap}"
4efe5116 »
2012-10-10 allow iedit-rectangle-mode to take beg end programatically
93 (interactive (when (iedit-region-active)
94 (list (region-beginning)
95 (region-end))))
50748a56 »
2013-02-09 ensure iedit-skip-modification-once is t in case of errors
96
97 ;; enforce skip modification once, errors may happen to cause this to be
98 ;; unset.
99 (setq iedit-skip-modification-once t)
2bf67ff6 »
2012-08-09 Merge changes in master and
100 (if iedit-rectangle-mode
101 (iedit-rectangle-done)
102 (iedit-barf-if-lib-active)
4efe5116 »
2012-10-10 allow iedit-rectangle-mode to take beg end programatically
103 (if (and beg end)
104 (progn (setq mark-active nil)
105 (run-hooks 'deactivate-mark-hook)
106 (iedit-rectangle-start beg end))
107 (error "no region available."))))
2bf67ff6 »
2012-08-09 Merge changes in master and
108
109 (defun iedit-rectangle-start (beg end)
110 "Start Iedit mode for the region as a rectangle."
111 (barf-if-buffer-read-only)
d6c99fa9 »
2012-10-10 set `iedit-rectangle` to the actual rectangle corners
112 (setq beg (copy-marker beg))
113 (setq end (copy-marker end t))
2bf67ff6 »
2012-08-09 Merge changes in master and
114 (setq iedit-occurrences-overlays nil)
115 (setq iedit-initial-string-local nil)
29869ebe »
2012-09-05 Add buffer local variable iedit-occurrence-keymap
116 (setq iedit-occurrence-keymap iedit-rect-keymap)
2bf67ff6 »
2012-08-09 Merge changes in master and
117 (save-excursion
118 (let ((beg-col (progn (goto-char beg) (current-column)))
119 (end-col (progn (goto-char end) (current-column))))
120 (when (< end-col beg-col)
121 (rotatef beg-col end-col))
122 (goto-char beg)
83c037d6 »
2013-02-01 Fix infinite loop problem in iedit-rect-mode
123 (while
124 (progn
125 (push (iedit-make-occurrence-overlay
126 (progn
127 (move-to-column beg-col t)
128 (point))
129 (progn
130 (move-to-column end-col t)
131 (point)))
132 iedit-occurrences-overlays)
133 (and (< (point) end) (forward-line 1))))))
d6c99fa9 »
2012-10-10 set `iedit-rectangle` to the actual rectangle corners
134 (setq iedit-rectangle (list beg end))
83c037d6 »
2013-02-01 Fix infinite loop problem in iedit-rect-mode
135 (setq iedit-rectangle-mode
136 (propertize
137 (concat " Iedit-rect:"
138 (number-to-string (length iedit-occurrences-overlays)))
139 'face
140 'font-lock-warning-face))
2bf67ff6 »
2012-08-09 Merge changes in master and
141 (force-mode-line-update)
142 (add-hook 'kbd-macro-termination-hook 'iedit-rectangle-done nil t)
143 (add-hook 'change-major-mode-hook 'iedit-rectangle-done nil t)
144 (add-hook 'iedit-aborting-hook 'iedit-rectangle-done nil t))
145
146 (defun iedit-rectangle-done ()
147 "Exit Iedit mode.
148 Save the current occurrence string locally and globally. Save
149 the initial string globally."
150 (when iedit-buffering
151 (iedit-stop-buffering))
1c410fc2 »
2012-08-10 Fix iedit-number-occurrences with conjointed occurrences problem
152 (iedit-cleanup)
2bf67ff6 »
2012-08-09 Merge changes in master and
153 (setq iedit-rectangle-mode nil)
154 (force-mode-line-update)
155 (remove-hook 'kbd-macro-termination-hook 'iedit-rectangle-done t)
156 (remove-hook 'change-major-mode-hook 'iedit-rectangle-done t)
157 (remove-hook 'iedit-aborting-hook 'iedit-rectangle-done t))
158
159 (defun iedit-kill-rectangle(&optional fill)
160 "Kill the rectangle.
161 The behavior is the same as `kill-rectangle' in rect mode."
162 (interactive "*P")
163 (or (and iedit-rectangle (iedit-same-column))
164 (error "Not a rectangle"))
c7abe595 »
2013-02-09 Remove confusing messages "Locate the fist occurrence"
165 (let ((inhibit-modification-hooks t))
166 (kill-rectangle (marker-position (car iedit-rectangle))
167 (marker-position (cadr iedit-rectangle)) fill)))
2bf67ff6 »
2012-08-09 Merge changes in master and
168
169 (provide 'iedit-rect)
170
171 ;;; iedit-rect.el ends here
172
173 ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
174 ;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc
175 ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
176 ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
177 ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
178 ;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed
Something went wrong with that request. Please try again.