From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Abrahamsen Subject: Re: function for inserting a block Date: Sun, 03 Sep 2017 08:56:30 -0700 Message-ID: <87ziabepxt.fsf@ericabrahamsen.net> References: <877exghblx.fsf@ericabrahamsen.net> <87efromccg.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59672) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1doXGn-00049F-HU for emacs-orgmode@gnu.org; Sun, 03 Sep 2017 11:57:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1doXGi-0006Si-Lf for emacs-orgmode@gnu.org; Sun, 03 Sep 2017 11:57:09 -0400 Received: from [195.159.176.226] (port=58408 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1doXGi-0006SH-E0 for emacs-orgmode@gnu.org; Sun, 03 Sep 2017 11:57:04 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1doXGT-0006Vh-SJ for emacs-orgmode@gnu.org; Sun, 03 Sep 2017 17:56:49 +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 Nicolas Goaziou writes: > Hello, > > Eric Abrahamsen writes: > >> The easy template entry thing is useful as far as it goes, but for some >> reason I find myself "marking up" existing text in Org as least as often >> as I'm writing new text from scratch. I've always wanted a "wrap region >> in block" command, and finally wrote one. Don't know why it took me so >> long. Would something like this be attractive for inclusion in Org? > > Thanks. I agree Org needs something like this. > >> (defun org-insert-structure-template (type start end) >> "Insert a block structure as in #+BEGIN_TYPE/#+END_TYPE. >> >> Prompts for a block TYPE, and inserts the block. With an active >> region, wrap the region in the block." >> (interactive "sBlock type: \nr") >> (let ((s (set-marker (make-marker) start)) >> (e (set-marker (make-marker) end))) > > (set-marker (make-marker) start) -> (copy-marker start) > > You need to clean these markers at the end of the function. Markers set > to a position are never garbage-collected. Usually, it happens at the > end of an `unwind-protect'. Okay. >> (goto-char s) >> (goto-char (line-beginning-position)) >> (insert (format "#+BEGIN_%s\n" (upcase type))) > > (upcase type) is wrong, because special blocks are case sensitive. I discovered that the moment I started using it! >> (goto-char e) >> (goto-char (line-end-position)) > > (end-of-line) ? Bah, I can never keep track of what's available. >> (insert (format "\n#+END_%s" (upcase type))))) > > The function also needs to take care about global indentation. Inserting > at column 0 may not be desirable. > > What happens if no region is active? Can it mark the element at point > (see `org-mark-element')? What happens if the chosen type is verbatim > (e.g. "example"), can it protect "#+", "*" and so on with commas (see > `org-escape-code-in-region')? The "r" interactive code just isn't that useful, I wish it wouldn't raise an error. Here's another stab at it. Is "example" the only block that should be verbatim? Will using `newline-and-indent' instead of inserting literal newlines solve the indentation problem? (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. With no active region, wrap the current element." (interactive "sBlock type: ") (unless (use-region-p) (org-mark-element)) (let ((s (copy-marker (min (point) (mark)))) (e (copy-marker (max (point) (mark))))) (when (string-equal (downcase type) "example") (org-escape-code-in-region s e)) (goto-char s) (beginning-of-line) (insert (format "#+BEGIN_%s" type)) (newline-and-indent) (goto-char e) (unless (bolp) (end-of-line) (newline-and-indent)) (insert (format "#+END_%s" type)) (newline-and-indent) (set-marker s nil) (set-marker e nil))) >> If this is acceptable, I'd like to bind it to "C-c i", and would provide >> docs. > > We cannot bind it to "C-c i", this is a reserved key-binding. But we can > suggest users to do so in the manual. Or find another binding. That's fine. Incidentally, why is "C-c i" reserved? It's not bound to anything here. Does it look like in terminal Emacs? Eric