unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* elisp beginner's parens question ??
@ 2007-04-26 17:23 William Case
  0 siblings, 0 replies; 10+ messages in thread
From: William Case @ 2007-04-26 17:23 UTC (permalink / raw)
  To: EMACS List

Hi;

I am working my way through the tutorial.  First, complements to whoever
wrote it.
I don't know if it has been updated in the last two years, or if I have
picked up enough to understand the very basic subject matter it deals
with, but my first try two and a half years ago left me completely
baffled.  This time through at a slow and steady pace -- everything is
clear.

Secondly, I have some questions about debugging parenthesis errors.

I had the following practice function which was giving me debug errors.
It has been fixed so I am not asking for a solution.

( ... ( ... " ..." ( ... })) 
		     ^ error.

However my questions are these:

When I used check-parens, the cursor stopped on the first left paren
whether the error was later tested on the last, second last or third
last right parens. 

Why is this? I can see a situation with far more than three parens that
would take more than a few seconds to chase down the error.  Is there or
can there be check-parens that shows which grouping has the wrong parens
or needs one added?
There didn't seem to be any distinction between a double quotes error
and a parenthesis error? 

The question that flows from the above is how does the interpreter nest
parens?
Does the outside right and left parens go with, say, a basic defun
declaration, and then work its way inwards? Or, does it just count the
number of left parens and compare it with the number of right?

Do you have any mental tips or tricks you use to check the parens
balance when you get an error that you can pass on to a newbie like me?

-- 
Regards Bill

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

* Re: elisp beginner's parens question ??
       [not found] <mailman.2563.1177608564.7795.help-gnu-emacs@gnu.org>
@ 2007-04-26 19:31 ` Thien-Thi Nguyen
  2007-04-27  2:38   ` William Case
       [not found]   ` <mailman.2572.1177641875.7795.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Thien-Thi Nguyen @ 2007-04-26 19:31 UTC (permalink / raw)
  To: help-gnu-emacs

() William Case <billlinux@rogers.com>
() Thu, 26 Apr 2007 13:23:20 -0400

   mental tips or tricks

not very mental, but practical: i use mic-paren (by mic).  see:

http://www.gnuvola.org/software/personal-elisp/dist-lisp-index.html

and search for "turn-on-mic-paren", for example.

thi

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

* Re: elisp beginner's parens question ??
  2007-04-26 19:31 ` elisp beginner's parens question ?? Thien-Thi Nguyen
@ 2007-04-27  2:38   ` William Case
       [not found]   ` <mailman.2572.1177641875.7795.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: William Case @ 2007-04-27  2:38 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: help-gnu-emacs

Thanks for your reply Thi

On Thu, 2007-04-26 at 21:31 +0200, Thien-Thi Nguyen wrote:
> () William Case <billlinux@rogers.com>
> () Thu, 26 Apr 2007 13:23:20 -0400
> 
>    mental tips or tricks
> 
> not very mental, but practical: i use mic-paren (by mic).  see:
> 
> http://www.gnuvola.org/software/personal-elisp/dist-lisp-index.html
> 
> and search for "turn-on-mic-paren", for example.
> 
I have looked at the site and downloaded the program(s) you have
suggested.

However, the "mental tips or tricks" was really a 'by the way' or an
after thought question.  What I was really asking was how the
interpreter or emacs uses parenthesis or how parenthesis are nested.
Put another way, I was trying to develop for myself a minds eye view of
how check-parens works.  Does check-parens just count left parens and
compare that to the number of right parens to find an error, or does it
actually examine nested parens pairs and work inword (or outward) ?
-- 
Regards Bill

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

* Re: elisp beginner's parens question ??
       [not found]   ` <mailman.2572.1177641875.7795.help-gnu-emacs@gnu.org>
@ 2007-04-27  2:58     ` Robert D. Crawford
  2007-04-27  5:43       ` William Case
  2007-04-27  9:59     ` Thien-Thi Nguyen
  1 sibling, 1 reply; 10+ messages in thread
From: Robert D. Crawford @ 2007-04-27  2:58 UTC (permalink / raw)
  To: help-gnu-emacs

William Case <billlinux@rogers.com> writes:

> However, the "mental tips or tricks" was really a 'by the way' or an
> after thought question.  What I was really asking was how the
> interpreter or emacs uses parenthesis or how parenthesis are nested.
> Put another way, I was trying to develop for myself a minds eye view of
> how check-parens works.  Does check-parens just count left parens and
> compare that to the number of right parens to find an error, or does it
> actually examine nested parens pairs and work inword (or outward) ?

This might provide your answer.  If you do C-h f and supply a function
name, the resulting buffer, in X anyway, will have a link to the
function's definition.

(defun check-parens ()			; lame name?
  "Check for unbalanced parentheses in the current buffer.
More accurately, check the narrowed part of the buffer for unbalanced
expressions (\"sexps\") in general.  This is done according to the
current syntax table and will find unbalanced brackets or quotes as
appropriate.  (See Info node `(emacs)Parentheses'.)  If imbalance is
found, an error is signaled and point is left at the first unbalanced
character."
  (interactive)
  (condition-case data
      ;; Buffer can't have more than (point-max) sexps.
      (scan-sexps (point-min) (point-max))
    (scan-error (goto-char (nth 2 data))
		;; Could print (nth 1 data), which is either
		;; "Containing expression ends prematurely" or
		;; "Unbalanced parentheses", but those may not be so
		;; accurate/helpful, e.g. quotes may actually be
		;; mismatched.
  		(error "Unmatched bracket or quote"))
    (error (cond ((eq 'scan-error (car data))
		  (goto-char (nth 2 data))
		  (error "Unmatched bracket or quote"))
		 (t (signal (car data) (cdr data)))))))


-- 
Robert D. Crawford                                      rdc1x@comcast.net

If ignorance is bliss, why aren't there more happy people?

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

* Re: elisp beginner's parens question ??
  2007-04-27  2:58     ` Robert D. Crawford
@ 2007-04-27  5:43       ` William Case
  0 siblings, 0 replies; 10+ messages in thread
From: William Case @ 2007-04-27  5:43 UTC (permalink / raw)
  To: Robert D. Crawford; +Cc: help-gnu-emacs

Thank you Robert

On Thu, 2007-04-26 at 21:58 -0500, Robert D. Crawford wrote:
> William Case <billlinux@rogers.com> writes:
> 
> > However, the "mental tips or tricks" was really a 'by the way' or an
> > after thought question.  What I was really asking was how the
> > interpreter or emacs uses parenthesis or how parenthesis are nested.
> > Put another way, I was trying to develop for myself a minds eye view of
> > how check-parens works.  Does check-parens just count left parens and
> > compare that to the number of right parens to find an error, or does it
> > actually examine nested parens pairs and work inword (or outward) ?
> 
> This might provide your answer.  If you do C-h f and supply a function
> name, the resulting buffer, in X anyway, will have a link to the
> function's definition.

Didn't know about the link to functions definition / source code.
> 
> (defun check-parens ()			; lame name?
>   "Check for unbalanced parentheses in the current buffer.
> More accurately, check the narrowed part of the buffer for unbalanced
> expressions (\"sexps\") in general.  This is done according to the
> current syntax table and will find unbalanced brackets or quotes as
> appropriate.  (See Info node `(emacs)Parentheses'.)  If imbalance is
> found, an error is signaled and point is left at the first unbalanced
> character."

Info node (emacs)Parentheses) lead me to 'show-paren-mode' which, when
turned on, showed me how to visualize what way happening with the
parentheses check.

I appreciate the time you took to answer. 

-- 
Regards Bill

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

* Re: elisp beginner's parens question ??
       [not found]   ` <mailman.2572.1177641875.7795.help-gnu-emacs@gnu.org>
  2007-04-27  2:58     ` Robert D. Crawford
@ 2007-04-27  9:59     ` Thien-Thi Nguyen
  2007-04-27 13:58       ` William Case
       [not found]       ` <mailman.2602.1177682661.7795.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 10+ messages in thread
From: Thien-Thi Nguyen @ 2007-04-27  9:59 UTC (permalink / raw)
  To: help-gnu-emacs

() William Case <billlinux@rogers.com>
() Thu, 26 Apr 2007 22:38:24 -0400

   What I was really asking was how the interpreter or emacs uses
   parenthesis or how parenthesis are nested.  Put another way, I was
   trying to develop for myself a minds eye view of how check-parens
   works.

i share this desire, and did this to get a quick (10sec) overview:
C-h f check-parens RET         ; what do you do?
C-x o TAB                      ; where do you do it?
RET                            ; how do you do it?
[ogle ogle]

from this i see that one of the funcs called is `scan-sexps' and there
is also some kind of error handling.

   Does check-parens just count left parens and compare that to the
   number of right parens to find an error, or does it actually examine
   nested parens pairs and work inword (or outward) ?

it uses `scan-sexps' and handles errors that `scan-sexps' throws.  these
are things i didn't know before starting this message, and will probably
forget a few moments after C-c C-c, but the method for re-knowing is
what is important.  perhaps that is the mental tip you seek.

thi

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

* Re: elisp beginner's parens question ??
  2007-04-27  9:59     ` Thien-Thi Nguyen
@ 2007-04-27 13:58       ` William Case
       [not found]       ` <mailman.2602.1177682661.7795.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: William Case @ 2007-04-27 13:58 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: help-gnu-emacs

Thanks Thi;

Just to finish off and share the answer with anyone else who is
interested ....

On Fri, 2007-04-27 at 11:59 +0200, Thien-Thi Nguyen wrote:
> () William Case <billlinux@rogers.com>
> () Thu, 26 Apr 2007 22:38:24 -0400
> 
>    What I was really asking was how the interpreter or emacs uses
>    parenthesis or how parenthesis are nested.  Put another way, I was
>    trying to develop for myself a minds eye view of how check-parens
>    works.
> 
> i share this desire, and did this to get a quick (10sec) overview:
> C-h f check-parens RET         ; what do you do?
> C-x o TAB                      ; where do you do it?
> RET                            ; how do you do it?
> [ogle ogle]
> 
> from this i see that one of the funcs called is `scan-sexps' and there
> is also some kind of error handling.
> 
>    Does check-parens just count left parens and compare that to the
>    number of right parens to find an error, or does it actually examine
>    nested parens pairs and work inword (or outward) ?
> 
> it uses `scan-sexps' and handles errors that `scan-sexps' throws.  
I chased down scan-sexps and this is what I got:

"scan-sexps is a built-in function.
(scan-sexps FROM COUNT)

Scan from character number FROM by COUNT balanced expressions.
If COUNT is negative, scan backwards.
Returns the character number of the position thus found.

Comments are ignored if `parse-sexp-ignore-comments' is non-nil.

If the beginning or end of (the accessible part of) the buffer is
reached
in the middle of a parenthetical grouping, an error is signaled.
If the beginning or end is reached between groupings
but before count is used up, nil is returned."

It seems that a given expression is scanned by'check-parens'.
'check-parens uses 'scan-sexps' (a builtin function) to couunt the
parens ( " " ()) established in the syntax table for a given mode.  If
the number of parens is even then scan-sexps returns nil, if the number
of parens is odd 'scan-exps' scans backwards checking for unbalanced
expressions.

Because 'scan-exps' is builtin, I don't have, or know how to look at,
its source code.  Therefore, I am unable to say how it goes about
checking for the position of the error on the backward scan.  It seems
to me, that that is the final piece needed for a full understanding.  I
have searched in info and googled the web looking for the source code
for 'scan-exps' and found nothing.  (That could just be a beginners lack
of correct search criteria.)


> these
> are things i didn't know before starting this message, and will probably
> forget a few moments after C-c C-c, but the method for re-knowing is
> what is important.  perhaps that is the mental tip you seek.

That is why I spend so much time chasing down how things work.  I find
that instead of just memorizing something (which for me is easily
forgettable) that if I can create a sensible mental image of what is
happening, I can then move a new fact from "memorized" to "known" and
its there in my head pretty much for good.

The mental tip I was looking for was perhaps there is a way to block out
all but one ))))) as I chase something down or a quick mental trick to
keep track of the parens for each sub expression as I check them one by
one.  The best answer I have come up with was hinted at by Robert D.
Crawford's response to this thread.  I have bound 'show-paren-mode' to
S-F4 to toggle 'show-paren-mode' on and off.  It helps me to visually
check each expression and sub-expression for balanced parens.

I have appreciated your help.

-- 
Regards Bill

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

* Re: elisp beginner's parens question ??
       [not found]       ` <mailman.2602.1177682661.7795.help-gnu-emacs@gnu.org>
@ 2007-04-27 15:41         ` Thien-Thi Nguyen
  2007-04-27 18:01           ` William Case
  0 siblings, 1 reply; 10+ messages in thread
From: Thien-Thi Nguyen @ 2007-04-27 15:41 UTC (permalink / raw)
  To: help-gnu-emacs

() William Case <billlinux@rogers.com>
() Fri, 27 Apr 2007 09:58:10 -0400

   (That could just be a beginners lack of correct
   search criteria.)

it's ok to look all over the place for the source code, as long
as you eventually find it: <http://www.gnu.org/software/emacs/>.

   can then move a new fact from "memorized" to "known" and its
   there in my head pretty much for good.

sometimes it's fun let the known fade, as well.

   bound 'show-paren-mode' to S-F4 to toggle 'show-paren-mode' on
   and off.  It helps me to visually check each expression and
   sub-expression for balanced parens.

personally, when writing code, i use M-( C-M-o CODE M-) for the
first time (recursively ;-).  after that, C-M-k, C-M-t and other
sexp-oriented stuff.  in this way, balance is never lost, and no
checking is required.  i find M-^ and the following useful, too:

(global-set-key "\C-c_" 'raise-sexp)

i chose underscore because raising a child "buries" the parent.
but don't take my word on it, just ask any parent!

thi

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

* Re: elisp beginner's parens question ??
  2007-04-27 15:41         ` Thien-Thi Nguyen
@ 2007-04-27 18:01           ` William Case
  2007-04-27 21:22             ` Dieter Wilhelm
  0 siblings, 1 reply; 10+ messages in thread
From: William Case @ 2007-04-27 18:01 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: help-gnu-emacs

Thanks again Thi;

Especially thank you for the bit of humour.  I find that some people
take their emacs a little too seriously.

On Fri, 2007-04-27 at 17:41 +0200, Thien-Thi Nguyen wrote:
> () William Case <billlinux@rogers.com>
> () Fri, 27 Apr 2007 09:58:10 -0400
> 
[snip]

> personally, when writing code, i use M-( C-M-o CODE M-) for the
> first time (recursively ;-).  after that, C-M-k, C-M-t and other
> sexp-oriented stuff.  in this way, balance is never lost, and no
> checking is required.  i find M-^ and the following useful, too:
> 
> (global-set-key "\C-c_" 'raise-sexp)
> 
Unfortunately, it seems emacs 21.4 doesn't have the raise-sexp function.
I tried C-H f, and v, and a.

> i chose underscore because raising a child "buries" the parent.
> but don't take my word on it, just ask any parent!
> 
> thi

You have been a lot of help showing me how to chase down answers to
queries.

-- 
Regards Bill

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

* Re: elisp beginner's parens question ??
  2007-04-27 18:01           ` William Case
@ 2007-04-27 21:22             ` Dieter Wilhelm
  0 siblings, 0 replies; 10+ messages in thread
From: Dieter Wilhelm @ 2007-04-27 21:22 UTC (permalink / raw)
  To: William Case; +Cc: help-gnu-emacs, Thien-Thi Nguyen

William Case <billlinux@rogers.com> writes:

> Thanks again Thi;
>
> Especially thank you for the bit of humour.  I find that some people
> take their emacs a little too seriously.

We have to take  E m a c s  serious it is our religion, please have a
respectful look at the offical site:

http://www.dina.kvl.dk/~abraham/religion/.

-- 
    Best wishes

    H. Dieter Wilhelm
    Darmstadt, Germany

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

end of thread, other threads:[~2007-04-27 21:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.2563.1177608564.7795.help-gnu-emacs@gnu.org>
2007-04-26 19:31 ` elisp beginner's parens question ?? Thien-Thi Nguyen
2007-04-27  2:38   ` William Case
     [not found]   ` <mailman.2572.1177641875.7795.help-gnu-emacs@gnu.org>
2007-04-27  2:58     ` Robert D. Crawford
2007-04-27  5:43       ` William Case
2007-04-27  9:59     ` Thien-Thi Nguyen
2007-04-27 13:58       ` William Case
     [not found]       ` <mailman.2602.1177682661.7795.help-gnu-emacs@gnu.org>
2007-04-27 15:41         ` Thien-Thi Nguyen
2007-04-27 18:01           ` William Case
2007-04-27 21:22             ` Dieter Wilhelm
2007-04-26 17:23 William Case

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).