Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting correctly in an emacs major mode

I am developing an emacs major mode for a language (aka mydsl). However, using the techniques on xahlee's site doesn't seem to be working for some reason (possibly older emacs dialect..)

The key issues I am fighting with are (1) highlighting comments is not working and (2), the use of regexp-opt lines is not working.

I've reviewed the GNU manual and looked over cc-mode and elisp mode... those are significantly more complicated than I need.

;;;Standard # to newline comment
;;;Eventually should also have %% to %% multiline block comments

(defun mydsl-comment-dwim (arg)
  "comment or uncomment"
  (interactive "*P")
  (require 'newcomment)
  (let
      ((deactivate-mark nil)
       (comment-start "#")
       (comment-end "")
       comment-dwim arg)))

(defvar mydsl-events
  '("reservedword1"  
    "reservedword2"))

(defvar mydsl-keywords
  '("other-keyword" "another-keyword"))

;;Highlight various elements
(setq mydsl-hilite
      '(
        ; stuff between "
        ("\"\\.\\*\\?" . font-lock-string-face)
        ; : , ; { } =>  @ $ = are all special elements
        (":\\|,\\|;\\|{\\|}\\|=>\\|@\\|$\\|=" . font-lock-keyword-face)
        ( ,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face)
        ( ,(regexp-opt mydsl-events 'words) . font-lock-constant-face)
))


(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode")

(define-derived-mode mydsl-mode fundamental-mode
  "MYDSL mode is a major mode for editing MYDSL  files"
  ;Recommended by manual
  (kill-all-local-variables)
  (setq mode-name "MYDSL script")
  (setq font-lock-defaults '((mydsl-hilite)))  
  (if (null mydsl-tab-width)
      (setq tab-width mydsl-tab-width)
    (setq tab-width default-tab-width)
    )

  ;Comment definitions
  (define-key mydsl-mode-map [remap comment-dwim] 'mydsl-comment-dwim)
  (modify-syntax-entry ?# "< b" mydsl-mode-syntax-table)
  (modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table)
  ;;A gnu-correct program will have some sort of hook call here.
  )

(provide 'mydsl-mode)
like image 873
Paul Nathan Avatar asked Jun 03 '10 23:06

Paul Nathan


1 Answers

You have a couple of syntactic problems in your code, but you got it nearly correct. Here's my edited version which appears to do the right thing for a buffer in mydsl-mode:

; No changes to the simple vars
(defvar mydsl-events
  '("reservedword1"  
    "reservedword2"))

(defvar mydsl-keywords
  '("other-keyword" "another-keyword"))

;; I'd probably put in a default that you want, as opposed to nil
(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode")

;; Two small edits.
;; First is to put an extra set of parens () around the list
;; which is the format that font-lock-defaults wants
;; Second, you used ' (quote) at the outermost level where you wanted ` (backquote)
;; you were very close
(defvar mydsl-font-lock-defaults
  `((
     ;; stuff between "
     ("\"\\.\\*\\?" . font-lock-string-face)
     ;; ; : , ; { } =>  @ $ = are all special elements
     (":\\|,\\|;\\|{\\|}\\|=>\\|@\\|$\\|=" . font-lock-keyword-face)
     ( ,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face)
     ( ,(regexp-opt mydsl-events 'words) . font-lock-constant-face)
     )))

(define-derived-mode mydsl-mode fundamental-mode "MYDSL script"
  "MYDSL mode is a major mode for editing MYDSL  files"

  ;; fundamental-mode kills all local variables, no need to do it again
  (setq mode-name "MYDSL script")

  ;; you again used quote when you had '((mydsl-hilite))
  ;; I just updated the variable to have the proper nesting (as noted above)
  ;; and use the value directly here
  (setq font-lock-defaults mydsl-font-lock-defaults)

  ;; when there's an override, use it
  ;; otherwise it gets the default value
  (when mydsl-tab-width
    (setq tab-width mydsl-tab-width))

  ;; for comments
  ;; overriding these vars gets you what (I think) you want
  ;; they're made buffer local when you set them
  (setq comment-start "#")
  (setq comment-end "")

  (modify-syntax-entry ?# "< b" mydsl-mode-syntax-table)
  (modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table)
  ;;A gnu-correct program will have some sort of hook call here.
  )

(provide 'mydsl-mode)
like image 142
Trey Jackson Avatar answered Nov 20 '22 21:11

Trey Jackson