Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for every group of spaces at the beginning of a line

Tags:

regex

emacs

elisp

I am trying to fix the regex for a highlight indentation module for emacs. The way it works currently is by highlighting once every %s spaces:

"\\( \\) \\{%s\\}"

And here's a sample result of the behavior for some Verilog code:

enter image description here

The current regex works well for the indentations in the beginning of each line. However, there is an undesirable artifact that sequences of spaces that are not at the beginning of the line are also matched by the regex. I would like to modify the regex to match the above behavior, but only at the beginning of the line. Here's what I want (I had to manually draw it):

enter image description here

Here's what I've tried. You can see from the result that the behavior isn't right.

"^\\(\\( \\) \\{%s\\}\\) \\{1,\\}"

Result:

enter image description here

Also tried

"^\\(\\( \\) \\{%s\\}\\)\\{1,\\}"

Result:

enter image description here

Here is the relevant code, but I apologize It is not a working example. Please use the above link for a working example.

(set (make-local-variable 'highlight-indentation-current-regex)
     (format "\\( \\) \\{%s\\}" (- highlight-indentation-offset 1)))
(font-lock-add-keywords nil `((,highlight-indentation-current-regex
                               (1 'highlight-indentation-face))))
like image 413
travisbartley Avatar asked Nov 12 '22 02:11

travisbartley


1 Answers

Assuming your current font-lock rule looks like:

(,highlight-indentation-current-regex (1 'highlight-indentation-face))

you could use

(,highlight-indentation-current-regex (1 (if (save-excursion (skip-chars-backward " \t") (bolp)) 'highlight-indentation-face)))
like image 124
Stefan Avatar answered Nov 15 '22 06:11

Stefan