reddit / emacs

Debugging fontification error while writing a new mode

idspispopd0 · 2022-12-18 17:51
I'm writing a `shell-session-mode` to be able to create org-mode source blocks similar to GitHub Flavored Markdown's: ```console $ command output more output $ another command output ... ``` All I want is for the `$ command`s to be colorized. I haven't created an emacs mode before so I'm following https://stackoverflow.com/questions/14885670/create-a-new-mode-in-emacs and of course the Emacs Lisp info pages. However when I try to use the mode in org mode's source block: #+begin_src shell-session #+end_src I immediately get the error in the Emacs mode line: Org mode fontification error in #<buffer foo.org> at 22 I can't seem to invoke a debugger at this error. Using `(setq debug-on-error t)` seems to have no effect. Here is my `shell-session-mode.el` file: ;;; shell-session --- Markup shell commands with their output. ;;; ;;; Commentary: ;;; ;;; Colorizes blocks similar to "console" in GitHub Flavored Markdown ;;; which is defined as "ShellSession" in ;;; https://github.com/github/linguist which uses the regexp in: ;;; https://github.com/atom/language-shellscript/blob/master/grammars/shell-session.cson ;;; ;;; Code: ;;; ;;; Include this in your .emacs init file: ;;; ;;; (require 'shell-session-mode) (defcustom shell-session-prompt-regexp (regexp-quote "^[^[:space:]]*$[[:space:]]+[^[:space:]]+$") "Regexp matching a shell $ prompt with a command." :type 'regexp :group 'shell-session) (define-derived-mode shell-session-mode fundamental-mode "shell-session-mode" "A major mode to display shell sessions." :group 'shell-session (font-lock-add-keywords nil '((shell-session-prompt-regexp . font-lock-keyword-face)))) (add-to-list 'auto-mode-alist '("\\.sh-session\\'" . shell-session-mode)) (provide 'shell-session-mode) ;;; shell-session-mode.el ends here. I have the file located in my `~/.emacs.d/lisp` and load it with use-package: (use-package shell-session-mode :load-path "lisp" :ensure nil)
yantar92 · 2022-12-18 19:32
https://github.com/Lindydancer/font-lock-studio Also, `jit-lock-debug-mode`. Also, M-x font-lock-debug-fontify Also, `backtrace-on-redisplay-error` (Emacs 29)
idspispopd0 · 2022-12-18 22:37
Thank you so much! I figured out the problems. 1. First, it's apparently a really bad idea to use `(regexp-quote ...)` because it will add quotes things that break the intended regular expressions and so one can use the function to figure out whether it's really necessary to quote some symbols or not. 1. Also it seems that enclosing the string with `\\<` and `\\>` is necessary to treat the string as a regular expression. 1. `font-lock-studio` also had some trouble finding my `defcustom` symbol, so I ended up directly substituting it: `(font-lock-add-keywords nil '(("^\\<.*\\$ .*\\>" . font-lock-keyword-face))))`
yantar92 · 2022-12-19 05:20
> Also it seems that enclosing the string with \\< and \\> is necessary to treat the string as a regular expression. No. I recommend to play around with regexp-builder and see what actually matches your regexp. > font-lock-studio also had some trouble finding my defcustom symbol No. You just cannot use symbol there. Emacs treats it is as a function to be called then. You had to expand the value \`(,symbol . face)
idspispopd0 · 2022-12-19 11:03
Wow, thank you so much! I didn't know about `M-x regexp-builder`. That makes it so much quicker to play with the regexp now. Also thank you for confirming the need to expand the value of the matcher inside the keywords list passed to `font-lock-add-keywords` and Emacs' fallback of treating the matcher as a function. Edit: Also I just found `\<` and `\>` described in `C-h i Emacs m Regexp Backslash`. They are word boundary left- and right-matchers.
yantar92 · 2022-12-19 12:00
You may consider using `rx` macro for regexps. It is less cryptic.