Skip to content
Browse files

Merge pull request #38 from mgalgs/narrow-around-current-line

Provide functions to narrow around current-line
A good start point to a more mature functionality
  • Loading branch information...
2 parents 1242c67 + d2f08e8 commit fd801e77dfdcfa0bdeadc153a9fb637dab2b4bb4 @victorhge committed Mar 2, 2013
Showing with 97 additions and 0 deletions.
  1. +8 −0 README.org
  2. +19 −0 iedit-lib.el
  3. +70 −0 iedit.el
View
8 README.org
@@ -35,6 +35,14 @@ Renaming refactoring is convinient in Iedit mode
- Last renaming refactoring is remembered and can be applied to other buffers
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*
highlighting, which is similar with cua mode rectangle support. But it's
lighter weight and uses iedit mechanisms.
View
19 iedit-lib.el
@@ -850,6 +850,25 @@ STRING is already `regexp-quote'ed"
(concat (substring string 0 50) "...")
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 ()
"Return t if region is active and not empty.
If variable `iedit-transient-mark-sensitive' is t, active region
View
70 iedit.el
@@ -132,6 +132,13 @@ Iedit mode is turned off last time.")
"This is buffer local variable which is the initial region
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-only-complete-symbol-local)
@@ -223,6 +230,9 @@ This is like `describe-bindings', but displays only Iedit keys."
(let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-occurrence-keymap-default)
(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-C") 'iedit-toggle-case-sensitive)
map)
@@ -414,6 +424,8 @@ the initial string globally."
(setq iedit-last-occurrence-local (iedit-current-occurrence-string))
(setq iedit-last-occurrence-global iedit-last-occurrence-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)
@@ -502,6 +514,64 @@ the initial string globally."
(message "Restricted in current function, %d matches."
(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)
"Restricting Iedit mode in a region."
(when iedit-buffering

0 comments on commit fd801e7

Please sign in to comment.
Something went wrong with that request. Please try again.