Skip to content

Commit

Permalink
Provide functions to narrow around current-line
Browse files Browse the repository at this point in the history
It's nice to be able to run Iedit on only the current line or lines
near the current line. Provide functions to narrow the Iedit search
region to the current line and to expand the search region near the
current line ad-hoc.
  • Loading branch information
mgalgs committed Feb 24, 2013
1 parent 27dbbdd commit d2f08e8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.org
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ Renaming refactoring is convinient in Iedit mode
- Last renaming refactoring is remembered and can be applied to other buffers - Last renaming refactoring is remembered and can be applied to other buffers
later later


- Restricting the search area to just the current line can be done by
pressing M-I.

- Restricting the search area to the lines near the current line can
be done by pressing M-{ and M-}. These will expand the search
region one line at a time from the top and bottom. Add a prefix
argument to go the opposite direction.

Iedit-rectangle-mode provides rectangle support with *visible rectangle* Iedit-rectangle-mode provides rectangle support with *visible rectangle*
highlighting, which is similar with cua mode rectangle support. But it's highlighting, which is similar with cua mode rectangle support. But it's
lighter weight and uses iedit mechanisms. lighter weight and uses iedit mechanisms.
Expand Down
19 changes: 19 additions & 0 deletions iedit-lib.el
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -848,6 +848,25 @@ STRING is already `regexp-quote'ed"
(concat (substring string 0 50) "...") (concat (substring string 0 50) "...")
string)))) string))))


(defun iedit-char-at-bol (&optional N)
"Get char position of the beginning of the current line. If `N'
is given, move forward (or backward) that many lines (using
`forward-line') and get the char position at the beginning of
that line."
(save-excursion
(forward-line (if N N 0))
(point)))

(defun iedit-char-at-eol (&optional N)
"Get char position of the end of the current line. If `N' is
given, move forward (or backward) that many lines (using
`forward-line') and get the char position at the end of that
line."
(save-excursion
(forward-line (if N N 0))
(end-of-line)
(point)))

(defun iedit-region-active () (defun iedit-region-active ()
"Return t if region is active and not empty. "Return t if region is active and not empty.
If variable `iedit-transient-mark-sensitive' is t, active region If variable `iedit-transient-mark-sensitive' is t, active region
Expand Down
70 changes: 70 additions & 0 deletions iedit.el
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ Iedit mode is turned off last time.")
"This is buffer local variable which is the initial region "This is buffer local variable which is the initial region
where Iedit mode is started from.") where Iedit mode is started from.")


(defvar iedit-num-lines-to-expand-up 0
"This is a global variable indicating how many lines up from
point should be included in the replacement region.")

(defvar iedit-num-lines-to-expand-down 0
"This is a global variable indicating how many lines down from
point should be included in the replacement region.")


(make-variable-buffer-local 'iedit-mode) (make-variable-buffer-local 'iedit-mode)
(make-variable-buffer-local 'iedit-only-complete-symbol-local) (make-variable-buffer-local 'iedit-only-complete-symbol-local)
Expand Down Expand Up @@ -223,6 +230,9 @@ This is like `describe-bindings', but displays only Iedit keys."
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-occurrence-keymap-default) (set-keymap-parent map iedit-occurrence-keymap-default)
(define-key map (kbd "M-H") 'iedit-restrict-function) (define-key map (kbd "M-H") 'iedit-restrict-function)
(define-key map (kbd "M-I") 'iedit-restrict-current-line)
(define-key map (kbd "M-{") 'iedit-expand-up-a-line)
(define-key map (kbd "M-}") 'iedit-expand-down-a-line)
(define-key map (kbd "M-G") 'iedit-apply-global-modification) (define-key map (kbd "M-G") 'iedit-apply-global-modification)
(define-key map (kbd "M-C") 'iedit-toggle-case-sensitive) (define-key map (kbd "M-C") 'iedit-toggle-case-sensitive)
map) map)
Expand Down Expand Up @@ -414,6 +424,8 @@ the initial string globally."
(setq iedit-last-occurrence-local (iedit-current-occurrence-string)) (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
(setq iedit-last-occurrence-global iedit-last-occurrence-local) (setq iedit-last-occurrence-global iedit-last-occurrence-local)
(setq iedit-last-initial-string-global iedit-initial-string-local) (setq iedit-last-initial-string-global iedit-initial-string-local)
(setq iedit-num-lines-to-expand-up 0)
(setq iedit-num-lines-to-expand-down 0)


(iedit-cleanup) (iedit-cleanup)


Expand Down Expand Up @@ -502,6 +514,64 @@ the initial string globally."
(message "Restricted in current function, %d matches." (message "Restricted in current function, %d matches."
(length iedit-occurrences-overlays))) (length iedit-occurrences-overlays)))


(defun iedit-restrict-current-line ()
"Restrict Iedit mode to current line."
(interactive)
(iedit-restrict-region (iedit-char-at-bol) (iedit-char-at-eol))
(message "Restricted to current line, %d match%s."
(length iedit-occurrences-overlays)
(if (= 1 (length iedit-occurrences-overlays)) "" "es")))

(defun iedit-expand-by-a-line (where amount)
"After restricting iedit to the current line with
`iedit-restrict-current-line', this function expands the top or
bottom of the search region upwards or downwards by `amount'
lines. The region being acted upon is controlled with
`where' ('top to act on the top, anything else for the
bottom). With a prefix, collapses the top or bottom of the search
region by `amount' lines."
(interactive "P")
;; Since iedit-done resets iedit-num-lines-to-expand-{down,up}, we
;; have to hang on to them in tmp variables
(let ((tmp-up iedit-num-lines-to-expand-up)
(tmp-down iedit-num-lines-to-expand-down)
;; we want to call iedit-mode with a universal prefix arg
(current-prefix-arg '(4)))
(iedit-done)
(call-interactively 'iedit-mode)
(setq iedit-num-lines-to-expand-up tmp-up)
(setq iedit-num-lines-to-expand-down tmp-down)
(if (eq where 'top)
(setq iedit-num-lines-to-expand-up (max 0
(+ amount iedit-num-lines-to-expand-up)))
(setq iedit-num-lines-to-expand-down (max 0
(+ amount iedit-num-lines-to-expand-down))))
(iedit-restrict-region (iedit-char-at-bol (- iedit-num-lines-to-expand-up))
(iedit-char-at-eol iedit-num-lines-to-expand-down))
(message "Now looking -%d/+%d lines around current line, %d match%s."
iedit-num-lines-to-expand-up
iedit-num-lines-to-expand-down
(length iedit-occurrences-overlays)
(if (= 1 (length iedit-occurrences-overlays)) "" "es"))))

(defun iedit-expand-up-a-line (&optional arg)
"After restricting iedit to the current line with
`iedit-restrict-current-line', this function expands the search
region upwards by one line. With a prefix, bring the top of the
region back down one line."
(interactive "P")
(iedit-expand-by-a-line 'top
(if arg -1 1)))

(defun iedit-expand-down-a-line (&optional arg)
"After restricting iedit to the current line with
`iedit-restrict-current-line', this function expands the search
region downwards by one line. With a prefix, bring the bottom of
the region back up one line."
(interactive "P")
(iedit-expand-by-a-line 'bottom
(if arg -1 1)))

(defun iedit-restrict-region (beg end &optional inclusive) (defun iedit-restrict-region (beg end &optional inclusive)
"Restricting Iedit mode in a region." "Restricting Iedit mode in a region."
(when iedit-buffering (when iedit-buffering
Expand Down

0 comments on commit d2f08e8

Please sign in to comment.