区分具有扩展名的文件,隐藏文件和无扩展名

时间:2013-12-03 04:44:14

标签: emacs elisp dired

我很难区分文件扩展名,文件没有扩展名和隐藏文件。我在dired-mode中使用(file-name-extension (dired-get-file-for-visit)),文件扩展名的类型确定要采取的操作 - 例如,在Emacs中打开,或在外部使用特定应用程序打开。

隐藏文件(例如.hidden)会返回nil而不是"hidden"的值。

无扩展名的文件(例如foo)也会返回nil的值。

有人可以建议另一种方法来解决这个问题吗?

(let* (
    (input-regexp '("odt" "wpd" "docx" "doc" "xls" "pdf" "tif" "bmp" "jpg"))
    (input-filename (dired-get-file-for-visit)) )
  (if (not (regexp-match-p input-regexp (file-name-extension input-filename))))
    (find-file input-filename) )

;; https://github.com/kentaro/auto-save-buffers-enhanced
(defun regexp-match-p (regexps string)
  (catch 'matched
    (dolist (regexp regexps)
      (if (string-match regexp string)
        (throw 'matched t)))))

这是调试器(部分):

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("odt" nil)
  (if (string-match regexp string) (throw (quote matched) t))
  (while --dolist-tail-- (setq regexp (car --dolist-tail--)) (if (string-match regexp string) (throw (quote matched) t)) (setq --dolist-tail-- (cdr --dolist-tail--)))
  (let ((--dolist-tail-- regexps) regexp) (while --dolist-tail-- (setq regexp (car --dolist-tail--)) (if (string-match regexp string) (throw (quote matched) t)) (setq --dolist-tail-- (cdr --dolist-tail--))))
  (progn (let ((--dolist-tail-- regexps) regexp) (while --dolist-tail-- (setq regexp (car --dolist-tail--)) (if (string-match regexp string) (throw (quote matched) t)) (setq --dolist-tail-- (cdr --dolist-tail--)))))
  (catch (quote matched) (progn (let ((--dolist-tail-- regexps) regexp) (while --dolist-tail-- (setq regexp (car --dolist-tail--)) (if (string-match regexp string) (throw (quote matched) t)) (setq --dolist-tail-- (cdr --dolist-tail--))))))
  regexp-match-p(("odt" "wpd" "docx" "doc" "xls" "pdf" "tif" "bmp" "jpg") nil)
  (not (regexp-match-p input-regexp (file-name-extension input-filename)))
***

1 个答案:

答案 0 :(得分:2)

怎么样

(let* ((input-regexp '("odt" "wpd" "docx" "doc" "xls" "pdf" "tif" "bmp" "jpg"))
       (input-filename (dired-get-file-for-visit))
       (ext (file-name-extension input-filename)))
  (unless (and ext (regexp-match-p input-regexp ext))
    (find-file input-filename)))

或者,重新定义

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