all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Wrapping code in a try/except
@ 2010-11-03 10:13 Andrea Crotti
  2010-11-03 10:51 ` Andreas Röhler
  2010-11-04  6:09 ` Kevin Rodgers
  0 siblings, 2 replies; 10+ messages in thread
From: Andrea Crotti @ 2010-11-03 10:13 UTC (permalink / raw)
  To: help-gnu-emacs

It would be nice sometimes to be able to wrap multiple lines

istruction1();
istruction2();

in something like

try {
    istruction1();
    istruction2();
} except ...

I can use for example
(kill-region (point) (mark))

then insert what the construct and yank what I killed, but I would
rather use a yasnippet snippet for it, but I can't use a snippet since
while I'm writing I don't have a region selected...

Should I create a command instead? Or is there something for these kind
of things?




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-03 10:13 Andrea Crotti
@ 2010-11-03 10:51 ` Andreas Röhler
  2010-11-19 14:39   ` Andrea Crotti
  2010-11-04  6:09 ` Kevin Rodgers
  1 sibling, 1 reply; 10+ messages in thread
From: Andreas Röhler @ 2010-11-03 10:51 UTC (permalink / raw)
  To: help-gnu-emacs

Am 03.11.2010 11:13, schrieb Andrea Crotti:
> It would be nice sometimes to be able to wrap multiple lines
>
> istruction1();
> istruction2();
>
> in something like
>
> try {
>      istruction1();
>      istruction2();
> } except ...
>
> I can use for example
> (kill-region (point) (mark))
>
> then insert what the construct and yank what I killed, but I would
> rather use a yasnippet snippet for it, but I can't use a snippet since
> while I'm writing I don't have a region selected...
>
> Should I create a command instead? Or is there something for these kind
> of things?
>
>
>

Hi Andrea,

if its just about editing and that simple as shown
M-x query-replace-regexp

should do all you need.

Maybe narrow the buffer to region before.


Andreas

--
https://code.launchpad.net/~a-roehler/python-mode/python-mode-components
https://code.launchpad.net/s-x-emacs-werkstatt/




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-03 10:13 Andrea Crotti
  2010-11-03 10:51 ` Andreas Röhler
@ 2010-11-04  6:09 ` Kevin Rodgers
  1 sibling, 0 replies; 10+ messages in thread
From: Kevin Rodgers @ 2010-11-04  6:09 UTC (permalink / raw)
  To: help-gnu-emacs

On 11/3/10 4:13 AM, Andrea Crotti wrote:
> It would be nice sometimes to be able to wrap multiple lines
>
> istruction1();
> istruction2();
>
> in something like
>
> try {
>      istruction1();
>      istruction2();
> } except ...

(condition-case nil
     (progn
       (istruction1)
       (istruction2))
   (error ...))

-- 
Kevin Rodgers
Denver, Colorado, USA




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
       [not found] <mailman.1.1288779213.27562.help-gnu-emacs@gnu.org>
@ 2010-11-04 19:17 ` Stefan Monnier
  0 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2010-11-04 19:17 UTC (permalink / raw)
  To: help-gnu-emacs

> It would be nice sometimes to be able to wrap multiple lines
> istruction1();
> istruction2();

> in something like

> try {
>     istruction1();
>     istruction2();
> } except ...

Don't know about yasnippet, but with skeletons, you'd do:

(define-skeleton my-try-except
  "Insert a try..except template."
  nil
  \n "try {" > \n _ \n "} except ..." > \n)

And then you just select the lines you want to wrap and
do M-x my-try-except RET.
You'll probably also want to make this command more easily accessible,
e.g. by adding it to your abbrevs or some key binding.


        Stefan


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-03 10:51 ` Andreas Röhler
@ 2010-11-19 14:39   ` Andrea Crotti
  2010-11-19 18:27     ` Andreas Röhler
  0 siblings, 1 reply; 10+ messages in thread
From: Andrea Crotti @ 2010-11-19 14:39 UTC (permalink / raw)
  To: help-gnu-emacs

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

> Hi Andrea,
>
> if its just about editing and that simple as shown
> M-x query-replace-regexp
>
> should do all you need.
>
> Maybe narrow the buffer to region before.
>
>
> Andreas
>
> --
> https://code.launchpad.net/~a-roehler/python-mode/python-mode-components
> https://code.launchpad.net/s-x-emacs-werkstatt/

Well I watend to use yasnippet so here it is ;):
--8<---------------cut here---------------start------------->8---
# -*- mode: snippet -*-
# name: tryw
# key: tryw
# contributor: Andrea Crotti
# --
try {
    `(or yas/selected-text (car kill-ring))`
} catch ${1:type} {

}
--8<---------------cut here---------------end--------------->8---

this is even nicer, because it uses the selected text or the last killed
string if nothing is found.

Of course is a bit intrusive so another "normal" snippet is also needed.
And another problem is that the indentation is not correct.
Or better it is correct inside the try/catch but it's not automatically
indented in the rest of the C++ code...




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-19 14:39   ` Andrea Crotti
@ 2010-11-19 18:27     ` Andreas Röhler
  2010-11-19 20:50       ` Andrea Crotti
  0 siblings, 1 reply; 10+ messages in thread
From: Andreas Röhler @ 2010-11-19 18:27 UTC (permalink / raw)
  To: help-gnu-emacs

  it's not automatically
> indented in the rest of the C++ code...
>

Write a function based on `indent-region' with reasonable defaults -- 
function-beginnning-pos, point.

Make it run with an idle-timer like fontifying does.

Could be a feature request if not existing already.





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-19 18:27     ` Andreas Röhler
@ 2010-11-19 20:50       ` Andrea Crotti
  2010-11-20 17:11         ` Andreas Röhler
  0 siblings, 1 reply; 10+ messages in thread
From: Andrea Crotti @ 2010-11-19 20:50 UTC (permalink / raw)
  To: help-gnu-emacs

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

>  it's not automatically
>> indented in the rest of the C++ code...
>>
>
> Write a function based on `indent-region' with reasonable defaults -- 
> function-beginnning-pos, point.
>
> Make it run with an idle-timer like fontifying does.
>
> Could be a feature request if not existing already.

Well in theory something like that would be perfect reading what those
things should do:

--8<---------------cut here---------------start------------->8---
(defun re-indent-buffer ()
  "reindent the whole buffer"
  (indent-region yas/snippet-beg yas/snippet-end))

(add-hook 'yas/after-exit-snippet-hook 're-indent-buffer)
--8<---------------cut here---------------end--------------->8---

in practice I guess there are some problems since now I have to force
the exit of the snippet instead, so it doens't work at all...




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-19 20:50       ` Andrea Crotti
@ 2010-11-20 17:11         ` Andreas Röhler
  2010-11-23 10:19           ` Andrea Crotti
  0 siblings, 1 reply; 10+ messages in thread
From: Andreas Röhler @ 2010-11-20 17:11 UTC (permalink / raw)
  To: help-gnu-emacs

Am 19.11.2010 21:50, schrieb Andrea Crotti:
> Andreas Röhler<andreas.roehler@easy-emacs.de>  writes:
>
>>   it's not automatically
>>> indented in the rest of the C++ code...
>>>
>>
>> Write a function based on `indent-region' with reasonable defaults --
>> function-beginnning-pos, point.
>>
>> Make it run with an idle-timer like fontifying does.
>>
>> Could be a feature request if not existing already.
>
> Well in theory something like that would be perfect reading what those
> things should do:
>
> --8<---------------cut here---------------start------------->8---
> (defun re-indent-buffer ()
>    "reindent the whole buffer"
>    (indent-region yas/snippet-beg yas/snippet-end))
>
> (add-hook 'yas/after-exit-snippet-hook 're-indent-buffer)
> --8<---------------cut here---------------end--------------->8---
>
> in practice I guess there are some problems since now I have to force
> the exit of the snippet instead, so it doens't work at all...
>
>
>

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









^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-20 17:11         ` Andreas Röhler
@ 2010-11-23 10:19           ` Andrea Crotti
  2010-11-23 14:32             ` Andrea Crotti
  0 siblings, 1 reply; 10+ messages in thread
From: Andrea Crotti @ 2010-11-23 10:19 UTC (permalink / raw)
  To: help-gnu-emacs

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} {

}




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Wrapping code in a try/except
  2010-11-23 10:19           ` Andrea Crotti
@ 2010-11-23 14:32             ` Andrea Crotti
  0 siblings, 0 replies; 10+ messages in thread
From: Andrea Crotti @ 2010-11-23 14:32 UTC (permalink / raw)
  To: help-gnu-emacs

Andrea Crotti <andrea.crotti.0@gmail.com> writes:
>
> 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} {
>
> }

I updated an tried again, actually the solution using

(defun my-whole-indent ()
         (indent-region (point-min)(point-max)))
         
(add-hook 'yas/after-exit-snippet-hook 'my-whole-indent)

is almost perfect, the only problem is that for some reasons after
exiting the first snippet, EVERY key I press triggers the exit snippet,
so emacs keeps reindenting the buffer all the time.

It doesn't make much sense to me, I thought it was my configuration but
also with 
Emacs -Q and only the minimal loaded it's the same...
Also using a timer of course can be fine, maybe even better




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-11-23 14:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1.1288779213.27562.help-gnu-emacs@gnu.org>
2010-11-04 19:17 ` Wrapping code in a try/except Stefan Monnier
2010-11-03 10:13 Andrea Crotti
2010-11-03 10:51 ` Andreas Röhler
2010-11-19 14:39   ` Andrea Crotti
2010-11-19 18:27     ` Andreas Röhler
2010-11-19 20:50       ` Andrea Crotti
2010-11-20 17:11         ` Andreas Röhler
2010-11-23 10:19           ` Andrea Crotti
2010-11-23 14:32             ` Andrea Crotti
2010-11-04  6:09 ` Kevin Rodgers

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.