From f8d1d373fcbcbd6642555de6b2c52c55d1f318c1 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 1 May 2015 21:14:19 +0200 Subject: [PATCH] org-src: Make area being edited read-only * lisp/org-src.el (org-src--make-source-overlay): Add read-only feature to overlay. (org-edit-src-save): Adapt to change above. --- lisp/org-src.el | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 5af0b2d7b..34c590709 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -309,15 +309,14 @@ END." (let ((map (make-sparse-keymap))) (define-key map [mouse-1] 'org-edit-src-continue) map)) - ;; TODO: The below line doesn't work for two reasons: - ;; - It should be 'read-only - ;; - 'read-only apparently doesn't work on overlays (also empirically tested): - ;; - ;; If this feature is to be kept, it should be implemented via text - ;; properties, which will require fiddling around in more places - ;; (such as when the contents are copied back into the buffer after - ;; editing is complete.) - (overlay-put overlay :read-only "Leave me alone") + (let ((read-only + (list + (lambda (&rest _) + (user-error + "Cannot modify an area being edited in a dedicated buffer"))))) + (overlay-put overlay 'modification-hooks read-only) + (overlay-put overlay 'insert-in-front-hooks read-only) + (overlay-put overlay 'insert-behind-hooks read-only)) overlay)) (defun org-src--remove-overlay () @@ -872,11 +871,14 @@ Throw an error if there is no such buffer." (with-current-buffer (org-src--source-buffer) (undo-boundary) (goto-char beg) + ;; Temporarily disable read-only features of OVERLAY in order to + ;; insert new contents. + (delete-overlay overlay) (delete-region beg end) (when (org-string-nw-p edited-code) (insert edited-code)) (unless (bolp) (insert "\n")) - (move-overlay overlay beg (point)) - (save-buffer)))) + (save-buffer) + (move-overlay overlay beg (point))))) (defun org-edit-src-exit () "Kill current sub-editing buffer and return to source buffer." -- 2.11.4.GIT