From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andrea Crotti Newsgroups: gmane.emacs.help Subject: Re: Wrapping code in a try/except Date: Tue, 23 Nov 2010 11:19:47 +0100 Message-ID: References: <4CD13E95.3040808@easy-emacs.de> <4CE6C190.8040007@easy-emacs.de> <4CE80132.10009@easy-emacs.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1290507641 32758 80.91.229.12 (23 Nov 2010 10:20:41 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 23 Nov 2010 10:20:41 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 23 11:20:37 2010 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.69) (envelope-from ) id 1PKpzH-0003hu-Si for geh-help-gnu-emacs@m.gmane.org; Tue, 23 Nov 2010 11:20:36 +0100 Original-Received: from localhost ([127.0.0.1]:43568 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PKpzH-00022d-8a for geh-help-gnu-emacs@m.gmane.org; Tue, 23 Nov 2010 05:20:35 -0500 Original-Received: from [140.186.70.92] (port=53023 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PKpym-00021n-0O for help-gnu-emacs@gnu.org; Tue, 23 Nov 2010 05:20:04 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PKpyk-0007XH-HW for help-gnu-emacs@gnu.org; Tue, 23 Nov 2010 05:20:03 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:57912) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PKpyk-0007VD-7n for help-gnu-emacs@gnu.org; Tue, 23 Nov 2010 05:20:02 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PKpyh-0003RB-C1 for help-gnu-emacs@gnu.org; Tue, 23 Nov 2010 11:19:59 +0100 Original-Received: from ip1-201.halifax.rwth-aachen.de ([137.226.108.201]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 23 Nov 2010 11:19:59 +0100 Original-Received: from andrea.crotti.0 by ip1-201.halifax.rwth-aachen.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 23 Nov 2010 11:19:59 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 66 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip1-201.halifax.rwth-aachen.de User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (darwin) Cancel-Lock: sha1:/WshYr1sgEmOO+dk2nKwROBJYUs= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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:75420 Archived-At: Andreas Röhler 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} { }