unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* which-function => DEFUN?
@ 2010-05-10 13:58 Lennart Borgman
  2010-05-10 14:22 ` Juanma Barranquero
  0 siblings, 1 reply; 14+ messages in thread
From: Lennart Borgman @ 2010-05-10 13:58 UTC (permalink / raw)
  To: Emacs-Devel devel

It is a bit annoying to see DEFUN as the function shown by which-function-mode.

I am sure someone here has written an
imenu-extract-index-name-function for use in Emacs C sources... ;-)

Where is it? How do you set it up?



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

* Re: which-function => DEFUN?
  2010-05-10 13:58 which-function => DEFUN? Lennart Borgman
@ 2010-05-10 14:22 ` Juanma Barranquero
  2010-05-10 14:39   ` Lennart Borgman
  2010-05-10 15:42   ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-05-10 14:22 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

On Mon, May 10, 2010 at 15:58, Lennart Borgman
<lennart.borgman@gmail.com> wrote:

> It is a bit annoying to see DEFUN as the function shown by which-function-mode.
>
> I am sure someone here has written an
> imenu-extract-index-name-function for use in Emacs C sources... ;-)

(defun which-func-identify-DEFUN ()
  (save-excursion
    (save-match-data
      (beginning-of-defun)
      (and (looking-at "DEFUN +(\"[^\"]+\",[ \n]+\\(F[^,]+\\),")
           (match-string-no-properties 1)))))

Likely the `save-match-data' is not required, but I tend to err on the
side of saving it on my code.

> Where is it? How do you set it up?

  (add-hook 'which-func-functions 'which-func-identify-DEFUN nil t)

on the required buffers (I set it through `c-mode-hook').

    Juanma



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

* Re: which-function => DEFUN?
  2010-05-10 14:22 ` Juanma Barranquero
@ 2010-05-10 14:39   ` Lennart Borgman
  2010-05-10 15:09     ` Juanma Barranquero
  2010-05-10 15:42   ` Stefan Monnier
  1 sibling, 1 reply; 14+ messages in thread
From: Lennart Borgman @ 2010-05-10 14:39 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: Emacs-Devel devel

On Mon, May 10, 2010 at 4:22 PM, Juanma Barranquero <lekktu@gmail.com> wrote:
> On Mon, May 10, 2010 at 15:58, Lennart Borgman
> <lennart.borgman@gmail.com> wrote:
>
>> It is a bit annoying to see DEFUN as the function shown by which-function-mode.
>>
>> I am sure someone here has written an
>> imenu-extract-index-name-function for use in Emacs C sources... ;-)
>
> (defun which-func-identify-DEFUN ()
>  (save-excursion
>    (save-match-data
>      (beginning-of-defun)
>      (and (looking-at "DEFUN +(\"[^\"]+\",[ \n]+\\(F[^,]+\\),")
>           (match-string-no-properties 1)))))
>
> Likely the `save-match-data' is not required, but I tend to err on the
> side of saving it on my code.
>
>> Where is it? How do you set it up?
>
>  (add-hook 'which-func-functions 'which-func-identify-DEFUN nil t)
>
> on the required buffers (I set it through `c-mode-hook').

Thanks Juanma, here is my slightly modified version built from your version:

  (defun my-which-func-identify-DEFUN ()
    (let ((here (point)))
      (save-match-data
        ;; Check if we are at the beginning of the function already
        (unless (eobp) (forward-char))
        (beginning-of-defun)
        (when (< here (point)) (beginning-of-defun))
        (prog1
            (and (looking-at "DEFUN +(\"[^\"]+\",[ \n]+\\(F[^,]+\\),")
                 (match-string-no-properties 1))
          (goto-char here)))))

  (defun my-add-DEFUN ()
    (add-hook 'which-func-functions 'my-which-func-identify-DEFUN nil t))
  (add-hook 'c-mode-hook 'my-add-DEFUN)



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

* Re: which-function => DEFUN?
  2010-05-10 14:39   ` Lennart Borgman
@ 2010-05-10 15:09     ` Juanma Barranquero
  2010-05-10 15:56       ` Lennart Borgman
  0 siblings, 1 reply; 14+ messages in thread
From: Juanma Barranquero @ 2010-05-10 15:09 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

On Mon, May 10, 2010 at 16:39, Lennart Borgman
<lennart.borgman@gmail.com> wrote:

>        ;; Check if we are at the beginning of the function already
>        (unless (eobp) (forward-char))

Yes, nice catch. Thanks.

>        (beginning-of-defun)
>        (when (< here (point)) (beginning-of-defun))

What does this protect against?

And, why did you go for (let ((here (point))) ...) instead of
`save-excursion'? Efficiency?

    Juanma



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

* Re: which-function => DEFUN?
  2010-05-10 14:22 ` Juanma Barranquero
  2010-05-10 14:39   ` Lennart Borgman
@ 2010-05-10 15:42   ` Stefan Monnier
  2010-05-10 16:05     ` Lennart Borgman
                       ` (2 more replies)
  1 sibling, 3 replies; 14+ messages in thread
From: Stefan Monnier @ 2010-05-10 15:42 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: Lennart Borgman, Emacs-Devel devel

> (defun which-func-identify-DEFUN ()
>   (save-excursion
>     (save-match-data
>       (beginning-of-defun)
>       (and (looking-at "DEFUN +(\"[^\"]+\",[ \n]+\\(F[^,]+\\),")
>            (match-string-no-properties 1)))))

> Likely the `save-match-data' is not required, but I tend to err on the
> side of saving it on my code.

The rule for where to place a save-match-data is:

  around the code run between "looking-at" and
  "match-string-no-properties".

In this case (as in 99% of the cases) this code is empty, so the
save-match-data can be dropped.

> (add-hook 'which-func-functions 'which-func-identify-DEFUN nil t)

If your save-match-data were needed, which-func-functions should say
so explicitly.


        Stefan



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

* Re: which-function => DEFUN?
  2010-05-10 15:09     ` Juanma Barranquero
@ 2010-05-10 15:56       ` Lennart Borgman
  2010-05-10 17:30         ` Juanma Barranquero
  0 siblings, 1 reply; 14+ messages in thread
From: Lennart Borgman @ 2010-05-10 15:56 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: Emacs-Devel devel

On Mon, May 10, 2010 at 5:09 PM, Juanma Barranquero <lekktu@gmail.com> wrote:
> On Mon, May 10, 2010 at 16:39, Lennart Borgman
> <lennart.borgman@gmail.com> wrote:
>
>>        ;; Check if we are at the beginning of the function already
>>        (unless (eobp) (forward-char))
>
> Yes, nice catch. Thanks.
>
>>        (beginning-of-defun)
>>        (when (< here (point)) (beginning-of-defun))
>
> What does this protect against?

We might have stepped into the next function.

> And, why did you go for (let ((here (point))) ...) instead of
> `save-excursion'? Efficiency?

Efficiency. And actually readability.

>    Juanma
>



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

* Re: which-function => DEFUN?
  2010-05-10 15:42   ` Stefan Monnier
@ 2010-05-10 16:05     ` Lennart Borgman
  2010-05-10 17:34       ` Stefan Monnier
  2010-05-10 17:34     ` Juanma Barranquero
  2010-05-11  2:09     ` Global match data considered harmful Stephen J. Turnbull
  2 siblings, 1 reply; 14+ messages in thread
From: Lennart Borgman @ 2010-05-10 16:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juanma Barranquero, Emacs-Devel devel

On Mon, May 10, 2010 at 5:42 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>
> The rule for where to place a save-match-data is:
>
>  around the code run between "looking-at" and
>  "match-string-no-properties".
>
> In this case (as in 99% of the cases) this code is empty, so the
> save-match-data can be dropped.
>
>> (add-hook 'which-func-functions 'which-func-identify-DEFUN nil t)
>
> If your save-match-data were needed, which-func-functions should say
> so explicitly.


Yes, but what is the rule for WHEN to use save-match-data. I just
looked in the elisp manual and could not find anything.



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

* Re: which-function => DEFUN?
  2010-05-10 15:56       ` Lennart Borgman
@ 2010-05-10 17:30         ` Juanma Barranquero
  0 siblings, 0 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-05-10 17:30 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

On Mon, May 10, 2010 at 17:56, Lennart Borgman
<lennart.borgman@gmail.com> wrote:

> We might have stepped into the next function.

In the case of DEFUN, which is only used in the Emacs sources, that's
unlikely unless you have

DEFUN (...)
{
}DEFUN (...)

which I don't think happens even once in the source (I doesn't, I just checked).

> Efficiency.

I have never noticed any slowness, but YMMV.

> And actually readability.

Let's agree to disagree on this one.

    Juanma



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

* Re: which-function => DEFUN?
  2010-05-10 15:42   ` Stefan Monnier
  2010-05-10 16:05     ` Lennart Borgman
@ 2010-05-10 17:34     ` Juanma Barranquero
  2010-05-11  2:09     ` Global match data considered harmful Stephen J. Turnbull
  2 siblings, 0 replies; 14+ messages in thread
From: Juanma Barranquero @ 2010-05-10 17:34 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Lennart Borgman, Emacs-Devel devel

On Mon, May 10, 2010 at 17:42, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> The rule for where to place a save-match-data is:
>
>  around the code run between "looking-at" and
>  "match-string-no-properties".
>
> In this case (as in 99% of the cases) this code is empty, so the
> save-match-data can be dropped.

The save-match-data is not there to protect the code between
`looking-at' and `match-string-no-properties', because as you point
out, that is an empty set. It is there to protect the caller of
which-func-identify-DEFUN.

> If your save-match-data were needed, which-func-functions should say
> so explicitly.

Yes, I know. As I said, in code for my own use I tend to err on the
side of caution.

    Juanma



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

* Re: which-function => DEFUN?
  2010-05-10 16:05     ` Lennart Borgman
@ 2010-05-10 17:34       ` Stefan Monnier
  0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2010-05-10 17:34 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Juanma Barranquero, Emacs-Devel devel

>> The rule for where to place a save-match-data is:
>> 
>>  around the code run between "looking-at" and
>>  "match-string-no-properties".
>> 
>> In this case (as in 99% of the cases) this code is empty, so the
>> save-match-data can be dropped.
>> 
>>> (add-hook 'which-func-functions 'which-func-identify-DEFUN nil t)
>> 
>> If your save-match-data were needed, which-func-functions should say
>> so explicitly.


> Yes, but what is the rule for WHEN to use save-match-data. I just
> looked in the elisp manual and could not find anything.

The "where":

   around the code run between "looking-at" and
   "match-string-no-properties".

also tells you "when": just look at the code around which to place
save-match-data and if it's clear that the save-match-data is not needed
(as in your case), then don't bother putting it there.


        Stefan



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

* Global match data considered harmful
  2010-05-10 15:42   ` Stefan Monnier
  2010-05-10 16:05     ` Lennart Borgman
  2010-05-10 17:34     ` Juanma Barranquero
@ 2010-05-11  2:09     ` Stephen J. Turnbull
  2010-05-11 13:54       ` Stefan Monnier
  2 siblings, 1 reply; 14+ messages in thread
From: Stephen J. Turnbull @ 2010-05-11  2:09 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juanma Barranquero, Lennart Borgman, Emacs-Devel devel

Stefan Monnier writes:

 > The rule for where to place a save-match-data is:
 > 
 >   around the code run between "looking-at" and
 >   "match-string-no-properties".

*snort*  I'm with Juanma.

This is just so wrong, you know.  Not returning the match data, instead
storing it in a global object, is arguably the worst design decision
in all of Emacs.



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

* Re: Global match data considered harmful
  2010-05-11  2:09     ` Global match data considered harmful Stephen J. Turnbull
@ 2010-05-11 13:54       ` Stefan Monnier
  2010-05-12  2:01         ` Stephen J. Turnbull
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2010-05-11 13:54 UTC (permalink / raw)
  To: Stephen J. Turnbull
  Cc: Juanma Barranquero, Lennart Borgman, Emacs-Devel devel

>> The rule for where to place a save-match-data is:
>> around the code run between "looking-at" and "match-string-no-properties".
> *snort*  I'm with Juanma.

I'm not sure what that means.

> This is just so wrong, you know.  Not returning the match data, instead
> storing it in a global object, is arguably the worst design decision
> in all of Emacs.

Preaching to the choir, eh?
[ Well "the worst" I don't know: it's not like there's a shortage of
  global-abuse in Emacs. ]


        Stefan



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

* Re: Global match data considered harmful
  2010-05-11 13:54       ` Stefan Monnier
@ 2010-05-12  2:01         ` Stephen J. Turnbull
  2010-05-12  5:09           ` tomas
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen J. Turnbull @ 2010-05-12  2:01 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juanma Barranquero, Lennart Borgman, Emacs-Devel devel

Stefan Monnier writes:
 > >> The rule for where to place a save-match-data is:
 > >> around the code run between "looking-at" and "match-string-no-properties".
 > > *snort*  I'm with Juanma.
 > 
 > I'm not sure what that means.

Most of the code I write is (ab)usable by .emacs; so I put
save-match-data in the functions I write to protect naive callers from
having their match-data tromped on by my using of matching code.  If
somebody complains about slow I defun a foo-internal without using
save-match-data, and call that from foo.

Why?  I'm pretty sure there's at least one match-data-munching bug in
XEmacs's minibuffer code at the moment (or it might be setcdr abuse),
but I don't have time for a full audit and it's sufficiently
intermittent that I don't have a strong incentive to do that
anyway....  Missing save-match-data is about as hard to debug as
missing GCPRO.  :-(  Better to put in too many than too few.

 > > This is just so wrong, you know.  Not returning the match data, instead
 > > storing it in a global object, is arguably the worst design decision
 > > in all of Emacs.
 > 
 > Preaching to the choir, eh?
 > [ Well "the worst" I don't know: it's not like there's a shortage of
 >   global-abuse in Emacs. ]

No shortage, for sure, but this one is like attaching an 'invisible
property to a low-hanging rope in a narrow hallway.



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

* Re: Global match data considered harmful
  2010-05-12  2:01         ` Stephen J. Turnbull
@ 2010-05-12  5:09           ` tomas
  0 siblings, 0 replies; 14+ messages in thread
From: tomas @ 2010-05-12  5:09 UTC (permalink / raw)
  Cc: Emacs-Devel devel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, May 12, 2010 at 11:01:10AM +0900, Stephen J. Turnbull wrote:

[...]

> No shortage, for sure, but this one is like attaching an 'invisible
> property to a low-hanging rope in a narrow hallway.

Ouch. Coffee. Keyboard. May I quote that?

(you made my day :-)

- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFL6jgfBcgs9XrR2kYRAlcgAJ9xivynoJrH0zkmkqtf+mHST1649wCffLoV
PUK3hBw1G9pR7aDM3n3JMuE=
=GsxQ
-----END PGP SIGNATURE-----



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

end of thread, other threads:[~2010-05-12  5:09 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-10 13:58 which-function => DEFUN? Lennart Borgman
2010-05-10 14:22 ` Juanma Barranquero
2010-05-10 14:39   ` Lennart Borgman
2010-05-10 15:09     ` Juanma Barranquero
2010-05-10 15:56       ` Lennart Borgman
2010-05-10 17:30         ` Juanma Barranquero
2010-05-10 15:42   ` Stefan Monnier
2010-05-10 16:05     ` Lennart Borgman
2010-05-10 17:34       ` Stefan Monnier
2010-05-10 17:34     ` Juanma Barranquero
2010-05-11  2:09     ` Global match data considered harmful Stephen J. Turnbull
2010-05-11 13:54       ` Stefan Monnier
2010-05-12  2:01         ` Stephen J. Turnbull
2010-05-12  5:09           ` tomas

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).