From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Abrahamsen Subject: Re: function for inserting a block Date: Sun, 10 Sep 2017 11:39:50 -0700 Message-ID: <874lsabdop.fsf@ericabrahamsen.net> References: <877exghblx.fsf@ericabrahamsen.net> <87efromccg.fsf@nicolasgoaziou.fr> <87ziabepxt.fsf@ericabrahamsen.net> <87bmml2fb0.fsf@ericabrahamsen.net> <87fubuzpsa.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:43882) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dr7Ap-00012g-Jm for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 14:41:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dr7Ak-0001VS-OM for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 14:41:39 -0400 Received: from [195.159.176.226] (port=43289 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dr7Ak-0001Tz-HD for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 14:41:34 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dr7AX-0001rN-J9 for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 20:41:21 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Cc: Nicolas Goaziou On 09/10/17 14:44 PM, Nicolas Goaziou wrote: [...] > Some free key bindings: > - C-c C-x C-e > - C-c C-x C-g > - C-c C-x C-h > - C-c C-x C-k > - C-c C-x h > - C-c C-x j > - C-c C-x k > - C-c C-x l > - C-c C-x m > - C-c C-x n > - C-c C-x r > - C-c C-x s > - C-c C-x t > - C-c C-x u > - C-c C-x w > - C-c C-x x > - C-c C-x y > - C-c C-x z > > For the record, `C-c C-x C-f' is `org-emphasize', which is related. How about `C-c C-x C-t', for "template"? >> +(defun org-insert-structure-template (type) >> + "Insert a block structure of the type #+BEGIN_FOO/#+END_FOO. >> +Prompts for a block type, and inserts the block. With an active >> +region, wrap the region in the block." >> + (interactive "sBlock type: ") >> + (unless (use-region-p) >> + (org-mark-element)) >> + (let ((s (copy-marker (min (point) (mark)))) >> + (e (copy-marker (max (point) (mark))))) > > Use: > > (region-beginning) > (region-end) > > not > > (mark) Whoops. > If there is no active region, is it better to mark element or to create > an empty block at point? That was your suggestion! :) We've already got easy templates for creating an empty block, so it seemed like a good one. >> + (when (string-equal (downcase type) "example") >> + (org-escape-code-in-region s e)) >> + (goto-char s) >> + (beginning-of-line) >> + (insert (format "#+BEGIN_%s" type)) > > I would store current indentation here and insert it in front of the > block openening, and closing, line. In any case, I would not use > `newline-and-indent' which could do unrelated stuff (e.g., re-indenting > a whole part of the buffer). Gotcha, I was not sure about that part. >> + (newline-and-indent) >> + (goto-char e) >> + (unless (bolp) >> + (end-of-line) >> + (newline-and-indent)) >> + (insert (format "#+END_%s" type)) >> + (newline-and-indent) > > See above. > >> + (goto-char s) >> + (end-of-line) > > Why going to S? Initial position might be at the end of the region. I was just thinking that a common use-case would be to add more options/arguments to the block beginning. I don't mind much either way. > Also, could you write some tests along with your function? Will do.