Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update twins in post-command-hook

  • Loading branch information...
commit ac39d78e3be71a1ed689300380346cc48635d861 1 parent 1b63a9a
@magnars authored
Showing with 44 additions and 4 deletions.
  1. +2 −0  Carton
  2. +14 −0 features/multifiles.feature
  3. +28 −4 multifiles.el
View
2  Carton
@@ -2,6 +2,8 @@
(package "multifiles" "0.0.1" "View and edit parts of multiple files in one buffer.")
+(depends-on "dash")
+
(development
(depends-on "ecukes")
(depends-on "espuds"))
View
14 features/multifiles.feature
@@ -129,3 +129,17 @@ Feature: Editing parts of multiple files in one buffer
line b
line c
"""
+
+ Scenario: Support for delete-selection-mode
+ Given I turn on delete-selection-mode
+ And I switch to buffer "test1.txt"
+ And I go to the end of the word "a"
+ And I insert "ff"
+ And I select "line b"
+ And I press "f"
+ Then I should see:
+ """
+ line aff
+ f
+ line c
+ """
View
32 multifiles.el
@@ -32,6 +32,8 @@
;;; Code:
+(require 'dash)
+
(defun mf/mirror-region-in-multifile (beg end &optional multifile-buffer)
(interactive "r")
(deactivate-mark)
@@ -42,7 +44,9 @@
(defun mf--add-mirror (buffer beg end)
(let (contents original-overlay mirror-overlay)
+ (mf--add-hook-if-necessary)
(with-current-buffer buffer
+ (mf--add-hook-if-necessary)
(setq contents (buffer-substring beg end))
(setq original-overlay (create-original-overlay beg end)))
(mf---insert-contents)
@@ -58,6 +62,18 @@
(setq end (point))
(newline 2))
+(defun mf--any-overlays-in-buffer ()
+ (--any? (memq (overlay-get it 'type) '(mf-original mf-mirror))
+ (overlays-in (point-min) (point-max))))
+
+(defun mf--add-hook-if-necessary ()
+ (unless (mf--any-overlays-in-buffer)
+ (add-hook 'post-command-hook 'mf--update-twins)))
+
+(defun mf--remove-hook-if-necessary ()
+ (unless (mf--any-overlays-in-buffer)
+ (remove-hook 'post-command-hook 'mf--update-twins)))
+
(defun create-original-overlay (beg end)
(let ((o (make-overlay beg end nil nil t)))
(overlay-put o 'type 'mf-original)
@@ -75,7 +91,8 @@
(overlay-put o 'insert-behind-hooks '(mf--on-modification))
o))
-(defvar mf--overlay-size nil)
+(defvar mf--changed-overlays nil)
+(make-variable-buffer-local 'mf--changed-overlays)
(defun mf--on-modification (o after? beg end &optional delete-length)
(when (not after?)
@@ -83,12 +100,17 @@
(mf--remove-mirror o)))
(when (and after? (not (null (overlay-start o))))
- (mf--update-twin o)))
+ (add-to-list 'mf--changed-overlays o)))
(defun mf---removed-entire-overlay ()
(and (<= beg (overlay-start o))
(>= end (overlay-end o))))
+(defun mf--update-twins ()
+ (when mf--changed-overlays
+ (-each mf--changed-overlays 'mf--update-twin)
+ (setq mf--changed-overlays nil)))
+
(defun mf--remove-mirror (o)
(let* ((twin (overlay-get o 'twin))
(original (if (mf--is-original o) o twin))
@@ -100,8 +122,10 @@
(delete-overlay mirror)
(delete-region mirror-beg mirror-end)
(goto-char mirror-beg)
- (delete-blank-lines)))
- (delete-overlay original)))
+ (delete-blank-lines)
+ (mf--remove-hook-if-necessary)))
+ (delete-overlay original)
+ (mf--remove-hook-if-necessary)))
(defun mf--is-original (o)
(equal 'mf-original (overlay-get o 'type)))
Please sign in to comment.
Something went wrong with that request. Please try again.