Using KDE’s kio-slaves with Emacs

Posted: November 29, 2006 in Emacs

With Emacs you can edit remote files with TRAMP. It’s really cool, but sometimes it fails because the remote host shell uses a hyper-fancy prompt. Once I had such troubles, so a replacement was needed.

I use KDE and I’m used to its kio-slaves, which enable the user to work with remote files as if they were on the local system. So I wrote an elisp function which utilize the KDE file dialog, the kio-slaves and emacsclient (only for remote files).

UPDATE: The function below was updated and includes all corrections and enhancements Noone mentioned in his comment.

(defun th-find-file-kio ()
  (let ((file-name
          "[\\n]+" ""
          (shell-command-to-string "kdialog --getopenurl ~ 2> /dev/null"))))
     ((string-match "^file://" file-name)
      ;; Work arround a bug in kioexec, which causes it to delete local
      ;; files. (See, Bug 127894.) Because of this we open the
      ;; file with `find-file' instead of emacsclient.
      (let ((local-file-name (substring file-name 7)))
        (message "Opening local file '%s'" local-file-name)
        (find-file local-file-name)))
     ((string-match "^[:space:]*$" file-name)
      (message "Empty file name given, doing nothing..."))
      (message "Opening remote file '%s'" file-name)
        (shell-command (concat "kioexec emacsclient " file-name "&")))))))

(defalias 'ffk 'th-find-file-kio)

With M-x ffk or M-x th-find-file-kio the KDE file dialog will be opened and you can choose a file, may it be local or on a remote host. Then the file will be opened. If it’s local, it will be opened in Emacs with the find-file function and you can edit it as if you opened it in Emacs directly.

If it’s a remote file the temporary file created by the kio-slave will be opened with emacsclient. Edit the file and when you’re finished type C-c g (server-edit). A dialog box will pop up and ask if the changes should be uploaded to the remote host.

  1. Noone says:

    Nice, thanks. Note that the code snippet deleted the regexp’s backslashes.

    You might want a three-clause cond rather than an if to handle empty file names. You strip trailing blanks down to an empty string, but you don’t handle the cancel button on the kdialog. That’ll return an empty string, and you call emacsclient with the empty string. Was that intentional?

    I don’t know if the form will eat this, but using cond and a clause like ((string-equal “” file-name) nil) lets cancel just fall through without the emacsclient error.

    And changing the kdialog call to:
    (format “kdialog –getopenurl %s 2> /dev/null”
    (if (buffer-file-name)
    (file-name-directory buffer-file-name)
    tries to guess a more relevant (for me) directory.

    This is a really useful function for me! Thanks!

  2. Tassilo Horn says:

    Oh, indeed, you’re absolutely right in everything. I’ll post a cleaned up version later that day.

    UPDATE: Done!

    Thanks for the feedback!

  3. Schimmi says:

    About TRAMP: switched to use the SCP mode of tramp because the remote shell made trouble. It’s a bit slower than SSH mode I guess, but at least it seems to work. Cannot use KIO slaves in Cygwin…

  4. Tassilo Horn says:

    switched to use the SCP mode of tramp because the remote shell made trouble.

    Thanks for the note, I’ll give it a try.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s