[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: jump between if-fi

From: Alan Mackenzie
Subject: Re: jump between if-fi
Date: Tue, 1 Jan 2008 19:12:05 +0000
User-agent: Mutt/1.5.9i

Hi, Xah!

On Tue, Jan 01, 2008 at 10:12:31AM -0800, Xah Lee wrote:
> 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 "<f8>") '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

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]