ShingoFukuyama
3/17/2014 - 3:39 AM

You can lock editable area from the region you make. #Emacs

You can lock editable area from the region you make. #Emacs

;;; Usage:

;;  Make reagion, and then M-x my-sanctuarize-region. You can only edit the region area you made.

;;  M-x my-sanctuarize-region-clear to clear the effect

(defvar my-sanctuarize-region-outer)
(defun my-sanctuarize-region ()
  (interactive)
  (if mark-active
      (let (($ov1 (make-overlay (point-min) (region-beginning)))
            ($ov2 (make-overlay (region-end) (point-max))))
        (set (make-local-variable 'my-sanctuarize-region-outer)
             (cons $ov1 $ov2))
        (overlay-put $ov1 'face 'font-lock-comment-face)
        (overlay-put $ov2 'face 'font-lock-comment-face)
        (overlay-put $ov1 'modification-hooks
                     '(idlwave-shell-comint-signal-read-only))
        (overlay-put $ov1 'insert-in-front-hooks
                     '(idlwave-shell-comint-signal-read-only))
        (overlay-put $ov2 'modification-hooks
                     '(idlwave-shell-comint-signal-read-only))
        (overlay-put $ov2 'insert-in-front-hooks
                     '(idlwave-shell-comint-signal-read-only))
        (deactivate-mark t))
  (error "You need to make region.")))

(defun my-sanctuarize-region-clear ()
  (interactive)
  (when (and (boundp 'my-sanctuarize-region-outer)
             my-sanctuarize-region-outer)
    (delete-overlay (car my-sanctuarize-region-outer))
    (delete-overlay (cdr my-sanctuarize-region-outer))
    (setq my-sanctuarize-region-outer nil)))

;; http://lists.gnu.org/archive/html/emacs-devel/2002-08/msg00428.html
(defvar idlwave-shell-save-comint-last-prompt-overlay nil)
(defun idlwave-shell-comint-signal-read-only (overlay after start end
                                                      &optional len)
  (if (and (not after)
           (or (< (overlay-start overlay) start)
               (> (overlay-end overlay) end)))
      (error "")))

(defadvice comint-output-filter (around swap-read-only activate)
  "Add a read-only equivalency to the last prompt overlay."
  ;; Caution: in Emacs <~21.2, a new overlay gets created for each
  ;; prompt... in later versions, text-properties for old prompts
  ;; are used instead, and the original overlay is recycled.  In
  ;; this case, we can advise snapshot-last-prompt to remove the
  ;; read-only *text properties* (not the overlay properties).
  ;; Here we test to ensure the prompt isn't in the same position as
  ;; the process-mark before removing the read-only overlay stuff.
  (when (and idlwave-shell-save-comint-last-prompt-overlay
             (not (equal
                   (marker-position (process-mark (get-buffer-process
                                                   (current-buffer))))
                   (overlay-end
                    idlwave-shell-save-comint-last-prompt-overlay))))
    (overlay-put idlwave-shell-save-comint-last-prompt-overlay
                 'modification-hooks nil)
    (overlay-put idlwave-shell-save-comint-last-prompt-overlay
                 'insert-in-front-hooks' nil))
  ad-do-it
  (when comint-last-prompt-overlay
    (setq idlwave-shell-save-comint-last-prompt-overlay
          comint-last-prompt-overlay)
    (overlay-put comint-last-prompt-overlay 'intangible t)
    (overlay-put comint-last-prompt-overlay 'modification-hooks
                 '(idlwave-shell-comint-signal-read-only))
    (overlay-put comint-last-prompt-overlay 'insert-in-front-hooks
                 '(idlwave-shell-comint-signal-read-only))))

(defadvice comint-snapshot-last-prompt (after remove-text-read-only activate)
  "Remove the read-only text properties potentially set by snapshot"
  (when comint-last-prompt-overlay
    (remove-text-properties
     (overlay-start comint-last-prompt-overlay)
     (overlay-end comint-last-prompt-overlay)
     '(modification-hooks nil insert-in-front-hooks nil))))