如何显示 Emacs 模型行中的总行数

发布于 2024-12-17 04:58:03 字数 51 浏览 1 评论 0原文

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

The default Emacs modeline only shows the current line number and its percentage in relation to the total line numbers. How do I make it show the line total as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-12-24 04:58:03

这可能有点棘手,因为如果您一直更新行数并且有一个很大的缓冲区,则可能会使 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 来适合您的口味,以上是我个人更喜欢的。

This can be a little tricky, because if you update the line count all the time and have a large buffer it can make Emacs somewhat unresponsive since it's counting lines over and over. I wrote this to take a lazy approach to counting: it only does it when the file is first read in or after you save/revert it. If the buffer is modified it doesn't lie about the line count, it simply isn't shown until you save again.

(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)

You might want to adjust mode-line-format to suit your taste of course, the above is what I personally prefer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文