【发布时间】:2011-06-29 17:03:54
【问题描述】:
我在 elisp 中编写了一些脚本,它合并了 ls -l 和 du(显示实际文件夹大小而不是 ls 中写入的内容)。我把它命名为 lsd。这是截图:
现在我将列出实现。我不是一个好的程序员,所以我会很感激任何关于错误和可以改进的东西的信息。
lsd.el
#!/usr/bin/emacs --script
(progn
(setq argz command-line-args-left)
(setq folder "./")
(while argz
;; (message (car argz))
(if (/= ?- (aref (car argz) 0))
(setq folder (car argz)))
(setq argz (cdr argz)))
(if (/= ?/ (aref folder (1- (length folder)))) (setq folder (concat folder "/")))
(switch-to-buffer " *lsd*")
(erase-buffer)
(shell-command (concat "ls -l -h --color=always " " " (apply 'concat (mapcar '(lambda(arg) (concat arg " ")) command-line-args-left))) (current-buffer))
(switch-to-buffer " *du*")
(erase-buffer)
(shell-command (concat "du -h -d 1 " folder) (current-buffer))
(goto-char 1)
(while (search-forward "Permission denied" (point-max) t nil)
(goto-char (point-at-bol))
(let ((beg (point)))
(forward-line)
(delete-region beg (point)))) ; Remove all permission denied lines, thus show only permitted size.
(goto-char 1)
(while (and (search-forward folder (point-max) t nil) (/= (point-max) (1+ (point-at-eol)))) ; we do not need last line(the folder itself), so this line is something complex.
(setq DIR (buffer-substring (point) (point-at-eol)))
(goto-char (point-at-bol))
(setq SIZE (buffer-substring (point) (1- (search-forward " " (point-at-eol) nil nil))))
(goto-char (point-at-eol))
(switch-to-buffer " *lsd*")
(goto-char 1)
(if (search-forward DIR (point-max) t nil)
(progn
(goto-char (point-at-bol))
(search-forward-regexp "[0-9]+" (point-at-eol) nil nil)
(search-forward-regexp " *[0-9]+[^ \n]*[ \n]*" (point-at-eol) nil nil)
;; If ls have options, that makes some numbers before size column - we are doomed. (-s, for example)
(setq SIZE (concat SIZE " "))
(while (< (length SIZE) (length (match-string 0))) (setq SIZE (concat " " SIZE)))
(replace-match SIZE)))
(switch-to-buffer " *du*"))
(switch-to-buffer " *lsd*")
(message "%s" (buffer-substring (point-min) (point-max)))
(defun error(&rest args) args)
(defun message(&rest args) args)) ; Do not show any messages.
lsd (我做了这个脚本来启动emacs,除了脚本什么都不加载。如果可以更简单,请指出)
#/bin/bash
emacs -Q --script /usr/local/bin/lsd.el $@
问题来了:如何在 dired 中使用这个 lsd?
我可以将某些东西更改为使用 lsd 而不是 ls 吗?
我可以在 oldls 中重命名 ls,并制作一些 ls bash 脚本,如果没有 --lsd 标志,则将所有参数传递给 ls,如果 --lsd 在这里,则将所有参数传递给 lsd?
这是个好主意吗?
【问题讨论】:
-
呃,我发现了这个stackoverflow.com/questions/6238331/… 所以,是的,一个小问题已经消失了,但主要问题仍然存在。