all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Greg Hill <ghill@synergymicro.com>
Subject: Re: parens matching not matching all matching parens
Date: Thu, 16 Sep 2004 19:18:45 -0700	[thread overview]
Message-ID: <p06002001bd6fa68b99eb@[10.1.5.75]> (raw)
In-Reply-To: <cicpfn$h66$1@reader11.wxs.nl>


[-- Attachment #1.1: Type: text/plain, Size: 3772 bytes --]

At 9:19 PM +0200 9/16/04, Arjan Bos wrote:
>Hi all,
>
>I'm currently developing yet another rich text format writer. And as 
>you might know, RTF is using curly braces a lot. Alas, every now and 
>then, a normal parentheses pops up between a set of matching {}. 
>Both the parens matching colouring and the forward-sexp / 
>backward-sexp can't handle this. How can I (help to) solve this?
>
>An rtf snippet is included here:
>
>{\rtf1\mac\ansicpg10000\uc1
>{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta )}}}
>
>This is a complete piece of (non-sensical, but correct) rtf. The 
>first `{' matches the last `}'. Only C-M-f jumps from the first `{' 
>to the one-to-last `}'.
>
>I'm wondering if this is a bug or not.
>
>TIA,
>
>Arjan

Arjan,

Personally, I would call it a bug in the 'scan-sexps built-in 
function, which is called by 'forward-sexp, which is normally bound 
to "C-M-f".  For some reason 'scan-sexps seems to treat a ")" as 
matching a "{".  You might want to submit a bug report, but there is 
no guanentee the maintainers of Emacs will not call that odd behavior 
a "feature."

On the other hand, if you just want to get on with life, you can try 
putting something like the following code in your .emacs file.  The 
'defun defines a function that scans forward for the first '(', '[', 
'<' or '{' following point, whatever it see first, then scans for a 
matching closing character, ignoring any other characters.  The 
'(setq forward-sexp-function... effectively replaces the guts of 
'forward-sexp with the new function, so you will not have to rebind 
your keystroke.  It works in the backward direction as well ("C-M-b").

(defun forward-pexp (&optional arg)
   (interactive "p")
   (or arg (setq arg 1))
   (let (open close next notstrc notstro notstre depth)
     (catch 'done
       (cond ((> arg 0)
	     (skip-chars-forward "^([{<")
	     (setq open (char-after))
	     (cond ((eq open ?\()
		    (setq close ?\)))
		   ((eq open ?\[)
		    (setq close ?\]))
		   ((eq open ?\{)
		    (setq close ?\}))
		   ((eq open ?\<)
		    (setq close ?\>))
		   (t
		    (throw 'done nil) ) )
	     (setq notstro (concat "^" (char-to-string open))
		   notstre (concat notstro (char-to-string close)) )
	     (while (and (> arg 0) (not (eobp)))
	       (skip-chars-forward notstro)
	       (forward-char 1)
	       (setq depth 1)
	       (while (and (> depth 0) (not (eobp)))
		 (skip-chars-forward notstre)
		 (setq next (char-after))
		 (cond ((eq next open)
			(setq depth (1+ depth)) )
		       ((eq next close)
			(setq depth (1- depth)) )
		       (t
			(throw 'done nil) ) )
		 (forward-char 1) )
	       (setq arg (1- arg) ) ) )
	    ((< arg 0)
	     (skip-chars-backward "^)]}>")
	     (setq close (char-before))
	     (cond ((eq close ?\))
		    (setq open ?\())
		   ((eq close ?\])
		    (setq open ?\[))
		   ((eq close ?\})
		    (setq open ?\{))
		   ((eq close ?\>)
		    (setq open ?\<))
		   (t
		    (throw 'done nil) ) )
	     (setq notstrc (concat "^" (char-to-string close))
		   notstre (concat notstrc (char-to-string open)) )
	     (while (and (< arg 0) (not (bobp)))
	       (skip-chars-backward notstrc)
	       (forward-char -1)
	       (setq depth 1)
	       (while (and (> depth 0) (not (bobp)))
		 (skip-chars-backward notstre)
		 (setq next (char-before))
		 (cond ((eq next close)
			(setq depth (1+ depth)) )
		       ((eq next open)
			(setq depth (1- depth)) )
		       (t
			(throw 'done nil) ) )
		 (forward-char -1) )
	       (setq arg (1+ arg)) ) )	) ) ))

(setq forward-sexp-function 'forward-pexp)

I should warn you, by the way, that I just hacked this code together 
in a few minutes after work tonight, and have not tested it 
exhuastively.  Let me know if you have any questions or problems.

--Greg

[-- Attachment #1.2: Type: text/html, Size: 14063 bytes --]

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

  parent reply	other threads:[~2004-09-17  2:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-16 19:19 parens matching not matching all matching parens Arjan Bos
2004-09-16 19:42 ` J. David Boyd
     [not found] ` <mailman.3032.1095364141.1998.help-gnu-emacs@gnu.org>
2004-09-16 20:19   ` Arjan Bos
2004-09-16 21:45     ` Miles Bader
2004-09-17 17:25       ` Arjan Bos
2004-09-17 18:17         ` Stefan Monnier
2004-09-18  0:25           ` Miles Bader
2004-09-17 18:22         ` Stefan Monnier
2004-09-17 18:36           ` Arjan Bos
2004-09-17  2:18 ` Greg Hill [this message]
     [not found] ` <mailman.3063.1095387904.1998.help-gnu-emacs@gnu.org>
2004-09-17 17:29   ` Arjan Bos
2004-09-17 20:43     ` Greg Hill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='p06002001bd6fa68b99eb@[10.1.5.75]' \
    --to=ghill@synergymicro.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.