3

I'm reading a bunch of MySQL files that use # (to end-of-line) comments, but my sql-mode doesn't support them. I found the syntax-table part of sql.el that defines /**/ and -- comments, but according to this, Emacs syntax tables support only 2 comment styles.

Is there a way to add support for # comments in sql.el easily?

Martin Clarke
  • 5,636
  • 7
  • 38
  • 58
Ken
  • 5,074
  • 6
  • 30
  • 26
  • Rather than lookup zvon.org, you can use the doc that comes with your Emacs: `C-h i m elisp RET m syntax flags RET`. If you have a recentish Emacs it'll say: `A comment style is a set of flags ‘b’, ‘c’, and ‘n’, so there can be up to 8 different comment styles.` But of course, in this particular case this is irrelevant, since `#...\n` and `--...\n` are considered as 1 style (since you can't have a comment starter (or ender) that belongs to two different styles). – Stefan Nov 21 '14 at 14:28

3 Answers3

4

Emacs-24 sql.el has this built-in! Simply run M-x sql-set-product MySQL RET and the syntax table is set up automatically, as are the font-lock-keywords for all the additional reserved words and types, interactive-mode, etc, etc. Brilliant!!

Of if you look under SQL in the menubar you can use the Product submenu to select MySQL.

You can also M-x customize-variable sql-product RET to set the default product away from ANSI.

kbro
  • 4,754
  • 7
  • 29
  • 40
2

The answer by Rolf did not seem to work for me. AFAIK, the character class for starting comments, of the alternative comment style should be "< b", not " b". This what I use:

    (add-hook 'sql-mode-hook 'my-sql-mode-hook) 
    (defun my-sql-mode-hook ()   
      ;; Make # start a new line comment in SQL. This is MySQL-specific
      ;; syntax.
      (modify-syntax-entry ?# "< b" sql-mode-syntax-table))
arvidj
  • 775
  • 5
  • 25
1

You can define ?# to start comment-style b, which means there are two ways of starting the alternative comment style (either -- or #):

(setq sql-mode-syntax-table
  (let ((table (make-syntax-table)))
    ;; C-style comments /**/ (see elisp manual "Syntax Flags"))
    (modify-syntax-entry ?/ ". 14" table)
    (modify-syntax-entry ?* ". 23" table)
    ;; double-dash starts comments
    (modify-syntax-entry ?- ". 12b" table)
    (modify-syntax-entry ?# " b" table)
    (modify-syntax-entry ?\f "> b" table)
    ;; single quotes (') delimit strings
    (modify-syntax-entry ?' "\"" table)
    ;; double quotes (") don't delimit strings
    (modify-syntax-entry ?\" "." table)
    ;; backslash is no escape character
    (modify-syntax-entry ?\\ "." table)
    table))

(This was copied from sql.el and modified, which means that this is GPL)

Rolf Rander
  • 3,221
  • 20
  • 21