区分单个(* .gz)和双(* .tar.gz)文件类型扩展

时间:2014-04-26 03:42:18

标签: emacs elisp dired

我正在寻求一些帮助,以区分dired-mode中的单个文件扩展名(例如*.gz)和双重文件扩展名(例如*.tar.gz)。

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

  
(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)))))))

1 个答案:

答案 0 :(得分:1)

不确定IIUC,这是一个如何做这个部分的草案:

(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))))