fix(lib): sudo-{this,save}-file: file path for indirect clones

If the buffer is an indirect clone, it may not have a buffer-file-name.

Also changes the command to throw an error if the current file has no
file path *and* is not a dired buffer.
This commit is contained in:
Henrik Lissner 2024-04-01 13:59:41 -04:00
parent ea4792ccd2
commit 392fe88ed0
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -495,16 +495,17 @@ If FORCE-P, overwrite the destination file if it exists, without confirmation."
(interactive)
(find-file
(doom--sudo-file-path
(or buffer-file-name
(or (buffer-file-name (buffer-base-buffer))
(when (or (derived-mode-p 'dired-mode)
(derived-mode-p 'wdired-mode))
default-directory)))))
default-directory)
(user-error "Cannot determine the file path of the current buffer")))))
;;;###autoload
(defun doom/sudo-save-buffer ()
"Save this file as root."
(interactive)
(let ((file (doom--sudo-file-path buffer-file-name)))
(let ((file (doom--sudo-file-path (buffer-file-name (buffer-base-buffer)))))
(if-let (buffer (find-file-noselect file))
(let ((origin (current-buffer)))
(copy-to-buffer buffer (point-min) (point-max))