区分带有扩展名的文件、隐藏文件和没有扩展名的文件 [英] Distinguishing files with extensions, from hidden files and no extensions

查看:27
本文介绍了区分带有扩展名的文件、隐藏文件和没有扩展名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难区分带有扩展名的文件、没有扩展名的文件和隐藏文件.我在 dired-mode 中使用 (file-name-extension (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 而不是 "hidden".

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆