0

I am trying to make simple major mode for syntax highlighting using define-generic-mode. I found that

(define-generic-mode 'mytest-mode
  '("//") nil
  '(
    ((regexp-opt '("int" "string" "bool")) . 'font-lock-type-face)
    )
  nil nil "mytest mode"
  )

is not working. But if I replace regexp-opt call with its manually calculated result, then all works as expected:

(define-generic-mode 'mytest-mode
  '("//") nil
  '(
    ("\\(?:bool\\|int\\|string\\)" . 'font-lock-type-face)
    )
  nil nil "mytest mode"
  )

So, why I cannot just put regexp-opt call in the mode definition?

EDIT

Hint about forced evaluation of items in quoted list from Lindidancer's answer:

(define-generic-mode 'mytest-mode
  '("//") nil
  '(
    (,(regexp-opt '("int" "string" "bool")) 'font-lock-type-face)
    )
  nil nil "mytest mode"
  )

doesn't help: no errors on mode activation but no highlighting also

Second hint about use list function to form lists:

(define-generic-mode 'mytest-mode
  '("//") nil
  (list
    ((regexp-opt '("int" "string" "bool")) 'font-lock-type-face)
    )
  nil nil "mytest mode"
  )

gives error on activating mode: (invalid-function (regexp-opt (quote ("int" "string" "bool"))))

same error when trying evaluate:

  (list
    ((regexp-opt '("int" "string" "bool")) 'font-lock-type-face)
    )

in scratch buffer.

EDIT 1

(list (list (regexp-opt '("int" "string" "bool")) 'font-lock-type-face))

doesn't help also - no errors, no highlighting.

EDIT 2

Steps, what I exactly do, are:

  1. Execute define-generic-mode call in the *Scratch* buffer
  2. Switch to buffer with some keywords under test
  3. M-x mytest-mode
shibormot
  • 1,638
  • 2
  • 12
  • 23

2 Answers2

2

It's because the call to regexp-opt is inside a quoted list, so it isn't seen as a function call.

You can either create the list using functions like list or use backquotes, where a , means that the next form should be evaluated.

`(
   (,(regexp-opt '("int" "string" "integer" "bool" "boolean" "float")) . 'font-lock-type-face)
)
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Thank you, your comma syntax return no errors but doesn't highlight text. When i try `(list ((regexp-opt '("int" "string" "bool")) 'font-lock-variable-name-face))` then I recieve `(invalid-function (regexp-opt (quote....` – shibormot Aug 22 '16 at 12:33
  • It should probably be `(list (list (regexp-opt ...) ' font-lock-type-face))`. – Lindydancer Aug 22 '16 at 12:46
  • `(list (list (...` doesn't help also. Updated question. – shibormot Aug 22 '16 at 13:03
  • 1
    Note the `\`` vs. `'` used to quote the list. You need to use a backtick instead of single quote. This is also known as quasiquote. – jpkotta Aug 22 '16 at 16:14
1
(define-generic-mode 'mytest-mode
  '("//") nil
  `(
    (,(regexp-opt '("int" "string" "bool")) . 'font-lock-type-face)
    )
  nil nil "mytest mode"
  )
Raoul HATTERER
  • 522
  • 5
  • 5