Separate font-lock keywords that are only for M files, and those that

work fine for files and the shell.
matlab.el:
(matlab*-font-lock-keywords): Use defconst.
(matlab-font-lock-keywords): Rename to:
(matlab-basic-font-lock-keywords): New
and remove lines to:
(matlab-file-basic-font-lock-keywords): New.
(matlab-gaudy-font-lock-keywords): Rename to:
(matlab-file-gaudy-font-lock-keywords): New
and only use keywords for -file- and both.
(matlab-mode): Use new named keywords.

matlab-shell.el:
(matlab*-font-lock-keywords): Use defconst.
Use only keywords from matlab.el meant for both files and shell.
(matlab-shell-object-output-font-lock-keywords): New
This commit is contained in:
Eric Ludlam 2021-02-27 11:41:29 -05:00
parent 6dbdf778f9
commit 3fd09f43f5
2 changed files with 94 additions and 50 deletions

View file

@ -204,10 +204,9 @@ If multiple prompts are seen together, only call this once.")
;;; Font Lock
;;
;; Extra font lock keywords for the MATLAB shell.
(defvar matlab-shell-font-lock-keywords
(defconst matlab-shell-font-lock-keywords
(list
;; Startup notices
;; Various notices
'(" M A T L A B " 0 'underline)
'("All Rights Reserved" 0 'italic)
'("\\(\\(?:(c)\\)?\\s-+Copyright[^\n]+\\)" 1 font-lock-comment-face)
@ -233,18 +232,41 @@ If multiple prompts are seen together, only call this once.")
"Additional keywords used by MATLAB when reporting errors in interactive\
mode.")
(defvar matlab-shell-font-lock-keywords-1
(append matlab-font-lock-keywords matlab-shell-font-lock-keywords)
"Keyword symbol used for font-lock mode.")
(defconst matlab-shell-font-lock-keywords-1
(append matlab-basic-font-lock-keywords
matlab-shell-font-lock-keywords)
"Keyword symbol used for basic font-lock for MATLAB shell.")
(defvar matlab-shell-font-lock-keywords-2
(append matlab-shell-font-lock-keywords-1 matlab-gaudy-font-lock-keywords)
"Keyword symbol used for gaudy font-lock symbols.")
(defconst matlab-shell-object-output-font-lock-keywords
(list
;; disp of objects usually looks like this:
'("^\\s-*\\(\\w+\\) with properties:" (1 font-lock-type-face))
;; object output - highlight property names after 'with properties:' indicator
;; NOTE: Normally a block like this would require us to use `font-lock-multiline' feature
;; but since this is shell output, and not a thing you edit, we can skip it and rely
;; on matlab-shell dumping the text as a unit.
'("^\\s-*\\(\\w+ with properties:\\)\n\\s-*\n"
("^\\s-*\\(\\w+\\):[^\n]+$" ;; match the property before the :
;; Extend search region across lines.
(save-excursion (re-search-forward "\n\\s-*\n" nil t)
(beginning-of-line)
(point))
nil
(1 font-lock-variable-name-face)))
'("[[{]\\([0-9]+\\(?:x[0-9]+\\)+ \\w+\\)[]}]" (1 font-lock-comment-face))
)
"Highlight various extra outputs that are typical for MATLAB.")
(defvar matlab-shell-font-lock-keywords-3
(defconst matlab-shell-font-lock-keywords-2
(append matlab-shell-font-lock-keywords-1
matlab-function-font-lock-keywords
matlab-shell-object-output-font-lock-keywords)
"Keyword symbol used for gaudy font-lock for MATLAB shell.")
(defconst matlab-shell-font-lock-keywords-3
(append matlab-shell-font-lock-keywords-2
matlab-really-gaudy-font-lock-keywords)
"Keyword symbol used for really gaudy font-lock symbols.")
"Keyword symbol used for really gaudy font-lock for MATLAB shell.")
;;; ROOT
;;

102
matlab.el
View file

@ -1126,8 +1126,17 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
(mapconcat (lambda (s) s) keywordlist "\\|"))
"\\)\\>"))
;; font-lock keywords
(defvar matlab-font-lock-keywords
;;; Font Lock keyword handling
;;
;; Many parts of the keyword handling are shared with matlab-shell.
;; The matlab based variables here are divided up between generic keywords
;; and keywords only for M files. This means the M shell won't highlight
;; some syntaxes like classdef stuff even though someone might paste them in.
;;
;; matlab-*-keywords -- MATLAB Files or Shell
;; matlab-file-*-keywords -- MATLAB Files only
(defconst matlab-basic-font-lock-keywords
(list
;; charvec and string quote chars are also used as transpose, but only if directly
;; after characters, numbers, underscores, or closing delimiters.
@ -1141,37 +1150,10 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
;; General keywords
(list (matlab-font-lock-regexp-opt matlab-keyword-list)
'(0 font-lock-keyword-face))
;; Keywords that should be the first word on a line
(list (concat "^\\s-*\\("
(matlab-font-lock-regexp-opt matlab-keyword-first-on-line-list)
"\\)")
'(1 font-lock-keyword-face))
;; The end keyword is only a keyword when not used as an array
;; dereferencing part.
'("\\(^\\|[;,]\\)[ \t]*\\(end\\)\\b"
2 (if (matlab-valid-end-construct-p) font-lock-keyword-face nil))
;; How about unreachable code? MUST BE AFTER KEYWORDS in order to
;; get double-highlighting.
'(matlab-find-unreachable-code
(1 'underline prepend) ;if part
(2 'underline prepend) ;end part
(3 'underline prepend) ;else part (if applicable)
(4 font-lock-comment-face prepend) ;commented out part.
)
;; block comments need to be commented out too!
'(matlab-find-block-comments
(1 font-lock-comment-face prepend) ; commented out
(2 'underline prepend)
(3 'underline prepend) ;the comment parts
)
;; Cell mode breaks get special treatment
'("^\\s-*\\(%%[^\n]*\n\\)" (1 matlab-cellbreak-face append))
;; Highlight cross function variables
'(matlab-font-lock-cross-function-variables-match
(1 matlab-cross-function-variable-face prepend))
;; Highlight nested function/end keywords
'(matlab-font-lock-nested-function-keyword-match
(0 matlab-nested-function-keyword-face prepend))
;; The global keyword defines some variables. Mark them.
'("^\\s-*global\\s-+"
("\\(\\w+\\)\\(\\s-*=[^,; \t\n]+\\|[, \t;]+\\|$\\)"
@ -1187,13 +1169,46 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
;; Imaginary number support
'("\\<[0-9]\\.?\\(i\\|j\\)\\>" 1 font-lock-reference-face)
)
"Expressions to highlight in MATLAB mode.")
"Basic Expressions to highlight in MATLAB mode or shell.")
(defconst matlab-file-basic-font-lock-keywords
(append
matlab-basic-font-lock-keywords
(list
;; Keywords that should be the first word on a line
(list (concat "^\\s-*\\("
(matlab-font-lock-regexp-opt matlab-keyword-first-on-line-list)
"\\)")
'(1 font-lock-keyword-face))
;; How about unreachable code? MUST BE AFTER KEYWORDS in order to
;; get double-highlighting.
'(matlab-find-unreachable-code
(1 'underline prepend) ;if part
(2 'underline prepend) ;end part
(3 'underline prepend) ;else part (if applicable)
(4 font-lock-comment-face prepend) ;commented out part.
)
;; block comments need to be commented out too!
'(matlab-find-block-comments
(1 font-lock-comment-face prepend) ; commented out
(2 'underline prepend)
(3 'underline prepend) ;the comment parts
)
;; Cell mode breaks get special treatment
'("^\\s-*\\(%%[^\n]*\n\\)" (1 matlab-cellbreak-face append))
;; Highlight cross function variables
'(matlab-font-lock-cross-function-variables-match
(1 matlab-cross-function-variable-face prepend))
;; Highlight nested function/end keywords
'(matlab-font-lock-nested-function-keyword-match
(0 matlab-nested-function-keyword-face prepend))
))
"Basic Expressions to highlight in MATLAB Files.")
(defconst matlab-function-arguments
"\\(([^)]*)\\)?\\s-*\\([,;\n%]\\|$\\)")
(defvar matlab-function-font-lock-keywords
(defconst matlab-function-font-lock-keywords
(list
;; defining a function, a (possibly empty) list of assigned variables,
;; function name, and an optional (possibly empty) list of input variables
@ -1239,7 +1254,7 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
"\\s-*\\(?2:(\\([^)]+\\))\\|\\)"
"Regular expression for matching an attributes block.")
(defvar matlab-class-font-lock-keywords
(defconst matlab-file-class-font-lock-keywords
(list
;; Classdefs keyword and the class name
(list (concat "^\\s-*\\(classdef\\)"
@ -1273,17 +1288,17 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
)
"List of font-lock keywords used when an MATLAB file contains a class.")
(defvar matlab-gaudy-font-lock-keywords
(defconst matlab-file-gaudy-font-lock-keywords
(append
matlab-font-lock-keywords
matlab-basic-font-lock-keywords
matlab-file-basic-font-lock-keywords
matlab-function-font-lock-keywords
matlab-class-font-lock-keywords
matlab-file-class-font-lock-keywords
)
"Expressions to highlight in MATLAB mode.")
(defvar matlab-really-gaudy-font-lock-keywords
(defconst matlab-really-gaudy-font-lock-keywords
(append
matlab-gaudy-font-lock-keywords
(list
;; Since it's a math language, how bout dem symbols?
'("\\([<>~]=?\\|\\.[/*^']\\|==\\|\\<xor\\>\\|[-!^&|*+\\/~:]\\)"
@ -1316,6 +1331,13 @@ Uses `regex-opt' if available. Otherwise creates a 'dumb' expression."
))
"Expressions to highlight in MATLAB mode.")
(defconst matlab-file-really-gaudy-font-lock-keywords
(append
matlab-file-gaudy-font-lock-keywords
matlab-really-gaudy-font-lock-keywords
)
"Expressions to highlight in MATLAB mode.")
;; Imenu support.
(defvar matlab-imenu-generic-expression
'((nil "^\\s-*function\\>[ \t\n.]*\\(\\(\\[[^]]*\\]\\|\\sw+\\)[ \t\n.]*\
@ -1497,9 +1519,9 @@ All Key Bindings:
;; give each file it's own parameter history
(make-local-variable 'matlab-shell-save-and-go-history)
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults '((matlab-font-lock-keywords
matlab-gaudy-font-lock-keywords
matlab-really-gaudy-font-lock-keywords
(setq font-lock-defaults '((matlab-file-font-lock-keywords
matlab-file-gaudy-font-lock-keywords
matlab-file-really-gaudy-font-lock-keywords
)
t ; do not do string/comment highlighting
nil ; keywords are case sensitive.