Skip to content

Commit

Permalink
Add the index of the current occurrence on mode-line
Browse files Browse the repository at this point in the history
A buffer local variable `iedit-occurrence-index' is added to trace the index.
It is initialized when iedit-mode is activated and updated when navitation
functions are called.

Test cases are also updated.

A few comile warnings are fixed.
  • Loading branch information
Victor Ren committed Feb 7, 2018
1 parent f668218 commit 412490d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
39 changes: 36 additions & 3 deletions iedit-lib.el
Expand Up @@ -3,12 +3,12 @@

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

;; Time-stamp: <2018-02-06 14:08:13 Victor Ren>
;; Time-stamp: <2018-02-07 18:11:06 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring
;; Version: 0.9.9
;; X-URL: http://www.emacswiki.org/emacs/Iedit
;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x, 25.x

;; This file is not part of GNU Emacs, but it is distributed under
;; the same terms as GNU Emacs.
Expand Down Expand Up @@ -85,6 +85,13 @@ mode, set it as nil."
:type 'integer
:group 'iedit)

(defcustom iedit-index-update-limit 200
"If the number of occurrence is great than this, the
`iedit-occurrence-index' will not be updated to avoid the
traverse of the long `iedit-occurrences-overlays' list."
:type 'integer
:group 'iedit)

(defvar iedit-occurrences-overlays nil
"The occurrences slot contains a list of overlays used to
indicate the position of each editable occurrence. In addition, the
Expand Down Expand Up @@ -141,6 +148,11 @@ is not applied to other occurrences when it is true.")
(defvar iedit-occurrence-context-lines 1
"The number of lines before or after the occurrence.")

(defvar iedit-occurrence-index 0
"The index of the current occurrence, counted from the beginning of the buffer.
Used in mode-line to indicate the position of the current
occurrence.")

(make-variable-buffer-local 'iedit-occurrences-overlays)
(make-variable-buffer-local 'iedit-read-only-occurrences-overlays)
(make-variable-buffer-local 'iedit-unmatched-lines-invisible)
Expand All @@ -153,6 +165,7 @@ is not applied to other occurrences when it is true.")
(make-variable-buffer-local 'iedit-buffering)
(make-variable-buffer-local 'iedit-post-undo-hook-installed)
(make-variable-buffer-local 'iedit-occurrence-context-lines)
(make-variable-buffer-local 'iedit-occurrence-index)

(defconst iedit-occurrence-overlay-name 'iedit-occurrence-overlay-name)
(defconst iedit-invisible-overlay-name 'iedit-invisible-overlay-name)
Expand Down Expand Up @@ -277,8 +290,21 @@ Return the number of occurrences."
(push (iedit-make-occurrence-overlay beginning ending)
iedit-occurrences-overlays))
(setq counter (1+ counter))))))
(iedit-update-index)
counter))

(defun iedit-update-index (&optional point)
"Update `iedit-occurrence-index' with the current occurrence,
if the total number of occurrences is less than
`iedit-index-update-limit'."
(if (< (length iedit-occurrences-overlays) iedit-index-update-limit)
(let ((pos (or point (point)))
(index 0))
(dolist (occurrence iedit-occurrences-overlays)
(if (>= pos (overlay-start occurrence))
(setq index (1+ index))))
(setq iedit-occurrence-index index))))

(defun iedit-add-next-occurrence-overlay (occurrence-exp &optional point)
"Create next occurrence overlay for `occurrence-exp'."
(iedit-add-occurrence-overlay occurrence-exp point t))
Expand Down Expand Up @@ -307,6 +333,7 @@ Return the start position of the new occurrence if successful."
(push (iedit-make-occurrence-overlay (match-beginning 0)
(match-end 0))
iedit-occurrences-overlays)
(iedit-update-index point)
(message "Add one match for \"%s\"." (iedit-printable occurrence-exp))
(when iedit-unmatched-lines-invisible
(iedit-show-all)
Expand All @@ -331,6 +358,7 @@ there are."
(error "Conflict region"))
(push (iedit-make-occurrence-overlay beg end)
iedit-occurrences-overlays)
(iedit-update-index)
)) ;; todo test this function

(defun iedit-cleanup ()
Expand Down Expand Up @@ -501,6 +529,7 @@ beginning of the buffer."
(setq iedit-forward-success t)
(message "Located the first occurrence."))))
(when iedit-forward-success
(iedit-update-index pos)
(goto-char pos))))

(defun iedit-prev-occurrence ()
Expand Down Expand Up @@ -530,13 +559,15 @@ the buffer."
(message "Located the last occurrence.")))
(setq iedit-forward-success t))
(when iedit-forward-success
(iedit-update-index pos)
(goto-char pos))))

(defun iedit-goto-first-occurrence ()
"Move to the first occurrence."
(interactive)
(goto-char (iedit-first-occurrence))
(setq iedit-forward-success t)
(setq iedit-occurrence-index 1)
(message "Located the first occurrence."))

(defun iedit-first-occurrence ()
Expand All @@ -551,6 +582,7 @@ the buffer."
(interactive)
(goto-char (iedit-last-occurrence))
(setq iedit-forward-success t)
(setq iedit-occurrence-index (length iedit-occurrences-overlays))
(message "Located the last occurrence."))

(defun iedit-last-occurrence ()
Expand Down Expand Up @@ -902,7 +934,8 @@ Return nil if occurrence string is empty string."
(dolist (overlay iedit-occurrences-overlays)
(if (overlay-buffer overlay)
(push overlay overlays)))
(setq iedit-occurrences-overlays overlays)))
(setq iedit-occurrences-overlays overlays)
(iedit-update-index)))

(defun iedit-printable (string)
"Return a omitted substring that is not longer than 50.
Expand Down
11 changes: 10 additions & 1 deletion iedit-tests.el
Expand Up @@ -2,7 +2,7 @@

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

;; Time-stamp: <2017-09-14 23:49:18 Victor Ren>
;; Time-stamp: <2018-02-07 18:08:22 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Version: 0.97
;; X-URL: http://www.emacswiki.org/emacs/Iedit
Expand Down Expand Up @@ -275,23 +275,32 @@ fob")))))
(lambda ()
(iedit-goto-last-occurrence)
(should (= (point) 24))
(should (= iedit-occurrence-index 3))
(iedit-goto-first-occurrence)
(should (= (point) 1))
(should (= iedit-occurrence-index 1))
(iedit-next-occurrence)
(should (= (point) 7))
(should (= iedit-occurrence-index 2))
(iedit-next-occurrence)
(should (= (point) 24))
(should (= iedit-occurrence-index 3))
(iedit-next-occurrence)
(should (= (point) 24)) ;; (should (string= (current-message) "This is the last occurrence."))
(should (= iedit-occurrence-index 3))
(iedit-next-occurrence)
(should (= (point) 1)) ;; (should (string= (current-message) "Located the first occurrence."))
(should (= iedit-occurrence-index 1))
(iedit-next-occurrence)
(should (= (point) 7))
(should (= iedit-occurrence-index 2))
(goto-char (point-max))
(iedit-prev-occurrence)
(should (= (point) 27))
(should (= iedit-occurrence-index 3))
(iedit-prev-occurrence)
(should (= (point) 24))
(should (= iedit-occurrence-index 3))
(iedit-prev-occurrence)
(should (= (point) 7))
(iedit-prev-occurrence)
Expand Down
14 changes: 8 additions & 6 deletions iedit.el
Expand Up @@ -2,7 +2,7 @@

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

;; Time-stamp: <2017-09-16 21:05:28 Victor Ren>
;; Time-stamp: <2018-02-07 18:08:39 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous refactoring
;; Version: 0.9.9.9
Expand Down Expand Up @@ -167,8 +167,8 @@ use this variable:
'$%@*' will be included in the occurrences in perl mode.")

(defcustom iedit-mode-line
`(" Iedit:" (:eval (format ,(propertize "%d" 'face 'font-lock-warning-face)
(iedit-counter))))
`(" Iedit:" (:eval (format ,(propertize "%d/%d" 'face 'font-lock-warning-face)
iedit-occurrence-index (iedit-counter))))
"Mode-line format for Iedit.
This should be set before Iedit is loaded."
:group 'iedit)
Expand Down Expand Up @@ -406,11 +406,13 @@ Keymap used within overlays:
;; (message "No matches found for %s" (iedit-regexp-quote occurrence))
(iedit-done)))))

(unless (boundp 'isearch-regexp-function)
(defvaralias 'isearch-regexp-function 'isearch-word))
(defun iedit-mode-from-isearch (regexp)
"Start Iedit mode using last search string as the regexp."
(interactive
(let ((regexp (cond
((functionp isearch-regexp-function)
((functionp isearch-regexp-function)
(funcall isearch-regexp-function isearch-string))
(isearch-regexp-function (word-search-regexp isearch-string))
(isearch-regexp isearch-string)
Expand Down Expand Up @@ -519,10 +521,10 @@ Return the tag if succeeded, nil if failed."
(cl-end (progn (skip-chars-forward "[:alnum:]-_.:") (point)))
(match
(if endp
(when (sgml-skip-tag-backward 1) (forward-char 1) t)
(with-no-warnings (when (sgml-skip-tag-backward 1) (forward-char 1) t))
(with-syntax-table sgml-tag-syntax-table
(up-list -1)
(when (sgml-skip-tag-forward 1)
(with-no-warnings (when (sgml-skip-tag-forward 1))
(backward-sexp 1)
(forward-char 2)
t)))))
Expand Down

0 comments on commit 412490d

Please sign in to comment.