Function: ediff-toggle-read-only

ediff-toggle-read-only is an interactive and byte-compiled function defined in ediff-util.el.gz.

Signature

(ediff-toggle-read-only &optional BUF)

Documentation

Toggle read-only in current buffer.

If buffer is under version control and locked, check it out first. If optional argument BUF is specified, toggle read-only in that buffer instead of the current buffer.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/ediff-util.el.gz
;; If BUF, this is the buffer to toggle, not current buffer.
(defun ediff-toggle-read-only (&optional buf)
  "Toggle read-only in current buffer.
If buffer is under version control and locked, check it out first.
If optional argument BUF is specified, toggle read-only in that buffer instead
of the current buffer."
  (interactive)
  (ediff-barf-if-not-control-buffer)
  (let ((ctl-buf (if (null buf) (current-buffer)))
	(buf-type (ediff-char-to-buftype last-command-event)))
    (or buf (ediff-recenter))
    (or buf
	(setq buf (ediff-get-buffer buf-type)))

    (ediff-with-current-buffer buf     ; eval in buf A/B/C
      (let* ((file (buffer-file-name buf))
	     (file-writable (and file
				 (file-exists-p file)
				 (file-writable-p file)))
	     (toggle-ro-cmd (cond (ediff-toggle-read-only-function)
				  ((ediff-file-checked-out-p file)
				   'read-only-mode)
				  (file-writable 'read-only-mode)
				  (t (key-binding "\C-x\C-q")))))
	;; If the file is checked in, make sure we don't make buffer modifiable
	;; without warning the user.  The user can fool our checks by making the
	;; buffer non-RO without checking the file out.  We regard this as a
	;; user problem.
	(if (and (ediff-file-checked-in-p file)
		 ;; If ctl-buf is null, this means we called this
		 ;; non-interactively, in which case don't ask questions
		 ctl-buf)
	    (cond ((not buffer-read-only)
		   (setq toggle-ro-cmd 'read-only-mode))
		  ((and (or (beep 1) t) ; always beep
			(y-or-n-p
			 (format
			  "File %s is under version control.  Check it out? "
			  (ediff-abbreviate-file-name file))))
		   (setq this-command 'ediff-toggle-read-only) ; bug#38219
		   ;; if we checked the file out, we should also change the
		   ;; original state of buffer-read-only to nil.  If we don't
		   ;; do this, the mode line will show %%, since the file was
		   ;; RO before ediff started, so the user will think the file
		   ;; is checked in.
		   (ediff-with-current-buffer ctl-buf
		     (ediff-change-saved-variable
		      'buffer-read-only nil buf-type)))
		  (t
		   (setq toggle-ro-cmd 'read-only-mode)
		   (beep 1) (beep 1)
		   (message
		    "Boy, this is risky! Don't modify this file...")
		   (sit-for 3)))) ; let the user see the warning
	(if (and toggle-ro-cmd
		 (string-match "read-only-mode" (symbol-name toggle-ro-cmd)))
	    (save-window-excursion
              (ediff-with-live-window (ediff-get-visible-buffer-window buf)
                (command-execute toggle-ro-cmd)))
	  (user-error "Don't know how to toggle read-only in buffer %S" buf))

	;; Check if we made the current buffer updatable, but its file is RO.
	;; Signal a warning in this case.
	(if (and file (not buffer-read-only)
		 (eq this-command 'ediff-toggle-read-only)
		 (file-exists-p file)
		 (not (file-writable-p file)))
	    (progn
	      (beep 1)
	      (message "Warning: file %s is read-only"
		       (ediff-abbreviate-file-name file))))
	))))