关于emacs:区分单(*.gz)和双(*.tar.gz)文件类型扩展名

Distinguish between single (*.gz) and double (*.tar.gz) file type extensions

我正在寻求帮助,以区分 dired 模式下的单个文件扩展名(例如 *.gz)和双文件扩展名(例如 *.tar.gz)。

以下是我在 dired 模式下选择一个或多个文件以执行特定操作时使用的功能摘录 - 例如,在 Emacs 中打开、启动进程并在外部打开或压缩/解压缩。我最初编写这个函数(从 dired-aux.el 中的 dired-do-create-files 摘录)只考虑了单个文件类型扩展名,现在想扩展它的功能以包括潜在的双文件类型扩展名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(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)))))))

不确定 IIUC,这里有一个草案如何做有问题的那部分:

1
2
3
4
5
6
7
8
9
(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))))