reddit / emacs

Looking for help with define-derived-mode, I just want to add syntax highlighting to a lisp variant

chiathrowaway123 · 2022-11-16 00:57
So I have defined a set of keywords and constants, and just want to add them to the emacs-lisp-mode syntax highlighting. Here's my work so far: (defvar chialisp-modules '("mod" "defun" "defun-inline" "defmacro" "defconstant" "include" "lambda")) (defvar chialisp-keywords '("+" "-" "*" "/" "divmod" "=" ">" ">s" "not" "all" "any" "if" "i" "x" "qq" "unquote" "q" "a" "@" "f" "r" "c" "l" "sha256" "sha256tree" "concat" "strlen" "substr" "logand" "logior" "logxor" "lognot" "ash" "lsh" "point_add" "pubkey_for_exp" "softfork" "list")) (defvar chialisp-conditions '("REMARK" "AGG_SIG_UNSAFE" "AGG_SIG_ME" "CREATE_COIN" "RESERVE_FEE" "CREATE_COIN_ANNOUNCEMENT" "ASSERT_COIN_ANNOUNCEMENT" "CREATE_PUZZLE_ANNOUNCEMENT" "ASSERT_PUZZLE_ANNOUNCEMENT" "ASSERT_MY_COIN_ID" "ASSERT_MY_PARENT_ID" "ASSERT_MY_ID" "ASSERT_MY_PUZZLEHASH" "ASSERT_MY_AMOUNT" "ASSERT_SECONDS_RELATIVE" "ASSERT_SECONDS_ABSOLUTE" "ASSERT_HEIGHT_RELATIVE" "ASSERT_HEIGHT_ABSOLUTE")) (defvar chialisp-font-lock-defaults `(( ( ,(regexp-opt chialisp-modules 'words) . font-lock-function-name-face) ( ,(regexp-opt chialisp-keywords 'words) . font-lock-keyword-face) ( ,(regexp-opt chialisp-conditions 'words) . font-lock-constant-face) ))) (define-derived-mode chialisp-mode emacs-lisp-mode "Chialisp Mode" "Major Mode for chialisp" (font-lock-add-keywords nil 'chialisp-font-lock-defaults) ) (add-to-list 'auto-mode-alist '("\\.clvm\\'" . chialisp-mode)) (provide 'chialisp-mode) I'm not sure where I'm going wrong and have basically been doing trial and error for an hour. Any help would be massively appreciated :)
7890yuiop · 2022-11-16 04:36
1. Too many parens. Quoting docs: Each element in a user-level keywords list should have one of these forms: ... (MATCHER . FACENAME) ... So you wanted: (defvar chialisp-font-lock-defaults `((,(regexp-opt chialisp-modules 'words) . font-lock-function-name-face) (,(regexp-opt chialisp-keywords 'words) . font-lock-keyword-face) (,(regexp-opt chialisp-conditions 'words) . font-lock-constant-face))) Although I would use: (defvar chialisp-font-lock-defaults (list (cons (regexp-opt chialisp-modules 'words) font-lock-function-name-face) (cons (regexp-opt chialisp-keywords 'words) font-lock-keyword-face) (cons (regexp-opt chialisp-conditions 'words) font-lock-constant-face))) 2. You need to supply the keyword *list* value; not a symbol: (define-derived-mode chialisp-mode emacs-lisp-mode "Chialisp Mode" "Major Mode for chialisp" (font-lock-add-keywords nil chialisp-font-lock-defaults) )
chiathrowaway123 · 2022-11-16 05:53
Oh I MUCH prefer your way of doing this! Thanks a lot :)
7890yuiop · 2022-11-16 07:42
You're welcome. I got confused when I looked back at this, as I'd verified at the time that the two approaches were equal, yet I could see that I'd failed to quote the face names after eliminating your backquote. It turns out that Emacs defines variables of the same name for each of those faces, with the value being the same symbol, so it ended up being equivalent :) I *imagine* it's best to use the variables, so I've left the suggested code as it was, but I don't actually have any insight into this.