emacsdired

Conditionally set `dired-listing-switches` locally, but `nil` remotely


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?


Solution

  • 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.