Function: denote-rename-file-and-buffer

denote-rename-file-and-buffer is a byte-compiled function defined in denote.el.

Signature

(denote-rename-file-and-buffer OLD-NAME NEW-NAME)

Documentation

Rename file named OLD-NAME to NEW-NAME, updating buffer name.

If the file exists on the file system, it is renamed. This function may be called when creating a new note and the file does not exist yet.

If a buffer is visiting the file, its name is updated.

Source Code

;; Defined in ~/.emacs.d/elpa/denote-4.2.3/denote.el
(defun denote-rename-file-and-buffer (old-name new-name)
  "Rename file named OLD-NAME to NEW-NAME, updating buffer name.

If the file exists on the file system, it is renamed.  This
function may be called when creating a new note and the file does
not exist yet.

If a buffer is visiting the file, its name is updated."
  (unless (string= (expand-file-name old-name) (expand-file-name new-name))
    (when (and (file-regular-p old-name)
               (file-writable-p new-name))
      (cond
       ((derived-mode-p 'dired-mode)
        (dired-rename-file old-name new-name nil))
       ;; NOTE 2024-02-25: The `vc-rename-file' requires the file to be
       ;; saved, but our convention is to not save the buffer after
       ;; changing front matter unless we absolutely have to (allows
       ;; users to do `diff-buffer-with-file', for example).
       ((and denote-save-buffers (not (buffer-modified-p)) (vc-backend old-name))
        (vc-rename-file old-name new-name))
       (t
        (rename-file old-name new-name nil))))
    (when-let* ((buffer (find-buffer-visiting old-name)))
      (with-current-buffer buffer
        (set-visited-file-name new-name nil t)))))