Regex to match underscore in function name in Python Mode?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Regex to match underscore in function name in Python Mode?

Nate Bargmann
In C mode I have the following in my ~/.emacs that will highlight a
function name:

;; Highlight punctuation in C mode
(add-hook
 'c-mode-common-hook
 (lambda ()
   (font-lock-add-keywords
    nil
    '(("[<>:&*=+^%!~,.?;/-]"
       0 font-lock-warning-face nil)))
   ))

In C mode this will highlight all characters in a function name (left of
the opening parentheses) to the word boundary including underscores.  I
am attempting to use the same for python mode, however, the regex matches
up to the first underscore, if there is one, but no further.  I'll admit
to not be intimately familiar with Emacs regexes and I did try re-builder
and was unable to get a match on the underscore.

Is it possible that Python Mode is blocking the regex from matching
underscores?

TIA

- Nate

--

"The optimist proclaims that we live in the best of all
possible worlds.  The pessimist fears this is true."

Ham radio, Linux, bikes, and more: http://www.n0nb.us
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

John Mastro
Nate Bargmann wrote:

> In C mode I have the following in my ~/.emacs that will highlight a
> function name:
>
> ;; Highlight punctuation in C mode
> (add-hook
>  'c-mode-common-hook
>  (lambda ()
>    (font-lock-add-keywords
>     nil
>     '(("[<>:&*=+^%!~,.?;/-]"
>        0 font-lock-warning-face nil)))
>    ))

I think you may have posted the wrong snippet? This seems to fontify
punctuation characters, not function names.

> In C mode this will highlight all characters in a function name (left
> of the opening parentheses) to the word boundary including
> underscores. I am attempting to use the same for python mode, however,
> the regex matches up to the first underscore, if there is one, but no
> further. I'll admit to not be intimately familiar with Emacs regexes
> and I did try re-builder and was unable to get a match on the
> underscore.
>
> Is it possible that Python Mode is blocking the regex from matching
> underscores?

Is the goal to fontify function names at use sites (e.g. the `foo` in
`foo(0)`) in addition to declaration and definition sites (where they're
already fontified by the major mode)?

        John

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Skip Montanaro
In reply to this post by Nate Bargmann
I
am attempting to use the same for python mode, however, the regex matches
up to the first underscore, if there is one, but no further.


Are you sure underscores are considered weird characters? I recall some
disagreement between the GNU folks and the Python folks about that.

Skip
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Emanuel Berg-4
Skip Montanaro wrote:

> Are you sure underscores are considered weird
> characters? I recall some disagreement
> between the GNU folks and the Python folks
> about that.

I think the disagreement was between the
underscore convention, sometimes_seen_in_C, and
the "camel case" convention, which
is recommendedForJava.

We lispers don't use either, we use dashes and
normal language and
no-doubt-it-is-the-best-way :)

But as have been said, doesn't the Python major
mode come with highlighting function names with
the `font-lock-function-name-face'?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

John Mastro
In reply to this post by John Mastro
John Mastro <[hidden email]> wrote:

>> In C mode I have the following in my ~/.emacs that will highlight a
>> function name:
>>
>> ;; Highlight punctuation in C mode
>> (add-hook
>>  'c-mode-common-hook
>>  (lambda ()
>>    (font-lock-add-keywords
>>     nil
>>     '(("[<>:&*=+^%!~,.?;/-]"
>>        0 font-lock-warning-face nil)))
>>    ))
>
> I think you may have posted the wrong snippet? This seems to fontify
> punctuation characters, not function names.

[snip]

> Is the goal to fontify function names at use sites (e.g. the `foo` in
> `foo(0)`) in addition to declaration and definition sites (where they're
> already fontified by the major mode)?

I tried this:

(add-hook
 'python-mode-hook
 (lambda ()
   (font-lock-add-keywords
    nil
    '(("\\(\\(?:\\sw\\|\\s_\\|\\.\\)+\\)("
       1 font-lock-function-name-face nil)))))

I'm not sure it's exactly the same thing you're trying to do, but
underscores didn't pose any problems for highlighting the full symbol.
Perhaps your regular expressions just isn't quite right.

        John

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Emanuel Berg-4
John Mastro wrote:

> I tried this:
>
> (add-hook 'python-mode-hook (lambda ()
> (font-lock-add-keywords nil
> '(("\\(\\(?:\\sw\\|\\s_\\|\\.\\)+\\)(" 1
> font-lock-function-name-face nil)))))

Interesting :)

Here is another way to do it:

Create a file, e.g. display.py .

Open it in Emacs. Notice that the file
extention is used to automatically put the
editor in Python mode (defined in python.el).

Now, write the following:

    def display_string():
        print("This string is for display purposes ONLY.")

    display_string()

Watch out! "display_string" is in
`font-lock-function-name-face'!

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

John Mastro
Emanuel Berg <[hidden email]> wrote:

>> I tried this:
>>
>> (add-hook 'python-mode-hook (lambda ()
>> (font-lock-add-keywords nil
>> '(("\\(\\(?:\\sw\\|\\s_\\|\\.\\)+\\)(" 1
>> font-lock-function-name-face nil)))))
>
> Interesting :)
>
> Here is another way to do it:
>
> Create a file, e.g. display.py .
>
> Open it in Emacs. Notice that the file
> extention is used to automatically put the
> editor in Python mode (defined in python.el).
>
> Now, write the following:
>
>     def display_string():
          ^^^^^^^^^^^^^^

By default, only this one is fontified with
font-lock-function-name-face.

>         print("This string is for display purposes ONLY.")
>
>     display_string()
      ^^^^^^^^^^^^^^

The snippet I posted causes this one to be fontified too.

> Watch out! "display_string" is in
> `font-lock-function-name-face'!

Since python-mode already fontifies the function name at definition
sites, I presume the OP is asking about having it fontified
"everywhere". However, he hasn't explained in detail.

        John

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Emanuel Berg-4
John Mastro wrote:

>> def display_string():
>      ^^^^^^^^^^^^^^
>
> By default, only this one is fontified with
> font-lock-function-name-face.

... yes?

>> Watch out! "display_string" is in
>> `font-lock-function-name-face'!
>
> Since python-mode already fontifies the
> function name at definition sites, I presume
> the OP is asking about having it fontified
> "everywhere". However, he hasn't explained
> in detail.

Don't help him. It is a bad idea. Or help him,
so he will realize it is a bad idea. And learn
something about regexps in the bargain.

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Nate Bargmann-2
In reply to this post by John Mastro
On Mon, 24 Jul 2017 11:38:49 -0700, John Mastro wrote:

> I tried this:
>
> (add-hook
>  'python-mode-hook (lambda ()
>    (font-lock-add-keywords
>     nil '(("\\(\\(?:\\sw\\|\\s_\\|\\.\\)+\\)("
>        1 font-lock-function-name-face nil)))))
>
> I'm not sure it's exactly the same thing you're trying to do, but
> underscores didn't pose any problems for highlighting the full symbol.
> Perhaps your regular expressions just isn't quite right.

Thanks, John!

Yes, I had posted the wrong function originally.  So much for proof
reading, eh?

This matches everything from the word boundary to the parentheses, which
is a bit much.  A quick modification of removing the escaped '.' fixed
things right up:

        nil '(("\\(\\(?:\\sw\\|\\s_\\)+\\)("

Now it appears to match only the characters and underscores to the left
of the opening parentheses to either the dot or the word boundary.

Much appreciated and the differences in the regex lines will give me
something to study.

My apologies for not replying sooner.  I had originally posted the
question to the gnu.emacs.help newsgroup and never saw any replies in
it.  Today I found the gmane.emacs.help groups with my OP and the
replies, so now I am trying to follow up through Gmane.

- Nate

--

"The optimist proclaims that we live in the best of all
possible worlds.  The pessimist fears this is true."

Ham radio, Linux, bikes, and more: http://www.n0nb.us


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regex to match underscore in function name in Python Mode?

Emanuel Berg-4
Nate Bargmann wrote:

> My apologies for not replying sooner. I had
> originally posted the question to the
> gnu.emacs.help newsgroup and never saw any
> replies in it. Today I found the
> gmane.emacs.help groups with my OP [...]

This is what I have suspected for some time,
that by now gnu.emacs.help only gets a fraction
of the traffic, or perhaps none at all (makes
more sense) of the mailing list traffic.

Because very few people were using the NG, and
the translation never worked 100% anyway,
perhaps the mailing list people thought it to
be OK to discontinue it. If we ever hear about
it again, I suppose we should just tell people
not to use it.

The only mystery that remains is, if the OP
posted his OP to gnu.emacs.help, how did it end
up on the mailing list and the Gmane NG?

--
underground experts united
http://user.it.uu.se/~embe8573


Loading...