Skip to content

Commit

Permalink
Use enhanced automatic sudo-edit from Prelude
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
12 changes: 4 additions & 8 deletions init.el
Expand Up @@ -269,9 +269,10 @@ re-downloaded in order to locate PACKAGE."
(use-package wacky-starter-kit)
;; (use-package wacky-starter-kit-js)

(use-package update-dns)
(use-package wacky-defuns)
(use-package misc)
(use-package init-prelude)


;; Many of the init-* are modified parts of https://github.com/purcell/emacs.d
;; Others just follow this pattern.
Expand Down Expand Up @@ -350,13 +351,6 @@ re-downloaded in order to locate PACKAGE."
ido-use-filename-at-point nil
ido-use-virtual-buffers t)

;; From: http://emacsredux.com/blog/2013/04/21/edit-files-as-root/
(defadvice ido-find-file (after find-file-sudo activate)
"Find file as root if necessary."
(unless (and buffer-file-name
(file-writable-p buffer-file-name))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

(use-package ido-ubiquitous
:ensure
:config (ido-ubiquitous-mode t)))
Expand Down Expand Up @@ -413,6 +407,8 @@ re-downloaded in order to locate PACKAGE."
(paradox-enable)
(setq paradox-execute-asynchronously t))))

(use-package update-dns)

(when *is-a-mac*
(use-package init-osx))

Expand Down
41 changes: 41 additions & 0 deletions lisp/init-prelude.el
@@ -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

0 comments on commit c5d5e15

Please sign in to comment.