Jelajahi Sumber

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.
Nicolas Goaziou 10 tahun lalu
induk
melakukan
f8d1d373fc
1 mengubah file dengan 13 tambahan dan 11 penghapusan
  1. 13 11
      lisp/org-src.el

+ 13 - 11
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):
-    ;;    <https://lists.gnu.org/archive/html/emacs-devel/2008-01/msg01598.html>
-    ;; 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."