unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* dired: More verbose subdir headers; show #files in subdir
@ 2016-09-09 17:50 Tino Calancha
  0 siblings, 0 replies; only message in thread
From: Tino Calancha @ 2016-09-09 17:50 UTC (permalink / raw)
  To: Emacs developers


Drew Adams define in files+ Dired header lines with information
on the number of files (see patch below).
Following shows the Emacs (1) and files+ (2) header lines for
my home directory:

1) total used in directory 87648 available 372113104
2) files 226/226 space used 87648 available 372086532

Where in X/Y, X is the number of files in the current subdir;
Y is the number of files in default-directory: in this example,
both numbers are the same because default-directory and
(dired-current-directory) are the same.
Note that the entries '.' and '..' are not counted.
If you have some directories in the listing you might try:
> i
then, another subdirectory will be added.
In my case, the header line for that subdir is:
files 0/226 space used 44K available 372086532
The 0 means that the directory is empty (i.e., just '.' and '..').

I like showing X in the Dired header line.  I think is nice if Emacs
add this feature.
I am not sure about the convenience of showing Y though.  Most of
the times i just look at X and ignore Y.  Furthermore, for Dired buffers
with just one subdir you get X/X which looks strange, specially
when you have many files.
For example, if i visit /usr/bin with Dired the header line is:
files 2349/2349 space used 376M available 41182232

Do you like adding # files info in the Dired header lines?
What is your opinion?

Tino

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
From 5fdde63547761dc2ea226b3ac6df3ffab576234c Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Sat, 10 Sep 2016 02:42:12 +0900
Subject: [PATCH] dired: More verbose subdir headers

* lisp/files (count-dired-files): New defun; return the
number of files in the current directory listing.
(insert-directory): Add in each Dired header line the number of files
in the current subdir and the total number of files in default-directory.
(update-dired-files-count): New defun; update file count in each Dired
header line.
---
  lisp/files.el | 82 
++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 75 insertions(+), 7 deletions(-)

diff --git a/lisp/files.el b/lisp/files.el
index 4bd708d..dce69a2 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -6431,6 +6431,59 @@ directory-listing-before-filename-regexp

  (defvar insert-directory-ls-version 'unknown)

+(defun count-dired-files ()
+  "Returns the number of files in the current Dired directory listing.
+This includes directory entries, as well as files, but it excludes `.'
+and `..'."
+  (save-excursion
+    (when (save-restriction (widen) (eobp)) (goto-char (1- (point))))
+    (re-search-backward "^$" nil 'to-bob)
+    (if (not (re-search-forward dired-move-to-filename-regexp nil t))
+        0
+      (let* ((beg     (line-beginning-position))
+             (end     (save-excursion (re-search-forward "^$" nil t)))
+             (dots-p  (save-excursion   ; Is `..' present?
+                        (goto-char beg)
+                        (re-search-forward
+                         (concat directory-listing-before-filename-regexp
+                                 "\\.\\./?$")
+                         end t))))
+        (if dots-p (- (count-lines beg end) 2) (count-lines beg end))))))
+
+(add-hook 'dired-after-readin-hook 'update-dired-files-count)
+(defun update-dired-files-count ()
+  "Update file count in Dired header for each directory listed."
+  (save-restriction
+    (widen)
+    (let* ((num-files      (count-dired-files))
+           (str-num-files  (number-to-string num-files)))
+      (save-excursion
+        (goto-char (point-min))
+        (while (re-search-forward "^  files \\([0-9]+\\)/\\([0-9]+\\)" 
nil t)
+          ;; No-op if the line should be hidden.
+          (unless (eq (get-text-property (match-beginning 0) 'invisible)
+                      'dired-hide-details-information)
+            (let ((buffer-read-only  nil)
+                  (map               (make-sparse-keymap)))
+              (define-key map [mouse-2] 
'dired-mouse-describe-listed-directory)
+              (define-key map "\r" 'dired-describe-listed-directory)
+              (replace-match str-num-files nil nil nil 1)
+              (replace-match (if (zerop num-files)
+                                 str-num-files
+                               (number-to-string (- (length 
(directory-files
+ 
default-directory
+                                                             nil nil t))
+                                                    2)))
+                             nil nil nil 2)
+              ;; Ignore any error, e.g. from `dired-details.el' hiding 
text.
+              (condition-case nil
+                  (add-text-properties
+                   (save-excursion (beginning-of-line) (+ 2 (point))) 
(match-end 2)
+                   `(mouse-face highlight keymap ,map
+                                help-echo "Files in subdir / files in 
default-directory"))
+                (error nil)))))
+        (set-buffer-modified-p nil)))))
+
  ;; insert-directory
  ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
  ;;   FULL-DIRECTORY-P is nil.
@@ -6706,13 +6759,28 @@ insert-directory
  	      (save-excursion
  		(goto-char beg)
  		;; First find the line to put it on.
-		(when (re-search-forward "^ *\\(total\\)" nil t)
-		  (let ((available (get-free-disk-space ".")))
-		    (when available
-		      ;; Replace "total" with "used", to avoid confusion.
-		      (replace-match "total used in directory" nil nil nil 
1)
-		      (end-of-line)
-		      (insert " available " available))))))))))
+		(while (re-search-forward "^ *\\(total\\)" nil t)
+                  (goto-char (match-beginning 1))
+                  (insert "files " (number-to-string (save-match-data
+ 
(count-dired-files)))
+                          "/" (number-to-string
+                               (- (length (directory-files 
default-directory
+                                                           nil nil t)) 
2))
+                          " ")
+                  (goto-char beg)
+                  (re-search-forward "^ *files [0-9]+/[0-9]+ \\(total\\)" 
nil t)
+                  (replace-match "space used" nil nil nil 1)
+                  (let ((available  (and (fboundp 'get-free-disk-space)
+                                         (get-free-disk-space ".")))
+                        (map        (make-sparse-keymap)))
+                    (define-key map [mouse-2] 
'dired-mouse-describe-listed-directory)
+                    (define-key map "\r" 
'dired-describe-listed-directory)
+                    (when available (end-of-line) (insert " available " 
available))
+                    (add-text-properties (line-beginning-position)
+                                         (1- (match-beginning 1))
+                                         `(mouse-face highlight keymap 
,map
+                                                      help-echo "Files in 
subdir / files in \
+default-directory"))))))))))

  (defun insert-directory-adj-pos (pos error-lines)
    "Convert `ls --dired' file name position value POS to a buffer 
position.
-- 
2.9.3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

In GNU Emacs 25.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.21.5)
  of 2016-09-08 built on calancha-pc
Repository revision: 367f8568bc9e759ebdfb423648891efa0346456b




^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-09-09 17:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-09 17:50 dired: More verbose subdir headers; show #files in subdir Tino Calancha

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).