diff options
| author | Oleh Krehel | 2015-10-16 13:57:22 +0200 |
|---|---|---|
| committer | Oleh Krehel | 2015-10-16 14:09:37 +0200 |
| commit | 7175459da149da7e46dc799796ad670eb122273d (patch) | |
| tree | 1bfc521f46f3aa7c5ffac322e187190544f8cc42 | |
| parent | b0d190fbe2cd4a092fa186b3d674ec89ff000776 (diff) | |
| download | emacs-7175459da149da7e46dc799796ad670eb122273d.tar.gz emacs-7175459da149da7e46dc799796ad670eb122273d.zip | |
Make dired-do-compress work for *.zip files
* lisp/dired-aux.el (dired-check-process): Transform the top-level
comment into a docstring.
(dired-shell-command): New command. This mirrors
`dired-check-process', but is more user-friendly for passing
arguments.
(dired-compress-file-suffixes): Allow to specify the command switches
along with input (%i) and output (%o) inside the PROGRAM part.
Add an entry for *.zip files, and update the entry for *.tar.gz files
to the new style. Update the docstring.
(dired-compress-file): When PROGRAM matches %i or %o, use the new
logic.
(dired-update-file-line): Avoid an error when at end of buffer.
Fixes Bug#21637
| -rw-r--r-- | lisp/dired-aux.el | 85 |
1 files changed, 60 insertions, 25 deletions
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index f1f9436e8fb..7469eb96364 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el | |||
| @@ -762,12 +762,12 @@ can be produced by `dired-get-marked-files', for example." | |||
| 762 | 762 | ||
| 763 | 763 | ||
| 764 | (defun dired-check-process (msg program &rest arguments) | 764 | (defun dired-check-process (msg program &rest arguments) |
| 765 | ; "Display MSG while running PROGRAM, and check for output. | 765 | "Display MSG while running PROGRAM, and check for output. |
| 766 | ;Remaining arguments are strings passed as command arguments to PROGRAM. | 766 | Remaining arguments are strings passed as command arguments to PROGRAM. |
| 767 | ; On error, insert output | 767 | On error, insert output |
| 768 | ; in a log buffer and return the offending ARGUMENTS or PROGRAM. | 768 | in a log buffer and return the offending ARGUMENTS or PROGRAM. |
| 769 | ; Caller can cons up a list of failed args. | 769 | Caller can cons up a list of failed args. |
| 770 | ;Else returns nil for success." | 770 | Else returns nil for success." |
| 771 | (let (err-buffer err (dir default-directory)) | 771 | (let (err-buffer err (dir default-directory)) |
| 772 | (message "%s..." msg) | 772 | (message "%s..." msg) |
| 773 | (save-excursion | 773 | (save-excursion |
| @@ -785,6 +785,23 @@ can be produced by `dired-get-marked-files', for example." | |||
| 785 | (kill-buffer err-buffer) | 785 | (kill-buffer err-buffer) |
| 786 | (message "%s...done" msg) | 786 | (message "%s...done" msg) |
| 787 | nil)))) | 787 | nil)))) |
| 788 | |||
| 789 | (defun dired-shell-command (cmd) | ||
| 790 | "Run CMD, and check for output. | ||
| 791 | On error, pop up the log buffer." | ||
| 792 | (let ((out-buffer " *dired-check-process output*")) | ||
| 793 | (with-current-buffer (get-buffer-create out-buffer) | ||
| 794 | (erase-buffer) | ||
| 795 | (setq res | ||
| 796 | (process-file | ||
| 797 | shell-file-name | ||
| 798 | nil | ||
| 799 | t | ||
| 800 | nil | ||
| 801 | shell-command-switch | ||
| 802 | cmd))) | ||
| 803 | (unless (zerop res) | ||
| 804 | (pop-to-buffer out-buffer)))) | ||
| 788 | 805 | ||
| 789 | ;; Commands that delete or redisplay part of the dired buffer. | 806 | ;; Commands that delete or redisplay part of the dired buffer. |
| 790 | 807 | ||
| @@ -864,7 +881,7 @@ command with a prefix argument (the value does not matter)." | |||
| 864 | from-file))) | 881 | from-file))) |
| 865 | 882 | ||
| 866 | (defvar dired-compress-file-suffixes | 883 | (defvar dired-compress-file-suffixes |
| 867 | '(("\\.tar\\.gz" "" "tar" "-zxvf") | 884 | '(("\\.tar\\.gz\\'" "" "tar -zxvf %i") |
| 868 | ("\\.gz\\'" "" "gunzip") | 885 | ("\\.gz\\'" "" "gunzip") |
| 869 | ("\\.tgz\\'" ".tar" "gunzip") | 886 | ("\\.tgz\\'" ".tar" "gunzip") |
| 870 | ("\\.Z\\'" "" "uncompress") | 887 | ("\\.Z\\'" "" "uncompress") |
| @@ -875,16 +892,21 @@ command with a prefix argument (the value does not matter)." | |||
| 875 | ("\\.tbz\\'" ".tar" "bunzip2") | 892 | ("\\.tbz\\'" ".tar" "bunzip2") |
| 876 | ("\\.bz2\\'" "" "bunzip2") | 893 | ("\\.bz2\\'" "" "bunzip2") |
| 877 | ("\\.xz\\'" "" "unxz") | 894 | ("\\.xz\\'" "" "unxz") |
| 895 | ("\\.zip\\'" "" "unzip -o -d %o %i") | ||
| 878 | ;; This item controls naming for compression. | 896 | ;; This item controls naming for compression. |
| 879 | ("\\.tar\\'" ".tgz" nil)) | 897 | ("\\.tar\\'" ".tgz" nil)) |
| 880 | "Control changes in file name suffixes for compression and uncompression. | 898 | "Control changes in file name suffixes for compression and uncompression. |
| 881 | Each element specifies one transformation rule, and has the form: | 899 | Each element specifies one transformation rule, and has the form: |
| 882 | (REGEXP NEW-SUFFIX PROGRAM &rest ARGS) | 900 | (REGEXP NEW-SUFFIX PROGRAM) |
| 883 | The rule applies when the old file name matches REGEXP. | 901 | The rule applies when the old file name matches REGEXP. |
| 884 | The new file name is computed by deleting the part that matches REGEXP | 902 | The new file name is computed by deleting the part that matches REGEXP |
| 885 | (as well as anything after that), then adding NEW-SUFFIX in its place. | 903 | (as well as anything after that), then adding NEW-SUFFIX in its place. |
| 886 | If PROGRAM is non-nil, the rule is an uncompression rule, | 904 | If PROGRAM is non-nil, the rule is an uncompression rule, |
| 887 | and uncompression is done by running PROGRAM. | 905 | and uncompression is done by running PROGRAM. |
| 906 | |||
| 907 | Within PROGRAM, %i denotes the input file, and %o denotes the | ||
| 908 | output file. | ||
| 909 | |||
| 888 | Otherwise, the rule is a compression rule, and compression is done with gzip. | 910 | Otherwise, the rule is a compression rule, and compression is done with gzip. |
| 889 | ARGS are command switches passed to PROGRAM.") | 911 | ARGS are command switches passed to PROGRAM.") |
| 890 | 912 | ||
| @@ -895,7 +917,8 @@ Return the name of the compressed or uncompressed file. | |||
| 895 | Return nil if no change in files." | 917 | Return nil if no change in files." |
| 896 | (let ((handler (find-file-name-handler file 'dired-compress-file)) | 918 | (let ((handler (find-file-name-handler file 'dired-compress-file)) |
| 897 | suffix newname | 919 | suffix newname |
| 898 | (suffixes dired-compress-file-suffixes)) | 920 | (suffixes dired-compress-file-suffixes) |
| 921 | command) | ||
| 899 | ;; See if any suffix rule matches this file name. | 922 | ;; See if any suffix rule matches this file name. |
| 900 | (while suffixes | 923 | (while suffixes |
| 901 | (let (case-fold-search) | 924 | (let (case-fold-search) |
| @@ -910,13 +933,22 @@ Return nil if no change in files." | |||
| 910 | (funcall handler 'dired-compress-file file)) | 933 | (funcall handler 'dired-compress-file file)) |
| 911 | ((file-symlink-p file) | 934 | ((file-symlink-p file) |
| 912 | nil) | 935 | nil) |
| 913 | ((and suffix (nth 2 suffix)) | 936 | ((and suffix (setq command (nth 2 suffix))) |
| 914 | ;; We found an uncompression rule. | 937 | (if (string-match "%[io]" command) |
| 915 | (when (not (apply 'dired-check-process | 938 | (prog1 (setq newname (file-name-as-directory newname)) |
| 916 | `(,(concat "Uncompressing " file) | 939 | (dired-shell-command |
| 917 | ,@(cddr suffix) | 940 | (replace-regexp-in-string |
| 918 | ,file))) | 941 | "%o" newname |
| 919 | newname)) | 942 | (replace-regexp-in-string |
| 943 | "%i" file | ||
| 944 | command)))) | ||
| 945 | ;; We found an uncompression rule. | ||
| 946 | (when (not | ||
| 947 | (dired-check-process | ||
| 948 | (concat "Uncompressing " file) | ||
| 949 | command | ||
| 950 | file)) | ||
| 951 | newname))) | ||
| 920 | (t | 952 | (t |
| 921 | ;; We don't recognize the file as compressed, so compress it. | 953 | ;; We don't recognize the file as compressed, so compress it. |
| 922 | ;; Try gzip; if we don't have that, use compress. | 954 | ;; Try gzip; if we don't have that, use compress. |
| @@ -931,8 +963,10 @@ Return nil if no change in files." | |||
| 931 | (not | 963 | (not |
| 932 | (if (file-directory-p file) | 964 | (if (file-directory-p file) |
| 933 | (let ((default-directory (file-name-directory file))) | 965 | (let ((default-directory (file-name-directory file))) |
| 934 | (dired-check-process (concat "Compressing " file) | 966 | (dired-check-process |
| 935 | "tar" "-czf" out-name (file-name-nondirectory file))) | 967 | (concat "Compressing " file) |
| 968 | "tar" "-czf" | ||
| 969 | out-name (file-name-nondirectory file))) | ||
| 936 | (dired-check-process (concat "Compressing " file) | 970 | (dired-check-process (concat "Compressing " file) |
| 937 | "gzip" "-f" file))) | 971 | "gzip" "-f" file))) |
| 938 | (or (file-exists-p out-name) | 972 | (or (file-exists-p out-name) |
| @@ -1132,15 +1166,16 @@ See Info node `(emacs)Subdir switches' for more details." | |||
| 1132 | ;; here is faster than with dired-add-entry's optional arg). | 1166 | ;; here is faster than with dired-add-entry's optional arg). |
| 1133 | ;; Does not update other dired buffers. Use dired-relist-entry for that. | 1167 | ;; Does not update other dired buffers. Use dired-relist-entry for that. |
| 1134 | (let* ((opoint (line-beginning-position)) | 1168 | (let* ((opoint (line-beginning-position)) |
| 1135 | (char (char-after opoint)) | 1169 | (char (char-after opoint)) |
| 1136 | (buffer-read-only)) | 1170 | (buffer-read-only)) |
| 1137 | (delete-region opoint (progn (forward-line 1) (point))) | 1171 | (delete-region opoint (progn (forward-line 1) (point))) |
| 1138 | (if file | 1172 | (if file |
| 1139 | (progn | 1173 | (progn |
| 1140 | (dired-add-entry file nil t) | 1174 | (dired-add-entry file nil t) |
| 1141 | ;; Replace space by old marker without moving point. | 1175 | ;; Replace space by old marker without moving point. |
| 1142 | ;; Faster than goto+insdel inside a save-excursion? | 1176 | ;; Faster than goto+insdel inside a save-excursion? |
| 1143 | (subst-char-in-region opoint (1+ opoint) ?\040 char)))) | 1177 | (when char |
| 1178 | (subst-char-in-region opoint (1+ opoint) ?\040 char))))) | ||
| 1144 | (dired-move-to-filename)) | 1179 | (dired-move-to-filename)) |
| 1145 | 1180 | ||
| 1146 | ;;;###autoload | 1181 | ;;;###autoload |