My goal is to color specific keywords as function definition such as log in Python-mode to a spefic color or color of functions.

Example, pattern for log("...."): coloring only log.

from utils import log  # no color for log
logging.info("hello")  # no color for log
log("hello world")  # color for log

I have tried follows answer for shell-mode alias keyword is not recognized as font-lock-builtin-face type but it colors all the log string.

(font-lock-add-keywords 'python-mode
                        '(("log" . font-lock-builtin-face)))

I think you want something like this where you only apply the face to part of the match, in this case we mark log as group 1, and only apply the face to it. This pattern won't match the other places you show log above.

(font-lock-add-keywords 'python-mode
                        '(("\\(log\\)(.*)" 1 font-lock-builtin-face)))
  • When I type log( than new line than type a string than again new line and type ); it does not color log ; could this be added as well? something like '(("\\(log\\)(.\n*)" – alper Mar 4 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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