From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.help Subject: Re: jump between if-fi Date: Tue, 1 Jan 2008 19:12:05 +0000 Message-ID: <20080101191205.GF3830@muc.de> References: <106c0d25-e7ad-4cd5-a536-ff12ff73018a@e23g2000prf.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1199214217 17751 80.91.229.12 (1 Jan 2008 19:03:37 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Jan 2008 19:03:37 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Xah Lee Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jan 01 20:03:56 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J9mP5-0003uZ-Hg for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Jan 2008 20:03:55 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J9mOj-0007b5-Lm for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Jan 2008 14:03:33 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J9mOQ-0007XM-09 for help-gnu-emacs@gnu.org; Tue, 01 Jan 2008 14:03:14 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J9mOP-0007V8-8Q for help-gnu-emacs@gnu.org; Tue, 01 Jan 2008 14:03:13 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J9mOP-0007Uv-65 for help-gnu-emacs@gnu.org; Tue, 01 Jan 2008 14:03:13 -0500 Original-Received: from colin.muc.de ([193.149.48.1] helo=mail.muc.de) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J9mOO-0008E0-JP for help-gnu-emacs@gnu.org; Tue, 01 Jan 2008 14:03:12 -0500 Original-Received: (qmail 80145 invoked by uid 3782); 1 Jan 2008 19:03:10 -0000 Original-Received: from acm.muc.de (p57AF58EF.dip.t-dialin.net [87.175.88.239]) by colin2.muc.de (tmda-ofmipd) with ESMTP; Tue, 01 Jan 2008 20:03:09 +0100 Original-Received: (qmail 8996 invoked by uid 1000); 1 Jan 2008 19:12:05 -0000 Content-Disposition: inline In-Reply-To: <106c0d25-e7ad-4cd5-a536-ff12ff73018a@e23g2000prf.googlegroups.com> User-Agent: Mutt/1.5.9i X-Delivery-Agent: TMDA/1.1.5 (Fettercairn) X-Primary-Address: acm@muc.de X-detected-kernel: by monty-python.gnu.org: FreeBSD 4.6-4.9 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: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:50420 Archived-At: Hi, Xah! On Tue, Jan 01, 2008 at 10:12:31AM -0800, Xah Lee wrote: > rea...@newsguy.com wrote: > How can I make emacs do something similar with if-fi constructs as it > does with parens? [ .... ] > Now, the problem is to make it work for shell's if/fi syntax. Alan > Mackenzie gave suggestions how to properly implement this, but he > drivels on like a militarist sees the world. And a Happy New Year to you too, Xah! > Here's a hack that does what you want. > (defun jump-to-if-fi () > "jump to ?$B!Hfi?$B!I if cursor is on word ?$B!Hif?$B!I, and vice versa." > (interactive) > (let (myword) > (setq myword (thing-at-point 'word)) > (if (equal myword "if") > (progn > (set-mark-command nil) > (search-forward "fi") > ) > (if (equal myword "fi") > (progn > (set-mark-command nil) > (search-backward "if") > ) > ) > ) > ) > ) > (global-set-key (kbd "") 'jump-to-if-fi) That's fine up to a point, my dear boy, but "if"s, "while"s, and so on nest in shell scripts. The above function thus wouldn't always find the right "fi" or "if". The approach is a perfectly good one and can be made to work, but it will involve some sort of recursion, either by explicitly building a push-down automaton, or recursively calling functions to scan over "if-fi"s and "while-done"s. However, this way will surely be more tedious and difficult to debug and modify than setting text properties and using Emacs's built in syntax table functionality. > Xah -- Alan Mackenzie (Nuremberg, Germany).