Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

use symbol instead of word when region not active #1

Closed
wants to merge 15 commits into from

3 participants

Le Wang victorhge Tassilo Horn
Le Wang
Collaborator

No description provided.

victorhge
Owner

What is the problem your patch is solving? Would you give me an example?

BR/
Victor Ren

Le Wang
Collaborator
Le Wang
Collaborator
victorhge
Owner
Le Wang added some commits
Le Wang fix search to use regexp-quote when needed 45784a9
Le Wang implement interactive string-rectangle mode with universal argument a…
…nd region selected

Conflicts:

	iedit.el
9a37ca1
Le Wang Merge branch 'master' of github.com:lewang/iedit
Conflicts:
	iedit.el
448ceb3
Le Wang add edit rectangle option
when region is activ eand Universal prefix arg is given, the region is edited
as a rectangle.
c854c28
Le Wang lewang closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 27, 2011
  1. Tassilo Horn
  2. Tassilo Horn

    add me as a contributor

    tsdh authored
Commits on Jan 15, 2012
  1. Fix syntax error

    authored
Commits on Jan 16, 2012
  1. really use symbol

    Le Wang authored
  2. Merge from tsdh/master

    authored
  3. Boolean `t'

    authored
  4. Remove duplicated comments

    authored
Commits on Jan 18, 2012
  1. fix search to use regexp-quote when needed

    Le Wang authored
  2. implement interactive string-rectangle mode with universal argument a…

    Le Wang authored
    …nd region selected
    
    Conflicts:
    
    	iedit.el
  3. Merge branch 'master' of github.com:lewang/iedit

    Le Wang authored
    Conflicts:
    	iedit.el
Commits on Jan 19, 2012
  1. add edit rectangle option

    Le Wang authored
    when region is activ eand Universal prefix arg is given, the region is edited
    as a rectangle.
This page is out of date. Refresh to see the latest.
Showing with 131 additions and 52 deletions.
  1. +3 −3 README
  2. +128 −49 iedit.el
6 README
View
@@ -1,5 +1,5 @@
iedit - a better alternative of replace-string
-
-It is an Emacs minor mode and allow you edit one occurrence of some text in a
+---------------------------------------------------------------------------------
+It is an Emacs minor mode and allows you to edit one occurrence of some text in a
buffer (possibly narrowed), and simultaneously have all other occurrences edited
-the same way.
+in the same way.
177 iedit.el
View
@@ -1,11 +1,11 @@
;;; iedit.el --- Edit multiple regions with the same content simultaneously.
-;; Copyright (C) 2010, 2011 Victor Ren
+;; Copyright (C) 2010, 2011, 2012 Victor Ren
-;; Time-stamp: <2012-01-14 23:12:48 Victor Ren>
+;; Time-stamp: <2012-01-17 02:58:46 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region replace simultaneous
-;; Version: 0.90
+;; Version: 0.91
;; X-URL: http://www.emacswiki.org/emacs/iedit.el
;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
@@ -30,7 +30,7 @@
;; This package provides a more intuitive way of replace-string operation:
;;
;; - Select the occurrence in the buffer
-;; In Transient Mark mode, just mark a region, the content of the
+;; In Transient Mark mode, just mark a region, the content of the
;; region will be used as the occurrence. (if Transient Mark mode is disabled,
;; using C-u C-x C-x or C-SPC C-SPC to activate it just for this one time).
;;
@@ -42,8 +42,8 @@
;;
;; - Finish - by pressing C-; again
-;; If Transient Mark mode is disabled or the region is not active,
-;; the `current-word' is used as the occurrence by default.
+;; If Transient Mark mode is disabled or the region is not active, the current
+;; symbol (returns from `current-word') is used as the occurrence by default.
;; You can also switch to iedit mode from isearch mode directly. The current
;; search string is used as the occurrence.
@@ -56,7 +56,7 @@
;; (define-key isearch-mode-map (kbd "C-;") 'iedit-mode)
;;; todo:
-;; - C-n,C-p is slow when unmachted lines are hided.
+;; - C-n,C-p is slow when unmatched lines are hided.
;; - Lazy highlight feature (from isearch)?
;; - toggle blank line between matched lines?
;; - ert unit test
@@ -65,6 +65,11 @@
;;; Contributors
;; Adam Lindberg <eproxus@gmail.com> added a case sensitivity option that can be toggled.
+;; Tassilo Horn <tassilo@member.fsf.org> added an option to match only complete
+;; words, not inside words
+
+;; Le Wang <l26wang@gmail.com> proposed to match only complete symbols, not inside symbols.
+
;;; Code:
(eval-when-compile (require 'cl))
@@ -80,16 +85,24 @@
:type 'face
:group 'iedit)
-(defcustom iedit-current-word-default 't
- "If no-nil, use current word by default for the occurrence."
+(defcustom iedit-current-symbol-default t
+ "If no-nil, use current symbol by default for the occurrence."
:type 'boolean
:group 'iedit)
-(defcustom iedit-case-sensitive-default 't
+(defcustom iedit-case-sensitive-default t
"If no-nil, matching is case sensitive"
:type 'boolean
:group 'iedit)
+(defcustom iedit-only-at-symbol-boundaries t
+ "If no-nil, matches have to start and end at symbol boundaries.
+ For example, when invoking iedit-mode on the \"in\" in the
+ sentence \"The king in the castle...\", the \"king\" is not
+ edited."
+ :type 'boolean
+ :group 'iedit)
+
(defcustom iedit-unmatched-lines-invisible-default nil
"If no-nil, hide lines that do not cover any occurrences by
default."
@@ -108,7 +121,7 @@ default."
(or (assq 'iedit-mode minor-mode-alist)
(nconc minor-mode-alist
- (list '(iedit-mode iedit-mode))))
+ (list '(iedit-mode iedit-mode))))
(defvar iedit-occurrences-overlays nil
"The occurrences slot contains a list of overlays used to
@@ -227,44 +240,70 @@ This is like `describe-bindings', but displays only Iedit keys."
If iedit mode is off, turn iedit mode on, off otherwise.
In Transient Mark mode, when iedit mode is turned on, all the
-occurrences of the current region are highlighted. If one
+occurrences of the current region are highlighted. If one
occurrence is modified, the change are propagated to all other
occurrences simultaneously.
If Transient Mark mode is disabled or the region is not active,
-the `current-word' is used as occurrence. All the occurrences of
-the `current-word' are highlighted.
+the current symbol (returns from `current-word') is used as the
+occurrence by default. The occurrences of the current
+symbol, but not include occurrences that are part of other
+symbols, are highlighted. This is good for renaming refactoring
+during programming. If you still want to match all the
+occurrences, even though they are parts of other symbols, you may
+have to select the symbol first.
You can also switch to iedit mode from isearch mode directly. The
current search string is used as occurrence. All occurrences of
the current search string are highlighted.
-With a prefix argument, the occurrence when iedit is turned off
-last time is used as occurrence. This is intended to recover
-last iedit which is turned off by mistake.
+With a universal prefix argument and no active region, the
+occurrence when iedit is turned off last time is used as
+occurrence. This is intended to recover last iedit which is
+turned off by mistake.
+
+With a universal prefix argument and region active, interactively
+edit region as a string rectangle.
Commands:
\\{iedit-mode-map}"
- (interactive "P")
+ (interactive "*P")
(if iedit-mode
(iedit-done)
- (let ((occurrence nil))
- (cond ((and arg iedit-last-occurrence-in-history)
+ (let (occurrence rect-string)
+ (cond ((and arg
+ (not (use-region-p))
+ iedit-last-occurrence-in-history)
(setq occurrence iedit-last-occurrence-in-history))
+ ((and arg
+ (use-region-p))
+ (setq rect-string t))
((and transient-mark-mode mark-active (not (equal (mark) (point))))
- (setq occurrence (buffer-substring-no-properties (mark) (point))))
+ (setq occurrence (regexp-quote (buffer-substring-no-properties
+ (mark) (point)))))
((and isearch-mode (not (string= isearch-string "")))
- (setq occurrence (buffer-substring-no-properties (point) isearch-other-end))
+ (setq occurrence (funcall (if isearch-regexp
+ 'eval
+ 'regexp-quote)
+ (buffer-substring-no-properties
+ (point) isearch-other-end)))
(isearch-exit))
- ((and iedit-current-word-default (current-word t))
- (setq occurrence (current-word)))
+ ((and iedit-current-symbol-default (current-word t))
+ (setq occurrence (regexp-quote (current-word)))
+ (when iedit-only-at-symbol-boundaries
+ (setq occurrence (concat "\\_<" (regexp-quote occurrence) "\\_>"))))
(t (error "No candidate of the occurrence, cannot enable iedit mode.")))
- (deactivate-mark)
- (iedit-start occurrence))))
+ (if rect-string
+ (let ((beg (region-beginning))
+ (end (region-end)))
+ (deactivate-mark)
+ (iedit-rectangle beg end))
+ (deactivate-mark)
+ (iedit-start occurrence)))))
(defun iedit-start (occurrence-exp)
"Start an iedit for the occurrence-exp in the current buffer."
- (setq iedit-mode " Iedit")
+ (setq iedit-mode (propertize " Iedit" 'face 'font-lock-warning-face))
(setq iedit-occurrences-overlays nil)
(setq iedit-unmatched-lines-invisible iedit-unmatched-lines-invisible-default)
(setq iedit-case-sensitive iedit-case-sensitive-default)
@@ -275,21 +314,48 @@ Commands:
;; Find and record each occurrence's markers and add the overlay to the occurrences
(let ((counter 0)
(case-fold-search (not iedit-case-sensitive)))
- (save-excursion
- (goto-char (point-min))
- (while (search-forward occurrence-exp nil t)
- (push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0))
- iedit-occurrences-overlays)
- (setq counter (1+ counter))) ; at less 1
+ (save-excursion
+ (goto-char (point-min))
+ (while (re-search-forward occurrence-exp nil t)
+ (push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0))
+ iedit-occurrences-overlays)
+ (setq counter (1+ counter))) ; at less 1
(setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))
(if iedit-unmatched-lines-invisible
(iedit-hide-unmatched-lines))
- (message "%d matches for \"%s\""
- counter
+ (message "%d matches for \"%s\""
+ counter
(if (> (length occurrence-exp) 50)
(concat (substring occurrence-exp 0 50) "...")
occurrence-exp)))))
+(defun iedit-rectangle (beg end)
+ "Start an iedit for the region as a rectangle"
+ (setq iedit-mode (propertize " Iedit-RECT" 'face 'font-lock-warning-face))
+ (setq iedit-occurrences-overlays nil)
+ (force-mode-line-update)
+ (run-hooks 'iedit-mode-hook)
+ ;; (add-hook 'mouse-leave-buffer-hook 'iedit-done)
+ (add-hook 'kbd-macro-termination-hook 'iedit-done)
+
+ (let ((orig-p (point-marker))
+ (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))
+ (goto-char orig-p)))
+
(defun iedit-hide-unmatched-lines ()
"Hide unmatched lines using invisible overlay."
(let ((prev-occurrence-end 0)
@@ -314,8 +380,8 @@ Commands:
(defun iedit-done ()
"Exit iedit mode."
(let ((ov (car iedit-occurrences-overlays)))
- (if ov
- (setq iedit-last-occurrence-in-history
+ (if ov
+ (setq iedit-last-occurrence-in-history
(buffer-substring (overlay-start ov) (overlay-end ov)))))
(remove-overlays (point-min) (point-max) iedit-occurrence-overlay-name t)
(remove-overlays (point-min) (point-max) iedit-invisible-overlay-name t)
@@ -347,22 +413,35 @@ occurrences if the user starts typing."
(overlay-put unmatched-lines-overlay 'intangible t)
unmatched-lines-overlay))
+;; `iedit-occurrence-update' gets called twice when change==0 and occurrence
+;; is zero-width
+;; -- for front and back insertion.
+(defvar iedit-last-overlay nil
+ "records processed overlay so they don't get processed multiple times. See code.")
+(defun iedit-post-command-func ()
+ (remove-hook 'post-command-hook 'iedit-post-command-func t)
+ (setq iedit-last-overlay nil))
+
(defun iedit-occurrence-update (occurrence after beg end &optional change)
"Update all occurrences.
This modification hook is triggered when a user edits any
occurrence and is responsible for updating all other
occurrences."
- (when (and after (not undo-in-progress)) ; undo will do all the work
- (let ((value (buffer-substring (overlay-start occurrence) (overlay-end occurrence)))
+ (when (and after
+ (not undo-in-progress) ; undo will do all the work
+ (not (< beg (overlay-start occurrence)))
+ (not (eq occurrence iedit-last-overlay)))
+ (setq iedit-last-overlay occurrence)
+ (add-hook 'post-command-hook 'iedit-post-command-func nil t)
+ (let ((replacement-str (buffer-substring-no-properties beg end))
+ (index (- beg (overlay-start occurrence)))
(inhibit-modification-hooks t))
(save-excursion
(dolist (like-occurrence iedit-occurrences-overlays)
- (if (not (eq like-occurrence occurrence))
- (progn
- (goto-char (overlay-start like-occurrence))
- (delete-region (overlay-start like-occurrence)
- (overlay-end like-occurrence))
- (insert value))))))))
+ (when (not (eq like-occurrence occurrence))
+ (goto-char (+ index (overlay-start like-occurrence)))
+ (delete-region (point) (+ (point) change))
+ (insert replacement-str)))))))
(defun iedit-next-occurrence ()
"Move forward to the next occurrence in the `iedit'.
@@ -375,13 +454,13 @@ beginning of the buffer."
(when in-occurrence
(setq pos (next-single-char-property-change pos 'iedit-occurrence-overlay-name)))
(setq pos (next-single-char-property-change pos 'iedit-occurrence-overlay-name))
-
+
(if (/= pos (point-max))
(setq iedit-forward-success t)
(if (and iedit-forward-success in-occurrence)
(progn (message "This is the last occurrence.")
(setq iedit-forward-success nil))
- (progn
+ (progn
(if (get-char-property (point-min) 'iedit-occurrence-overlay-name)
(setq pos (point-min))
(setq pos (next-single-char-property-change (point-min) 'iedit-occurrence-overlay-name)))
@@ -404,12 +483,12 @@ the buffer."
;; At the start of the first occurrence
(if (or (and (eq pos (point-min))
(not (get-char-property (point-min) 'iedit-occurrence-overlay-name)))
- (and (eq (point) (point-min))
+ (and (eq (point) (point-min))
in-occurrence))
(if (and iedit-forward-success in-occurrence)
(progn (message "This is the first occurrence.")
(setq iedit-forward-success nil))
- (progn
+ (progn
(setq pos (previous-single-char-property-change (point-max) 'iedit-occurrence-overlay-name))
(if (not (get-char-property (- (point-max) 1) 'iedit-occurrence-overlay-name))
(setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name)))
Something went wrong with that request. Please try again.