Dave Lawrence
Dave Lawrence

Reputation: 1291

How to set emacs line color based on start character

I use emacs day to day, but I'm not a lisp expert. How can I define a simple mode to color the lines in a buffer according to the first character?

e.g. if it's an 'p' then text is green, if it's an 'i' then the text is red. ... I have about about half a dozen rules I would like to define.

This mode only applies to a very specific text file, I obviously don't need functionality in other situations!

Thanks,

Upvotes: 2

Views: 304

Answers (1)

Dave Lawrence
Dave Lawrence

Reputation: 1291

Thanks to aartist, here is what I ended up with

(defface mymode-q '((t :foreground "red" )) "" )
(defface mymode-p '((t :foreground "green" )) "" )
(defface mymode-i '((t :foreground "yellow" )) "" )
(defface mymode-r '((t :foreground "blue" )) "" )
(defface mymode-c '((t :foreground "orange" )) "" )
(defface mymode-x '((t :foreground "grey50" )) "" )

(setq mymode-highlights '(
    ("^q .*$" . 'mymode-q)
    ("^p .*$" . 'mymode-p)
    ("^i .*$" . 'mymode-i)
    ("^r .*$" . 'mymode-r)
    ("^c .*$" . 'mymode-c)
    ("^x .*$" . 'mymode-x)
    ))

(define-derived-mode mymode-mode text-mode "mymode" "major mode mymode."
    (setq font-lock-defaults '(mymode-highlights)))

Upvotes: 2

Related Questions