我用elisp写了一些脚本,它合并了ls -l和du(显示实际文件夹大小而不是用ls写的东西)。我将其命名为lsd。这是屏幕截图:

http://i.imgur.com/PfSq6.png

现在,我将列出实现。我不是一个好的程序员,因此,我将感谢您提供有关错误和可以改进的更多信息。

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.

资讯系统
(我使此脚本启动emacs而不加载脚本以外的任何内容。如果可以更轻松地完成操作,请指出这一点)
#/bin/bash
emacs -Q --script /usr/local/bin/lsd.el $@

这是问题所在:如何在Dired中使用此lsd?

我可以改变某些东西以使用lsd而不是ls吗?

我是否可以在oldls中重命名ls,并制作一些ls bash脚本,如果没有--lsd标志,则将所有参数传递给ls,如果--lsd在这里,则将所有参数传递给lsd?

这是个好主意吗?

最佳答案

我不知道这是否是最有效的处理方式,但我还是Emacs初学者。但是,这就是我的做法。

  • 由于您使用的是Linux,因此应从告诉emacs使用其内置的ls仿真开始。您的init文件中一个简单的(需要'ls-lisp)就足够了。
  • 将变量ls-lisp-use-insert-directory-program设置为true。这告诉emacs将外部程序用于ls。
  • 可以通过将变量insert-directory-program设置为指向您的lsd脚本来自定义其使用的实际程序。

  • 这是如何执行此操作的示例:
    ;; 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仿真部分)。

    07-24 12:52