【问题标题】:Emacs: extending dired-do-compress to directoriesEmacs:将 dired-do-compress 扩展到目录
【发布时间】:2013-03-21 16:42:47
【问题描述】:

如果我在 emacs 中的 dired(或 x-dired)模式下按“Z”,则光标下的文件将被压缩或未压缩。

我想将此行为扩展到目录。也就是说,如果光标位于目录“stuff”上,我希望运行“Z”

tar -zcf stuff.tgz stuff

(假设 'tar' 由操作系统提供)。

旁注: 反过来(将 stuff.tgz 扩展为完整的目录树)已经可以通过 '!' 完成,这表明对扩展的猜测。不对称('Z' 压缩和 '!' 解压缩)不会打扰我。

【问题讨论】:

    标签: emacs dired


    【解决方案1】:

    好主意。这是一个适合我的开始:只需要在dired-compress-file 中更改几行。我已经用BEGIN EDITEND EDIT cmets 突出显示了这些变化(抱歉,如果有更好的方法来突出显示差异)。我敢肯定这并不可靠,但也许它可以为您指明正确的方向。

    编辑:我应该说以下在 GNU Emacs 24.3.1 中对我有用。

    (defun dired-compress-file (file)
      ;; Compress or uncompress FILE.
      ;; Return the name of the compressed or uncompressed file.
      ;; Return nil if no change in files.
      (let ((handler (find-file-name-handler file 'dired-compress-file))
            suffix newname
            (suffixes dired-compress-file-suffixes))
        ;; See if any suffix rule matches this file name.
        (while suffixes
          (let (case-fold-search)
            (if (string-match (car (car suffixes)) file)
                (setq suffix (car suffixes) suffixes nil))
            (setq suffixes (cdr suffixes))))
        ;; If so, compute desired new name.
        (if suffix
            (setq newname (concat (substring file 0 (match-beginning 0))
                                  (nth 1 suffix))))
        (cond (handler
               (funcall handler 'dired-compress-file file))
              ((file-symlink-p file)
               nil)
              ((and suffix (nth 2 suffix))
               ;; We found an uncompression rule.
               (if (not (dired-check-process (concat "Uncompressing " file)
                                             (nth 2 suffix) file))
                   newname))
              (t
           ;;; We don't recognize the file as compressed, so compress it.
           ;;; Try gzip; if we don't have that, use compress.
               (condition-case nil
                   ;; BEGIN EDIT - choose the correct name if looking at a directory
                   (let ((out-name (if (file-directory-p file) (concat file ".tar.gz") (concat file ".gz"))))
                     ;; END EDIT
                         (and (or (not (file-exists-p out-name))
    
                                  (y-or-n-p
                                   (format "File %s already exists.  Really compress? "
                                           out-name)))
                              ;; BEGIN EDIT: create a tarball if we're looking at a directory
                              (not (if (file-directory-p file)
                                       (dired-check-process (concat "Compressing " file)
                                                            "tar" "-zcf" out-name file)
                                     (dired-check-process (concat "Compressing " file)
                                                          "gzip" "-f" file)))
                              ;; END EDIT
                              (or (file-exists-p out-name)
                                  (setq out-name (concat file ".z")))
                              ;; Rename the compressed file to NEWNAME
                              ;; if it hasn't got that name already.
                              (if (and newname (not (equal newname out-name)))
                                  (progn
                                    (rename-file out-name newname t)
                                    newname)
                                out-name)))
                     (file-error
                      (if (not (dired-check-process (concat "Compressing " file)
                                                    "compress" "-f" file))
                          ;; Don't use NEWNAME with `compress'.
                          (concat file ".Z"))))))))
    

    【讨论】:

    • 看起来很酷,但我正在寻找一种单行解决方案。简洁可以很容易地确认它是正确的。就像一个人在按下“!”时收到关于 gunzip 变体的建议一样。在压缩文件或压缩存档上,最好在目录 dirname 上按 dired-more 中的“Z”时提供运行“tar -zcf dirname.tgz dirname”的建议。
    【解决方案2】:

    以下是库dired-tar 的链接——它会压缩目录和文件。我已经使用 2014 年 3 月 19 日构建的最新版本的 Emacs Trunk 验证了它可以在 OSX 上运行。

    http://www.emacswiki.org/emacs/DiredTar

    【讨论】:

      【解决方案3】:

      此功能现已安装在 master 中:

      • 在目录上按 Z 以运行tar -czf dirname.tar.gz dirname
      • 在 *.tar.gz 文件上按 Z 以运行 tar -zxvf dirname

      如果您需要使用 -zxvf 以外的其他内容,可以通过 dired-compress-file-suffixes 自定义后者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多