Closed Apteryks closed 2 years ago
I've edited logview.el a bit like so (note: font-lock-defaults
):
(define-derived-mode logview-mode nil "Logview"
"Major mode for viewing and filtering various log files."
(logview--update-keymap)
(add-hook 'read-only-mode-hook #'logview--update-keymap nil t)
(setq font-lock-defaults
`(nil nil nil nil
(font-lock-fontify-region-function . ,'logview--fontify-region)))
;; (set (make-local-variable 'font-lock-fontify-region-function) #'logview--fontify-region)
(set (make-local-variable 'filter-buffer-substring-function) #'logview--buffer-substring-filter)
(set (make-local-variable 'isearch-filter-predicate) #'logview--isearch-filter-predicate)
(add-hook 'after-change-functions #'logview--invalidate-region-entries nil t)
(add-hook 'change-major-mode-hook #'logview--exiting-mode nil t)
(logview--guess-submode)
(logview--update-invisibility-spec)
(unless (logview-initialized-p)
(message "Cannot determine log format; press C-c C-c to choose manually or C-c C-s to customize relevant options")))
This helps a bit as if I then use this in my code:
(define-derived-mode robot-log-mode logview-mode
"Robot-Log"
"Major mode for viewing RobotFramework debug logs.
\\{robot-log-mode-map}"
;;(font-lock-add-keywords nil robot-log-font-lock-keywords t)
(setq font-lock-defaults `(robot-log-font-lock-keywords
nil nil nil
nil;(font-lock-fontify-region-function . ,'logview--fontify-region)
)
imenu-generic-expression robot-log-imenu-generic-expression))
I at least get my font lock keywords. Replacing the trailing nil
with (font-lock-fontify-region-function . ,'logview--fontify-region)
in font-lock-defaults
, I get only logview
's font lock though, so my composition problem still isn't solved.
Attached a sample debug.log file from RobotFramework to exercise the mode with: debug.log
I currently don't have time or desire to work on the mode heavily, but will accept (with necessary polish as I see fit) any sane PR you might produce. In short: logview--fontify-region
is needed for high-performance fontifying of huge log files, see commit 664c5d4. It cannot be replaced with only standard Emacs functionality. However, it can probably be improved to honor font-lock-keywords
and other standard things when those are set, likely by a submode, as in your case. I would start by looking at font-lock-default-fontify-region
and copying/adapting relevant parts into logview--fontify-region
.
Bare minimum for a PR: raw Logview must work as before, performance must not suffer (if every addition is conditioned on checking variables that will be normally nil
anyway, performance will certainly not be affected). It should also make deriving possible, but this you must be interested in yourself.
Alternatively to creating a derived mode, you can try merging at least some of functionality you need into Logview itself. However, it needs to be generic enough, i.e. of use to at least some other people too. Before heavily investing time into this, I recommend you formulate what exactly you are going to do to see if I'm interested.
Hi, and thanks for the reply.
Bare minimum for a PR: raw Logview must work as before, performance must not suffer (if every addition is conditioned on checking variables that will be normally
nil
anyway, performance will certainly not be affected). It should also make deriving possible, but this you must be interested in yourself.
If I get to it, this is the route I'll pursue since it would provide the most flexibility and add the less code to logview itself.
So I tried this, thinking perhaps if I could do my font lock things first, it'd layer on top. Unfortunately, it seems to not preserve existing font lock properties. Here's the failed attempt:
1 file changed, 14 insertions(+), 1 deletion(-)
logview.el | 15 ++++++++++++++-
modified logview.el
@@ -1051,6 +1051,14 @@ Mode is inactive when the buffer is not read-only (to not
interfere with editing) or if submode wasn't guessed
successfully.")
+(defvar logview-mode-font-lock-defaults
+ `(nil nil nil nil
+ (font-lock-fontify-region-function . ,'logview--fontify-region))
+ "The default FONT-LOCK-DEFAULTS value used by `logview-mode'.")
+
+(defun custom-font-lock-defaults-p ()
+ "Predicate to verify if FONT-LOCK-DEFAULTS has been overridden."
+ (not (equal logview-mode-font-lock-defaults font-lock-defaults)))
;;;###autoload
(define-derived-mode logview-mode nil "Logview"
@@ -3238,7 +3246,12 @@ This list is preserved across Emacs session in
(setq region-end (or (next-single-property-change region-end 'logview-entry) (point-max))))))
(remove-list-of-text-properties region-start region-end '(logview-entry fontified)))))
-(defun logview--fontify-region (region-start region-end _loudly)
+(defun logview--fontify-region (region-start region-end loudly)
+ ;; After logview's fontifying routine is done,
+ ;; invoke the normal fontifying mechanism when
+ ;; FONT-LOCK-DEFAULTS appear customized.
+ (when (custom-font-lock-defaults-p)
+ (font-lock-default-fontify-region region-start region-end loudly))
(when (logview-initialized-p)
(logview--std-temporarily-widening
;; We are very fast. Don't fontify too little to avoid overhead.
The end result is as if I hadn't changed anything. If I comment the rest of the proc (the (when ...)
tree), I see my font lock keywords applied.
I currently don't have time or desire to work on the mode
Found an old unfinished stash together with desire to finish it. If you are still interested, I can look into this font-lock stuff for 0.15 too.
Neat! I went full custom for now, due to this and other issues I was struggling with when trying to derive my mode from logview, but perhaps I could revisit this at a later point (see: https://git.sr.ht/~apteryx/emacs-robot-log/tree/main/item/robot-log.el).
See file test/logview.el
for an example of a derived mode with additional fontification. I will release 0.15 with this soon, likely in a few days.
Released Logview 0.15 with these improvements. As I haven't got any feedback, I'll assume they have fixed the problem.
Hi,
I'm trying to come up with a derived mode. This currently corresponds of a logview submode configuration (which could be integrated here) but also of additional font-lock keywords and extra navigation code (future work). It currently looks like this:
The problem I'm having is that my
font-lock-defauts
are not honored. After investigating and reading, this seems to be caused bylogview
directly overriding the internalfont-lock-fontify-region-function
procedure. Ininfo 'elisp (Font Lock Basics)'
, it says:font-lock-defaults
can be provided withOTHER-VARS
, which includesfont-lock-fontify-region-function
, so I thinklogview
should usefont-lock-defaults
to bind#'logview--fontify-region
tofont-lock-fontify-region-function
. Do you think that'd help with my issue?Thank you for this useful mode!