help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Wrapping code in a try/except


From: Andrea Crotti
Subject: Re: Wrapping code in a try/except
Date: Tue, 23 Nov 2010 11:19:47 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (darwin)

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> below an example, how I would approach the issue
> (must not be the best way, just for discussion...)
> It takes a little bit more code.
>
> Yasnippet seems not designed for use from programms.
>
> ;;;;
>
> (defun my-try-wrap-function (beg end)
>     (interactive "r*")
>     (my-wrap-function-base beg end "try {\n" "\n} except"))
>
> (defun my-wrap-function-base (beg end &optional before after)
>   " "
>   (let ((end (copy-marker end)))
>     (my-wrap-function-intern beg end before after)))
>
> (defun my-wrap-function-intern (beg end &optional before after)
>   (goto-char beg)
>   (when before (insert before))
>   (goto-char end)
>   (when after (insert after))
>   (indent-region beg end)
>   (beginning-of-line)
>   (indent-according-to-mode))
>
> BTW the second function might be used to cover default arguments in
> other cases like this:
>
>   (let ((beg (cond (beg)
>                    ((region-active-p)
>                     (region-beginning))
>                    ((bobp)
>                     (point))
>                    (t (defun-beginning-position))))
>         (end (cond (end)
>                    ((region-active-p)
>                     (copy-marker (region-end)))
>                    ((eobp)
>                     (point))
>                    (t (defun-end-position))))
>
>  so I use this step, which might be omitted here.
>
> happy hacking
>
> Andreas

puff that'a a lot of code ;)
You would be right about yasnippet, if it wasn't that there are
variables like

yas/selected-text, and wrap-region.
This for example works perfectly when I use the kill-ring.
When I use the selected text it just doesn't indent after, maybe the
hook is a bit buggy.
I'll try to update and see if anything changes, but actually I would
prefer it a lot...

try {
    `(or yas/selected-text (car kill-ring))`
} catch ${1:Exception} {

}




reply via email to

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