Frage

I'm looking for some assistance please, to distinguish between a single file extension in dired-mode (e.g., *.gz) and a double file extension (e.g., *.tar.gz).

The following is an excerpt of the function that I use when selecting one or more files in dired-mode to take specific actions -- e.g., open in Emacs, start a process and open externally, or compress / decompress. I originally wrote this function (borrowing excerpts from dired-do-create-files within dired-aux.el) with only single file type extensions in mind, and would now like to expand its functionality to include potential double file type extensions.

(defun test-for-tar-gz-extension ()
  (interactive)
  (let* (
      (fn-list (dired-get-marked-files))
      (rfn-list (mapcar (function dired-make-relative) fn-list))
      (dired-one-file (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
      (input-filename (if dired-one-file dired-one-file fn-list))
      (ext
        (cond
          ((stringp input-filename)
            (file-name-extension input-filename))
          ((listp input-filename)
            (file-name-extension (car input-filename)))))
      (path (if (stringp input-filename) (file-name-directory input-filename)))
      (dired-buffer-name (buffer-name))
      (msword-regexp '("doc" "docx"))
      (dired-tar '("tar.gz")))
    (cond
      ;; http://www.emacswiki.org/emacs/DiredTar
      ((extension equals ".tar.gz")
        (dired-tar-pack-unpack))
      ((extension equals ".gz" (but not .tar.gz))
        (dired-do-compress))
      ((regexp-match-p msword-regexp ext)
        (start-process "ms-word" nil "open" "-a" "Microsoft Word" input-filename))
      (t
        (message "Go fish.")))))

;; https://github.com/kentaro/auto-save-buffers-enhanced
;; `regexp-match-p` function modified by @sds on stackoverflow
;; http://stackoverflow.com/a/20343715/2112489
(defun regexp-match-p (regexps string)
  (and string
       (catch 'matched
         (let ((inhibit-changing-match-data t)) ; small optimization
           (dolist (regexp regexps)
             (when (string-match regexp string)
               (throw 'matched t)))))))
War es hilfreich?

Lösung

Not sure IIUC, here a draft how to do that part in question:

(defun gz-only ()
  "List marked files in dired-buffer ending at `.gz', but not ending at `.tar.gz'"
  (interactive)
  (let ((flist (dired-get-marked-files))
        erg)
    (dolist (ele flist)
      (and (string-match "\.gz$" ele)(not (string-match "\.tar\.gz$" ele))
      (add-to-list 'erg ele)))
    (when (interactive-p) (message "%s" erg))))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top