2013-03-21 1 views
3

Если я нажимаю 'Z' в режиме dired (или x-dired) в emacs, файл под курсором сжимается или несжатый.Emacs: расширение dired-do-compress на каталоги

Я хотел бы распространить это поведение на каталоги. То есть, если курсор находится в директории «вещи», я хотел бы «Z», чтобы запустить

tar -zcf stuff.tgz stuff 

(в предположении, что «смола» обеспечивается ОС).

Примечание стороны: Обратное (расширение stuff.tgz в полное дерево каталогов) уже может быть сделано, что свидетельствует о догадках расширения «!». Асимметрия ('Z' для сжатия и '!' Для разжатия) не беспокоит меня.

ответ

0

Ниже приводится ссылка на библиотеку dired-tar - это гудронов и сжимает файлы и каталоги. Я проверил это работает на OSX с последней версией Emacs Магистральные построен 19 марта 2014.

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

1

Прохладный идея. Вот начало, которое работает для меня: просто нужно изменить несколько строк в dired-compress-file. Я выделил изменения с комментариями BEGIN EDIT и END EDIT (извините, если есть лучший способ выделить diff). Я уверен, что это не надежный, но, возможно, он может указать вам в правильном направлении.

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, когда один нажимает «!» в сжатом файле или в сжатом архиве было бы идеально предложить предложение запустить «tar -zcf dirname.tgz dirname», когда один нажимает «Z» в dired-больше на директорию dirname. – Calaf

0

Эта функциональность теперь устанавливается в мастера:

  • пресс Z на каталог для запуска tar -czf dirname.tar.gz dirname
  • нажмите Z на * .tar.gz файл для запуска tar -zxvf dirname

Последний может быть настроен через dired-compress-file-suffixes, если вам когда-либо понадобится что-то другое, кроме -zxvf.