我在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?
我可以更改dired中的内容以使用lsd而不是ls吗?
我可以在oldls中重命名ls,并制作一些ls bash脚本,如果没有--lsd标志,则将所有参数传递给ls,如果--lsd在这里,则将所有参数传递给lsd吗?
根本不是好主意吗?
答案 0 :(得分:2)
在Emacs24中,还有`insert-directory-program'来设置ls可执行文件。把
(setq insert-directory-program "/usr/local/bin/lsd")
(相应地调整路径),dired
会使用您的lsd
脚本。
答案 1 :(得分:1)
我不知道这是否是最有效的做事方式,我仍然是一个Emacs初学者。但是我会这样做。
以下是如何执行此操作的示例:
;; Put this in your init file
(require 'ls-lisp)
(setq ls-lisp-use-insert-directory-program T)
(setq insert-directory-program "~/path/to/lsd")
如果这对您有用,请告诉我。我在Windows上使用emacs,所以我不确定这个端口到linux的效果如何(ls仿真部分)。