Skip to content
This repository has been archived by the owner on Aug 8, 2020. It is now read-only.

Commit

Permalink
Differentiate lib keymap and occurrence keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
victorhge committed Aug 24, 2012
1 parent 78ac0ea commit 6f80f88
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 58 deletions.
46 changes: 16 additions & 30 deletions iedit-lib.el
Expand Up @@ -3,7 +3,7 @@


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


;; Time-stamp: <2012-08-13 11:19:08 Victor Ren> ;; Time-stamp: <2012-08-24 09:38:53 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com> ;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring ;; Keywords: occurrence region simultaneous rectangle refactoring
;; Version: 0.97 ;; Version: 0.97
Expand All @@ -28,7 +28,7 @@


;;; Commentary: ;;; Commentary:


;; This package is APIs library that allow you to write your own minor mode. ;; This package is iedit APIs library that allow you to write your own minor mode.


;;; todo: ;;; todo:
;; - Update comments for APIs ;; - Update comments for APIs
Expand Down Expand Up @@ -70,7 +70,8 @@
"The occurrences slot contains a list of overlays used to "The occurrences slot contains a list of overlays used to
indicate the position of each occurrence. In addition, the indicate the position of each occurrence. In addition, the
occurrence overlay is used to provide a different face occurrence overlay is used to provide a different face
configurable via `iedit-occurrence-face'.") configurable via `iedit-occurrence-face'. The list is sorted by
the position of overlays.")


(defvar iedit-case-sensitive-local iedit-case-sensitive-default (defvar iedit-case-sensitive-local iedit-case-sensitive-default
"This is buffer local variable. "This is buffer local variable.
Expand Down Expand Up @@ -112,9 +113,6 @@ insertion against a zero-width occurrence.")
buffering, which means the modification to the current occurrence buffering, which means the modification to the current occurrence
is not applied to other occurrences when it is true.") is not applied to other occurrences when it is true.")


;; (defvar iedit-occurrence-keymap 'iedit-occurrence-keymap
;; "The current keymap, `iedit-occurrence-keymap'")

(defvar iedit-occurrence-context-lines 1 (defvar iedit-occurrence-context-lines 1
"The number of lines before or after the occurrence.") "The number of lines before or after the occurrence.")


Expand All @@ -127,7 +125,6 @@ is not applied to other occurrences when it is true.")
(make-variable-buffer-local 'iedit-skip-modification-once) (make-variable-buffer-local 'iedit-skip-modification-once)
(make-variable-buffer-local 'iedit-aborting) (make-variable-buffer-local 'iedit-aborting)
(make-variable-buffer-local 'iedit-buffering) (make-variable-buffer-local 'iedit-buffering)
;; (make-variable-buffer-local 'iedit-occurrence-keymap)
(make-variable-buffer-local 'iedit-occurrence-context-lines) (make-variable-buffer-local 'iedit-occurrence-context-lines)


(defconst iedit-occurrence-overlay-name 'iedit-occurrence-overlay-name) (defconst iedit-occurrence-overlay-name 'iedit-occurrence-overlay-name)
Expand Down Expand Up @@ -203,9 +200,8 @@ This is like `describe-bindings', but displays only Iedit keys."
map) map)
"Keymap used while Iedit mode is enabled.") "Keymap used while Iedit mode is enabled.")


(defvar iedit-occurrence-keymap (defvar iedit-occurrence-keymap-default
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-lib-keymap)
(define-key map (kbd "M-U") 'iedit-upcase-occurrences) (define-key map (kbd "M-U") 'iedit-upcase-occurrences)
(define-key map (kbd "M-L") 'iedit-downcase-occurrences) (define-key map (kbd "M-L") 'iedit-downcase-occurrences)
(define-key map (kbd "M-R") 'iedit-replace-occurrences) (define-key map (kbd "M-R") 'iedit-replace-occurrences)
Expand All @@ -218,10 +214,10 @@ This is like `describe-bindings', but displays only Iedit keys."
(define-key map (kbd "M->") 'iedit-last-occurrence) (define-key map (kbd "M->") 'iedit-last-occurrence)
(define-key map (kbd "C-?") 'iedit-help-for-occurrences) (define-key map (kbd "C-?") 'iedit-help-for-occurrences)
map) map)
"Keymap used within overlays in Iedit mode.") "Default keymap used within overlays.")


(defun iedit-help-for-occurrences () (defun iedit-help-for-occurrences ()
"Display `iedit-occurrence-keymap'" "Display `iedit-occurrence-keymap-default'"
(interactive) (interactive)
(message (concat (substitute-command-keys "\\[iedit-upcase-occurrences]") "/" (message (concat (substitute-command-keys "\\[iedit-upcase-occurrences]") "/"
(substitute-command-keys "\\[iedit-downcase-occurrences]") ":up/downcase " (substitute-command-keys "\\[iedit-downcase-occurrences]") ":up/downcase "
Expand All @@ -235,18 +231,20 @@ This is like `describe-bindings', but displays only Iedit keys."
(substitute-command-keys "\\[iedit-last-occurrence]") ":first/last " (substitute-command-keys "\\[iedit-last-occurrence]") ":first/last "
))) )))


(defun iedit-make-occurrences-overlays (occurrence-exp beg end) (defun iedit-make-occurrences-overlays (occurrence-exp beg end &optional keymap)
"Create occurrence overlays for `occurrence-exp' in a region. "Create occurrence overlays for `occurrence-exp' in a region.
Return the number of occurrences." Return the number of occurrences."
(setq iedit-aborting nil) (setq iedit-aborting nil)
(setq iedit-occurrences-overlays nil) (setq iedit-occurrences-overlays nil)
(unless keymap
(setq keymap iedit-occurrence-keymap-default))
;; Find and record each occurrence's markers and add the overlay to the occurrences ;; Find and record each occurrence's markers and add the overlay to the occurrences
(let ((counter 0) (let ((counter 0)
(case-fold-search (not iedit-case-sensitive-local))) (case-fold-search (not iedit-case-sensitive-local)))
(save-excursion (save-excursion
(goto-char beg) (goto-char beg)
(while (re-search-forward occurrence-exp end t) (while (re-search-forward occurrence-exp end t)
(push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0)) (push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0) keymap)
iedit-occurrences-overlays) iedit-occurrences-overlays)
(setq counter (1+ counter))) (setq counter (1+ counter)))
(message "%d matches for \"%s\"" counter (iedit-printable occurrence-exp)) (message "%d matches for \"%s\"" counter (iedit-printable occurrence-exp))
Expand All @@ -264,49 +262,37 @@ there are."
(error "No region")) (error "No region"))
(if (null iedit-occurrences-overlays) (if (null iedit-occurrences-overlays)
(push (push
(iedit-make-occurrence-overlay beg end) (iedit-make-occurrence-overlay beg end iedit-occurrence-keymap-default)
iedit-occurrences-overlays) iedit-occurrences-overlays)
(or (= (- end beg) (iedit-occurrence-string-length)) (or (= (- end beg) (iedit-occurrence-string-length))
(error "Wrong region.")) (error "Wrong region."))
(if (or (iedit-find-overlay-at-point beg 'iedit-occurrence-overlay-name) (if (or (iedit-find-overlay-at-point beg 'iedit-occurrence-overlay-name)
(iedit-find-overlay-at-point end 'iedit-occurrence-overlay-name)) (iedit-find-overlay-at-point end 'iedit-occurrence-overlay-name))
(error "Conflict region.")) (error "Conflict region."))
(push (iedit-make-occurrence-overlay beg end) (push (iedit-make-occurrence-overlay beg end iedit-occurrence-keymap-default)
iedit-occurrences-overlays) iedit-occurrences-overlays)
(sort iedit-occurrences-overlays (sort iedit-occurrences-overlays
(lambda (left right) (lambda (left right)
(< (overlay-start left) (overlay-start right)))))) ;; todo test this function (< (overlay-start left) (overlay-start right)))))) ;; todo test this function


(defun iedit-cleanup () (defun iedit-cleanup ()
"Clean up occurrence overlay, invisible overlay and local variables." "Clean up occurrence overlay, invisible overlay and local variables."
;; (when iedit-buffering
;; (iedit-stop-buffering))
;; (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-case-sensitive-global iedit-case-sensitive-local)
(remove-overlays nil nil iedit-occurrence-overlay-name t) (remove-overlays nil nil iedit-occurrence-overlay-name t)
(iedit-show-all) (iedit-show-all)
(setq iedit-occurrences-overlays nil) (setq iedit-occurrences-overlays nil)
(setq iedit-aborting nil) (setq iedit-aborting nil)
(setq iedit-before-modification-string "") (setq iedit-before-modification-string "")
(setq iedit-before-modification-undo-list nil) (setq iedit-before-modification-undo-list nil))
;; (setq iedit-mode nil)
;; (force-mode-line-update)
;; (remove-hook 'kbd-macro-termination-hook 'iedit-done t)
;; (remove-hook 'change-major-mode-hook 'iedit-done t)
;; (run-hooks 'iedit-mode-end-hook)
)


(defun iedit-make-occurrence-overlay (begin end) (defun iedit-make-occurrence-overlay (begin end keymap)
"Create an overlay for an occurrence in Iedit mode. "Create an overlay for an occurrence in Iedit mode.
Add the properties for the overlay: a face used to display a Add the properties for the overlay: a face used to display a
occurrence's default value, and modification hooks to update occurrence's default value, and modification hooks to update
occurrences if the user starts typing." occurrences if the user starts typing."
(let ((occurrence (make-overlay begin end (current-buffer) nil t))) (let ((occurrence (make-overlay begin end (current-buffer) nil t)))
(overlay-put occurrence iedit-occurrence-overlay-name t) (overlay-put occurrence iedit-occurrence-overlay-name t)
(overlay-put occurrence 'face iedit-occurrence-face) (overlay-put occurrence 'face iedit-occurrence-face)
(overlay-put occurrence 'keymap iedit-occurrence-keymap) (overlay-put occurrence 'keymap keymap)
(overlay-put occurrence 'insert-in-front-hooks '(iedit-occurrence-update)) (overlay-put occurrence 'insert-in-front-hooks '(iedit-occurrence-update))
(overlay-put occurrence 'insert-behind-hooks '(iedit-occurrence-update)) (overlay-put occurrence 'insert-behind-hooks '(iedit-occurrence-update))
(overlay-put occurrence 'modification-hooks '(iedit-occurrence-update)) (overlay-put occurrence 'modification-hooks '(iedit-occurrence-update))
Expand Down
13 changes: 7 additions & 6 deletions iedit-rect.el
Expand Up @@ -2,7 +2,7 @@


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


;; Time-stamp: <2012-08-10 16:20:22 Victor Ren> ;; Time-stamp: <2012-08-23 15:30:08 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com> ;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring ;; Keywords: occurrence region simultaneous rectangle refactoring
;; Version: 0.97 ;; Version: 0.97
Expand All @@ -28,8 +28,8 @@
;;; Commentary: ;;; Commentary:


;; This package also provides rectangle support with *visible rectangle* ;; This package also provides rectangle support with *visible rectangle*
;; highlighting, which is similar with cua mode rectangle support, but still ;; highlighting, which is similar with cua mode rectangle support. But it's
;; quite different. ;; lighter weight and uses iedit mechanisms.


;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially ;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially
;; tested on Gnu Emacs 22. If you have any compatible problem, please let me ;; tested on Gnu Emacs 22. If you have any compatible problem, please let me
Expand Down Expand Up @@ -69,14 +69,14 @@ current mode is iedit-rect. Otherwise it is nil.
;;; Define Iedit rect mode map ;;; Define Iedit rect mode map
(defvar iedit-rect-keymap (defvar iedit-rect-keymap
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-occurrence-keymap) (set-keymap-parent map iedit-occurrence-keymap-default)
(define-key map (kbd "M-K") 'iedit-kill-rectangle) (define-key map (kbd "M-K") 'iedit-kill-rectangle)
map) map)
"Keymap used within overlays in iedit-RECT mode.") "Keymap used within overlays in iedit-RECT mode.")


(or (assq 'iedit-rectangle-mode minor-mode-map-alist) (or (assq 'iedit-rectangle-mode minor-mode-map-alist)
(setq minor-mode-map-alist (setq minor-mode-map-alist
(cons (cons 'iedit-rectangle-mode iedit-rect-keymap) minor-mode-map-alist))) (cons (cons 'iedit-rectangle-mode iedit-lib-keymap) minor-mode-map-alist)))




;; Avoid to restore Iedit-rect mode when restoring desktop ;; Avoid to restore Iedit-rect mode when restoring desktop
Expand Down Expand Up @@ -115,7 +115,8 @@ current mode is iedit-rect. Otherwise it is nil.
(point)) (point))
(progn (progn
(move-to-column end-col t) (move-to-column end-col t)
(point))) (point))
iedit-rect-keymap)
iedit-occurrences-overlays) iedit-occurrences-overlays)
(forward-line 1)) (forward-line 1))
until (> (point) end)) until (> (point) end))
Expand Down
40 changes: 18 additions & 22 deletions iedit.el
Expand Up @@ -2,9 +2,9 @@


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


;; Time-stamp: <2012-08-10 11:13:42 Victor Ren> ;; Time-stamp: <2012-08-24 09:40:07 Victor Ren>
;; Author: Victor Ren <victorhge@gmail.com> ;; Author: Victor Ren <victorhge@gmail.com>
;; Keywords: occurrence region simultaneous rectangle refactoring ;; Keywords: occurrence region simultaneous refactoring
;; Version: 0.97 ;; Version: 0.97
;; X-URL: http://www.emacswiki.org/emacs/Iedit ;; 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
Expand Down Expand Up @@ -34,19 +34,15 @@
;; Normal scenario of iedit-mode is like: ;; Normal scenario of iedit-mode is like:
;; ;;
;; - Highlight certain contents - by press C-; ;; - Highlight certain contents - by press C-;
;; All occurrences of a symbol, string or a rectangle in the buffer or a ;; All occurrences of a symbol, string in the buffer or a region may be
;; region may be highlighted corresponding to current mark, point and prefix ;; highlighted corresponding to current mark, point and prefix argument.
;; argument. Refer to the document of `iedit-mode' for details. ;; Refer to the document of `iedit-mode' for details.
;; ;;
;; - Edit one of the occurrences ;; - Edit one of the occurrences
;; The change is applied to other occurrences simultaneously. ;; The change is applied to other occurrences simultaneously.
;; ;;
;; - Finish - by pressing C-; again ;; - Finish - by pressing C-; again
;; ;;
;; This package also provides rectangle support with *visible rectangle*
;; highlighting, which is similar with cua mode rectangle support, but still
;; quite different.

;; You can also use Iedit mode as a quick way to temporarily show only the ;; You can also use Iedit mode as a quick way to temporarily show only the
;; buffer lines that match the current text being edited. This gives you the ;; buffer lines that match the current text being edited. This gives you the
;; effect of a temporary `keep-lines' or `occur'. To get this effect, hit C-' ;; effect of a temporary `keep-lines' or `occur'. To get this effect, hit C-'
Expand Down Expand Up @@ -83,15 +79,8 @@
;;; Code: ;;; Code:


(eval-when-compile (require 'cl)) (eval-when-compile (require 'cl))
(require 'rect) ;; kill rectangle
(require 'iedit-lib) (require 'iedit-lib)


(defgroup iedit nil
"Edit multiple regions in the same way simultaneously."
:prefix "iedit-"
:group 'replace
:group 'convenience)

(defcustom iedit-current-symbol-default t (defcustom iedit-current-symbol-default t
"If no-nil, use current symbol by default for the occurrence." "If no-nil, use current symbol by default for the occurrence."
:type 'boolean :type 'boolean
Expand Down Expand Up @@ -138,6 +127,7 @@ Iedit mode is turned off last time.")
(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)
(make-variable-buffer-local 'iedit-last-occurrence-local) (make-variable-buffer-local 'iedit-last-occurrence-local)
(make-variable-buffer-local 'iedit-initial-string-local)


(or (assq 'iedit-mode minor-mode-alist) (or (assq 'iedit-mode minor-mode-alist)
(nconc minor-mode-alist (nconc minor-mode-alist
Expand All @@ -162,14 +152,20 @@ Iedit mode is turned off last time.")
;;; Define iedit help map. ;;; Define iedit help map.
(eval-when-compile (require 'help-macro)) (eval-when-compile (require 'help-macro))


(defvar iedit-mode-keymap (defvar iedit-mode-occurrence-keymap
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-occurrence-keymap) (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-C") 'iedit-toggle-case-sensitive) (define-key map (kbd "M-C") 'iedit-toggle-case-sensitive)
map) map)
"Keymap used within overlays in Iedit mode.") "Keymap used within overlays in Iedit mode.")


(defvar iedit-mode-keymap
(let ((map (make-sparse-keymap)))
(set-keymap-parent map iedit-lib-keymap)
(define-key map (kbd "C-'") 'iedit-toggle-unmatched-lines-visible)
map)
"Keymap used while Iedit mode is enabled.")


;;; Define Iedit mode map ;;; Define Iedit mode map
(or (assq 'iedit-mode minor-mode-map-alist) (or (assq 'iedit-mode minor-mode-map-alist)
Expand Down Expand Up @@ -220,8 +216,8 @@ as occurrence. If region active, Iedit mode is limited within
the current region. the current region.
If Iedit mode is on and region is active, Iedit mode is If Iedit mode is on and region is active, Iedit mode is
restricted in the region, e.g. the occurrences outside of the region restricted in the region, e.g. the occurrences outside of the
is excluded. region is excluded.
If Iedit mode is on and region is active, with an universal If Iedit mode is on and region is active, with an universal
prefix argument, Iedit mode is restricted outside of the region, prefix argument, Iedit mode is restricted outside of the region,
Expand Down Expand Up @@ -298,7 +294,7 @@ Commands:
(propertize (propertize
(concat " Iedit:" (concat " Iedit:"
(number-to-string (number-to-string
(iedit-make-occurrences-overlays occurrence-exp beg end))) (iedit-make-occurrences-overlays occurrence-exp beg end iedit-mode-occurrence-keymap)))
'face 'face
'font-lock-warning-face)) 'font-lock-warning-face))
(force-mode-line-update)) (force-mode-line-update))
Expand Down Expand Up @@ -415,7 +411,7 @@ Todo: how about region"
;;; iedit.el ends here ;;; iedit.el ends here


;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc ;; LocalWords: eval defgroup defcustom boolean defvar assq alist nconc
;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
Expand Down

0 comments on commit 6f80f88

Please sign in to comment.