unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* patch for Dired second header line info
@ 2008-03-01  1:03 Drew Adams
  2008-03-01 13:31 ` Richard Stallman
  2008-03-02  2:55 ` Juri Linkov
  0 siblings, 2 replies; 23+ messages in thread
From: Drew Adams @ 2008-03-01  1:03 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1739 bytes --]

The second header line in a Dired buffer currently looks like this:

 total used in directory 49439 available 56233408

Attached is a patch (for `files.el' and `ls-lisp.el') that changes that text
to this:

 files 691 space used 49439 available 56233408

IOW, it adds the number of files and directories (`files'), and it shortens
`total used in directory' to `space used'. That also makes it clearer what
"total used" means (total what?). We might want to change `space used' to
`Kbytes used' or whatever, to be even clearer.

Entries `.' and `..' are not counted, but other directories are, along with
non-directory files. The patch works for `-lR' listings also, updating the
subdir totals.

Room for improvement: 

1. Like the current code, this code replaces the initial text `total' that
is written to the buffer. Perhaps we could just write the correct text to
begin with, instead of doing a text substitution for the placeholder
`total'?

2. The proposed code does this:
(add-hook 'dired-after-readin-hook
          'update-dired-files-count)

Perhaps we could do the `update-dired-files-count' directly in the code that
runs the hook, instead?

As it stands now, the file count and the space counts are updated only
during directory read-in - e.g. when you hit `g' (nothing new in that, BTW).
I could not find a hook for when a file or dir is added/deleted (or
hidden/shown), etc. Wouldn't it be useful to have a hook for whenever the
Dired display changes? 


Here's a change-log entry:

2008-02-29 Drew Adams  <drew.adams@oracle.com>
	* files.el:
	(insert-directory): Print number of files.
	(count-dired-files): New function.
	(update-dired-files-count): New function.
	* ls-lisp.el (insert-directory): Print number of files.


[-- Attachment #2: ls-lisp-2008-02-29.patch --]
[-- Type: application/octet-stream, Size: 1434 bytes --]

diff -c -w "ls-lisp-CVS-2008-02-29.el" "ls-lisp-patched-2008-02-29.el"
*** ls-lisp-CVS-2008-02-29.el	Fri Feb 29 14:58:50 2008
--- ls-lisp-patched-2008-02-29.el	Fri Feb 29 15:01:32 2008
***************
*** 263,274 ****
  	;; Try to insert the amount of free space.
  	(save-excursion
  	  (goto-char (point-min))
! 	  ;; 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 "total used", to avoid confusion.
- 		(replace-match "total used in directory")
  		(end-of-line)
  		(insert " available " available)))))))))
  
--- 263,279 ----
  	;; Try to insert the amount of free space.
  	(save-excursion
  	  (goto-char (point-min))
! 	  (while (re-search-forward "^total" nil t)
!             (beginning-of-line)
!             (insert "files "
!                     (number-to-string (save-match-data (count-dired-files)))
!                     " ")
!             (goto-char (point-min))
!             (re-search-forward "^files [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 "."))))
  	      (when available
  		(end-of-line)
  		(insert " available " available)))))))))
  

Diff finished at Fri Feb 29 15:19:17

[-- Attachment #3: files-2008-02-29.patch --]
[-- Type: application/octet-stream, Size: 3667 bytes --]

diff -c -w files-CVS-2008-02-29.el" "files-patched-2008-02-29.el"
*** files-CVS-2008-02-29.el	Fri Feb 29 14:59:08 2008
--- files-patched-2008-02-29.el	Fri Feb 29 15:17:20 2008
***************
*** 5284,5303 ****
  		      (if val
  			  (put-text-property pos (point)
  					     'dired-filename t)))))))
! 
! 	  (if full-directory-p
! 	      ;; Try to insert the amount of free space.
  	      (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)))))))))))
  
  (defun insert-directory-adj-pos (pos error-lines)
    "Convert `ls --dired' file name position value POS to a buffer position.
  File name position values returned in ls --dired output
--- 5284,5336 ----
  		      (if val
  			  (put-text-property pos (point)
  					     'dired-filename t)))))))
! 	  (when full-directory-p
              (save-excursion
                (goto-char beg)
!               (while (re-search-forward "^ *\\(total\\)" nil t)
!                 (beginning-of-line)
!                 (insert "files " (number-to-string (save-match-data
!                                                      (count-dired-files)))
!                         " ")
!                 (goto-char beg)
!                 (re-search-forward "^files [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 "."))))
                    (when available
                      (end-of-line)
                      (insert " available " available)))))))))))
  
+ (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 `..'."
+   ;; Should we skip `#' files also, as in `dired-trivial-filenames'? Dunno.
+   (save-excursion
+     (re-search-backward "^$" nil 'to-bob)
+     (re-search-forward dired-move-to-filename-regexp nil t)
+     (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."
+   (let ((num-files (number-to-string (count-dired-files))))
+     (save-excursion
+       (goto-char (point-min))
+       (while (re-search-forward "^  files \\([0-9]+\\)" nil t)
+         (let ((buffer-read-only nil))
+           (replace-match (number-to-string (save-match-data (count-dired-files)))
+                          nil nil nil 1))
+         (set-buffer-modified-p nil)))))
+ 
  (defun insert-directory-adj-pos (pos error-lines)
    "Convert `ls --dired' file name position value POS to a buffer position.
  File name position values returned in ls --dired output

Diff finished at Fri Feb 29 15:17:44

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2008-03-05 21:33 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-01  1:03 patch for Dired second header line info Drew Adams
2008-03-01 13:31 ` Richard Stallman
2008-03-01 16:31   ` Drew Adams
2008-03-01 23:09     ` Richard Stallman
2008-03-02  2:55 ` Juri Linkov
2008-03-02  8:05   ` Drew Adams
2008-03-02 14:36     ` Juri Linkov
2008-03-02 16:27       ` Drew Adams
2008-03-02 17:49         ` Drew Adams
2008-03-02 17:57           ` Juri Linkov
2008-03-02 18:56             ` Drew Adams
2008-03-02 19:06               ` Drew Adams
2008-03-03  0:47                 ` Drew Adams
2008-03-03 18:27             ` Richard Stallman
2008-03-03 21:45               ` Stefan Monnier
2008-03-03 22:29                 ` Drew Adams
2008-03-04 17:38                   ` Richard Stallman
2008-03-04 19:44                     ` Drew Adams
2008-03-04 22:28                       ` Drew Adams
2008-03-05 21:33                       ` Richard Stallman
2008-03-03 18:27           ` Richard Stallman
2008-03-03 19:01             ` Drew Adams
2008-03-02 17:56         ` Juri Linkov

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