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: Andreas Röhler
Subject: Re: Wrapping code in a try/except
Date: Sat, 20 Nov 2010 18:11:14 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6

Am 19.11.2010 21:50, schrieb Andrea Crotti:
Andreas Röhler<address@hidden>  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









reply via email to

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