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)))))
user2053036