gpt4 book ai didi

macos - Emacs 可以跟踪像 BBEdit 这样的文件吗?

转载 作者:行者123 更新时间:2023-12-05 00:26:16 25 4
gpt4 key购买 nike

报价 a feature request for Sublime Text :

BBEdit has this functionality on OS X:
- In BBEdit, open "myfile.txt"
- In the Finder, rename "myfile.txt" to "myfile2.txt"
- Now, in BBEdit, the document appears as "myfile2.txt", and saving the file updates "myfile2.txt"

This is much better than the ST2 use case:
- In Sublime Text 2, open "myfile.txt"
- In the Finder, rename "myfile.txt" to "myfile2.txt"
- Now, saving the document in ST2 silently creates a duplicate file "myfile.txt". This results in two slightly different versions of the same file in my workspace, causing headaches later.


Emacs 和 Sublime Text 也会发生类似的事情。所以,我很想找到一种方法让 Emacs 做 BBEdit 正在做的事情。

我在 Google 上搜索过,但实际上我不确定要在这里搜索什么。对此有特定的艺术术语吗?无论哪种方式,我都没有发现任何有趣的东西。

有没有现有的方法可以做到这一点?或者它会非常复杂? This postNSURL 的“书签”功能是这里使用的。

最佳答案

来自 NEWS最新的 Emacs 主干文件(未发布)

Support for filesystem notifications.

Emacs now supports notifications of filesystem changes, such as creation, modification, and deletion of files. This requires the `glib' API, or the 'inotify' API (on GNU/Linux systems only). On MS-Windows systems, this is supported for Windows XP and newer versions.



因此,您要求的功能对于 GNU/Linux 和 Windows 用户来说是可能的,不幸的是,似乎不支持(在 Emacs 中)在 OS X 上生成的文件通知。

下面的代码应该做你想要的(虽然它没有经过彻底的测试)。它需要最新的(未发布的)Emacs

(require 'filenotify)
(require 'cl-lib)

(defvar my-file-to-fd-hash (make-hash-table))

(defun my-file-notify-add-rename-watch (&optional file)
(let ((file-name (or file buffer-file-name)))
(when file-name
(puthash file-name
(file-notify-add-watch file-name
'(change)
'my-handle-file-change)
my-file-to-fd-hash))))

(defun my-file-notify-rm-rename-watch (&optional file)
(let* ((file-name (or file
buffer-file-name))
(fd (gethash file-name my-file-to-fd-hash)))
;; Stop watching the file
(when fd
(file-notify-rm-watch fd)
(remhash file-name my-file-to-fd-hash))))

(add-to-list 'find-file-hook 'my-file-notify-add-rename-watch)
(add-to-list 'kill-buffer-hook 'my-file-notify-rm-rename-watch)

(defun my-handle-file-change (event)
(let* ((fd (cl-first event))
(action (cl-second event))
(file (cl-third event))
(renamed-to (cl-fourth event))
(visiting-buffer (get-file-buffer file)))
;; Ignore events other than `rename` and also the `rename` events
;; generated due to emacs backing up file
(when (and (eq action 'renamed)
(not (backup-file-name-p renamed-to)))
(message (format "File %s was renamed" file))

;; If file is not open ignore the notification
(when visiting-buffer
(with-current-buffer visiting-buffer
(set-visited-file-name renamed-to))
(my-file-notify-rm-rename-watch file)
(my-file-notify-add-rename-watch renamed-to)))))

关于macos - Emacs 可以跟踪像 BBEdit 这样的文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22442267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com