0

I am attempting to develop an Emacs major mode for writing Tecplot macros. The Tecplot macro language has lots of peculiar constructs, such as a # sign for comments and $! for the beginning of a macro command. When I use the following code to define the syntax table, I get an invalid character error for the comments:

(defconst tecplot-macro-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; ' is a string delimiter
    (modify-syntax-entry ?' "\"" table)
    ;; " is a string delimiter too
    (modify-syntax-entry ?\" "\"" table)
    ;; " is a string delimiter too
    (modify-syntax-entry ?| "|" table)

    ;; / is punctuation, but # is a comment starter
    (modify-syntax-entry ?# ". 1b" table)
    table))

The error I get is:

**Lisp error: (error "Invalid syntax description letter: #")**

How do I get the # sign to be resolved as a comment? Am I going to have similar issues with the $!?

  • Derive a new mode from another, similar mode using define-derived-mode: don't build it from scratch. E.g. python uses # for comments. It may not be the best one to use, but you can see how it does it. – NickD Mar 3 at 19:29
  • I don't see how you would get this error for this code. Are you sure it wasn't a slightly different version of the code? ". 1b" isn't what you want since it declares that # is the start of a two-character comment sequence (like / in C where comments are /*…*/), but it's valid. – Gilles 'SO- stop being evil' Mar 3 at 22:19
1

Stefan beat me with his answer. Nevertheless, I have got a basis for you where you can start with:

(defconst tecplot-macro-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; ' is a string delimiter
    (modify-syntax-entry ?' "\"" table)
    ;; " is a string delimiter too
    (modify-syntax-entry ?\" "\"" table)
    ;; |...| delimits variable names
    (modify-syntax-entry ?| "_" table)
    ;; $!... delimits macro calls
    (modify-syntax-entry ?$ "_" table)
    (modify-syntax-entry ?! "_" table)

    ;; / is punctuation, but # is a comment starter
    (modify-syntax-entry ?# "<" table)
    (modify-syntax-entry ?\n ">" table)
    table))

(defvar tecplot-macro-mode-keywords
  '(("$![[:alpha:]]+" . font-lock-function-name-face)
    ("|\\(?:[[:alpha:]]\\|\\s_\\)+|" . font-lock-variable-name-face))
  "List of keywords for `tecplot-macro-mode'.")

(define-derived-mode tecplot-macro-mode prog-mode "Tecplot"
  "Mode for editing Tecplot macros."
  :syntax-table tecplot-macro-mode-syntax-table
  (setq font-lock-defaults '(tecplot-macro-mode-keywords))
  (font-lock-mode)
  )
| improve this answer | |
  • The :syntax keyword seems redundant. And please don't forcefully emnable font-lock-mode from the major. It's enabled by default anyway, so there's no point making life more difficult for those who explicitly disable font-lock. – Stefan Mar 4 at 0:21
1

Use ?\# instead of ?#. This won't be needed for ! nor $ because these chars happen not be special for Elisp, whereas # is special (e.g. it is used for hexadecimal numbers like #xFF), but you can also use ?\! and it won't hurt.

BTW, if |...| is a string, you can use "\"" rather than "|" as its syntax (BTW, there's a copy&paste error in your comment about |).

The 1b says that # is the first char of a two-chars comment starter (e.g. if a comment starter takes the form #| then you'd put a 2 into the syntax of |), but in your case it seems like you just want to use "<" for ?\#.

| improve this answer | |
  • I tried the ?\# in the second argument, and that works providing I do not use any form of # in the third argument, such as (modify-syntax-entry ?\# "\#" table). I tried different options for the third argument, and found "<" to be the only option that doesn't produce an error. But with this, the $! is seen as comment, not a command. – Stephen Alter Mar 3 at 20:58
  • 1
    Hi Stefan. ?# works. It is even in the Elisp manual. See the "defvar lisp-mode-syntax-table" example. – Tobias Mar 3 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.