Next: , Previous: , Up: System Interface   [Contents][Index]


38.19 Notifications on File Changes

Several operating systems support watching of filesystems for changes of files. If configured properly, Emacs links a respective library like gfilenotify, inotify, or w32notify statically. These libraries enable watching of filesystems on the local machine.

It is also possible to watch filesystems on remote machines, see Remote Files in The GNU Emacs Manual This does not depend on one of the libraries linked to Emacs.

Since all these libraries emit different events on notified file changes, there is the Emacs library filenotify which provides a unique interface.

Function: file-notify-add-watch file flags callback

Add a watch for filesystem events pertaining to file. This arranges for filesystem events pertaining to file to be reported to Emacs.

The returned value is a descriptor for the added watch. Its type depends on the underlying library, it cannot be assumed to be an integer as in the example below. It should be used for comparison by equal only.

If the file cannot be watched for some reason, this function signals a file-notify-error error.

Sometimes, mounted filesystems cannot be watched for file changes. This is not detected by this function, a non-nil return value does not guarantee that changes on file will be notified.

flags is a list of conditions to set what will be watched for. It can include the following symbols:

change

watch for file changes

attribute-change

watch for file attribute changes, like permissions or modification time

If file is a directory, changes for all files in that directory will be notified. This does not work recursively.

When any event happens, Emacs will call the callback function passing it a single argument event, which is of the form

(descriptor action file [file1])

descriptor is the same object as the one returned by this function. action is the description of the event. It could be any one of the following symbols:

created

file was created

deleted

file was deleted

changed

file has changed

renamed

file has been renamed to file1

attribute-changed

a file attribute was changed

file and file1 are the name of the file(s) whose event is being reported. For example:

(require 'filenotify)
     ⇒ filenotify

(defun my-notify-callback (event)
  (message "Event %S" event))
     ⇒ my-notify-callback

(file-notify-add-watch
  "/tmp" '(change attribute-change) 'my-notify-callback)
     ⇒ 35025468

(write-region "foo" nil "/tmp/foo")
     ⇒ Event (35025468 created "/tmp/.#foo")
        Event (35025468 created "/tmp/foo")
        Event (35025468 changed "/tmp/foo")
        Event (35025468 deleted "/tmp/.#foo")

(write-region "bla" nil "/tmp/foo")
     ⇒ Event (35025468 created "/tmp/.#foo")
        Event (35025468 changed "/tmp/foo") [2 times]
        Event (35025468 deleted "/tmp/.#foo")

(set-file-modes "/tmp/foo" (default-file-modes))
     ⇒ Event (35025468 attribute-changed "/tmp/foo")

Whether the action renamed is returned, depends on the used watch library. It can be expected, when a directory is watched, and both file and file1 belong to this directory. Otherwise, the actions deleted and created could be returned in a random order.

(rename-file "/tmp/foo" "/tmp/bla")
     ⇒ Event (35025468 renamed "/tmp/foo" "/tmp/bla")

(file-notify-add-watch
  "/var/tmp" '(change attribute-change) 'my-notify-callback)
     ⇒ 35025504

(rename-file "/tmp/bla" "/var/tmp/bla")
     ⇒ ;; gfilenotify
        Event (35025468 renamed "/tmp/bla" "/var/tmp/bla")

     ⇒ ;; inotify
        Event (35025504 created "/var/tmp/bla")
        Event (35025468 deleted "/tmp/bla")
Function: file-notify-rm-watch descriptor

Removes an existing file watch specified by its descriptor. descriptor should be an object returned by file-notify-add-watch.


Next: , Previous: , Up: System Interface   [Contents][Index]