如何使用Emacs Lisp检查文件是否存在? [英] How can I check if a file exists using Emacs Lisp?

查看:116
本文介绍了如何使用Emacs Lisp检查文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望emacs标记在打开时以只读方式生成的文件。我遗漏的部分难题是如何检查文件是否存在。我目前有以下几个:

I would like emacs to mark files that are generated as read-only when they're opened. The part of the puzzle that I'm missing is how to check if a file "exists". I currently have the following:

;;
;; get file extension
;;
(defun get-ext (file-name)
  (car (cdr (split-string file-name "\\."))))

;; 
;; get the base name of the file
;;
(defun base-name (file-name)
  (car (split-string file-name "\\.")))

;;
;; if an 'lzz' file exists for this header, mark it as read only
;;
(defun mark-read-only ()
  (if (string= (get-ext (cur-file)) "h")
      (if ( ??file-exists??? (concat (base-name (cur-file)) ".lzz") )
          (toggle-read-only))))

我可以用什么???文件存在????

What can I use for "???file-exists???"?

一旦我找到这个,我'添加只读标记到适当的钩子(我认为是find-file-hook)。

Once I find this, I'll add "mark-read-only" to the appropriate hook (which I think is the find-file-hook).

背景

我们使用 lzz 作为代码生成器简化我们的C / C ++开发过程。简单来说,lzz需要一个单一的输入文件(看起来很像C / C ++),并根据需要生成头文件和源文件。

We use lzz as a code generator to simplify our C/C++ development process. Briefly, lzz takes a single input file (which looks very like C/C++) and generates header and source files as appropriate.

默认情况下,lzz包括#line指令调试器指向原始源而不是生成的源,但是为了减少编译依赖关系,我们通常在头文件中禁用这些指令。结果是,当调试模板或内联函数时,调试器通常会指向生成的头文件,而不是原始源文件。

By default, lzz includes #line directives so that the debugger points to the original source and not the generated source, however, to reduce compilation dependencies we normally disable these directives in header files. The result is that when debugging templates or inline functions, the debugger normally points to the generated header file and not the original source file.

这不是一件大事,最近我发现调试时我会对显示的文件进行快速修改,然后重建。当然,这通常意味着我所做的更改消失了,因为我编辑的文件是生成的,所以更改在图书馆重建过程中被吹走。

This is not a big deal, however, recently I've found that when debugging I'll make a quick modification to the displayed file and then I'll rebuild. Of course this normally means that the change I made disappears because the file I edited is generated and so the changes are "blown away" during the library rebuild.

解决方案

感谢大家的帮助和意见。特别感谢 cobbal 指出正确使用的功能。

Thanks to everyone for their help and comments. A special thanks to cobbal for pointing out the correct function to use.

这是结果代码(根据这里的其他注释也有更新):

Here's the resulting code (with updates based on the other comments here too):

(defun cur-file ()
  "Return the filename (without directory) of the current buffer"
  (file-name-nondirectory (buffer-file-name (current-buffer)))
  )

(defun mark-generated-as-read-only ()
  "Mark generated source files as read only.
Mark generated files (lzz or gz) read only to avoid accidental updates."
  (if
      (or (string= (file-name-extension (cur-file)) "h")
          (string= (file-name-extension (cur-file)) "cpp"))
      (cond
       (
        (file-exists-p (concat (file-name-sans-extension (cur-file)) ".lzz"))
        (toggle-read-only))
       (
        (file-exists-p (concat (file-name-sans-extension (cur-file)) ".gz") )
        (toggle-read-only))
       )
    )
  )


推荐答案

尝试 file-exists-p

文件名存在(无论您是否可以阅读它)。

"Return t if file filename exists (whether or not you can read it.)"

这篇关于如何使用Emacs Lisp检查文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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