From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Greg Hill Newsgroups: gmane.emacs.help Subject: Re: parens matching not matching all matching parens Date: Fri, 17 Sep 2004 13:43:03 -0700 Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0325747633==" X-Trace: sea.gmane.org 1095453833 9675 80.91.229.6 (17 Sep 2004 20:43:53 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 17 Sep 2004 20:43:53 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Sep 17 22:43:44 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1C8PZz-0001e7-00 for ; Fri, 17 Sep 2004 22:43:39 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C8Pfg-00036Q-8R for geh-help-gnu-emacs@m.gmane.org; Fri, 17 Sep 2004 16:49:32 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1C8PfQ-000338-Le for help-gnu-emacs@gnu.org; Fri, 17 Sep 2004 16:49:16 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1C8PfP-00032F-Lf for help-gnu-emacs@gnu.org; Fri, 17 Sep 2004 16:49:16 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C8PfP-000326-Fp for help-gnu-emacs@gnu.org; Fri, 17 Sep 2004 16:49:15 -0400 Original-Received: from [207.158.54.5] (helo=renfield.synergymicro.com) by monty-python.gnu.org with esmtp (TLSv1:DES-CBC3-SHA:168) (Exim 4.34) id 1C8PZV-0004eH-3n for help-gnu-emacs@gnu.org; Fri, 17 Sep 2004 16:43:10 -0400 Original-Received: from synergy.san.synergymicro.com (synergy.san.synergymicro.com [10.1.2.12]) by renfield.synergymicro.com (8.12.10/8.12.10) with ESMTP id i8HKjR9f001154; Fri, 17 Sep 2004 13:45:27 -0700 Original-Received: from [10.1.5.75] ([10.1.5.75]) by synergy.san.synergymicro.com (8.12.10/8.12.10) with ESMTP id i8HKhqkK014668; Fri, 17 Sep 2004 13:43:52 -0700 In-Reply-To: Original-To: Arjan.Bos@ISeeYou.nl, help-gnu-emacs@gnu.org X-Virus-Scanned: clamd / ClamAV version 0.70, clamav-milter version 0.70j X-CanItPRO-Stream: default X-Canit-Stats-ID: 266442 - b39ff5d8a829 X-Scanned-By: CanIt (www . canit . ca) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:20746 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:20746 --===============0325747633== Content-Type: multipart/alternative; boundary="============_-1116668710==_ma============" --============_-1116668710==_ma============ Content-Type: text/plain; charset="us-ascii" ; format="flowed" At 7:29 PM +0200 9/17/04, Arjan Bos wrote: >I already had something similar. Looking at the current bracket >under point, finding its opposite and start counting the brackets >you encounter. Another of the same bracket ups the ante, the >opposite pops one from the ante. When the ante is empty, all bets >are off, and the bracket is found. > >My code isn't perfect, so I will borrow heavily from yours. > >Thanks! Arjan, I cleaned up my code to the point that it will also work in place of the regular forward-sexp and backward-sexp for everyday use. Here is a copy, if you're interested. Please let me know if you try it and find any problems. I will be using it myself as a permenant replacement for forward-sexp and backward-sexp. If you are interested in being notified of any problems I may find, please let me know. (defun skip-string-forward (&optional limit) (if (eq (char-after) ?\") (catch 'done (forward-char 1) (while t (skip-chars-forward "^\\\\\"" limit) (cond ((eq (point) limit) (throw 'done nil) ) ((eq (char-after) ?\") (forward-char 1) (throw 'done nil) ) (t (forward-char 1) (if (eq (point) limit) (throw 'done nil) (forward-char 1) ) ) ) ) ) ) ) (defun skip-string-backward (&optional limit) (if (eq (char-before) ?\") (catch 'done (forward-char -1) (while t (skip-chars-backward "^\"" limit) (if (eq (point) limit) (throw 'done nil) ) (forward-char -1) (if (eq (point) limit) (throw 'done nil) ) (if (not (eq (char-before) ?\\)) (throw 'done nil) ) ) ) ) ) (defun forward-pexp (&optional arg) (interactive "p") (or arg (setq arg 1)) (let (open close next notstrc notstro notstre depth pair) (catch 'done (cond ((> arg 0) (skip-chars-forward " \t\n") (if (not (memq (char-after) '(?\( ?\[ ?\{ ?\<))) (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) (skip-chars-forward "^([{<\"") (while (eq (char-after) ?\") (skip-string-forward) (skip-chars-forward "^([{<\"") ) (setq open (char-after)) (if (setq close (cadr (assq open '( (?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) ) ) ) ) (progn (setq notstro (string ?^ open ?\") notstre (string ?^ open close ?\") ) (while (and (> arg 0) (not (eobp))) (skip-chars-forward notstro) (while (eq (char-after) ?\") (if (eq (char-before) ?\\) (forward-char 1) (skip-string-forward) ) (skip-chars-forward notstro) ) (forward-char 1) (setq depth 1) (while (and (> depth 0) (not (eobp))) (skip-chars-forward notstre) (while (eq (char-after) ?\") (if (eq (char-before) ?\\) (forward-char 1) (skip-string-forward) ) (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 " \t\t") (if (not (memq (char-before) '(?\) ?\] ?\} ?\>))) (progn (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) (backward-prefix-chars) ) (skip-chars-backward "^)]}>\"") (while (eq (char-before) ?\") (skip-string-backward) (skip-chars-backward "^)]}>\"") ) (setq close (char-before)) (if (setq open (cadr (assq close '( (?\) ?\() (?\] ?\[) (?\} ?\{) (?\> ?\<) ) ) ) ) (progn (setq notstrc (string ?^ close ?\") notstre (string ?^ close open ?\") ) (while (and (< arg 0) (not (bobp))) (skip-chars-backward notstrc) (while (eq (char-before) ?\") (if (eq (char-before (1- (point))) ?\\) (forward-char -1) (skip-string-backward) ) (skip-chars-backward notstrc) ) (forward-char -1) (setq depth 1) (while (and (> depth 0) (not (bobp))) (skip-chars-backward notstre) (while (eq (char-before) ?\") (if (eq (char-before (1- (point))) ?\\) (forward-char -1) (skip-string-backward) ) (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)) ) ) ) ) ) ) ) )) --Greg --============_-1116668710==_ma============ Content-Type: text/html; charset="us-ascii" Re: parens matching not matching all matching parens
At 7:29 PM +0200 9/17/04, Arjan Bos wrote:
I already had something similar. Looking at the current bracket under point, finding its opposite and start counting the brackets you encounter. Another of the same bracket ups the ante, the opposite pops one from the ante. When the ante is empty, all bets are off, and the bracket is found.

My code isn't perfect, so I will borrow heavily from yours.

Thanks!

Arjan,
I cleaned up my code to the point that it will also work in place of the regular forward-sexp and backward-sexp for everyday use.  Here is a copy, if you're interested.  Please let me know if you try it and find any problems.  I will be using it myself as a permenant replacement for forward-sexp and backward-sexp.  If you are interested in being notified of any problems I may find, please let me know.

(defun skip-string-forward (&optional limit)
  (if (eq (char-after) ?\")
      (catch 'done
        (forward-char 1)
        (while t
          (skip-chars-forward "^\\\\\"" limit)
          (cond ((eq (point) limit)
                (throw 'done nil) )
                ((eq (char-after) ?\")
                (forward-char 1)
                (throw 'done nil) )
                (t
                (forward-char 1)
                (if (eq (point) limit)
                     (throw 'done nil)
                   (forward-char 1) ) ) ) ) ) ) )

(defun skip-string-backward (&optional limit)
  (if (eq (char-before) ?\")
      (catch 'done
        (forward-char -1)
        (while t
          (skip-chars-backward "^\"" limit)
          (if (eq (point) limit)
              (throw 'done nil) )
          (forward-char -1)
          (if (eq (point) limit)
              (throw 'done nil) )
          (if (not (eq (char-before) ?\\))
              (throw 'done nil) ) ) ) ) )



(defun forward-pexp (&optional arg)
  (interactive "p")
  (or arg (setq arg 1))
  (let (open close next notstrc notstro notstre depth pair)
    (catch 'done
      (cond ((> arg 0)
             (skip-chars-forward " \t\n")
             (if (not (memq (char-after) '(?\( ?\[ ?\{ ?\<)))
                (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
               (skip-chars-forward "^([{<\"")
               (while (eq (char-after) ?\")
                (skip-string-forward)
                (skip-chars-forward "^([{<\"") )
               (setq open (char-after))
               (if (setq close (cadr (assq open '( (?\( ?\))
                                                   (?\[ ?\])
                                                   (?\{ ?\})
                                                   (?\< ?\>) ) ) ) )
                   (progn
                     (setq notstro (string ?^ open ?\")
                           notstre (string ?^ open close ?\") )
                     (while (and (> arg 0) (not (eobp)))
                       (skip-chars-forward notstro)
                       (while (eq (char-after) ?\")
                        (if (eq (char-before) ?\\)
                             (forward-char 1)
                           (skip-string-forward) )
                        (skip-chars-forward notstro) )
                       (forward-char 1)
                       (setq depth 1)
                       (while (and (> depth 0) (not (eobp)))
                        (skip-chars-forward notstre)
                        (while (eq (char-after) ?\")
                           (if (eq (char-before) ?\\)
                               (forward-char 1)
                             (skip-string-forward) )
                           (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 " \t\t")
             (if (not (memq (char-before) '(?\) ?\] ?\} ?\>)))
                (progn
                   (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
                   (backward-prefix-chars) )
               (skip-chars-backward "^)]}>\"")
               (while (eq (char-before) ?\")
                (skip-string-backward)
                (skip-chars-backward "^)]}>\"") )
               (setq close (char-before))
               (if (setq open (cadr (assq close '( (?\) ?\()
                                                   (?\] ?\[)
                                                   (?\} ?\{)
                                                   (?\> ?\<) ) ) ) )
                   (progn
                     (setq notstrc (string ?^ close ?\")
                           notstre (string ?^ close open ?\") )
                     (while (and (< arg 0) (not (bobp)))
                       (skip-chars-backward notstrc)
                       (while (eq (char-before) ?\")
                        (if (eq (char-before (1- (point))) ?\\)
                             (forward-char -1)
                           (skip-string-backward) )
                        (skip-chars-backward notstrc) )
                       (forward-char -1)
                       (setq depth 1)
                       (while (and (> depth 0) (not (bobp)))
                        (skip-chars-backward notstre)
                        (while (eq (char-before) ?\")
                           (if (eq (char-before (1- (point))) ?\\)
                               (forward-char -1)
                             (skip-string-backward) )
                           (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)) ) ) ) ) ) ) ) ))
 

--Greg
--============_-1116668710==_ma============-- --===============0325747633== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Help-gnu-emacs mailing list Help-gnu-emacs@gnu.org http://lists.gnu.org/mailman/listinfo/help-gnu-emacs --===============0325747633==--