emacs如何刷新缓冲区并保存高亮 - 特别是对于日志 [英] emacs how to refresh buffer and preserve highlighting - especially for logs
问题描述
Ms hr
+ 正则表达式强调特定的字符序列来帮助可视化
,也称为命令 highlight-regexp
或 hi-lock-face-buffer
。有时我需要重新生成日志文件,这个文件是通过 Cx Cv
或 find-alternate-file
命令完成的。不幸的是,再生也失去了我的全部亮点。 TLDR
有没有办法在保留所有突出显示的情况下重新生成我的缓冲区文件?
更新
我正在使用文本模式
,正则表达式因广泛而异,取决于我的任务和日志文件。我想要将所有突出显示的内容传递给刷新的缓冲区。是否有东西已经存在?
答案:
甚至比刷新刷新后的亮点更好,所选答案使用 add-hook
为特定日志创建一个突出显示方案。另外,每个日志可以有不同的高亮方案。结果是匹配特定日志文件名称的自动突出显示方案,可以切换各种亮点的方案。
你可以添加:
(add-hook'text-mode-hook
(lambda()
(font-lock-add-keywords nil'((^ \\([^,] * \\),1'font-lock-function- name-face)))))
你想要突出显示什么是regexp?如果根据日志使用几个正则表达式,则可能将它们全部钩住。或者您可以编程条件:哪个正则表达式为hightlight:
(add-hook' mode-hook
(lambda()
(if(condition)
;; condition true:
(font-lock-add-keywords nil'((regexp1 1'font- lock-function-name-face)))
;; condition false:
(font-lock-add-keywords nil'((regexp2 1'font-lock-function-name-face)))
)
))
另外,如果你控制日志的生成,你可以在第一行生成hightlight规则:
# - * - eval:(highlight-regexp REGEXP) - * -
(假设#是日志中的注释字符)。
修改:
这里是defun,它切换hightlighting为 TestQueryLogic
或调用fork-join
和测试Gudermann
:
(defun testing-MapAppLog.txt()
切换突出显示TestQueryLogic或调用fork-join和testGudermann。
(interactive)
(cond
((获取此命令状态)
(highlight-regexpTestQueryLogicfont-lock-variable-name-face)
(highlight-regexp调用fork-joinfont-lock-type-face)
(unhighlight-regexp
(this命令state nil)
(t
(unhighlight-regexpTestQueryLogic)
(消息突出显示:TestQueryLogic,调用fork-join )
(unhighlight-regexp调用fork-join)
(highlight-regexptestGudermannfont-lock-preprocessor-face)
(消息突出显示:testGudermann)
(put this-command'state t))))
所以调用一次hightlight TestQueryLogic
,调用fork-join
,再次调用改为使用 testGudermann
。您可以绑定到一个键:
(define-key text-mode-map(kbdMs t)' MapAppLog.txt)
,然后按一下或两次,具体取决于您要突出显示的内容。 / p>
I am often parsing log files, which I help visualize by highlighting specific char sequences via M-s h r
+ regex
, aka the command highlight-regexp
or hi-lock-face-buffer
. Sometimes I will need to regenerate the log file, which is done via the C-x C-v
or find-alternate-file
command. Unfortunately, the regeneration also loses all of my highlighting.
TLDR Is there a way to regenerate my buffer file while maintaining all of the highlighting?
Update
I am using text-mode
, and the regexes vary widely depending on my task and the log file. I would like something that carries over all of my highlighting to the refreshed buffer. Does something already exist?
Answer:
Even better than maintaining highlights after a refresh, the selected answer uses an add-hook
to create a highlighting scheme for a particular log. Additionally, each log can have different highlight schemes. The result is an automated highlighting scheme that matches a particular log file's name, with the ability to toggle the schemes of various highlights.
You can add-hook that:
(add-hook 'text-mode-hook
(lambda ()
(font-lock-add-keywords nil '(("^\\([^,]*\\)," 1 'font-lock-function-name-face)))))
What regexp you'd like to highlight? If You are using several regexps depending on the log, it might be possible to hook them all. Or you might program the condition: which regexp to hightlight:
(add-hook 'text-mode-hook
(lambda ()
(if (condition)
;; condition true:
(font-lock-add-keywords nil '((regexp1 1 'font-lock-function-name-face)))
;; condition false:
(font-lock-add-keywords nil '((regexp2 1 'font-lock-function-name-face)))
)
))
Also if you control the generation of logs you can generate the hightlight rules in the first line:
# -*- eval: (highlight-regexp REGEXP) -*-
(assuming # is the comment char in your log).
Edit:
Here's defun which toggles hightlighting for TestQueryLogic
or invoking fork-join
and testGudermann
:
(defun testing-MapAppLog.txt ()
"Toggle highlighting `TestQueryLogic' or `invoking fork-join' and `testGudermann'."
(interactive)
(cond
((get this-command 'state)
(highlight-regexp "TestQueryLogic" font-lock-variable-name-face)
(highlight-regexp "invoking fork-join" font-lock-type-face)
(unhighlight-regexp "testGudermann")
(message "Highlighting: TestQueryLogic, invoking fork-join")
(put this-command 'state nil))
(t
(unhighlight-regexp "TestQueryLogic")
(unhighlight-regexp "invoking fork-join")
(highlight-regexp "testGudermann" font-lock-preprocessor-face)
(message "Highlighting: testGudermann")
(put this-command 'state t))))
So call it once to hightlight TestQueryLogic
, invoking fork-join
, call it again to hightlight testGudermann
instead. You can bind to a key:
(define-key text-mode-map (kbd "M-s t") 'testing-MapAppLog.txt)
and press that once or twice depending on what you'd like to highlight.
这篇关于emacs如何刷新缓冲区并保存高亮 - 特别是对于日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!