Frage

The following setting works great locally (OSX), but prevents Emacs from properly displaying directories/ files on the remote server (Linux):

(setq dired-listing-switches "-aBhl --group-directories-first")

I log in to the remote server with:

C-x C-f /ssh:user@server:/home/user/

So dired-listing-switches needs to be set locally, but nil remotely. How can this be accomplished programmatically?

War es hilfreich?

Lösung 2

If you are doing this only for interactive use of dired and not also for programmatic calls to function dired, then you do not need to (and so should probably not) advise dired.

Instead, just define a new command, my-dired, which tests its arg using file-remote-p and then calls dired using the appropriate switches. Then remap dired to your command, for key bindings. Similarly, for dired-other-window.

If you want to allow for similar prefix arg behavior to that of dired, you can do that as well. E.g., if current-prefix-arg then read and use the switches provided by the user, else do as described above: test arg and set switches accordingly.

For example (untested):

(defun my-dired (dirname &optional switches)
  "..."
  (interactive
   (let* ((dir  (if (next-read-file-uses-dialog-p)
                    (read-directory-name "Dired (directory): "
                                         nil default-directory nil)
                  (read-file-name "Dired (directory): "
                                  nil default-directory nil)))
          (sws  (if current-prefix-arg
                    (read-string "Dired listing switches: "
                                 dired-listing-switches)
                  (if (file-remote-p dir)
                      "-al"
                    "-aBhl --group-directories-first"))))
     (list dir sws)))
  (switch-to-buffer (dired-noselect dirname switches)))

Remember that advising changes the function you advise. Ask yourself whether you want to do that in general or just for interactive use of the function. If the latter, then you typically do not need or want to advise the function - just define and use a different command.

Andere Tipps

As much as I wanted to use a hook, none of the dired hooks seemed to allow this customization because dired tries to be a little lazy as to how it loads things.

Advice will allow you to switch the value of dired-listing-switches programmatically by allowing us to surround the body of the `dired' function with more code.

The first value passed into dired is the directory, so you can use file-remote-p to determine if it's a server location. You access the args to the function you are advising by using ad-get-arg <index>

(defadvice dired (around no-switches-for-server-file activate)
  "When opening dired for a remote location, use the default switches"
  (let ((dired-listing-switches (if (file-remote-p (ad-get-arg 0))
                                    "-al" ;; the default switches
                                  dired-listing-switches)))
    ad-do-it))

If this is your first encounter with function advice give the docs a read.

M-: (info "(elisp) Advising Functions") enter

I do not believe you want the variable to be nil, rather the default value. When I tried using nil, dired would give me an error. If you don't even want the default value, switch it out for an empty string.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top