I'm using SampleMode and the ModeTutorial to create my own mode. I have a problem with the syntax table. As I understand, // and # start one-line comments like this:

(defvar my-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?_ "w" st)
    (modify-syntax-entry ?# "<" st)
    (modify-syntax-entry ?\n ">" st)
    (modify-syntax-entry ?/ ". 124b" st)
    (modify-syntax-entry ?\n "> b" st)
  "Syntax table for `my-mode'.")

(define-derived-mode my-mode fundamental-mode "My"
  "A major mode for editing My files."
  :syntax-table my-mode-syntax-table
  (setq-local comment-start "// ")
  (setq-local comment-end "")
  (setq-local comment-start-skip "\\s<+\\s-*")
  (setq-local font-lock-defaults
              '(my-font-lock-keywords nil t)
  (setq-local indent-line-function 'my-indent-line)
  ;; (setq-local imenu-generic-expression
  ;;             my-imenu-generic-expression)
  ;; (setq-local outline-regexp my-outline-regexp)
  ;; ...

I have a test file that contains these lines:

#pragma Index = 0

Even without my-mode-syntax-table, the #pragma is font-locked as comment. The lines starting with // appear as normal text. what-cursor-position gives me this information for the //:

             position: 89 of 47986 (0%), column: 0
            character: / (displayed as /) (codepoint 47, #o57, #x2f)
    preferred charset: iso-8859-1 (Latin-1 (ISO/IEC 8859-1))
code point in charset: 0x2F
               script: latin
               syntax: _    which means: symbol
             category: .:Base, a:ASCII, l:Latin, r:Roman
             to input: type "C-x 8 RET 2f" or "C-x 8 RET SOLIDUS"
          buffer code: #x2F
            file code: #x2F (encoded by coding system iso-latin-1-dos)
              display: by this font (glyph code)
    uniscribe:-outline-DejaVu Sans Mono-normal-normal-normal-mono-17-*-*-*-c-*-iso8859-1 (#x12)

Character code properties: customize what to show
  name: SOLIDUS
  old-name: SLASH
  general-category: Po (Punctuation, Other)
  decomposition: (47) ('/')

There are text properties here:
  charset              iso-8859-1
  fontified            t

As I understand it, this means that the syntax is not detected correctly. For reference, if I run what-cursor-position in a .cpp file on a /, the output contains:

           syntax: . 124b   which means: punctuation,
  is the first character of a comment-start sequence,
  is the second character of a comment-start sequence,
  is the second character of a comment-end sequence (comment style b)

This is right what I would like to have in my-mode, but I don't see what I do wrong...

(Just fyi: font-locking and other stuff is fine, and the mode appears on the mode-line, so I'm convinced that my-mode works ok in general...)

Many thanks for your help!

  • I can't reproduce this. When I run the code you provide and call C-u what-cursor-position I get syntax: . 124b which means: punctuation – Tyler Jul 13 '17 at 17:08
  • After your comment, I tried a minimum example at home: with that, I can't reproduce as well ... I'll strip down my mode tomorrow (I have it at work). – Markus Jul 13 '17 at 17:35
  • My problem seems to have been that I only re-evaluated the buffer containing my mode and then reloaded the testfile. After I quit emacs and restarted it, everything behaves as it should. – Markus Jul 14 '17 at 6:50

Your Answer

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

Browse other questions tagged or ask your own question.