user52366
user52366

Reputation: 1147

emacs major mode definition

I am trying to write a very basic emacs major mode to edit assembler source files (for a specific microcontroller). I used https://www.emacswiki.org/emacs-test/ModeTutorial as a starting point. It works but I would like to change two things but can't find a solution.

And yes, I am keen to develop a more thorough understanding of eLisp with time, but I also try to quickly hack my way to a working mode definition because I need it now (without really understanding the intricacies - I know, RTFM...).

Anyway, I hope to get an answer to two questions:

1) While syntax highlighting works, I don't get it to be case-insensitive. I tried adding (setq font-lock-keywords-case-fold-search t) to the function below, but it does not seem to have the desired effect.

(defun xasm-mode ()
   (interactive)
   (kill-all-local-variables)
   (use-local-map xasm-mode-map)         
   (set-syntax-table xasm-mode-syntax-table)
   ;; set up font-lock
   (set (make-local-variable 'font-lock-defaults) '(xasm-font-lock-keywords)) 
   (setq font-lock-keywords-case-fold-search t)
   (setq major-mode 'xasm-mode)
   (setq mode-name "XASM")       
   (run-hooks 'xasm-mode-hook))

2) The second question for sure demonstrates my ignorance... Basically, in an expression with the structure below, I'd like to substitute the "regexp" literal (which works) by a value that is in a variable (here: x, with x being (correctly) computed by regexp-opt)... But I don't know how to insert the value of x here :-(

(setq x 'xyz)
(defconst v2
  (list
     '( "regexp" . foo)
))

Thanks for your hints.

Upvotes: 2

Views: 412

Answers (2)

Stefan
Stefan

Reputation: 28571

Check the documentation with C-h v font-lock-defaults RET:

... Defaults should be of the form:

(KEYWORDS [KEYWORDS-ONLY [CASE-FOLD [SYNTAX-ALIST ...]]]) ...

Which tells you that you can set the "case-fold" behavior right there:

(set (make-local-variable 'font-lock-defaults)
     '(xasm-font-lock-keywords nil t))

Also, do yourself a favor and use define-derived-mode (and update whichever doc you found that pointed you to defun+interactive+setqmode-name+... so it refers to define-derived-mode as well).

Upvotes: 3

Brian Malehorn
Brian Malehorn

Reputation: 2685

While syntax highlighting works, I don't get it to be case-insensitive. I tried adding (setq font-lock-keywords-case-fold-search t) to the function below, but it does not seem to have the desired effect.

Perhaps it's because you use defun instead of using the more-modern define-derived-mode:

(define-derived-mode xasm-mode prog-mode "XASM"
  ;; set up font-lock
  (set (make-local-variable 'font-lock-defaults) '(xasm-font-lock-keywords)) 
  (set (make-local-variable 'font-lock-keywords-case-fold-search) t))

If that doesn't work, I guess you'll just have to make it insensitive by hand, [a-z] -> [a-zA-Z], etc.

The second question for sure demonstrates my ignorance... Basically, in an expression with the structure below, I'd like to substitute the "regexp" literal (which works) by a value that is in a variable (here: x, with x being (correctly) computed by regexp-opt)... But I don't know how to insert the value of x here :-(

Use the comma operator as legoscia pointed out. In your case, it will look like this:

(defconst v2
  `(
    ( "regexp" . ,foo)
    ))

To evaluate foo inside the backtick, we put a comma in front of it, ,foo.

Upvotes: 2

Related Questions