shosei

.emacs root

Feb 23rd, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.52 KB | None | 0 0
  1. ;; Open a file as root, easily [from Alex Schroeder]
  2.     (defvar find-file-root-prefix "/sudo:root@localhost:"
  3.       "*The filename prefix used to open a file with `find-file-root'.
  4.      This should look something like \"/sudo:root@localhost:\" (new style
  5.      TRAMP) or \"/[sudo:root@localhost]/\" (XEmacs or old style TRAMP).")
  6.  
  7.     (defvar find-file-root-history nil
  8.       "History list for files found using `find-file-root'.")
  9.  
  10.     (defvar find-file-root-hook nil
  11.       "Normal hook for functions to run after finding a \"root\" file.")
  12.  
  13.     (defun find-file-root ()
  14.       "*Open a file as the root user.
  15.      Prepends `find-file-root-prefix' to the selected file name so that it
  16.      maybe accessed via the corresponding TRAMP method."
  17.       (interactive)
  18.       (require 'tramp)
  19.       (let* (;; We bind the variable `file-name-history' locally so we can
  20.              ;; use a separate history list for "root" files.
  21.              (file-name-history find-file-root-history)
  22.              (name (or buffer-file-name default-directory))
  23.              (tramp (and (tramp-tramp-file-p name)
  24.                          (tramp-dissect-file-name name)))
  25.              path dir file)
  26.         ;; If called from a "root" file, we need to fix up the path.
  27.         (when tramp
  28.           (setq path (tramp-file-name-path tramp)
  29.                 dir (file-name-directory path)))
  30.         (when (setq file (read-file-name "Find file (UID = 0): " dir path))
  31.           (find-file (concat find-file-root-prefix file))
  32.           ;; If this all succeeded save our new history list.
  33.           (setq find-file-root-history file-name-history)
  34.           ;; allow some user customization
  35.           (run-hooks 'find-file-root-hook))))
  36.  
  37.     (defface find-file-root-header-face
  38.       '((t (:foreground "white" :background "red3")))
  39.       "*Face use to display header-lines for files opened as root.")
  40.  
  41.     (defun find-file-root-header-warning ()
  42.       "*Display a warning in header line of the current buffer.
  43.      This function is suitable to add to `find-file-root-hook'."
  44.       (let* ((warning "WARNING: EDITING FILE WITH ROOT PRIVILEGES!")
  45.              (space (+ 6 (- (frame-width) (length warning))))
  46.              (bracket (make-string (/ space 2) ?-))
  47.              (warning (concat bracket warning bracket)))
  48.         (setq header-line-format
  49.               (propertize warning 'face 'find-file-root-header-face))))
  50.  
  51.     (add-hook 'find-file-root-hook 'find-file-root-header-warning)
  52.  
  53.     (global-set-key (kbd "C-x C-S-r") 'find-file-root)
  54. ))
Advertisement
Add Comment
Please, Sign In to add comment