all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Regex to match underscore in function name in Python Mode?
@ 2017-07-23  2:12 Nate Bargmann
  2017-07-23  3:06 ` John Mastro
  2017-07-23  3:20 ` Skip Montanaro
  0 siblings, 2 replies; 10+ messages in thread
From: Nate Bargmann @ 2017-07-23  2:12 UTC (permalink / raw
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-23  2:12 Regex to match underscore in function name in Python Mode? Nate Bargmann
@ 2017-07-23  3:06 ` John Mastro
  2017-07-24 18:38   ` John Mastro
  2017-07-23  3:20 ` Skip Montanaro
  1 sibling, 1 reply; 10+ messages in thread
From: John Mastro @ 2017-07-23  3:06 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org

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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-23  2:12 Regex to match underscore in function name in Python Mode? Nate Bargmann
  2017-07-23  3:06 ` John Mastro
@ 2017-07-23  3:20 ` Skip Montanaro
  2017-07-23  3:34   ` Emanuel Berg
  1 sibling, 1 reply; 10+ messages in thread
From: Skip Montanaro @ 2017-07-23  3:20 UTC (permalink / raw
  To: Nate Bargmann; +Cc: Help GNU Emacs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-23  3:20 ` Skip Montanaro
@ 2017-07-23  3:34   ` Emanuel Berg
  0 siblings, 0 replies; 10+ messages in thread
From: Emanuel Berg @ 2017-07-23  3:34 UTC (permalink / raw
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-23  3:06 ` John Mastro
@ 2017-07-24 18:38   ` John Mastro
  2017-07-24 21:09     ` Emanuel Berg
  2017-07-29  2:10     ` Nate Bargmann
  0 siblings, 2 replies; 10+ messages in thread
From: John Mastro @ 2017-07-24 18:38 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org

John Mastro <john.b.mastro@gmail.com> 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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-24 18:38   ` John Mastro
@ 2017-07-24 21:09     ` Emanuel Berg
  2017-07-24 21:23       ` John Mastro
  2017-07-29  2:10     ` Nate Bargmann
  1 sibling, 1 reply; 10+ messages in thread
From: Emanuel Berg @ 2017-07-24 21:09 UTC (permalink / raw
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-24 21:09     ` Emanuel Berg
@ 2017-07-24 21:23       ` John Mastro
  2017-07-24 23:43         ` Emanuel Berg
  0 siblings, 1 reply; 10+ messages in thread
From: John Mastro @ 2017-07-24 21:23 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org

Emanuel Berg <moasen@zoho.com> 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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-24 21:23       ` John Mastro
@ 2017-07-24 23:43         ` Emanuel Berg
  0 siblings, 0 replies; 10+ messages in thread
From: Emanuel Berg @ 2017-07-24 23:43 UTC (permalink / raw
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-24 18:38   ` John Mastro
  2017-07-24 21:09     ` Emanuel Berg
@ 2017-07-29  2:10     ` Nate Bargmann
  2017-07-29 23:21       ` Emanuel Berg
  1 sibling, 1 reply; 10+ messages in thread
From: Nate Bargmann @ 2017-07-29  2:10 UTC (permalink / raw
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Regex to match underscore in function name in Python Mode?
  2017-07-29  2:10     ` Nate Bargmann
@ 2017-07-29 23:21       ` Emanuel Berg
  0 siblings, 0 replies; 10+ messages in thread
From: Emanuel Berg @ 2017-07-29 23:21 UTC (permalink / raw
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-07-29 23:21 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-23  2:12 Regex to match underscore in function name in Python Mode? Nate Bargmann
2017-07-23  3:06 ` John Mastro
2017-07-24 18:38   ` John Mastro
2017-07-24 21:09     ` Emanuel Berg
2017-07-24 21:23       ` John Mastro
2017-07-24 23:43         ` Emanuel Berg
2017-07-29  2:10     ` Nate Bargmann
2017-07-29 23:21       ` Emanuel Berg
2017-07-23  3:20 ` Skip Montanaro
2017-07-23  3:34   ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.