-;;; sudo-edit.el --- Edit files as root               -*- lexical-binding: t -*-
+;;; sudo-edit.el --- Open files as another user       -*- lexical-binding: t -*-
 
-;; Copyright (C) 2014 Nathaniel Flath <flat0103@gmail.com>
+;; Copyright (C) 2014, 2016 Nathaniel Flath <flat0103@gmail.com>
 
 ;; Author: Nathaniel Flath <flat0103@gmail.com>
 ;; URL: https://github.com/nflath/sudo-edit
+;; Keywords: convenience
 ;; Version: 0.0.1
-;; Package-Requires: ((emacs "24.4"))
+;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
 
 ;; This file is not part of GNU Emacs.
 


;;; Commentary:

;; This file provides several utility functions for opening buffers
;; as root using 'sudo'.  They are:
;;
;;     `sudo-edit'
;;

        (setq hop (string-remove-prefix tramp-prefix-format hop))
        (setq hop (string-remove-suffix tramp-postfix-host-format hop))
        (setq hop (concat hop tramp-postfix-hop-format))
        (tramp-make-tramp-file-name "sudo" user (tramp-file-name-host vec) (tramp-file-name-localname vec) hop))
    (tramp-make-tramp-file-name "sudo" user "localhost" filename)))

;;;###autoload
(defun sudo-edit (&optional arg)
  "Edit currently visited file as another user, by default `sudo-edit-user'.

With a prefix ARG prompt for a file to visit.  Will also prompt
for a file to visit if current buffer is not visiting a file."
  (interactive "P")
  (let ((user (if arg
                  (completing-read "User: " (system-users) nil nil nil 'sudo-edit-user-history sudo-edit-user)
                sudo-edit-user))
        (filename (or buffer-file-name
                      (and (derived-mode-p 'dired-mode) default-directory))))
    (cl-assert (not (string-blank-p user)) nil "User must not be a empty string")
    (if (or arg (not filename))
        (find-file (sudo-edit-filename (read-file-name (format "Find file (as \"%s\"): " user)) user))
      (let ((position (point)))
        (find-alternate-file (sudo-edit-filename filename user))
        (goto-char position)))))

(define-obsolete-function-alias 'sudo-edit-current-file 'sudo-edit)

