unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Tino Calancha <f92capac@gmail.com>
Cc: Lars Ingebrigtsen <larsi@gnus.org>, 22829@debbugs.gnu.org
Subject: bug#22829: Acknowledgement (25.1.50; Display number of marked files)
Date: Wed, 02 Mar 2016 02:32:39 +0200	[thread overview]
Message-ID: <87fuw9203w.fsf@mail.linkov.net> (raw)
In-Reply-To: <alpine.LRH.2.20.1602281529080.3831@calancha-ilc.kek.jp> (Tino Calancha's message of "Sun, 28 Feb 2016 15:36:48 +0900 (JST)")

>> And I'm not sure I'm feeling the utility of this function, either.
>> What's the use case?
>
> For those marking files in several directories quite often, and using
> dired-change-marks to separate files in different categories: those
> people may be interested in counting number of marked files. I need
> this every day, but I agree not too many people would find it useful.

I confirm this is very useful.  I'm using the same for a long time,
but additionally also displaying a total sum of sizes on every mark
(this emulates the behavior of File Commanders on marking files by INS).

The code I'm using in ~/.emacs is very very old, and nowadays
you could implement the same with less code.

;; 2 new functions:
(defun dired-get-file-info ()
  "Get file info files for which PREDICATE returns non-nil."
  ;; code for this function is borrowed from dired-x.el::dired-mark-sexp
  (let (inode s mode nlink uid gid size time name sym)
    (save-excursion
      (if (dired-move-to-filename)
          (let (pos
                (mode-len 10)
                (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
            (beginning-of-line)
            (forward-char 2)
            (if (looking-at dired-re-inode-size)
                (progn
                  (goto-char (match-end 0))
                  (setq inode (string-to-int (buffer-substring (match-beginning 1)
                                                               (match-end 1)))
                        s (string-to-int (buffer-substring (match-beginning 2)
                                                           (match-end 2)))))
              (setq inode nil
                    s nil))
            (setq mode (buffer-substring (point) (+ mode-len (point))))
            (forward-char mode-len)
            (setq nlink (read (current-buffer)))
            (setq uid (buffer-substring (+ (point) 1) (progn (forward-word 1) (point))))
            ;; works only with ls patch
            ;; patched in dired.el:dired-move-to-filename-regexp
            ;; (re-search-forward "\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)")
	    ;; try standard expression
            (re-search-forward directory-listing-before-filename-regexp)
            (goto-char (match-beginning 2))
            (forward-char -1)
            (setq size (string-to-int (replace-regexp-in-string
				       ;; handle thousand separators in sizes
				       "," ""
				       (buffer-substring (save-excursion
							   ;; (backward-word 1)
							   (skip-chars-backward "[0-9,.]")
							   (setq pos (point)))
							 (point)))))
            (goto-char pos)
            (backward-word 1)
            (setq gid (buffer-substring (save-excursion (forward-word 1) (point))
                                        (point))
                  time (buffer-substring (match-beginning 1)
                                         (1- (dired-move-to-filename)))
                  name (buffer-substring (point)
                                         (or (dired-move-to-end-of-filename t)
                                             (point)))
                  sym  (progn
                         (if (looking-at " -> ")
                             (buffer-substring (progn (forward-char 4) (point))
                                               (progn (end-of-line) (point)))
                           "")))
            (list
             (cons 'inode inode)
             (cons 's s)
             (cons 'mode mode)
             (cons 'nlink nlink)
             (cons 'uid uid)
             (cons 'gid gid)
             (cons 'size size)
             (cons 'time time)
             (cons 'name name)
             (cons 'sym sym)))
        nil))))

;; TODO: use `pint2hrstr' in Lisp
(defun dired-count-sizes (&optional mark)
  "Count sizes of files marked by MARK mark."
  ;; TODO: add this info to mode-line and file count too, e.g.: F32 S64k
  ;; and make minor mode
  ;; see `dired-change-marks'
  (interactive
   (let* ((cursor-in-echo-area t)
          (mark (progn (message "Count files marked by mark: ")
                       (read-char))))
     (list mark)))
  (if (or (eq mark ?\r))
      (ding)
    (let ((string (format "\n%c" mark))
          (buffer-read-only)
          (total-size 0)
          total-size-str
          (total-count 0))
      (save-excursion
        (goto-char (point-min))
        (while (search-forward string nil t)
          (if (if (= mark ?\ )
                  (save-match-data
                    (dired-get-filename 'no-dir t))
                t)
              (if (equal (buffer-substring-no-properties
                          (match-beginning 0) (match-end 0))
                         string)
                  (setq total-size
                        (+ total-size
                           (*;;(/
                            (cdr (assoc 'size (dired-get-file-info)))
                            1.0);;1024)
                           )
                        total-count (+ total-count 1))))))
      (setq total-size-str (replace-regexp-in-string
                            "^," ""
                            (apply 'string
                                   (reverse
                                    (string-to-list
                                     (replace-regexp-in-string
                                      "\\([0-9]\\{3\\}\\)" "\\1,"
                                      (apply 'string
                                             (reverse
                                              (string-to-list
                                               (replace-regexp-in-string
                                                "\.0$" ""
                                                (number-to-string
                                                 total-size)))))))))))
      (message "Marked %s files with %s bytes" total-count total-size-str))))

(define-key dired-mode-map [(shift f5)] 'dired-count-sizes)

(defun my-dired-mark (arg)
  "Mark ARG files and print the total size of marked files."
  (interactive "P")
  (dired-mark arg)
  (dired-count-sizes dired-marker-char))
(define-key dired-mode-map [insert] 'my-dired-mark)

(defun my-dired-unmark-backward (arg)
  "Move up lines, remove deletion flag there and print size of marked files."
  (interactive "p")
  (dired-unmark-backward arg)
  (dired-count-sizes dired-marker-char))
(define-key dired-mode-map [backspace] 'my-dired-unmark-backward)





  reply	other threads:[~2016-03-02  0:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-27 14:29 bug#22829: 25.1.50; Display number of marked files Tino Calancha
     [not found] ` <handler.22829.B.145658318020704.ack@debbugs.gnu.org>
2016-02-27 14:41   ` bug#22829: Acknowledgement (25.1.50; Display number of marked files) Tino Calancha
2016-02-28  5:38     ` Lars Ingebrigtsen
2016-02-28  6:36       ` Tino Calancha
2016-03-02  0:32         ` Juri Linkov [this message]
2016-03-02 17:28           ` Lars Ingebrigtsen
2016-03-02 20:04             ` Marcin Borkowski
2016-03-03  0:17               ` Juri Linkov
2016-03-03  5:52                 ` Lars Ingebrigtsen
2016-03-03 11:20                   ` Richard Stallman
2016-03-03 11:25                     ` Lars Ingebrigtsen
2019-06-25 14:29             ` Lars Ingebrigtsen
2016-03-03  9:58           ` Tino Calancha
2016-03-03 12:27             ` Michael Heerdegen
2016-03-03 12:55               ` Constantino Calancha
2019-06-25 14:29                 ` Lars Ingebrigtsen
2016-03-07  0:04             ` Juri Linkov
2016-03-08 10:20               ` Tino Calancha
2016-03-08  0:19             ` Juri Linkov
2016-03-08 10:34               ` Tino Calancha
2016-03-09 16:37                 ` Richard Stallman
2016-03-01 11:26 ` bug#22829: 25.1.50; Display number of marked files Constantino Calancha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fuw9203w.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=22829@debbugs.gnu.org \
    --cc=f92capac@gmail.com \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).