tags:

views:

313

answers:

4

I'm trying to add a special markup to Python documentation strings in emacs (python-mode).

Currently I'm able to extract a single line with:

(font-lock-add-keywords
 'python-mode
 '(("\\(\"\\{3\\}\\.+\"\\{3\\}\\)"
    1 font-lock-doc-face prepend)))

This works now:

"""Foo"""

But as soon there is a newline like:

"""
Foo

"""

It doesn't work anymore. This is logical, since . doesn't include newlines (\n). Should I use a character class?

How can I correct this regular expression to include everything between """ """?

Thanks in advance!

A: 

I am not sure for this regex engine, but may be the use of:

(?:.|[\r\n])+

would work instead of just .+.

You need to add the right \\ in front of the (
(also maybe before the [ and other characters: this is the part where I am not sure)

VonC
+2  A: 
"\\(\"\\{3\\}\\(.*\n?\\)*?\"\\{3\\}\\)"

The "*?" construct is the non-greedy version of "*".

huaiyuan
For some strange reason my emacs hangs when using this regexp.Hangs at `Loading vc...done`
wunki
+1  A: 

A newline in emacs regexps is entered by C-q C-j so just stick a group containing . and C-q C-j into your regexp. As I use regexp-tool to build them, mine isn't fully quoted as yours, but I hope the changes are obvious.

\("\{3\}\(.\|C-qC-j\)+"\{3\}\)

Sorry I can't format it better, stackoverflow doesn't agree with me.

Also it will probably display as a newline in emacs.

JBF
A: 

This works half:

(font-lock-add-keywords
     'python-mode
      '(("\\(\"\\{3\\}\\(.\\|\n\\)*?\"\\{3\\}\\)" 
         1 font-lock-warning-face prepend)))

But when adding RET's the markup is gone.

The suggested regexp "\\(\"\\{3\\}\\(.*\n?\\)*?\"\\{3\\}\\)" makes my emacs hang when opening a .py file.

Maybe it's time to visit the emacs mailinglist..

wunki
\n isn't newline in emacs
JBF