3 added 181 characters in body
source | link
(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      ;; Keep text at a fixed width when redrawing.
      (format-count (format "%%%dd" (length (number-to-string count))))
      (format-text (concat "Reverting [" format-count " of " format-count "] %4d%%: %s"))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
         format-text index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )
 
(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
          index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )
 
(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      ;; Keep text at a fixed width when redrawing.
      (format-count (format "%%%dd" (length (number-to-string count))))
      (format-text (concat "Reverting [" format-count " of " format-count "] %4d%%: %s"))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message format-text index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )
2 edited body
source | link

Based on @Drew's andseranswer, with progress reporting since it can be slow with many files open.

(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
          index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )

Based on @Drew's andser, with progress reporting since it can be slow with many files open.

(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
          index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )

Based on @Drew's answer, with progress reporting since it can be slow with many files open.

(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
          index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )

1
source | link

Based on @Drew's andser, with progress reporting since it can be slow with many files open.

(defun revert-all-buffers ()
  "Refresh all open buffers from their respective files."
  (interactive)

  (message "Begin reverting all buffers...")
  (let*
    (
      ;; pairs of (filename, buf)
      (ls
        (mapcar
          (lambda (buf) (list (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))
      (count (length ls))
      (index 1)
      )
    (while ls
      (cl-destructuring-bind (filename buf) (pop ls)
        ;; Revert only buffers containing files, which are not modified;
        ;; do not try to revert non-file buffers like *Messages*.
        (message "Reverting [%3d of %3d] %4d%%: %S"
          index count (round (* 100 (/ (float index) count))) filename)
        (if (file-exists-p filename)
          ;; If the file exists, revert the buffer.
          (with-demoted-errors "Error: %S"
            (with-current-buffer buf
              (revert-buffer :ignore-auto :noconfirm))
            )
          ;; If the file doesn't exist, kill the buffer.
          (let (kill-buffer-query-functions) ; No query done when killing buffer.
            (kill-buffer buf)
            (message "Killed non-existing file buffer: %s" buf)
            )
          )
        (setq index (1+ index))
        )
      )
    (message "Finished reverting %d file buffer(s)." count)
    )
  )