From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: function for inserting a block Date: Sun, 10 Sep 2017 14:44:37 +0200 Message-ID: <87fubuzpsa.fsf@nicolasgoaziou.fr> References: <877exghblx.fsf@ericabrahamsen.net> <87efromccg.fsf@nicolasgoaziou.fr> <87ziabepxt.fsf@ericabrahamsen.net> <87bmml2fb0.fsf@ericabrahamsen.net> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53280) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dr1bP-0007Jb-CL for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 08:44:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dr1bM-0002u5-9l for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 08:44:43 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:51664) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dr1bM-0002sz-40 for emacs-orgmode@gnu.org; Sun, 10 Sep 2017 08:44:40 -0400 In-Reply-To: <87bmml2fb0.fsf@ericabrahamsen.net> (Eric Abrahamsen's message of "Fri, 08 Sep 2017 11:52:51 -0700") 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: Eric Abrahamsen Cc: emacs-orgmode@gnu.org Hello, Eric Abrahamsen writes: > And here's an actual patch, with docs. Thank you! Some comments follow. > +This function can be used to wrap existing text or Org elements in > +a #+BEGIN_FOO/#+END_FOO block. Not bound to a key by default. 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. > +(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) If there is no active region, is it better to mark element or to create an empty block at point? > + (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). > + (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. Also, could you write some tests along with your function? Regards, -- Nicolas Goaziou