Can Emacs track files like BBEdit?

To quote a function request for Sublime Text :

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

This is much better than using ST2:
- In Sublime Text 2, open "myfile.txt"
- In Finder, rename "myfile.txt" to "myfile2.txt"
- Now saving the document to ST2 silently creates a duplicate file " myfile.txt ". This leads to two small variants of the same file in my workspace, which causes headaches later.

Similar things happen with Emacs, as with Sublime Text. So, I would like to find a way to get Emacs to do what BBEdit does.

I searched Google, but I'm not really sure what to look for here. For this there is a certain term for art? In any case, I did not find anything interesting.

Is there any existing way to do this? Or will it be quite difficult? This post says the Bookmarks feature is NSURLused here.

+4
source share
1 answer

From NEWSEmacs latest trunk file (not released)

Support for filesystem notifications .

Emacs , , . API glib API inotify ( GNU/Linux). MS-Windows Windows XP .

, , 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)))))
+6

Source: https://habr.com/ru/post/1532044/


All Articles