2011-11-18 52 views
12

默認的Emacs模式行僅顯示當前行號及其佔總行數的百分比。我怎樣才能讓它顯示行總數?如何顯示Emacs模式行中的總行數

+0

在這個相關的線程中使用'(format-mode-line「%l」)'而不是'count-lines'來獲得性能/速度的提升:http:// emacs .stackexchange.com/a/26724/2287 – lawlist

回答

12

這可能有點棘手,因爲如果你一直更新行數並有一個很大的緩衝區,它可能會使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,以滿足您當然味道,上面是我個人比較喜歡。

+0

它工作正常,thx =) – dkiyatkin