bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch] 21.3 autorevert.el - Added dired buffer support


From: Jari Aalto+mail.emacs
Subject: [patch] 21.3 autorevert.el - Added dired buffer support
Date: Sat, 24 Jan 2004 22:41:16 +0200


2004-01-24 Sat  Jari Aalto  <address@hidden>

        * autorevert.el
        Added support to detect changed dired buffers.
        (auto-revert-active-p): New.
        (auto-revert-list-diff): New.
        (auto-revert-dired-file-list): New.
        (auto-revert-dired-changed-p): New.
        (auto-revert-handler): New.
        (auto-revert-active-p): New.
        (auto-revert-buffers): Moved logic to
        `auto-revert-handler' and `auto-revert-active-p'



Index: autorevert.el
===================================================================
RCS file: 
/cygdrive/h/data/version-control/cvsroot/emacs/gnu-emacs/lisp213/autorevert.el,v
retrieving revision 1.1.1.1
diff -u -IId: -b -w -u -r1.1.1.1 autorevert.el
--- autorevert.el       26 Jun 2003 18:06:05 -0000      1.1.1.1
+++ autorevert.el       24 Jan 2004 20:36:08 -0000
@@ -273,6 +273,81 @@
                                              'auto-revert-buffers))

     (setq auto-revert-timer nil)))

 

+(defun auto-revert-active-p ()
+  "Check if auto-revert is active (in current buffer or globally)."
+  (or auto-revert-mode
+      (and
+       global-auto-revert-mode
+       (not global-auto-revert-ignore-buffer)
+       (not (memq major-mode
+                  global-auto-revert-ignore-modes)))))
+
+(defun auto-revert-list-diff (a b)
+  "Check if strings in list A differ from list B."
+  (when (and a b)
+    (setq a (sort a 'string-lessp))
+    (setq b (sort b 'string-lessp))
+    (let (elt1 elt2)
+      (catch 'break
+        (while (and (setq elt1 (and a (pop a)))
+                    (setq elt2 (and b (pop b))))
+          (if (not (string= elt1 elt2))
+              (throw 'break t)))))))
+
+(defun auto-revert-dired-file-list ()
+  "Return list of dired files."
+  (let (list)
+    (save-excursion
+      (goto-char (point-min))
+      (while (not (eobp))
+        (if (setq file (dired-get-filename t t))
+            (push file list))
+        (forward-line 1)))
+    list))
+
+(defun auto-revert-dired-changed-p ()
+  "Check if dired buffer has changed."
+  (when (and (stringp dired-directory)
+             ;;   Exclude remote buffers, would be too slow for user
+             ;;   modem, timeouts, network lag ... all is possible
+             (not (string-match "@" dired-directory))
+             (file-directory-p dired-directory))
+    (let ((files (directory-files dired-directory))
+          (dired (auto-revert-dired-file-list)))
+      (or (not (eq (length files) (length dired)))
+          (auto-revert-list-diff files dired)))))
+
+(defun auto-revert-buffer-p ()
+  "Check if current buffer should be reverted."
+  ;;  Always include dired buffers to list. It would be too expensive
+  ;;  to test the "revert" status here each time timer launches.
+  (or (eq major-mode 'dired-mode)
+      (and (not (buffer-modified-p))
+           (if (buffer-file-name)
+               (and (file-readable-p (buffer-file-name))
+                    (not (verify-visited-file-modtime buf)))
+             (and revert-buffer-function
+                  (or (and global-auto-revert-mode
+                           global-auto-revert-non-file-buffers)
+                      auto-revert-mode))))))
+
+(defun auto-revert-handler ()
+  "Revert current buffer."
+  (let (done)
+    (cond
+     ((eq major-mode 'dired-mode)
+      ;;  Dired includes revert-buffer-function
+      (when (and revert-buffer-function
+                 (auto-revert-dired-changed-p))
+        (setq done t)
+        (revert-buffer t t t)))
+     ((or (buffer-file-name)
+          revert-buffer-function)
+      (setq done t)
+      (revert-buffer t t t)))
+    (if (and done
+             auto-revert-verbose)
+        (message "Reverting buffer `%s'." (buffer-name)))))
 
 (defun auto-revert-buffers ()
   "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
@@ -325,24 +400,9 @@
                       (memq buf auto-revert-buffer-list))
                  (setq auto-revert-buffer-list
                        (delq buf auto-revert-buffer-list)))
-             (when (and
-                    (or auto-revert-mode
-                        (and
-                         global-auto-revert-mode
-                         (not global-auto-revert-ignore-buffer)
-                         (not (memq major-mode
-                                    global-auto-revert-ignore-modes))))
-                    (not (buffer-modified-p))
-                    (if (buffer-file-name)
-                        (and (file-readable-p (buffer-file-name))
-                             (not (verify-visited-file-modtime buf)))
-                      (and revert-buffer-function
-                           (or (and global-auto-revert-mode
-                                    global-auto-revert-non-file-buffers)
-                               auto-revert-mode))))
-               (if auto-revert-verbose
-                   (message "Reverting buffer `%s'." buf))
-               (revert-buffer t t t)))
+              (when (and (auto-revert-active-p)
+                         (auto-revert-buffer-p))
+                (auto-revert-handler)))
          ;; Remove dead buffer from `auto-revert-buffer-list'.
          (setq auto-revert-buffer-list
                (delq buf auto-revert-buffer-list))))
@@ -360,6 +420,7 @@
 (unless (assq 'auto-revert-mode minor-mode-alist)
   (push '(auto-revert-mode auto-revert-mode-text)
        minor-mode-alist))
+
 (unless (assq 'global-auto-revert-mode minor-mode-alist)
   (push '(global-auto-revert-mode global-auto-revert-mode-text)
        minor-mode-alist))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]