emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] function for inserting a block


From: Eric Abrahamsen
Subject: Re: [O] function for inserting a block
Date: Sun, 03 Sep 2017 08:56:30 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Nicolas Goaziou <address@hidden> writes:

> Hello,
>
> Eric Abrahamsen <address@hidden> 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 <TAB> in terminal Emacs?

Eric




reply via email to

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