2

我正在寻找在大小指示模式下在模式行上打印文件大小的功能。我在源代码中搜索了 size-indication-mode 但找不到对它的代码引用。那么打印例如22k的功能在哪里

当文件大约 22 KB 大?

- 千字节 (kB),1000 字节和 - 千字节 (KiB),1024 字节之间的区别,定义见

Emacs 不应该同时支持两者吗?

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

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

4

4 回答 4

3

C-hf format-mode-line RETC-hv mode-line-format RET

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

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

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

C-hf ls-lisp-format-file-size RET

于 2011-04-07T12:17:15.990 回答
2

我注意到 emacs dev (bzr) 刚刚file-size-human-readable()定义了一个新功能。它正是我所要求的。

一些 emacs 开发人员一定听到了我的电话 :)

于 2011-04-12T07:56:24.067 回答
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))))

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

(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)))))))
于 2011-04-12T22:35:10.023 回答
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)
于 2011-04-07T12:59:27.750 回答