【问题标题】:Change "ls" from emacs dired to other app将“ls”从 emacs dired 更改为其他应用程序
【发布时间】:2011-06-29 17:03:54
【问题描述】:

我在 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.

lsd (我做了这个脚本来启动emacs,除了脚本什么都不加载。如果可以更简单,请指出)

#/bin/bash
emacs -Q --script /usr/local/bin/lsd.el $@

问题来了:如何在 dired 中使用这个 lsd?

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

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

这是个好主意吗?

【问题讨论】:

标签: linux emacs elisp


【解决方案1】:

在 Emacs24 中还有 `insert-directory-program' 来设置 ls 可执行文件。放

(setq insert-directory-program "/usr/local/bin/lsd")

(相应地调整路径)在您的 .emacs 或 init.el 中,dired 采用您的 lsd 脚本。

【讨论】:

    【解决方案2】:

    我不知道这是否是最有效的做事方式,我还是一个 Emacs 初学者。但我会这样做。

    1. 由于您使用的是 Linux,因此您应该首先告诉 emacs 使用其内置的 ls 仿真。初始化文件中的一个简单(需要 'ls-lisp)就足够了。
    2. 将变量 ls-lisp-use-insert-directory-program 设置为 true。这告诉 emacs 为 ls 使用外部程序。
    3. 可以通过将变量 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 仿真部分)。

    【讨论】:

    • i.imgur.com/omh8A.png 这行得通!感谢帮助。我仍然在传递参数和 --dired 选项时遇到一些麻烦(这个选项是什么意思?它在 ls 列表下给了我奇怪的可见表)。无论如何,dired 效果很好,它仍然可以打开文件或目录。而且,我应该怎么做才能分享这个脚本?当然,我自己会找到它,但获得一些有用的链接会很棒。
    • 没问题。您是否尝试通过 insert-directory-program 变量本身将参数传递给 lsd ?那可能行不通。发送开关的正确方法是设置变量 dired-listing-switches。我不确定您要如何共享脚本。如果您只是想把它放在其他人可以下载和使用的地方,我建议您查看GitHubBitbucket。它们允许您分别使用 Git 和 Mercurial 在公共存储库上托管代码。
    • 不,我不是这个意思。当 ls 和 emacs 具有相同的参数(例如,-t)时,我会遇到问题。我的脚本不能接受 -t 作为参数,因为 emacs 尝试使用它。除了制作 --sortbytime (已经制作)之外,是否有任何解决方案?我从那里偷了一些有用的东西,并使它与 lsd 一起工作。 emacswiki.org/emacs/DiredSortBySizeAndExtension 我很高兴,我什至给出了这个截图i.imgur.com/FIJLp.png 是的,按大小排序不适用于文件夹(因为 lsd 不排序),但我认为它可以修复。我去看看如何使用存储库。谢谢。
    • desudesudesu:由于stackoverflow.com/a/6807133/324105,您在使用命令行参数时遇到了问题。另请注意该答案的 cmets,另请参阅 stackoverflow.com/a/6259330/324105
    • @desudesudesu 重新定义 ls-lisp 函数来调用 lsd 会更有意义......这样您就不必每次打开目录时都启动另一个 emacs 进程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多