Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 166 lines (135 sloc) 6.153 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
84896f8b »
2012-08-13 *** empty log message ***
5 ;; Time-stamp: <2012-08-10 16:20:22 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*
31 ;; highlighting, which is similar with cua mode rectangle support, but still
32 ;; quite different.
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:
56 (define-key global-map [C-return] 'iedit-rectangle-mode)
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 help map.
67 (eval-when-compile (require 'help-macro))
68
69 ;;; Define Iedit rect mode map
70 (defvar iedit-rect-keymap
71 (let ((map (make-sparse-keymap)))
72 (set-keymap-parent map iedit-occurrence-keymap)
73 (define-key map (kbd "M-K") 'iedit-kill-rectangle)
74 map)
75 "Keymap used within overlays in iedit-RECT mode.")
76
77 (or (assq 'iedit-rectangle-mode minor-mode-map-alist)
78 (setq minor-mode-map-alist
79 (cons (cons 'iedit-rectangle-mode iedit-rect-keymap) minor-mode-map-alist)))
80
81
82 ;; Avoid to restore Iedit-rect mode when restoring desktop
83 (add-to-list 'desktop-minor-mode-handlers
84 '(iedit-rectangle-mode . nil))
85
86 ;;;###autoload
87 (defun iedit-rectangle-mode ()
88 "Toggle iedit-RECT mode."
89 (interactive)
90 (if iedit-rectangle-mode
91 (iedit-rectangle-done)
92 (iedit-barf-if-lib-active)
93 (if (iedit-region-active)
94 (let ((beg (region-beginning))
95 (end (region-end)))
96 (set-mark nil)
97 (iedit-rectangle-start beg end)))))
98
99 (defun iedit-rectangle-start (beg end)
100 "Start Iedit mode for the region as a rectangle."
101 (barf-if-buffer-read-only)
102 (setq iedit-occurrences-overlays nil)
103 (setq iedit-rectangle (list beg end))
104 (setq iedit-initial-string-local nil)
105 (save-excursion
106 (let ((beg-col (progn (goto-char beg) (current-column)))
107 (end-col (progn (goto-char end) (current-column))))
108 (when (< end-col beg-col)
109 (rotatef beg-col end-col))
110 (goto-char beg)
111 (loop do (progn
112 (push (iedit-make-occurrence-overlay
113 (progn
114 (move-to-column beg-col t)
115 (point))
116 (progn
117 (move-to-column end-col t)
118 (point)))
119 iedit-occurrences-overlays)
120 (forward-line 1))
121 until (> (point) end))
122 (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))))
123 (setq iedit-rectangle-mode (propertize
124 (concat " Iedit-RECT:" (number-to-string (length iedit-occurrences-overlays)))
125 'face 'font-lock-warning-face))
126 (force-mode-line-update)
127 (add-hook 'kbd-macro-termination-hook 'iedit-rectangle-done nil t)
128 (add-hook 'change-major-mode-hook 'iedit-rectangle-done nil t)
129 (add-hook 'iedit-aborting-hook 'iedit-rectangle-done nil t))
130
131 (defun iedit-rectangle-done ()
132 "Exit Iedit mode.
133 Save the current occurrence string locally and globally. Save
134 the initial string globally."
135 (when iedit-buffering
136 (iedit-stop-buffering))
1c410fc2 »
2012-08-10 Fix iedit-number-occurrences with conjointed occurrences problem
137 (iedit-cleanup)
2bf67ff6 »
2012-08-09 Merge changes in master and
138 (setq iedit-rectangle-mode nil)
139 (force-mode-line-update)
140 (remove-hook 'kbd-macro-termination-hook 'iedit-rectangle-done t)
141 (remove-hook 'change-major-mode-hook 'iedit-rectangle-done t)
142 (remove-hook 'iedit-aborting-hook 'iedit-rectangle-done t))
143
144 (defun iedit-kill-rectangle(&optional fill)
145 "Kill the rectangle.
146 The behavior is the same as `kill-rectangle' in rect mode."
147 (interactive "*P")
148 (or (and iedit-rectangle (iedit-same-column))
149 (error "Not a rectangle"))
150 (let ((inhibit-modification-hooks t)
151 (beg (overlay-start (car iedit-occurrences-overlays)))
152 (end (overlay-end (progn (iedit-last-occurrence)
153 (iedit-find-current-occurrence-overlay)))))
154 (kill-rectangle beg end fill)))
155
156 (provide 'iedit-rect)
157
158 ;;; iedit-rect.el ends here
159
160 ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
161 ;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc
162 ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
163 ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
164 ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
165 ;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed
Something went wrong with that request. Please try again.