使用适用于kB,MB,GB,...和kiB,MiB,GiB的ISO前缀格式化整数

时间:2011-04-07 11:51:21

标签: emacs iso number-formatting prefixes

我在size-indication-mode中寻找在模式行上打印文件大小的函数。我在源代码中搜索了size-indication-mode但是找不到代码引用。那么打印的功能在哪里呢? 22K

当文件大约22千字节时?

之间的区别 - 千字节(kB),1000字节和 - kibibytes(KiB),1024字节,在

中定义

Emacs不应该支持两者吗?

这当然不难写,但为什么要重新发明轮子?

http://en.wikipedia.org/wiki/Kibibyte

4 个答案:

答案 0 :(得分:3)

参见 Ch f format-mode-line RET Ch v mode-line-format RET

启用size-indication-mode只会导致of %I被添加到缓冲区的本地模式行格式字符串中,而C函数会处理其余部分。

您可以使用%i来查看缓冲区大小(以字节为单位)。

如果你想要一些可以提供任意值的东西,那么Emacs提供的这些行中至少有一个elisp函数(在ls-lisp模块中):

C-h f ls-lisp-format-file-size RET

答案 1 :(得分:2)

我注意到emacs dev(bzr)只是定义了一个新函数file-size-human-readable()。它完全符合我的要求。

有些emacs开发者必须听过我的电话:)

答案 2 :(得分:2)

这是我使用的功能。

(defconst number-to-string-approx-suffixes
  '("k" "M" "G" "T" "P" "E" "Z" "Y"))
(defun number-to-string-approx-suffix (n &optional binary)
  "Return an approximate decimal representation of NUMBER as a string,
followed by a multiplier suffix (k, M, G, T, P, E, Z, Y). The representation
is at most 5 characters long for numbers between 0 and 10^19-5*10^16.
Uses a minus sign if negative.
NUMBER may be an integer or a floating point number.
If the optional argument BINARY is non-nil, use 1024 instead of 1000 as
the base multiplier."
  (if (zerop n)
      "0"
    (let ((sign "")
          (b (if binary 1024 1000))
          (suffix "")
          (bigger-suffixes number-to-string-approx-suffixes))
      (if (< n 0)
          (setq n (- n)
                sign "-"))
      (while (and (>= n 9999.5) (consp bigger-suffixes))
        (setq n (/ n b) ; TODO: this is rounding down; nearest would be better
              suffix (car bigger-suffixes)
              bigger-suffixes (cdr bigger-suffixes)))
      (concat sign
                  (if (integerp n)
                  (int-to-string n)
                (number-to-string (floor n)))
              suffix))))

我在缓冲区菜单的size列中使用它。

(defvar Buffer-menu-buffer+size-shorten 'binary)
(defadvice Buffer-menu-buffer+size (before Buffer-menu-shorten-size
                                    compile activate)
  "Shorten the size column in a buffer menu by using multiplier suffixes
\(k, M, G, T\).
This is done only if `Buffer-menu-buffer+size-shorten' is non-nil.
If `Buffer-menu-buffer+size-shorten' is the symbol `binary', use binary
multipliers (powers of 1024). Otherwise use decimal (powers of 1000)
multipliers."
  (if Buffer-menu-buffer+size-shorten
      (let ((binary (eq Buffer-menu-buffer+size-shorten 'binary)))
        (save-match-data
          (if (string-match "^[0-9]+$" size)
              (setq size (number-to-string-approx-suffix (string-to-number size)
                                                         binary)))))))

答案 3 :(得分:0)

好的,我自己破解了一个解决方案。应该接近size-indication-mode提供的内容

(defun number-to-iso-postfixed-string (number &optional binary)
  "Convert NUMBER to ISO-postfixed string.
If BINARY is non-nil use bibytes prefixes KiB, MiB, GiB instead of
kB, MB, GB etc."
  (let ((scale (if binary 1024.0 1000.0))
        (postfix nil))
    (concat (if (< number scale)
                (if (zerop number) "0" (number-to-string number))
                (format "%.1f"
                        (cond ((< number (expt scale 2)) (setq postfix "k") (/ number (expt scale 1)))
                              ((< number (expt scale 3)) (setq postfix "M") (/ number (expt scale 2)))
                              ((< number (expt scale 4)) (setq postfix "G") (/ number (expt scale 3)))
                              ((< number (expt scale 5)) (setq postfix "T") (/ number (expt scale 4)))
                              ((< number (expt scale 6)) (setq postfix "P") (/ number (expt scale 5)))
                              ((< number (expt scale 7)) (setq postfix "E") (/ number (expt scale 6)))
                              (t
                               number)
                              )))
            postfix
            (when (and postfix binary) "i"))))
;; Use: (number-to-iso-postfixed-string 999 nil)
;; Use: (number-to-iso-postfixed-string 1001 nil)
;; Use: (number-to-iso-postfixed-string 1024)
;; Use: (number-to-iso-postfixed-string 1024 t)
;; Use: (number-to-iso-postfixed-string (* 1024 1024) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1023) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1025) t)
;; Use: (number-to-iso-postfixed-string (* 1024.0 1024 1025) t)