Skip to content

Commit

Permalink
Read-only text handling
Browse files Browse the repository at this point in the history
Set read-only occurrence in different face

Add read-only occurrence to a separate list
Add test case for read-only  occurrence handling
  • Loading branch information
victorhge committed Dec 8, 2012
1 parent bceb12e commit 8149b66
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 35 deletions.
47 changes: 37 additions & 10 deletions iedit-lib.el
Expand Up @@ -3,7 +3,7 @@

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

;; Time-stamp: <2012-12-07 23:16:43 Victor Ren>
;; Time-stamp: <2012-12-08 23:35:32 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring
;; Version: 0.97
Expand Down Expand Up @@ -50,6 +50,11 @@
"*Face used for the occurrences' default values."
:group 'iedit)

(defface iedit-read-only-occurrence
'((t :inherit region))
"*Face used for the read-only occurrences' default values."
:group 'iedit)

(defcustom iedit-case-sensitive-default t
"If no-nil, matching is case sensitive."
:type 'boolean
Expand All @@ -70,10 +75,16 @@ mode, set it as nil."

(defvar iedit-occurrences-overlays nil
"The occurrences slot contains a list of overlays used to
indicate the position of each occurrence. In addition, the
indicate the position of each editable occurrence. In addition, the
occurrence overlay is used to provide a different face
configurable via `iedit-occurrence'. The list is sorted by
the position of overlays.") ;todo remove sort

(defvar iedit-read-only-occurrences-overlays nil
"The occurrences slot contains a list of overlays used to
indicate the position of each read-only occurrence. In addition, the
occurrence overlay is used to provide a different face
configurable via `iedit-occurrence-face'. The list is sorted by
the position of overlays.")
configurable via `iedit-ready-only-occurrence'.")

(defvar iedit-case-sensitive iedit-case-sensitive-default
"This is buffer local variable.
Expand Down Expand Up @@ -120,6 +131,7 @@ is not applied to other occurrences when it is true.")
"The number of lines before or after the 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)
(make-local-variable 'iedit-case-sensitive)
(make-variable-buffer-local 'iedit-forward-success)
Expand Down Expand Up @@ -188,17 +200,23 @@ It should be set before occurrence overlay is created.")
Return the number of occurrences."
(setq iedit-aborting nil)
(setq iedit-occurrences-overlays nil)
(setq iedit-read-only-occurrences-overlays nil)
;; 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 beg)
(while (re-search-forward occurrence-regexp end t)
(push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0))
iedit-occurrences-overlays)
(setq counter (1+ counter)))
(let ((beginning (match-beginning 0))
(ending (match-end 0)))
(if (text-property-not-all beginning ending 'read-only nil)
(push (iedit-make-read-only-occurrence-overlay beginning ending)
iedit-read-only-occurrences-overlays)
(push (iedit-make-occurrence-overlay beginning ending)
iedit-occurrences-overlays))
(setq counter (1+ counter))))
(message "%d matches for \"%s\"" counter (iedit-printable occurrence-regexp))
(when (/= 0 counter)
(when (/= 0 counter) ;todo: remove nreverse
(setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))
(if iedit-unmatched-lines-invisible
(iedit-hide-unmatched-lines iedit-occurrence-context-lines))))
Expand Down Expand Up @@ -262,6 +280,7 @@ there are."
(remove-overlays nil nil iedit-occurrence-overlay-name t)
(iedit-show-all)
(setq iedit-occurrences-overlays nil)
(setq iedit-read-only-occurrences-overlays nil)
(setq iedit-aborting nil)
(setq iedit-before-modification-string "")
(setq iedit-before-modification-undo-list nil))
Expand All @@ -280,6 +299,13 @@ occurrences if the user starts typing."
(overlay-put occurrence 'modification-hooks '(iedit-occurrence-update))
occurrence))

(defun iedit-make-read-only-occurrence-overlay (begin end)
"Create an overlay for an read-only occurrence in Iedit mode."
(let ((occurrence (make-overlay begin end (current-buffer) nil t)))
(overlay-put occurrence iedit-occurrence-overlay-name t)
(overlay-put occurrence 'face 'iedit-read-only-occurrence)
occurrence))

(defun iedit-make-unmatched-lines-overlay (begin end)
"Create an overlay for lines between two occurrences in Iedit mode."
(let ((unmatched-lines-overlay (make-overlay begin end (current-buffer) nil t)))
Expand Down Expand Up @@ -814,8 +840,9 @@ it just means mark is active."
mark-active (not (equal (mark) (point)))))

(defun iedit-barf-if-lib-active()
"Signal error if `iedit-occurrences-overlays' is not nil."
(or (null iedit-occurrences-overlays )
"Signal error if Iedit lib is active."
(or (and (null iedit-occurrences-overlays)
(null iedit-read-only-occurrences-overlays))
(error "Iedit lib is active.")))

(provide 'iedit-lib)
Expand Down
82 changes: 58 additions & 24 deletions iedit-tests.el
Expand Up @@ -2,7 +2,7 @@

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

;; Time-stamp: <2012-10-22 14:01:57 Victor Ren>
;; Time-stamp: <2012-12-08 23:40:48 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Version: 0.97
;; X-URL: http://www.emacswiki.org/emacs/Iedit
Expand Down Expand Up @@ -285,6 +285,32 @@ fob")))))
barfoo
1")))))

(ert-deftest iedit-occurrence-update-with-read-only-test ()
(with-iedit-test-fixture
"foo
foo
barfoo
foo"
(lambda ()
(iedit-mode)
(put-text-property 1 2 'read-only t)
(iedit-mode)
(goto-char 2)
(should-error (insert "1"))
(should (string= (buffer-string)
"foo
foo
barfoo
foo"))
(goto-char 7)
(insert "1")
(should (string= (buffer-string)
"foo
1foo
barfoo
1foo"))
)))

(ert-deftest iedit-aborting-test ()
(with-iedit-test-fixture
"foo
Expand Down Expand Up @@ -420,12 +446,12 @@ fob")))))
barfoo
foo"
(lambda ()
(iedit-mode)
(set-mark-command nil)
(forward-char 3)
(forward-line 3)
(iedit-rectangle-mode)
(should (equal iedit-rectangle '(1 19))))))
(iedit-mode)
(set-mark-command nil)
(forward-char 3)
(forward-line 3)
(iedit-rectangle-mode)
(should (equal iedit-rectangle '(1 19))))))

(ert-deftest iedit-kill-rectangle-error-test ()
(with-iedit-test-fixture
Expand All @@ -434,16 +460,16 @@ fob")))))
barfoo
foo"
(lambda ()
(iedit-mode)
(set-mark-command nil)
(goto-char 22)
(iedit-rectangle-mode)
(should (iedit-same-column))
(should (equal iedit-rectangle '(1 22)))
(iedit-prev-occurrence)
(delete-char -1)
(should (not (iedit-same-column)))
(should-error (iedit-kill-rectangle)))))
(iedit-mode)
(set-mark-command nil)
(goto-char 22)
(iedit-rectangle-mode)
(should (iedit-same-column))
(should (equal iedit-rectangle '(1 22)))
(iedit-prev-occurrence)
(delete-char -1)
(should (not (iedit-same-column)))
(should-error (iedit-kill-rectangle)))))

(ert-deftest iedit-kill-rectangle-test ()
(with-iedit-test-fixture
Expand Down Expand Up @@ -531,14 +557,22 @@ abcd" "12345678901234567890123456789012345678901234567890...")))
(should (string= (iedit-printable (car test)) (cadr test)))))


;; (elp-instrument-list '(insert-and-inherit
;; delete-region
;; goto-char
;; iedit-occurrence-update
;; buffer-substring-no-properties
;; string=
;; (elp-instrument-list '(;; insert-and-inherit
;; ;; delete-region
;; ;; goto-char
;; ;; iedit-occurrence-update
;; ;; buffer-substring-no-properties
;; ;; string=
;; re-search-forward
;; replace-match))
;; ;; replace-match
;; text-property-not-all
;; iedit-make-occurrence-overlay
;; nreverse
;; iedit-make-occurrences-overlays
;; match-beginning
;; match-end
;; push
;; ))


;;; iedit-tests.el ends here
2 changes: 1 addition & 1 deletion iedit.el
Expand Up @@ -2,7 +2,7 @@

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

;; Time-stamp: <2012-12-08 14:17:05 Victor Ren>
;; Time-stamp: <2012-12-08 23:57:42 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous refactoring
;; Version: 0.97
Expand Down

0 comments on commit 8149b66

Please sign in to comment.