Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2bf67ff6c7
Fetching contributors…

Cannot retrieve contributors at this time

file 165 lines (135 sloc) 6.153 kb
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
;;; iedit-rect.el --- visible rectangle editing support based on Iedit.

;; Copyright (C) 2010, 2011, 2012 Victor Ren

;; Time-stamp: <2012-08-09 17:17:25 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring
;; Version: 0.97
;; X-URL: http://www.emacswiki.org/emacs/Iedit
;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x

;; This file is not part of GNU Emacs, but it is distributed under
;; the same terms as GNU Emacs.

;; GNU Emacs 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 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This package also provides rectangle support with *visible rectangle*
;; highlighting, which is similar with cua mode rectangle support, but still
;; quite different.

;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially
;; tested on Gnu Emacs 22. If you have any compatible problem, please let me
;; know.

;;; todo:
;; - Add restrict function back

;;; Code:

(eval-when-compile (require 'cl))
(require 'rect) ;; kill rectangle
(require 'iedit-lib)

(defvar iedit-rectangle-mode nil) ;; Name of the minor mode

(make-variable-buffer-local 'iedit-rectangle-mode)
(or (assq 'iedit-rectangle-mode minor-mode-alist)
    (nconc minor-mode-alist
           (list '(iedit-rectangle-mode iedit-rectangle-mode))))


;;; Default key bindings:
(define-key global-map [C-return] 'iedit-rectangle-mode)

(defvar iedit-rectangle nil
  "This buffer local variable which is the rectangle geometry if
current mode is iedit-rect. Otherwise it is nil.
\(car iedit-rectangle) is the top-left corner and
\(cadr iedit-rectangle) is the bottom-right corner" )

(make-variable-buffer-local 'iedit-rectangle)

;;; Define iedit help map.
(eval-when-compile (require 'help-macro))

;;; Define Iedit rect mode map
(defvar iedit-rect-keymap
  (let ((map (make-sparse-keymap)))
    (set-keymap-parent map iedit-occurrence-keymap)
    (define-key map (kbd "M-K") 'iedit-kill-rectangle)
    map)
  "Keymap used within overlays in iedit-RECT mode.")

(or (assq 'iedit-rectangle-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
          (cons (cons 'iedit-rectangle-mode iedit-rect-keymap) minor-mode-map-alist)))


;; Avoid to restore Iedit-rect mode when restoring desktop
(add-to-list 'desktop-minor-mode-handlers
             '(iedit-rectangle-mode . nil))

;;;###autoload
(defun iedit-rectangle-mode ()
  "Toggle iedit-RECT mode."
  (interactive)
  (if iedit-rectangle-mode
      (iedit-rectangle-done)
    (iedit-barf-if-lib-active)
    (if (iedit-region-active)
        (let ((beg (region-beginning))
              (end (region-end)))
          (set-mark nil)
          (iedit-rectangle-start beg end)))))

(defun iedit-rectangle-start (beg end)
  "Start Iedit mode for the region as a rectangle."
  (barf-if-buffer-read-only)
  (setq iedit-occurrences-overlays nil)
  (setq iedit-rectangle (list beg end))
  (setq iedit-initial-string-local nil)
  (save-excursion
    (let ((beg-col (progn (goto-char beg) (current-column)))
          (end-col (progn (goto-char end) (current-column))))
      (when (< end-col beg-col)
        (rotatef beg-col end-col))
      (goto-char beg)
      (loop do (progn
                 (push (iedit-make-occurrence-overlay
                        (progn
                          (move-to-column beg-col t)
                          (point))
                        (progn
                          (move-to-column end-col t)
                          (point)))
                       iedit-occurrences-overlays)
                 (forward-line 1))
            until (> (point) end))
      (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))))
  (setq iedit-rectangle-mode (propertize
                    (concat " Iedit-RECT:" (number-to-string (length iedit-occurrences-overlays)))
                    'face 'font-lock-warning-face))
  (force-mode-line-update)
  (add-hook 'kbd-macro-termination-hook 'iedit-rectangle-done nil t)
  (add-hook 'change-major-mode-hook 'iedit-rectangle-done nil t)
  (add-hook 'iedit-aborting-hook 'iedit-rectangle-done nil t))

(defun iedit-rectangle-done ()
  "Exit Iedit mode.
Save the current occurrence string locally and globally. Save
the initial string globally."
  (when iedit-buffering
      (iedit-stop-buffering))
  (iedit-clearup)
  (setq iedit-rectangle-mode nil)
  (force-mode-line-update)
  (remove-hook 'kbd-macro-termination-hook 'iedit-rectangle-done t)
  (remove-hook 'change-major-mode-hook 'iedit-rectangle-done t)
  (remove-hook 'iedit-aborting-hook 'iedit-rectangle-done t))

(defun iedit-kill-rectangle(&optional fill)
  "Kill the rectangle.
The behavior is the same as `kill-rectangle' in rect mode."
  (interactive "*P")
  (or (and iedit-rectangle (iedit-same-column))
      (error "Not a rectangle"))
  (let ((inhibit-modification-hooks t)
        (beg (overlay-start (car iedit-occurrences-overlays)))
        (end (overlay-end (progn (iedit-last-occurrence)
                                 (iedit-find-current-occurrence-overlay)))))
    (kill-rectangle beg end fill)))

(provide 'iedit-rect)

;;; iedit-rect.el ends here

;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc
;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed
Something went wrong with that request. Please try again.