Clean up file-size-function

It is now called `byte-count-to-string-function', and used instead of
calling `file-size-human-readable' directly where appropriate.

* 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.
This commit is contained in:
Mattias Engdegård 2019-07-22 17:10:37 +02:00
parent b49d987a8c
commit 408e75e819
5 changed files with 35 additions and 23 deletions

View file

@ -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.
+++
** Emacs now interprets RGB triplets like HTML, SVG, and CSS do.

View file

@ -3642,12 +3642,12 @@ object files--just `.o' will mark more than you might think."
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.

View file

@ -1426,6 +1426,21 @@ in all cases, since that is the standard symbol for byte."
(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))
"^//[^/]+/"
@ -2093,7 +2108,7 @@ think it does, because \"free\" is pretty hard to define in practice."
(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)
@ -2145,10 +2160,10 @@ returns nil or exits non-locally."
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.
@ -6705,22 +6720,13 @@ This variable is obsolete; Emacs no longer uses it."
"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

View file

@ -1025,7 +1025,7 @@ should be shown to the user."
;; 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.
@ -1038,16 +1038,16 @@ the callback to be triggered."
(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)))

View file

@ -1280,7 +1280,12 @@ renaming only, rather than modified in-place."
(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."