I'm trying to create a major mode that handles the following syntax:

  function someName(value1, value2)
    # some contents
  end function

I'm using the following to highlight the keywords function, and end function.

(define-derived-mode my-mode prog-mode "My Mode"
  "Major mode for editing my mode code."
  (setq-local font-lock-defaults

How can I target the function name (someName) and its arguments with font lock (value1, value2)?

1 Answer 1


In my "my-mode-font-lock" function, this is what I ended up using:

; This regexp matches "function someName(", and sets a font face to "someName"
("^[[:space:]]*?sub\\(?:[[:space:]]*\\)?\\([[:word:]]+\\)?\\(?:[^( \t\n]\\)?"
 (1 font-lock-function-name-face)

 ; This starts at the end of the previous regexp
 ; It matches everything up to "," or ")" - whichever comes first
 ; The ".*" is there to allow extra information, such as "someName(value1 as String)"
    (goto-char (match-end 0))
  ; Once a match was found, it tries the find another, beginning at the end of the last result
  (goto-char (match-end 0))
  (1 font-lock-variable-name-face)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.