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.