private: No


;;; sudo-ext.el --- sudo support
;; Time-stamp: <2011-01-17 15:52:34 rubikitch>

;; Copyright (C) 2010  rubikitch

;; Author: rubikitch <rubikitch@ruby-lang.org>
;; Keywords: unix

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; `sudo' support in Emacs.  Currently it has one feature.
;;
;; * `sudoedit' command opens files as root using sudoedit program.
;;   This command needs emacsserver or gnuserv.
;;   Try M-x server-start or M-x gnuserv-start first.
;;   Be sure to you can run `sudoedit FILE' in shell.
;;
;;; Code:

(defgroup sudo-ext nil
  "sudo-ext"
  :group 'emacs)

(defun sudoedit-editor ()
  (cond ((and (fboundp 'server-running-p) (server-running-p)) "emacsclient")
        ((and (fboundp 'gnuserv-running-p) (gnuserv-running-p)) "gnuclient")
        (t (error (substitute-command-keys
                   "Not running server. Start server by \\[server-start] or \\[gnuserv-start]")))))

(defun sudo-process-filter (proc string)
  (when (string-match "password" string)
    (process-send-string proc (concat (read-passwd "Sudo Password: ") "\n"))))

(defun sudoedit (file)
  "Run `sudoedit FILE' to edit FILE as root.
Be sure to you can run `sudoedit FILE' in shell."
  (interactive "FSudoedit: ")
  (let ((process-environment (copy-sequence process-environment)))
    (setenv "EDITOR" (sudoedit-editor))
    (setenv "VISUAL" (sudoedit-editor))
    (with-temp-buffer
      (set-process-filter
       (start-process "sudoedit" (current-buffer) "sudoedit" file)
       'sudo-process-filter))))

(provide 'sudo-ext)
;;; sudo-ext.el ends here