2013-03-21 74 views
3

如果我在emacs的dired(或x-dired)模式下按'Z',光标下的文件将被压缩或解压缩。Emacs:将dired-do-compress扩展到目录

我想将此行为扩展到目录。也就是说,如果光标在一个目录下的“东西”,我想“Z”运行

tar -zcf stuff.tgz stuff 

(对“焦油”是由OS提供的假设)。

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

回答

1

好主意。这里有一个适合我的开始:只需要在dired-compress-file中更改几行。我已经用BEGIN EDITEND EDIT注释突出显示了这些更改(对不起,如果有更好的方法突出显示差异)。我相信这不是很强大,但也许它可以指引你朝着正确的方向前进。

编辑:我应该说,下面的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")))))))) 
+0

它看起来很酷,但我正在寻找一种单线解决方案。简洁会让你很容易确认它是正确的。就像一个人收到关于gunzip变体的建议,当你按下'!'时,在压缩文件或压缩存档上,当在目录dirname上直接按下'z'时,提供运行“tar -zcf dirname.tgz dirname”的建议将是理想的。 – Calaf 2013-04-12 17:41:36

0

此功能已经安装在了主:

  • ž上目录上的* .tar.gz文件运行tar -czf dirname.tar.gz dirname
  • ž运行tar -zxvf dirname

后者可以通过dired-compress-file-suffixes定制,如果您需要使用除-zxvf以外的其他东西。