all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to get the bounds of the current defun?
@ 2017-10-08 20:28 Michael Heerdegen
  2017-10-08 21:00 ` Emanuel Berg
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Heerdegen @ 2017-10-08 20:28 UTC (permalink / raw)
  To: Emacs mailing list

Hi,

I need a function that for any elisp mode buffer receives a position as
argument and returns non-nil if inside a defun (i.e. when there is a
top-level expression covering POS).  If this is the case, the return
value is a cons (BEG . END) where BEG and END are the beginning and end
positions of this toplevel sexp.

This seems surprisingly hard, I can only think of solutions that are
really complicated.  The most complicated part is actually to find out
whether the value is non-nil or nil.  Not every toplevel expression
needs to be a list!  If POS is not inside a level of parens, I think I
could use the second element ("character address of start of last
complete sexp terminated") of `syntax-ppss' but that's still a bit of
way from a result.  There are really strange cases like the positions of
space characters in symbol\ with\ spaces.

I want to know if anyone knows some elegant solution I fail to see.


TIA,

Michael.



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

* Re: How to get the bounds of the current defun?
  2017-10-08 20:28 How to get the bounds of the current defun? Michael Heerdegen
@ 2017-10-08 21:00 ` Emanuel Berg
  2017-10-08 22:21   ` Michael Heerdegen
  0 siblings, 1 reply; 3+ messages in thread
From: Emanuel Berg @ 2017-10-08 21:00 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen wrote:

> I need a function that for any elisp mode
> buffer receives a position as argument and
> returns non-nil if inside a defun (i.e.
> when there is a top-level expression covering
> POS). If this is the case, the return value
> is a cons (BEG . END) where BEG and END are
> the beginning and end positions of this
> toplevel sexp.

(defun is-defun ()
  (interactive)
  (save-excursion
    (let ((pos   (point))
          (start (progn (beginning-of-defun) (point)))
          (end   (progn (end-of-defun)       (point))) )
      (when (and (<= start pos)
                 (<=   pos end) )
        (cons start end) ))))

Hm, it seems `end-of-defun' is a bit too
generous - perhaps that can be replaced...

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




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

* Re: How to get the bounds of the current defun?
  2017-10-08 21:00 ` Emanuel Berg
@ 2017-10-08 22:21   ` Michael Heerdegen
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Heerdegen @ 2017-10-08 22:21 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <moasen@zoho.com> writes:

> (defun is-defun ()
>   (interactive)
>   (save-excursion
>     (let ((pos   (point))
>           (start (progn (beginning-of-defun) (point)))
>           (end   (progn (end-of-defun)       (point))) )
>       (when (and (<= start pos)
>                  (<=   pos end) )
>         (cons start end) ))))

If it only would be so simple.  But that doesn't work for non-list
top-level expressions - try with point over a number, for example - your
function returns nil.  That is because AFAIK `beginning-of-defun' and
`end-of-defun' only work for listp toplevel expressions.

Hmm, but I guess when paren depth at POS is 0 (syntax-ppss can tell you
that), `scan-sexps' could be used in the way you use
`beginning-of-defun' and `end-of-defun' above to find whether we are
between an expression beginning and the according expression end.


Thanks,

Michael.



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

end of thread, other threads:[~2017-10-08 22:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-08 20:28 How to get the bounds of the current defun? Michael Heerdegen
2017-10-08 21:00 ` Emanuel Berg
2017-10-08 22:21   ` Michael Heerdegen

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.