区分文件与扩展名,隐藏文件和扩展名 [英] Distinguishing files with extensions, from hidden files and no extensions
问题描述
我难以将文件与扩展名区分开来,不需要扩展名的文件和隐藏的文件。我在dired模式下使用(文件扩展名(dired-get-file-for-visit))
,文件扩展名的类型决定了采取 - 例如,在Emacs中打开,或者在特定应用程序外部打开。
I'm having difficulty distinguishing files with extensions, from files without extensions, and hidden files. I'm using (file-name-extension (dired-get-file-for-visit))
in dired-mode, and the type of file extension determines what action to take -- e.g., open in Emacs, or open externally with a particular application.
隐藏文件(例如 .hidden
)返回值 nil
,而不是隐藏
。
A hidden file (e.g., .hidden
) returns a value of nil
instead of "hidden"
.
具有不扩展名的文件(例如, foo
)也返回值 nil
。
A file with no extension (e.g., foo
) also returns a value of nil
.
任何人都可以提出替代方法来处理这个问题?
Can anyone suggest an alternative method to handle this?
(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)))
***
推荐答案
(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)))))))
这篇关于区分文件与扩展名,隐藏文件和扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!