all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: "Basil L. Contovounesios" <contovob@tcd.ie>
Cc: Oleh Krehel <ohwoeowho@gmail.com>, Eli Zaretskii <eliz@gnu.org>,
	Stefan Monnier <monnier@iro.umontreal.ca>,
	emacs-devel <emacs-devel@gnu.org>
Subject: Re: [Emacs-diffs] master fd54102: * lisp/files.el (file-size-function): New defcustom
Date: Fri, 26 Jul 2019 23:39:11 +0200	[thread overview]
Message-ID: <AD8D5EDF-8308-4324-9543-BBE0BB271A59@acm.org> (raw)
In-Reply-To: <87pnlw6ess.fsf@tcd.ie>

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

26 juli 2019 kl. 16.39 skrev Basil L. Contovounesios <contovob@tcd.ie>:
> 
> The backtick no longer seems necessary.

Fixed.

> Isn't :tag a no-op for function-items?

Correct; removed.

>> +          (function :tag "Custom function" number-to-string))
> 
> This seems to work, but my reading of the manual is that
> number-to-string must be given as the value of the :value tag, not on
> its own.  Does the manual need updating in this regard?

Don't know if it was intended behaviour, but I've added the :value keyword in the patch.

Thank you!


[-- Attachment #2: 0001-Clean-up-file-size-function.patch --]
[-- Type: application/octet-stream, Size: 8628 bytes --]

From bcbb6b49d78e1f3690ef90052e5c49a438b38ef2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Mon, 22 Jul 2019 17:10:37 +0200
Subject: [PATCH] Clean up file-size-function

* lisp/files.el (file-size-human-readable-iec): New.
(file-size-function): Rename to byte-count-to-string-function.  Better
default value.  Eliminate lambda.  Better default for custom choice.
Put in group `files'.  More descriptive doc string.  Move.
(out-of-memory-warning-percentage, warn-maybe-out-of-memory)
(get-free-disk-space):
* lisp/dired.el (dired-number-of-marked-files):
* lisp/url/url-http.el (url-http-simple-after-change-function)
(url-http-content-length-after-change-function):
Use byte-count-to-string-function.
* test/lisp/files-test.el (files-test-file-size-human-readable):
Test file-size-human-readable-iec.
---
 etc/NEWS                 |  3 ++-
 lisp/dired.el            |  4 ++--
 lisp/files.el            | 34 ++++++++++++++++++++--------------
 lisp/url/url-http.el     | 10 +++++-----
 test/lisp/files-tests.el |  7 ++++++-
 5 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5673fc1b42..2fc52bea52 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -405,7 +405,8 @@ mode they are described in the manual "(emacs) Display".
 ** New variable 'xref-file-name-display' controls the display of file
 names in xref buffers.
 
-** New variable `file-size-function' controls how file sizes are displayed.
+** New customizable variable 'byte-count-to-string-function'.
+It is used for displaying file sizes and disk space in some cases.
 
 \f
 * Editing Changes in Emacs 27.1
diff --git a/lisp/dired.el b/lisp/dired.el
index c455a5cde4..640d62fd41 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -3630,12 +3630,12 @@ dired-number-of-marked-files
                         sum (file-attribute-size (file-attributes file)))))
     (if (zerop nmarked)
         (message "No marked files"))
-    (message "%d marked file%s (%sB total size)"
+    (message "%d marked file%s (%s total size)"
              nmarked
              (if (= nmarked 1)
                  ""
                "s")
-             (file-size-human-readable size))))
+             (funcall byte-count-to-string-function size))))
 
 (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
   "Mark all files with contents containing REGEXP for use in later commands.
diff --git a/lisp/files.el b/lisp/files.el
index 81ca948bd2..34a2b1d936 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1419,6 +1419,21 @@ file-size-human-readable
               (if (string= prefixed-unit "") "" (or space ""))
               prefixed-unit))))
 
+(defun file-size-human-readable-iec (size)
+  "Human-readable string for SIZE bytes, using IEC prefixes."
+  (file-size-human-readable size 'iec " "))
+
+(defcustom byte-count-to-string-function #'file-size-human-readable-iec
+  "Function that turns a number of bytes into a human-readable string.
+It is for use when displaying file sizes and disk space where other
+constraints do not force a specific format."
+  :type '(radio
+          (function-item file-size-human-readable-iec)
+          (function-item file-size-human-readable)
+          (function :tag "Custom function" :value number-to-string))
+  :group 'files
+  :version "27.1")
+
 (defcustom mounted-file-systems
   (if (memq system-type '(windows-nt cygwin))
       "^//[^/]+/"
@@ -2086,7 +2101,7 @@ out-of-memory-warning-percentage
 (defun files--ask-user-about-large-file (size op-type filename offer-raw)
   (let ((prompt (format "File %s is large (%s), really %s?"
 		        (file-name-nondirectory filename)
-		        (file-size-human-readable size 'iec " ") op-type)))
+		        (funcall byte-count-to-string-function size) op-type)))
     (if (not offer-raw)
         (if (y-or-n-p prompt) nil 'abort)
       (let* ((use-dialog (and (display-popup-menus-p)
@@ -2138,10 +2153,10 @@ warn-maybe-out-of-memory
 exceeds the %S%% of currently available free memory (%s).
 If that fails, try to open it with `find-file-literally'
 \(but note that some characters might be displayed incorrectly)."
-	     (file-size-human-readable size 'iec " ")
+	     (funcall byte-count-to-string-function size)
 	     out-of-memory-warning-percentage
-	     (file-size-human-readable (* total-free-memory 1024)
-                                       'iec " "))))))))
+	     (funcall byte-count-to-string-function
+                      (* total-free-memory 1024)))))))))
 
 (defun files--message (format &rest args)
   "Like `message', except sometimes don't print to minibuffer.
@@ -6698,22 +6713,13 @@ directory-free-space-args
 			"ignored, as Emacs uses `file-system-info' instead"
 			"27.1")
 
-(defcustom file-size-function #'file-size-human-readable
-  "Function that transforms the number of bytes into a human-readable string."
-  :type `(radio
-          (function-item :tag "Default" file-size-human-readable)
-          (function-item :tag "IEC"
-                         ,(lambda (size) (file-size-human-readable size 'iec " ")))
-          (function :tag "Custom function"))
-  :version "27.1")
-
 (defun get-free-disk-space (dir)
   "String describing the amount of free space on DIR's file system.
 If DIR's free space cannot be obtained, this function returns nil."
   (save-match-data
     (let ((avail (nth 2 (file-system-info dir))))
       (if avail
-          (funcall file-size-function avail)))))
+          (funcall byte-count-to-string-function avail)))))
 
 ;; The following expression replaces `dired-move-to-filename-regexp'.
 (defvar directory-listing-before-filename-regexp
diff --git a/lisp/url/url-http.el b/lisp/url/url-http.el
index 527760118d..e34f90f6f3 100644
--- a/lisp/url/url-http.el
+++ b/lisp/url/url-http.el
@@ -1017,7 +1017,7 @@ url-http-simple-after-change-function
   ;; Function used when we do NOT know how long the document is going to be
   ;; Just _very_ simple 'downloaded %d' type of info.
   (url-lazy-message "Reading %s..."
-                    (file-size-human-readable (buffer-size) 'iec " ")))
+                    (funcall byte-count-to-string-function (buffer-size))))
 
 (defun url-http-content-length-after-change-function (_st nd _length)
   "Function used when we DO know how long the document is going to be.
@@ -1030,16 +1030,16 @@ url-http-content-length-after-change-function
        (url-percentage (- nd url-http-end-of-headers)
 		       url-http-content-length)
        url-http-content-type
-       (file-size-human-readable (- nd url-http-end-of-headers) 'iec " ")
-       (file-size-human-readable url-http-content-length 'iec " ")
+       (funcall byte-count-to-string-function (- nd url-http-end-of-headers))
+       (funcall byte-count-to-string-function url-http-content-length)
        (url-percentage (- nd url-http-end-of-headers)
 		       url-http-content-length))
     (url-display-percentage
      "Reading... %s of %s (%d%%)"
      (url-percentage (- nd url-http-end-of-headers)
 		     url-http-content-length)
-     (file-size-human-readable (- nd url-http-end-of-headers) 'iec " ")
-     (file-size-human-readable url-http-content-length 'iec " ")
+     (funcall byte-count-to-string-function (- nd url-http-end-of-headers))
+     (funcall byte-count-to-string-function url-http-content-length)
      (url-percentage (- nd url-http-end-of-headers)
 		     url-http-content-length)))
 
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index df2c3f47ae..ed23f7675c 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -1280,7 +1280,12 @@ files-tests-file-attributes-equal
   (should (equal (file-size-human-readable 4294967296 'iec " ") "4 GiB"))
   (should (equal (file-size-human-readable 10000 nil " " "bit") "9.8 kbit"))
   (should (equal (file-size-human-readable 10000 'si " " "bit") "10 kbit"))
-  (should (equal (file-size-human-readable 10000 'iec " " "bit") "9.8 Kibit")))
+  (should (equal (file-size-human-readable 10000 'iec " " "bit") "9.8 Kibit"))
+
+  (should (equal (file-size-human-readable-iec 0) "0 B"))
+  (should (equal (file-size-human-readable-iec 1) "1 B"))
+  (should (equal (file-size-human-readable-iec 9621) "9.4 KiB"))
+  (should (equal (file-size-human-readable-iec 72528034765) "67.5 GiB")))
 
 (ert-deftest files-test-magic-mode-alist-re-baseline ()
   "Test magic-mode-alist with RE, expected behaviour for match."
-- 
2.20.1 (Apple Git-117)


  reply	other threads:[~2019-07-26 21:39 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190719165710.17673.32987@vcs0.savannah.gnu.org>
     [not found] ` <20190719165711.741F1206A7@vcs0.savannah.gnu.org>
2019-07-20 16:53   ` [Emacs-diffs] master fd54102: * lisp/files.el (file-size-function): New defcustom Basil L. Contovounesios
2019-07-20 17:06     ` Eli Zaretskii
2019-07-20 17:38       ` Oleh Krehel
2019-07-20 17:45         ` Eli Zaretskii
2019-07-20 17:50           ` Oleh Krehel
2019-07-20 17:56             ` Eli Zaretskii
2019-07-20 18:50         ` Basil L. Contovounesios
2019-07-22 15:26           ` Mattias Engdegård
2019-07-22 16:15             ` Oleh Krehel
2019-07-22 20:14               ` Stefan Monnier
2019-07-26 10:43                 ` Mattias Engdegård
2019-07-26 14:39                   ` Basil L. Contovounesios
2019-07-26 21:39                     ` Mattias Engdegård [this message]
2019-07-22 17:29             ` Eli Zaretskii
2019-07-20 17:30     ` Mattias Engdegård
2019-07-20 18:48       ` Basil L. Contovounesios
2019-07-20 19:06         ` Mattias Engdegård

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

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

  git send-email \
    --in-reply-to=AD8D5EDF-8308-4324-9543-BBE0BB271A59@acm.org \
    --to=mattiase@acm.org \
    --cc=contovob@tcd.ie \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=ohwoeowho@gmail.com \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.