* dired, toggle ls flags? [elisp] @ 2024-04-28 4:06 Emanuel Berg 2024-04-28 7:05 ` Emanuel Berg 2024-04-28 9:16 ` Michael Heerdegen via Emacs development discussions. 0 siblings, 2 replies; 6+ messages in thread From: Emanuel Berg @ 2024-04-28 4:06 UTC (permalink / raw) To: emacs-devel How can I hit a key, say h, to toggle the -h flag for ls in dired? Answer, like this! But question, is there already a better way to do it? Or, if you can improve what I wrote? If there isn't such a way in core Emacs, I think such a feature should be added to Emacs since it is really handy. (defun dired-ls-toggle (opt) (let ((opt-re (format "\\(^\\|[[:blank:]]\\)%s\\([[:blank:]]\\|\\b\\)" opt))) (if (string-match-p opt-re dired-listing-switches) (setq dired-listing-switches (replace-regexp-in-string opt-re " " dired-listing-switches) ) (setq dired-listing-switches (format "%s %s" dired-listing-switches opt))) (setq dired-listing-switches (string-clean-whitespace dired-listing-switches)) (dired-sort-other dired-listing-switches) )) (defun dired-toggle-human () (interactive) (dired-ls-toggle "-h") ) (defun dired-toggle-modification-sort () (interactive) (dired-ls-toggle "-t") ) -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: dired, toggle ls flags? [elisp] 2024-04-28 4:06 dired, toggle ls flags? [elisp] Emanuel Berg @ 2024-04-28 7:05 ` Emanuel Berg 2024-04-28 9:16 ` Michael Heerdegen via Emacs development discussions. 1 sibling, 0 replies; 6+ messages in thread From: Emanuel Berg @ 2024-04-28 7:05 UTC (permalink / raw) To: emacs-devel > (defun dired-ls-toggle (opt) > (let ((opt-re (format "\\(^\\|[[:blank:]]\\)%s\\([[:blank:]]\\|\\b\\)" opt))) > (if (string-match-p opt-re dired-listing-switches) > (setq dired-listing-switches > (replace-regexp-in-string opt-re " " dired-listing-switches) ) > (setq dired-listing-switches (format "%s %s" dired-listing-switches opt))) > (setq dired-listing-switches (string-clean-whitespace dired-listing-switches)) > (dired-sort-other dired-listing-switches) )) I realized the lispy way of doing it, one should first make a list from the string, then add or remove from that list, then make a new string from the list. -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: dired, toggle ls flags? [elisp] 2024-04-28 4:06 dired, toggle ls flags? [elisp] Emanuel Berg 2024-04-28 7:05 ` Emanuel Berg @ 2024-04-28 9:16 ` Michael Heerdegen via Emacs development discussions. 2024-04-28 23:25 ` Emanuel Berg 1 sibling, 1 reply; 6+ messages in thread From: Michael Heerdegen via Emacs development discussions. @ 2024-04-28 9:16 UTC (permalink / raw) To: emacs-devel Emanuel Berg <incal@dataswamp.org> writes: > How can I hit a key, say h, to toggle the -h flag for ls > in dired? > > Answer, like this! > > But question, is there already a better way to do it? IIUC, the intended way of changing any flags is C-u s. Is this good enough? We don't have a toggle for all flags. Michael. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: dired, toggle ls flags? [elisp] 2024-04-28 9:16 ` Michael Heerdegen via Emacs development discussions. @ 2024-04-28 23:25 ` Emanuel Berg 2024-04-29 6:17 ` Yuri Khan 0 siblings, 1 reply; 6+ messages in thread From: Emanuel Berg @ 2024-04-28 23:25 UTC (permalink / raw) To: emacs-devel Michael Heerdegen wrote: >> How can I hit a key, say h, to toggle the -h flag for ls >> in dired? >> >> Answer, like this! >> >> But question, is there already a better way to do it? > > IIUC, the intended way of changing any flags is C-u s. > Is this good enough? It is the general case, which one should always have. The specific case, which you do often, you want faster than that, often. However this is actually a nontrivial thing to do for the arbitrary specific case, since, well, take a look at my initial configuration (setq dired-listing-switches "-A -G -h -l -X --group-directories-first --time-style=long-iso -I \"*.meta\" -I \"#*#\" -I \"*.elc\"") And it can actually get even worse, as those initial options can be expressed -AGhlX as well! So to toggle in general some arbitrary option or option argument, one would have to parse all those cases: short options with and without arguments, and the same for long options, and then grouped short options as well. So it is a lot of work for a small gain, agreed. > We don't have a toggle for all flags. We don't have toggle at all, right? But you can set it explicitely to whatever you want using what is already there, which should be fast enough. If one wants to toggle, well, I'll use my poor man's solution for the time being at least all tho it would be interesting to do this so it would work for the entire ls(1) option and argument syntax and also in an elegant lispy way. Because not only ls use that syntax as you know, but a lot of other GNU tools - maybe one can convince oneself it isn't overengineering? Also, maybe some other part of Emacs maybe has this kind of GNU CLI parser already? -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: dired, toggle ls flags? [elisp] 2024-04-28 23:25 ` Emanuel Berg @ 2024-04-29 6:17 ` Yuri Khan 2024-04-29 11:27 ` Emanuel Berg 0 siblings, 1 reply; 6+ messages in thread From: Yuri Khan @ 2024-04-29 6:17 UTC (permalink / raw) To: emacs-devel On Mon, 29 Apr 2024 at 12:48, Emanuel Berg <incal@dataswamp.org> wrote: > (setq dired-listing-switches > "-A -G -h -l -X --group-directories-first --time-style=long-iso -I \"*.meta\" -I \"#*#\" -I \"*.elc\"") > > And it can actually get even worse, as those initial options > can be expressed -AGhlX as well! > > So to toggle in general some arbitrary option or option > argument, one would have to parse all those cases: short > options with and without arguments, and the same for long > options, and then grouped short options as well. If I had this itch (which I don’t) I think I’d approach it from the “don’t even try to parse” angle. 1. Choose a subset of switches supported by your ‘ls’. (“All of them” is a valid subset.) 2. Build a ‘transient’ exposing those. Have it fill out some reasonable data structure where each switch is a separate field. 3. From that structure, build the ‘dired-listing-switches’ string and refresh the dired buffer. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: dired, toggle ls flags? [elisp] 2024-04-29 6:17 ` Yuri Khan @ 2024-04-29 11:27 ` Emanuel Berg 0 siblings, 0 replies; 6+ messages in thread From: Emanuel Berg @ 2024-04-29 11:27 UTC (permalink / raw) To: emacs-devel Yuri Khan wrote: >> So to toggle in general some arbitrary option or option >> argument, one would have to parse all those cases: short >> options with and without arguments, and the same for long >> options, and then grouped short options as well. > > If I had this itch (which I don’t) I think I’d approach it > from the “don’t even try to parse” angle. > > 1. Choose a subset of switches supported by your ‘ls’. (“All > of them” is a valid subset.) > 2. Build a ‘transient’ exposing those. Have it fill out some > reasonable data structure where each switch is > a separate field. > 3. From that structure, build the ‘dired-listing-switches’ > string and refresh the dired buffer. Yes, I thought about this idea. It would work, and one can do that for the local config, if desired, but not really a solution to be excited about or push anywhere else. But this parsing already happens everywhere, in C, if not the tools themself wouldn't work. Surely it must happen somewhere in Lisp as well already? Either in Emacs or in some shell/CLI tool written in Lisp that use the same style. For example some shell/command interpreter written in Lisp, since it has to parse arbitrary options and arguments. C, bash, zsh, Python all have methods to do it, they are optional but one is often benefited from using them including for pretty simple possible input. They come with advantages like automated interactive documentation and also, simple input tends to grow when one adds new features. So one should look for something like that in Lisp as well! -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-04-29 11:27 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-04-28 4:06 dired, toggle ls flags? [elisp] Emanuel Berg 2024-04-28 7:05 ` Emanuel Berg 2024-04-28 9:16 ` Michael Heerdegen via Emacs development discussions. 2024-04-28 23:25 ` Emanuel Berg 2024-04-29 6:17 ` Yuri Khan 2024-04-29 11:27 ` Emanuel Berg
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.