2010年8月11日水曜日

Emacs でファイルをsudoで開き直す。その2

 前回、root所有のファイルをsudoで自動的に開き直してくれる Emacs lisp の記事を書きました。わりと好評だったようでよかったです。
 今回、rootユーザだけでなく、他のユーザのファイルも同様に開いて編集したいとのリクエストがきたので、修正してみました。以下になります。差し替えて使ってみてください。

(defun file-other-p (filename)
"Return t if file FILENAME created by others."
(if (file-exists-p filename)
(/= (user-real-uid) (nth 2 (file-attributes filename))) t))

(defun file-username (filename)
"Return File Owner."
(if (file-exists-p filename)
(user-full-name (nth 2 (file-attributes filename)))
(user-full-name (nth 2 (file-attributes (file-name-directory filename))))))

(defun th-rename-tramp-buffer ()
(when (file-remote-p (buffer-file-name))
(rename-buffer
(format "%s:%s"
(file-remote-p (buffer-file-name) 'method)
(buffer-name)))))

(add-hook 'find-file-hook
'th-rename-tramp-buffer)

(defadvice find-file (around th-find-file activate)
"Open FILENAME using tramp's sudo method if it's read-only."
(if (and (file-other-p (ad-get-arg 0))
(not (file-writable-p (ad-get-arg 0)))
(y-or-n-p (concat "File "
(ad-get-arg 0) " is "
(if (file-exists-p (ad-get-arg 0)) "read-only." "newer file.")
" Open it as "
(file-username (ad-get-arg 0)) "? ")))
(th-find-file-sudo (ad-get-arg 0))
ad-do-it))

(defun th-find-file-sudo (file)
"Opens FILE with root privileges."
(interactive "F")
(set-buffer (find-file (concat "/sudo:"
(file-username file) "@" (system-name) ":" file))))



 これで他のユーザのファイルでもそのユーザとして開いて編集する事ができます。



2010/08/15:修正


 新規ファイルを開こうとするとエラーになってしまうのを修正した。

2010/08/16:修正


 他ユーザの新規ファイルを開けるようにした。
 再度ダウンしなおして差し替えてください。