Subject: bug#41250: 28.0.50; Dired displays unconditionally ls-switches on modeline

From: Arthur Miller <[email-masked]>
Date: Fri, 15 May 2020 21:54:50 +0200

Drew Adams <[email-masked]> writes:

> Attached is a patch that does what I think
> should be done.  The option value can be:
>
> nil - to get the current behavior
> `as-is' - show the full switches
> an integer - show first N chars of switches
> a function - show whatever it returns, when
>              passed `dired-actual-switches'

Cool. I was actually sitting and coding but you have already done it,
nice.

I have just one question/suggestion:

You first choice: indicate by name or date, else full. Does it really
need to be there? ls-switches are displayed only when dired is not
sorted by name or date, i.e. custom hence displaying ls switches as part
of mode name. Thus this customization only touches displaying of
switches when they are displayd. I.e. it should be about "how", not "when".

To explain my thought: that is a hard-coded behaviour which user can't
customize anway. By looking at your code, that bevahviour indeed
persists. 2nd choice is the one that actually consider how switches will
be displayed.

I have same consideration about 3rd choice too. Function choice gives
option to run custom hook as format. I think it is cool to have custom
format function to display when in dired mode, so I like it, but it is a
bit different purpose then regulating display of switches. As I perceive it.
Maybe it should get it's own custom variable instead? Like
dired-mode-line-display-hook or something similar?

2nd option, one with number does what the proposed variable name
suggests. Personally I ment to code just short (first switch) and long
(all switches), since probably the first one is the most important one.
I would also prefer nil to mean don't show switches at all, but it works
with N chars set to 0 as well I guess. 

Observe also that if I turn off display by using 0 chars as suggested
there will be a small gap between word "Dired" and closing parenthesis.
It will look like: (Dired ) on modeline. Not a deal breaker, but kind of
small artefact. Easily fixed though. I can rework it if you wish, but
since it is yours, you might prefer to do it yourself.

That is just my opinion, in general I think it does the jobb anyway, so
if you guys are happy, I am happy. As long as it is possible to get rid
of switches on modeline in some way, it is an improvement.

> diff -u dired.el dired-2020-05-15a-PATCHED.el
> --- dired.el	2020-05-15 11:23:32.804823800 -0700
> +++ dired-2020-05-15a-PATCHED.el	2020-05-15 11:26:20.702051000 -0700
> @@ -4114,22 +4114,40 @@
>    "Non-nil means the Dired sort command is disabled.
>  The idea is to set this buffer-locally in special Dired buffers.")
>  
> +(defcustom dired-switches-in-mode-line nil
> +  "How to indicate `dired-actual-switches' in mode-line.
> +Possible values:
> + * `nil':    Indicate name-or-date sort order, if possible.
> +             Else show full switches.
> + * `as-is':  Show full switches.
> + * Integer:  Show only the first N chars of full switches.
> + * Function: Pass `dired-actual-switches' as arg and show result."
> +  :group 'Dired-Plus
> +  :type '(choice
> +          (const    :tag "Indicate by name or date, else full"   nil)
> +          (const    :tag "Show full switches"                    as-is)
> +          (integer  :tag "Show first N chars of switches" :value 10)
> +          (function :tag "Format with function"           :value identity)))
> +
>  (defun dired-sort-set-mode-line ()
> -  ;; Set mode line display according to dired-actual-switches.
> -  ;; Mode line display of "by name" or "by date" guarantees the user a
> -  ;; match with the corresponding regexps.  Non-matching switches are
> -  ;; shown literally.
> +  "Set mode-line according to option `dired-switches-in-mode-line'."
>    (when (eq major-mode 'dired-mode)
>      (setq mode-name
> -	  (let (case-fold-search)
> -	    (cond ((string-match-p
> -		    dired-sort-by-name-regexp dired-actual-switches)
> -		   "Dired by name")
> -		  ((string-match-p
> -		    dired-sort-by-date-regexp dired-actual-switches)
> -		   "Dired by date")
> -		  (t
> -		   (concat "Dired " dired-actual-switches)))))
> +	  (let ((case-fold-search  nil))
> +            (if dired-switches-in-mode-line
> +                (concat "Dired "
> +                        (cond ((integerp dired-switches-in-mode-line)
> +                               (substring dired-actual-switches
> +                                          0 dired-switches-in-mode-line))
> +                              ((functionp dired-switches-in-mode-line)
> +                               (format "%s" (funcall dired-switches-in-mode-line
> +                                                     dired-actual-switches)))
> +                              (t dired-actual-switches)))
> +              (cond ((string-match-p dired-sort-by-name-regexp dired-actual-switches)
> +                     "Dired by name")
> +                    ((string-match-p dired-sort-by-date-regexp dired-actual-switches)
> +                     "Dired by date")
> +                    (t (concat "Dired " dired-actual-switches))))))
>      (force-mode-line-update)))
>  
>  (define-obsolete-function-alias 'dired-sort-set-modeline