如何在Emacs模式行中显示总行数

时间:2011-11-18 23:10:06

标签: emacs lines-of-code modeline

默认的Emacs模式行仅显示当前行号及其相对于总行数的百分比。如何让它显示总线数?

1 个答案:

答案 0 :(得分:14)

这可能有点棘手,因为如果你一直更新行数并且有一个大的缓冲区,它可能会使Emacs有点无响应,因为它反复计算行数。我写这个是为了采取一种懒惰的计数方法:它只在首次读入文件时或保存/恢复它后才会这样做。如果缓冲区被修改,它不会与行计数有关,只有在您再次保存之后才会显示它。

(defvar my-mode-line-buffer-line-count nil)
(make-variable-buffer-local 'my-mode-line-buffer-line-count)

(setq-default mode-line-format
              '("  " mode-line-modified
                (list 'line-number-mode "  ")
                (:eval (when line-number-mode
                         (let ((str "L%l"))
                           (when (and (not (buffer-modified-p)) my-mode-line-buffer-line-count)
                             (setq str (concat str "/" my-mode-line-buffer-line-count)))
                           str)))
                "  %p"
                (list 'column-number-mode "  C%c")
                "  " mode-line-buffer-identification
                "  " mode-line-modes))

(defun my-mode-line-count-lines ()
  (setq my-mode-line-buffer-line-count (int-to-string (count-lines (point-min) (point-max)))))

(add-hook 'find-file-hook 'my-mode-line-count-lines)
(add-hook 'after-save-hook 'my-mode-line-count-lines)
(add-hook 'after-revert-hook 'my-mode-line-count-lines)
(add-hook 'dired-after-readin-hook 'my-mode-line-count-lines)

您可能希望根据自己的喜好调整mode-line-format,以上是我个人喜欢的。