Function: magit-toggle-buffer-lock

magit-toggle-buffer-lock is an interactive and byte-compiled function defined in magit-mode.el.

Signature

(magit-toggle-buffer-lock)

Documentation

Lock the current buffer to its value or unlock it.

Locking a buffer to its value prevents it from being reused to display another value. The name of a locked buffer contains its value, which allows telling it apart from other locked buffers and the unlocked buffer.

Not all Magit buffers can be locked to their values, for example it wouldn't make sense to lock a status buffer.

There can only be a single unlocked buffer using a certain major-mode per repository. So when a buffer is being unlocked and another unlocked buffer already exists for that mode and repository, then the former buffer is instead deleted and the latter is displayed in its place.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260411.1452/magit-mode.el
;;; Buffer Lock

(defun magit-toggle-buffer-lock ()
  "Lock the current buffer to its value or unlock it.

Locking a buffer to its value prevents it from being reused to
display another value.  The name of a locked buffer contains its
value, which allows telling it apart from other locked buffers
and the unlocked buffer.

Not all Magit buffers can be locked to their values, for example
it wouldn't make sense to lock a status buffer.

There can only be a single unlocked buffer using a certain
major-mode per repository.  So when a buffer is being unlocked
and another unlocked buffer already exists for that mode and
repository, then the former buffer is instead deleted and the
latter is displayed in its place."
  (interactive)
  (cond-let
    (magit-buffer-locked-p
     (if-let ((unlocked (magit-get-mode-buffer major-mode)))
         (let ((locked (current-buffer)))
           (switch-to-buffer unlocked nil t)
           (kill-buffer locked))
       (setq magit-buffer-locked-p nil)
       (let ((name (funcall magit-generate-buffer-name-function major-mode))
             (buffer (current-buffer))
             (mode major-mode))
         (rename-buffer (generate-new-buffer-name name))
         (with-temp-buffer
           (magit--maybe-uniquify-buffer-names buffer name mode)))))
    ([value (magit-buffer-value)]
     (if-let ((locked (magit-get-mode-buffer major-mode value)))
         (let ((unlocked (current-buffer)))
           (switch-to-buffer locked nil t)
           (kill-buffer unlocked))
       (setq magit-buffer-locked-p t)
       (let ((name (funcall magit-generate-buffer-name-function
                            major-mode value))
             (buffer (current-buffer))
             (mode major-mode))
         (rename-buffer (generate-new-buffer-name name))
         (with-temp-buffer
           (magit--maybe-uniquify-buffer-names buffer name mode)))))
    ((user-error "Buffer has no value it could be locked to"))))