开发者

How to customize dired's display

How do I make Dired display its files using an arbitrary function or set of columns? Basically I want to change from:

 -rw-r--r--  1 konrad konrad  3847863 Out 18 14:17 ClojureinAction.pdf
 -rw-rw-r--  1 tamara tamara 27338341 Out 20 07:16 Halliday, Resnick, Walker - Fundamentals of Physics.pdf
 -rw-r--r--  1开发者_Python百科 konrad konrad  3921024 Set 22 11:11 Pragmatic.Programming.Clojure.May.2009.pdf

To something like

644 1-5MB    ClojureinAction.pdf       PDF  (5 days ago, 400pgs)
664 10-100MB Halliday, Resnic...pdf    PDF  (3 days ago, 1000pgs, Tamara's)
644 1-5MB    Pragmatic.Progra...pdf    PDF  (1 min ago, 100 pages)

Thanks!


EDIT: Thanks for the answer, Gareth, but could you be more verbose, please? Apparently the hook will just allow me to run arbitrary code when the buffer loads up. Dired won't even stop loading up the buffer :(

(defun foo (&rest args) (unlocking-buffer (message "foo") (insert "foo\n")))

How to customize dired's display

Glancing at dired's source code, it seems that it gets information from these very formats I'm trying to replace, so I wonder if it's viable to change it this way, or if I'll end up having to rewrite everything.


Dired mode makes quite a few assumptions about ls output format, so changing the format will break many things. I think a more feasible approach is to leave the format alone, and use either text-properties or overlays to change the presentation.


This isn't nearly as drastic as what you're looking for, but it is possible to customize how Emacs calls ls in dired-mode.

M-x customize-variable RET dired-listing-switches RET

I used it to omit the group ID of files with the -o option, saving some horizontal screen real estate.


Write a function that transforms the output of ls into the format you want, and then add that function to dired-after-readin-hook.

Edited to add: dired-mode determines the name of the file by parsing the output of ls, so some features of dired will break when you change the format. You can make (some of) them work again by changing directory-listing-before-filename-regexp and dired-permission-flags-regexp. Since your change to the format is quite radical, you may prefer to rewrite the functions dired-move-to-filename and dired-move-to-end-of-filename.

(This sounds tough but it's not impossible. I customize the format of dired buffers in the manner I described, though not nearly so radically as you propose to do.)

You will have a lot of work if you insist on truncating the filename, because dired-mode assumes that it can get the filename on the current line by calling

(buffer-substring (dired-move-to-filename) (dired-move-to-end-of-filename))


I'd use the answer here: https://stackoverflow.com/a/1796009/714357 and the 'compile command which will run in your default-directory. So it will give you a new buffer, but it is the information you are looking for.

(defun my/ls-mode-numbers ()
  (interactive)
  (compile " ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) \
             *2^(8-i));if(k)printf(\"%0o \",k);print}'"))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜