Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use enhanced automatic sudo-edit from Prelude
Here I was wondering why C-x C-f C-d kept asking for my sudo password. Now I wonder no more.
- Loading branch information
Sharif Nassar
committed
Feb 3, 2016
1 parent
e26bd57
commit c5d5e15
Showing
2 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
;;; init-prelude --- Init code from Prelude | ||
;;; Commentary: | ||
;;; Bits of code I have borrowed from Prelude | ||
;;; https://github.com/bbatsov/prelude | ||
;;; License: GPLv3 | ||
|
||
|
||
;;; Code: | ||
(require 'tramp) | ||
|
||
(defun prelude-file-owner-uid (filename) | ||
"Return the UID of the FILENAME as an integer. | ||
See `file-attributes' for more info." | ||
(nth 2 (file-attributes filename 'integer))) | ||
|
||
(defun prelude-file-owned-by-user-p (filename) | ||
"Return t if file FILENAME is owned by the currently logged in user." | ||
(equal (prelude-file-owner-uid filename) | ||
(user-uid))) | ||
|
||
(defun prelude-find-alternate-file-as-root (filename) | ||
"Wraps `find-alternate-file' with opening a FILENAME as root." | ||
(find-alternate-file (concat "/sudo:root@localhost:" filename))) | ||
|
||
(defun prelude-reopen-as-root () | ||
"Find file as root if necessary." | ||
(unless (or (tramp-tramp-file-p buffer-file-name) | ||
(equal major-mode 'dired-mode) | ||
(not (file-exists-p (file-name-directory buffer-file-name))) | ||
(file-writable-p buffer-file-name) | ||
(prelude-file-owned-by-user-p buffer-file-name)) | ||
(prelude-find-alternate-file-as-root buffer-file-name))) | ||
|
||
;; I originally advised ido-find-file with code from | ||
;; http://emacsredux.com/blog/2013/04/21/edit-files-as-root/ | ||
;; But it is incomplete, and this version from Prelude is better. | ||
(add-hook 'find-file-hook 'prelude-reopen-as-root) | ||
|
||
(provide 'init-prelude) | ||
;;; init-prelude.el ends here |