1

我想以 Emacs 模式行格式调用一些方法。例如count-words,查看选择了多少个字符或类/方法名光标所在的位置。

这是我当前的模式行格式,但是调用count-words,它会显示*invalid*为结果,而且我不确定它是否会在任何更改中被调用。

(setq-default mode-line-format
      (list "---File:[%b%+] Line:[%l] Size:[%i] "
        (count-words (point-min) (point-max))
        ))

我想在模式行区域中调用一些经常更新的自定义方法。例如,我选择了多少个字符,谁更改了这一行(git blame),光标现在所在的当前类名是什么,等等。

4

2 回答 2

3

您提出的问题的答案是:

(setq mode-line-format
  (list "---File:[%b%+] Line:[%l] Size:[%i] %d"
    (format "count: %d" (count-words (point-min) (point-max)))))

但我认为这不是您要问的问题,因为该值不会更新。让我们解决这个问题。

我选择在您保存文件后更新它,因为如果您经常这样做,在缓冲区大小的情况下计算缓冲区中的单词会变得很慢。

(setq mode-line-format
      (list "---File:[%b%+] Line:[%l] Size:[%i] %d"
        'count-words-string))

(defun update-count-words-string-for-modeline ()
  (interactive)
  (setq count-words-string 
        (format "word count: %d" (count-words (point-min) (point-max))))
  (force-mode-line-update))

(add-hook 'after-save-hook 'update-count-words-string-for-modeline)

message(保存后简单地用字数调用可能同样适合您的目的。)

于 2019-11-23T01:25:14.883 回答
1
‘(:eval FORM)’
     A list whose first element is the symbol ‘:eval’ says to evaluate
     FORM, and use the result as a string to display.  Make sure this
     evaluation cannot load any files, as doing so could cause infinite
     recursion.

--C-hig (elisp)Mode Line Data

一般来说,:eval如果不需要,请避免使用。在许多情况下,在模式行中显示变量的值会更有效,并在其他地方安排在必要时更新该变量(这可能远低于重新显示模式行的频率)。

您调用count-words整个缓冲区的示例可能会导致大型缓冲区出现性能问题。

例子:

(setq-default mode-line-format '(:eval (count-words--buffer-message)))
于 2019-11-23T22:57:19.770 回答