From 0dc4c7b00573c7baa4f4c5848810599f81ac6f5c Mon Sep 17 00:00:00 2001 From: Lin Sun Date: Mon, 15 Mar 2021 09:43:54 +0800 Subject: [PATCH 2/2] [*dired] new user option 'dired-compress-file-default-suffix' *dired-aux.el: user option 'dired-compress-file-default-suffix' for default suffix for compressing file in dired-mode. --- etc/NEWS | 6 +++++ lisp/dired-aux.el | 68 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index e20beee..340538d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -641,6 +641,12 @@ This user option controls default suffix for compressing directory. If it's nil, the ".tar.gz" will be used. Refer the 'dired-compress-files-alist' for supported suffix list. ++++ +*** New user option 'dired-compress-file-default-suffix'. +This user option controls default suffix for compressing files. If it's +nil, the ".gz" will be used. Refer the 'dired-compress-file-alist' for +supported suffix list. + --- *** Broken and circular links are shown with the 'dired-broken-symlink' face. diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 237182e..c1f51a5 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -1166,6 +1166,27 @@ dired-compress-file-suffixes Otherwise, the rule is a compression rule, and compression is done with gzip. ARGS are command switches passed to PROGRAM.") +(defcustom dired-compress-file-default-suffix nil + "Default suffix for compressing single file. +If nil, the \".gz\" will be used." + :type 'string + :group 'dired + :version "28.1") + +(defvar dired-compress-file-alist + '(("\\.gz\\'" . "gzip -9f %i") + ("\\.bz2\\'" . "bzip2 -9f %i") + ("\\.xz\\'" . "xz -9f %i") + ("\\.zst\\'" . "zstd -qf -19 --rm -o %o %i")) + "Control the compression shell command for `dired-do-compress-to'. + +Each element is (REGEXP . CMD), where REGEXP is the name of the +archive to which you want to compress, and CMD is the +corresponding command. + +Within CMD, %i denotes the input file(s), and %o denotes the +output file. %i path(s) are relative, while %o is absolute.") + (defcustom dired-compress-files-default-suffix nil "Default suffix for compressing directory. If nil, the \".tar.gz\" will be used." @@ -1302,23 +1323,36 @@ dired-compress-file `dired-compress-files-default-suffix' %s, see `dired-compress-files-alist' for\ the supported suffixes list." dired-compress-files-default-suffix))) - (let ((out-name (concat file ".gz"))) - (and (or (not (file-exists-p out-name)) - (y-or-n-p - (format "File %s already exists. Really compress? " - out-name))) - (not - (dired-check-process (concat "Compressing " file) - "gzip" "-f" file)) - (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)))) + (let* ((suffix (or dired-compress-file-default-suffix ".gz")) + (out-name (concat file suffix)) + (rule (cl-find-if + (lambda (x) (string-match-p (car x) suffix)) + dired-compress-file-alist))) + (if (not rule) + (user-error "No compression rule found for suffix %s, \ +see `dired-compress-file-alist' for the supported suffixes list." + dired-compress-file-default-suffix) + (and (or (not (file-exists-p out-name)) + (y-or-n-p + (format "File %s already exists. Really compress? " + out-name))) + (dired-shell-command + (replace-regexp-in-string + "%o" (shell-quote-argument out-name) + (replace-regexp-in-string + "%i" (shell-quote-argument (file-name-nondirectory file)) + (cdr rule) + nil t) + nil t)) + (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)) -- 2.7.0