2013-03-21 72 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以外的其他東西。