Re: [O] function for inserting a block

2017-12-10 Thread Thorsten Jolitz

Hello,
this is a long thread so I did not read everything, just want to mention
that in org-dp.el I did something similar using

- tempo templates (for a programmer who wants to insert an
org-dp-create call in his emacs-lisp program) and 

- a universal (org) prompt function for a user who wants to insert a
src-block in his org file.

This one for productive use in emacs-lisp programs (note that there is
tab completion for values):

,[ C-h f tempo-template-org-dp-create RET ]
| tempo-template-org-dp-create is an interactive Lisp function.
| 
| (tempo-template-org-dp-create  ARG)
| 
| Insert org-dp-create template.
| 
| [back]
`

This one with comments to give a bit more explanation to the programmer:

,[ C-h f tempo-template-org-dp-create-with-comments RET ]
| tempo-template-org-dp-create-with-comments is an interactive Lisp
| function.
| 
| (tempo-template-org-dp-create-with-comments  ARG)
| 
| Insert org-dp-create template.
| 
| [back]
`

This one is on the user level (in org files), C-c w w ist just my
personnal key binding from my .emacs:

,[ C-h f org-dp-wrap-in-block RET ]
| org-dp-wrap-in-block is an interactive Lisp function in
| ‘org-dp-lib.el’.
| 
| It is bound to C-c w w.
| 
| (org-dp-wrap-in-block  LINES USER-INFO  PROMPT-SPEC)
| 
| Wrap sexp-at-point or region in Org block.
| 
| A region instead of the sexp-at-point is wrapped if either
| 
|- optional arg LINES is an (positive or negative) integer or
| 
|- the region is active
| 
| In the first case the region is determined by moving LINES lines
| up (LINES is positive) or down (LINES is negative) from point
| using ‘forward-line’, in the second case the active region is
| used.
| 
| If point is already inside of a block, modify it or unwrap its
| content/value instead of wrapping it in another block, except if
| explicitly asked for by user.
| 
| If USER-INFO is given, it should be a list in the format returned
| by ‘org-dp-prompt-all’, i.e.
| 
|  (elem-type contents replace affiliated args)
| 
| Look up that function’s docstring for more information about the
| list’s elements. A non-nil USER-INFO suppresses calls to
| ‘org-dp-prompt-all’ and is used instead of its return value.
| 
| Possible  PROMPT-SPEC should be keyword/value pairs used for
| restricting user-prompting via ‘org-dp-prompt-all’, e.g.
| 
|   :noprompt-affiliated t :noprompt-replace t
| 
| see the docstring of that function for more info.
| 
| [back]
`

org-dp-prompt-all is the universal function, with  argument
PROMPT-SPEC you can reduce the prompting and such easily create your own
specific 'wrap in block' utility commands without much programming.

Hope this is not off-topic.

-- 
cheers,
Thorsten





Re: [O] function for inserting a block

2017-11-20 Thread Eric Abrahamsen
Rasmus  writes:

> Hi Eric,
>
>
>> It incorporates Rasmus' org-tempo.el file, with modifications, and
>> Thomas' suggestion to re-instate `org-try-structure-completion', and,
>> erm, stardiviner's request to honor
>> `org-babel-uppercase-example-markers'.
>>
>> Remaining issues:
>>
>> 1. The "org-include" tempo template doesn't work, for reasons I don't
>>understand (I've never used tempo before). Nothing happens when I hit
>>.
>> 2. Now it seems like there should be completion when prompting for a
>>string key. Feature creep! But likely worthwhile feature creep.
>> 3. I've rather rashly renamed the relevant customization options
>>`org-structure-block-alist' (for blocks added via
>>`org-insert-structure-template') and `org-structure-keyword-alist'
>>(for keywords insertable via the tempo system). Perhaps this was a
>>bad idea. If it's not a bad idea, maybe
>>`org-insert-structure-template' should be renamed `org-insert-block'
>>or something like that.
>> 3. Docs need to be updated.
>
> I will have a look at org-tempo and try to figure out why it isn’t
> working.  As it would probably warrant changes to ORG-NEWS and maybe the
> manual I will do it in a separate commit if people find it useful.

Thanks! NEWS and the manual will have to be updated, yes. But I still
think this whole change should go in at once, because we'll need to
coordinate the shift from char keys to string keys in both
`org-structure-block-alist' and `org-structure-keyword-alist' (or
whatever those end up getting named).

Eric




Re: [O] function for inserting a block

2017-11-20 Thread Rasmus
Hi Eric,


> It incorporates Rasmus' org-tempo.el file, with modifications, and
> Thomas' suggestion to re-instate `org-try-structure-completion', and,
> erm, stardiviner's request to honor
> `org-babel-uppercase-example-markers'.
>
> Remaining issues:
>
> 1. The "org-include" tempo template doesn't work, for reasons I don't
>understand (I've never used tempo before). Nothing happens when I hit
>.
> 2. Now it seems like there should be completion when prompting for a
>string key. Feature creep! But likely worthwhile feature creep.
> 3. I've rather rashly renamed the relevant customization options
>`org-structure-block-alist' (for blocks added via
>`org-insert-structure-template') and `org-structure-keyword-alist'
>(for keywords insertable via the tempo system). Perhaps this was a
>bad idea. If it's not a bad idea, maybe
>`org-insert-structure-template' should be renamed `org-insert-block'
>or something like that.
> 3. Docs need to be updated.

I will have a look at org-tempo and try to figure out why it isn’t
working.  As it would probably warrant changes to ORG-NEWS and maybe the
manual I will do it in a separate commit if people find it useful.

Rasmus
-- 
Hvor meget poesi tror De kommer ud af et glas isvand?




Re: [O] function for inserting a block

2017-11-17 Thread numbch...@gmail.com
Ok, I see, Just I have to replace all uppercased babels and example blocks
to lowercase to keep same.
I have a lot of org files. Huge for me.
I can take with the lower-case tag.

[stardiviner] GPG key ID: 47C32433
IRC(freeenode): stardiviner Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

On Sat, Nov 18, 2017 at 3:14 AM, Eric Abrahamsen 
wrote:

> "numbch...@gmail.com"  writes:
>
> > Yeah, I misunderstand your code. Anyway, It should be like this:
> >
> > #+BEGIN_SRC python
> > ...
> > #+END_SRC
> >
> > #+BEGIN_EXAMPLE
> > ...
> > #+END_EXAMPLE
> >
> > etc
>
> If you look back up this (admittedly very long) thread, you'll see the
> general consensus is to move to lower-case tags. I've intentionally left
> things lowercase, and now will have to handle "example" specially, based
> on the value of `org-babel-uppercase-example-markers'.
>
>
>


Re: [O] function for inserting a block

2017-11-17 Thread Eric Abrahamsen
"numbch...@gmail.com"  writes:

> Yeah, I misunderstand your code. Anyway, It should be like this:
>
> #+BEGIN_SRC python
> ...
> #+END_SRC
>
> #+BEGIN_EXAMPLE
> ...
> #+END_EXAMPLE
>
> etc

If you look back up this (admittedly very long) thread, you'll see the
general consensus is to move to lower-case tags. I've intentionally left
things lowercase, and now will have to handle "example" specially, based
on the value of `org-babel-uppercase-example-markers'.




Re: [O] function for inserting a block

2017-11-17 Thread numbch...@gmail.com
Yeah, I misunderstand your code. Anyway, It should be like this:

#+BEGIN_SRC python
...
#+END_SRC

#+BEGIN_EXAMPLE
...
#+END_EXAMPLE

etc

[stardiviner] GPG key ID: 47C32433
IRC(freeenode): stardiviner Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

On Thu, Nov 16, 2017 at 12:24 AM, Eric Abrahamsen 
wrote:

> "numbch...@gmail.com"  writes:
>
> > I think the function `org-insert-structure-template' also should
> respect the `org-babel-uppercase-example-markers'.
> > Besides, your new diff does not have a condition on
> `org-babel-uppercase-example-markers', you just use `upcase-initials` by
> default. Then the new `tempo` snippets
> > will be uppercase, but the `org-insert-structure-template` inserted
> templates will be different.
>
> I'm not entirely understanding you here -- it is
> `org-insert-structure-template' that respects
> `org-babel-uppercase-example-markers', not the tempo snippet (which was
> an omission). And none of the code is using `upcase-initials'.
>
> As far as I can see, the bug is that EXAMPLE is uppercased but the
> "begin" and "end" aren't, and also that the tempo snippet doesn't
> capitalize at all...
>


Re: [O] function for inserting a block

2017-11-15 Thread Eric Abrahamsen
"numbch...@gmail.com"  writes:

> I think the function `org-insert-structure-template' also should respect the 
> `org-babel-uppercase-example-markers'.
> Besides, your new diff does not have a condition on 
> `org-babel-uppercase-example-markers', you just use `upcase-initials` by 
> default. Then the new `tempo` snippets
> will be uppercase, but the `org-insert-structure-template` inserted templates 
> will be different.

I'm not entirely understanding you here -- it is
`org-insert-structure-template' that respects
`org-babel-uppercase-example-markers', not the tempo snippet (which was
an omission). And none of the code is using `upcase-initials'.

As far as I can see, the bug is that EXAMPLE is uppercased but the
"begin" and "end" aren't, and also that the tempo snippet doesn't
capitalize at all...



Re: [O] function for inserting a block

2017-11-15 Thread numbch...@gmail.com
I think the function `org-insert-structure-template' also should respect
the `org-babel-uppercase-example-markers'.
Besides, your new diff does not have a condition on
`org-babel-uppercase-example-markers', you just use `upcase-initials` by
default. Then the new `tempo` snippets will be uppercase, but the
`org-insert-structure-template` inserted templates will be different.

[stardiviner] GPG key ID: 47C32433
IRC(freeenode): stardiviner Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

On Wed, Nov 15, 2017 at 5:36 AM, Eric Abrahamsen 
wrote:

> "Thomas S. Dye"  writes:
>
> > Eric Abrahamsen writes:
> >
> >> Rasmus  writes:
> >>
> >>> Hi Eric,
> >>>
> >>> Eric Abrahamsen  writes:
> >>>
> > Also, Eric, it seems that org-structure-template-alist only supports
> a
> > single letter for short-hands (the car of an entry in
> > org-structure-template-alist is a char).  I used to have blocks like
> " > expanding to an "abstract" special-block, which I guess isn’t
> possible
> > anymore?
> 
>  I hadn't thought of that. Really, all I ever wanted was to wrap things
>  in blocks...
> 
>  I don't see any reason why org-structure-template-alist couldn't go
> back
>  to using string keys. Then we could use read-string, and wouldn't have
>  to have special  behavior -- a string that didn't exist in the
>  alist could just be used literally to make a block.
> >>>
> >>> I’d prefer that.  For some special blocks, a few characters might
> makes it
> >>> more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.
> >>
> >> Here's the simplest solution.
> >>
> >> There still remains the fact that `org-structure-template-alist' has
> >> changed format, and `org-try-structure-completion' no longer exists.
> >> That may still annoy some people who were using the internals of the
> >> process, but...
> >
> > Would something like this work?
> >
> > (defun org-try-structure-completion ()
> >   (tempo-complete-tag))
>
> Here's the newest version!
>
> It incorporates Rasmus' org-tempo.el file, with modifications, and
> Thomas' suggestion to re-instate `org-try-structure-completion', and,
> erm, stardiviner's request to honor
> `org-babel-uppercase-example-markers'.
>
> Remaining issues:
>
> 1. The "org-include" tempo template doesn't work, for reasons I don't
>understand (I've never used tempo before). Nothing happens when I hit
>.
> 2. Now it seems like there should be completion when prompting for a
>string key. Feature creep! But likely worthwhile feature creep.
> 3. I've rather rashly renamed the relevant customization options
>`org-structure-block-alist' (for blocks added via
>`org-insert-structure-template') and `org-structure-keyword-alist'
>(for keywords insertable via the tempo system). Perhaps this was a
>bad idea. If it's not a bad idea, maybe
>`org-insert-structure-template' should be renamed `org-insert-block'
>or something like that.
> 3. Docs need to be updated.
>
> Comments welcome!
>
> Eric
>
>


Re: [O] function for inserting a block

2017-11-14 Thread Eric Abrahamsen
"Thomas S. Dye"  writes:

> Eric Abrahamsen writes:
>
>> Rasmus  writes:
>>
>>> Hi Eric,
>>>
>>> Eric Abrahamsen  writes:
>>>
> Also, Eric, it seems that org-structure-template-alist only supports a
> single letter for short-hands (the car of an entry in
> org-structure-template-alist is a char).  I used to have blocks like " expanding to an "abstract" special-block, which I guess isn’t possible
> anymore?

 I hadn't thought of that. Really, all I ever wanted was to wrap things
 in blocks...

 I don't see any reason why org-structure-template-alist couldn't go back
 to using string keys. Then we could use read-string, and wouldn't have
 to have special  behavior -- a string that didn't exist in the
 alist could just be used literally to make a block.
>>>
>>> I’d prefer that.  For some special blocks, a few characters might makes it
>>> more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.
>>
>> Here's the simplest solution.
>>
>> There still remains the fact that `org-structure-template-alist' has
>> changed format, and `org-try-structure-completion' no longer exists.
>> That may still annoy some people who were using the internals of the
>> process, but...
>
> Would something like this work?
>
> (defun org-try-structure-completion ()
>   (tempo-complete-tag))

Here's the newest version!

It incorporates Rasmus' org-tempo.el file, with modifications, and
Thomas' suggestion to re-instate `org-try-structure-completion', and,
erm, stardiviner's request to honor
`org-babel-uppercase-example-markers'.

Remaining issues:

1. The "org-include" tempo template doesn't work, for reasons I don't
   understand (I've never used tempo before). Nothing happens when I hit
   .
2. Now it seems like there should be completion when prompting for a
   string key. Feature creep! But likely worthwhile feature creep.
3. I've rather rashly renamed the relevant customization options
   `org-structure-block-alist' (for blocks added via
   `org-insert-structure-template') and `org-structure-keyword-alist'
   (for keywords insertable via the tempo system). Perhaps this was a
   bad idea. If it's not a bad idea, maybe
   `org-insert-structure-template' should be renamed `org-insert-block'
   or something like that.
3. Docs need to be updated.

Comments welcome!

Eric

diff --git a/lisp/org-tempo.el b/lisp/org-tempo.el
new file mode 100644
index 0..ebe5a26c2
--- /dev/null
+++ b/lisp/org-tempo.el
@@ -0,0 +1,118 @@
+;;; org-tempo.el --- Tempo-style templates for Org -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2017 Free Software Foundation, Inc.
+;;
+;; Author: Rasmus Pank Roulund 
+;; Keywords: outlines, hypermedia, calendar, wp
+;; Homepage: http://orgmode.org
+;;
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see .
+;;
+;;; Commentary:
+
+;; Block and structure templates, to replace the previous Org-specific
+;; system.  The old function `org-try-structure-completion' is
+;; provided as a thin wrapper around `tempo-complete-tag', for
+;; backwards compatibility.
+
+;;; Code:
+
+(require 'tempo)
+(require 'cl-lib)
+
+(defvar org-tempo-tags nil
+  "Tempo tags for org-mode")
+
+(defcustom org-structure-keyword-alist
+  '(("L" . "latex")
+("H" . "html")
+("A" . "ascii")
+("i" . "index"))
+  "Keyword templates expanded using the tempo package."
+  :group 'org-tempo
+  :type '(repeat
+	  (cons (string :tag "Key")
+		(string :tag "Template")))
+  :package-version '(Org . "9.2"))
+
+(defun org-tempo-setup ()
+  (tempo-use-tag-list 'org-tempo-tags)
+  (setq tempo-match-finder "^ *\\(<[[:word:]]\\)\\="))
+
+(add-hook 'org-mode-hook 'org-tempo-setup)
+
+(defun org-tempo-add-templates ()
+  "Update all org-tempo templates.
+Goes through `org-structure-block-alist' and
+`org-structure-keyword-alist'."
+  (let ((keys (mapcar (apply-partially #'format "<%s")
+		  (mapcar #'car (append org-structure-block-alist
+	org-structure-keyword-alist)
+(if (> (length keys)
+   (length (delete-dups keys)))
+	(user-error "Duplicate keys in `org-structure-template-alist' and `org-structure-template-alist-keywords'"))
+(dolist (key keys)
+  (if (assoc-string key org-tempo-tags)
+	  (setq org-tempo-tags
+		(delete (assoc-string key org-tempo-tags)
+			

Re: [O] function for inserting a block

2017-11-11 Thread numbch...@gmail.com
+1 I added a snippet for ` wrote:

> Carsten Dominik  writes:
>
> > I have always come down on the side of NOT breaking backward
> > compatibility unless we really HAVE TO in order to make progress. The
> > reason for this bias is because most Org users are not reading this
> > maling list and just want the system to function and to continue to
> > function the way they are used to, while it is hopefully improving. It
> > will stop them from upgrading if such breakage happens too often.
> >
> > So I would support reimplement the expansion (including
> > org-try-structure-completion for people who use that in custom code),
> > if possible of course on the back of one of the built-in expansion
> > systems in Emacs, before pushing this change out in a release. I would
> > certainly reimplement this in some way for myself, because using these
> > abbreviations is already hardcoded in my spine, I think.
>
> I agree. I support removing redundant code behind the scenes, but I also
> think we should preserve backwards compatibility in the user interface.
> A fair number of people around here have been using Org Mode for more
> than a decade, and, for better or for worse, everything about the user
> interface is now hardwired in their brains.
>
> In short, we have a time-saving expansion system that works well for
> lots of people. I support re-implementing it on top of another snippet
> engine but also leaving it in place until a suitable replacement is
> ready.
>
> Matt
>
>
>


Re: [O] function for inserting a block

2017-11-11 Thread Matt Lundin
Carsten Dominik  writes:

> I have always come down on the side of NOT breaking backward
> compatibility unless we really HAVE TO in order to make progress. The
> reason for this bias is because most Org users are not reading this
> maling list and just want the system to function and to continue to
> function the way they are used to, while it is hopefully improving. It
> will stop them from upgrading if such breakage happens too often.
>
> So I would support reimplement the expansion (including
> org-try-structure-completion for people who use that in custom code),
> if possible of course on the back of one of the built-in expansion
> systems in Emacs, before pushing this change out in a release. I would
> certainly reimplement this in some way for myself, because using these
> abbreviations is already hardcoded in my spine, I think.

I agree. I support removing redundant code behind the scenes, but I also
think we should preserve backwards compatibility in the user interface.
A fair number of people around here have been using Org Mode for more
than a decade, and, for better or for worse, everything about the user
interface is now hardwired in their brains. 

In short, we have a time-saving expansion system that works well for
lots of people. I support re-implementing it on top of another snippet
engine but also leaving it in place until a suitable replacement is
ready.

Matt




Re: [O] function for inserting a block

2017-11-11 Thread Thomas S. Dye

Eric Abrahamsen writes:

> Rasmus  writes:
>
>> Hi Eric,
>>
>> Eric Abrahamsen  writes:
>>
 Also, Eric, it seems that org-structure-template-alist only supports a
 single letter for short-hands (the car of an entry in
 org-structure-template-alist is a char).  I used to have blocks like ">>
>>> I hadn't thought of that. Really, all I ever wanted was to wrap things
>>> in blocks...
>>>
>>> I don't see any reason why org-structure-template-alist couldn't go back
>>> to using string keys. Then we could use read-string, and wouldn't have
>>> to have special  behavior -- a string that didn't exist in the
>>> alist could just be used literally to make a block.
>>
>> I’d prefer that.  For some special blocks, a few characters might makes it
>> more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.
>
> Here's the simplest solution.
>
> There still remains the fact that `org-structure-template-alist' has
> changed format, and `org-try-structure-completion' no longer exists.
> That may still annoy some people who were using the internals of the
> process, but...

Would something like this work?

(defun org-try-structure-completion ()
  (tempo-complete-tag))

All the best,
Tom

--
Thomas S. Dye
http://www.tsdye.com



Re: [O] function for inserting a block

2017-11-10 Thread stardiviner
I agree. I hope this feature too.

+1


On 11/10/2017 05:31 PM, Rasmus wrote:
> Hi Eric,
>
> Eric Abrahamsen  writes:
>
>>> Also, Eric, it seems that org-structure-template-alist only supports a
>>> single letter for short-hands (the car of an entry in
>>> org-structure-template-alist is a char).  I used to have blocks like ">> expanding to an "abstract" special-block, which I guess isn’t possible
>>> anymore?
>> I hadn't thought of that. Really, all I ever wanted was to wrap things
>> in blocks...
>>
>> I don't see any reason why org-structure-template-alist couldn't go back
>> to using string keys. Then we could use read-string, and wouldn't have
>> to have special  behavior -- a string that didn't exist in the
>> alist could just be used literally to make a block.
> I’d prefer that.  For some special blocks, a few characters might makes it
> more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.
>
> Thanks,
> Rasmus
>




Re: [O] function for inserting a block

2017-11-10 Thread Eric Abrahamsen
Rasmus  writes:

> Hi Eric,
>
> Eric Abrahamsen  writes:
>
>>> Also, Eric, it seems that org-structure-template-alist only supports a
>>> single letter for short-hands (the car of an entry in
>>> org-structure-template-alist is a char).  I used to have blocks like ">> expanding to an "abstract" special-block, which I guess isn’t possible
>>> anymore?
>>
>> I hadn't thought of that. Really, all I ever wanted was to wrap things
>> in blocks...
>>
>> I don't see any reason why org-structure-template-alist couldn't go back
>> to using string keys. Then we could use read-string, and wouldn't have
>> to have special  behavior -- a string that didn't exist in the
>> alist could just be used literally to make a block.
>
> I’d prefer that.  For some special blocks, a few characters might makes it
> more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.

Here's the simplest solution. 

There still remains the fact that `org-structure-template-alist' has
changed format, and `org-try-structure-completion' no longer exists.
That may still annoy some people who were using the internals of the
process, but...

diff --git a/lisp/org.el b/lisp/org.el
index f873f1021..7c451d525 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -11857,42 +11857,40 @@ keywords relative to each registered export back-end."
 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
 
 (defcustom org-structure-template-alist
-  '((?a . "export ascii")
-(?c . "center")
-(?C . "comment")
-(?e . "example")
-(?E . "export")
-(?h . "export html")
-(?l . "export latex")
-(?q . "quote")
-(?s . "src")
-(?v . "verse"))
+  '(("a" . "export ascii")
+("c" . "center")
+("C" . "comment")
+("e" . "example")
+("E" . "export")
+("h" . "export html")
+("l" . "export latex")
+("q" . "quote")
+("s" . "src")
+("v" . "verse"))
   "Structure completion elements.
 This is an alist of characters and values.  When
-`org-insert-structure-template' is called, an additional key is
-read.  The key is first looked up in this alist, and the
-corresponding structure is inserted, with \"#+BEGIN_\" and
-\"#+END_\" added automatically."
+`org-insert-structure-template' is called, a string key is read.
+The key is first looked up in this alist, and the corresponding
+structure is inserted, with \"#+BEGIN_\" and \"#+END_\" added
+automatically."
   :group 'org-completion
   :type '(repeat
-	  (cons (character :tag "Key")
+	  (cons (string :tag "Key")
 		(string :tag "Template")))
   :package-version '(Org . "9.2"))
 
 (defun org-insert-structure-template (type)
   "Insert a block structure of the type #+begin_foo/#+end_foo.
-First read a character, which can be one of the keys in
-`org-structure-template-alist'.  When it is , prompt the
-user for a string to use.  With an active region, wrap the region
-in the block.  Otherwise, insert an empty block."
+First read a string, which is used as a lookup key in
+`org-structure-template-alist' or, failing that, used literally.
+With an active region, wrap the region in the block.  Otherwise,
+insert an empty block."
   (interactive
(list
-(let* ((key (read-key "Key: "))
+(let* ((key (read-string "Key: "))
 	   (struct-string
-	(or (cdr (assq key org-structure-template-alist))
-		(and (= key ?\t)
-		 (read-string "Structure type: "))
-		(user-error "`%c' has no structure definition" key
+	(or (cdr (assoc-string key org-structure-template-alist))
+		key)))
   struct-string)))
   (let* ((region? (use-region-p))
 	 (s (if region? (region-beginning) (point)))


Re: [O] function for inserting a block

2017-11-10 Thread Rasmus
Hi Eric,

Eric Abrahamsen  writes:

>> Also, Eric, it seems that org-structure-template-alist only supports a
>> single letter for short-hands (the car of an entry in
>> org-structure-template-alist is a char).  I used to have blocks like "> expanding to an "abstract" special-block, which I guess isn’t possible
>> anymore?
>
> I hadn't thought of that. Really, all I ever wanted was to wrap things
> in blocks...
>
> I don't see any reason why org-structure-template-alist couldn't go back
> to using string keys. Then we could use read-string, and wouldn't have
> to have special  behavior -- a string that didn't exist in the
> alist could just be used literally to make a block.

I’d prefer that.  For some special blocks, a few characters might makes it
more intuitive, e.g. "def" → "definition", "hyp" → "hypothesis" etc.

Thanks,
Rasmus

-- 
Warning: Everything saved will be lost




Re: [O] function for inserting a block

2017-11-09 Thread James Harkins
One more vote in favor of not removing my own use, I added 

Anyway, just an opinion. "<" macros save me tons of time with ox-latex 
markup. I like them.


hjh

Sent with AquaMail for Android
http://www.aqua-mail.com






Re: [O] function for inserting a block

2017-11-09 Thread Eric Abrahamsen
Rasmus  writes:

> Rasmus  writes:
>
>> Nicolas Goaziou  writes:
>>
>>> Takaaki Ishikawa  writes:
>>>
 I also support the idea of keeping ">>
>>> I explained why I thought it could be removed. I also suggested
>>> solutions to get an equivalent feature without implementing it in Org.
>>
>> Which sounds fair.
>>
>>> What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
>>> bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
>>> NIH. Or, to put it differently: why in the world would Org implement its
>>> own template system?
>>
>> tempo.el, which I was unaware of, will be able to do this.  Thanks for the
>> pointer.
>>
>> I have started to write a replacement.  It seems to work fairly OK so far.
>> Not all keywords have been added, and no mechanism for adding additional
>> keywords is there yet.
>>
>> Nicolas, what would be the best way to hook ‘tempo-complete-tag’ into
>> "space"?  Should I add support directly in org-self-insert-command or add
>> it to post-command-hook?
>
> Actually, it should just hook into TAB (doh).
>
> The attached version seems to replicate the old behavior more or less.  I
> had to add a new custom variable to store keywords.
>
> Also, Eric, it seems that org-structure-template-alist only supports a
> single letter for short-hands (the car of an entry in
> org-structure-template-alist is a char).  I used to have blocks like " expanding to an "abstract" special-block, which I guess isn’t possible
> anymore?

I hadn't thought of that. Really, all I ever wanted was to wrap things
in blocks...

I don't see any reason why org-structure-template-alist couldn't go back
to using string keys. Then we could use read-string, and wouldn't have
to have special  behavior -- a string that didn't exist in the
alist could just be used literally to make a block.

Eric




Re: [O] function for inserting a block

2017-11-09 Thread Rasmus
Rasmus  writes:

> Nicolas Goaziou  writes:
>
>> Takaaki Ishikawa  writes:
>>
>>> I also support the idea of keeping ">> Please give importance to the backward compatibility in this case.
>>
>> I explained why I thought it could be removed. I also suggested
>> solutions to get an equivalent feature without implementing it in Org.
>
> Which sounds fair.
>
>> What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
>> bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
>> NIH. Or, to put it differently: why in the world would Org implement its
>> own template system?
>
> tempo.el, which I was unaware of, will be able to do this.  Thanks for the
> pointer.
>
> I have started to write a replacement.  It seems to work fairly OK so far.
> Not all keywords have been added, and no mechanism for adding additional
> keywords is there yet.
>
> Nicolas, what would be the best way to hook ‘tempo-complete-tag’ into
> "space"?  Should I add support directly in org-self-insert-command or add
> it to post-command-hook?

Actually, it should just hook into TAB (doh).

The attached version seems to replicate the old behavior more or less.  I
had to add a new custom variable to store keywords.

Also, Eric, it seems that org-structure-template-alist only supports a
single letter for short-hands (the car of an entry in
org-structure-template-alist is a char).  I used to have blocks like "

Re: [O] function for inserting a block

2017-11-09 Thread Rasmus
Nicolas Goaziou  writes:

> Takaaki Ishikawa  writes:
>
>> I also support the idea of keeping "> Please give importance to the backward compatibility in this case.
>
> I explained why I thought it could be removed. I also suggested
> solutions to get an equivalent feature without implementing it in Org.

Which sounds fair.

> What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
> bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
> NIH. Or, to put it differently: why in the world would Org implement its
> own template system?

tempo.el, which I was unaware of, will be able to do this.  Thanks for the
pointer.

I have started to write a replacement.  It seems to work fairly OK so far.
Not all keywords have been added, and no mechanism for adding additional
keywords is there yet.

Nicolas, what would be the best way to hook ‘tempo-complete-tag’ into
"space"?  Should I add support directly in org-self-insert-command or add
it to post-command-hook?

> The onbly argument so far is "<" cannot be expanded since it not word
> constituent. Seriously. "<" has no meaning anyway. You can use "@",
> which is word constituent and just as meaningless. So, you can define,
> e.g., a skeleton, that will expand "@s" to "#+begin_src\n#+end_src".

A small aside: on my keyboard layout < is easy to type.  I don’t like
typing @.

> We can even document how to do it in the manual.

Of course.

Thanks,
Rasmus

-- 
I feel emotional landscapes they puzzle me


org-tempo.el
Description: application/emacs-lisp


Re: [O] function for inserting a block

2017-11-08 Thread Carsten Dominik
On Thu, Nov 9, 2017 at 5:31 AM, Thomas S. Dye  wrote:

> Aloha Nicolas,
>
> Nicolas Goaziou writes:
>
> > Hello,
> >
> > Takaaki Ishikawa  writes:
> >
> >> I also support the idea of keeping " >> Please give importance to the backward compatibility in this case.
> >
> > I explained why I thought it could be removed. I also suggested
> > solutions to get an equivalent feature without implementing it in Org.
> >
> > What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
> > bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
> > NIH. Or, to put it differently: why in the world would Org implement its
> > own template system?
>
> The "why in the world" question is likely one that can be answered by
> the author of the Org template system.
>

I guess that would be me.

The reason why I implemented it was the same reason why I implemented
org-mode.  It scratched an itch that I was having, and so I implemented it
to solve this issue, without much regard for existing expansion systems -
back then, I am not sure which of them did even exist and how easy it would
have been to deal with dependencies.

The reason why the leading character is "<" lies in the fact that, at the
time, I was experimenting with HTML tags, mainly because this was how emacs
MUSE was handling markup.  I left it at that because it led to very few
conflicts and because "<" is a character that is easily typed.

I have always come down on the side of NOT breaking backward compatibility
unless we really HAVE TO in order to make progress.  The reason for this
bias is because most Org users are not reading this maling list and just
want the system to function and to continue to function the way they are
used to, while it is hopefully improving.  It will stop them from upgrading
if such breakage happens too often.

So I would support reimplement the expansion (including
org-try-structure-completion
for people who use that in custom code), if possible of course on the back
of one of the built-in expansion systems in Emacs, before pushing this
change out in a release.  I would certainly reimplement this in some way
for myself, because using these abbreviations is already hardcoded in my
spine, I think.

That does not take away from that fact that I am really happy we now can
wrap existing text into a block structure.  Very useful indeed.

Carsten


>
> > The only argument so far is "<" cannot be expanded since it not word
> > constituent. Seriously. "<" has no meaning anyway. You can use "@",
> > which is word constituent and just as meaningless. So, you can define,
> > e.g., a skeleton, that will expand "@s" to "#+begin_src\n#+end_src".
> >
> > We can even document how to do it in the manual.
>
> For me, the issue isn't about how the template system is implemented, it
> is about backwards compatibility of (org-try-structure-completion).
>
> All the best,
> Tom
>
> --
> Thomas S. Dye
> http://www.tsdye.com
>
>


Re: [O] function for inserting a block

2017-11-08 Thread Thomas S. Dye
Aloha Nicolas,

Nicolas Goaziou writes:

> Hello,
>
> Takaaki Ishikawa  writes:
>
>> I also support the idea of keeping "> Please give importance to the backward compatibility in this case.
>
> I explained why I thought it could be removed. I also suggested
> solutions to get an equivalent feature without implementing it in Org.
>
> What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
> bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
> NIH. Or, to put it differently: why in the world would Org implement its
> own template system?

The "why in the world" question is likely one that can be answered by
the author of the Org template system.

> The only argument so far is "<" cannot be expanded since it not word
> constituent. Seriously. "<" has no meaning anyway. You can use "@",
> which is word constituent and just as meaningless. So, you can define,
> e.g., a skeleton, that will expand "@s" to "#+begin_src\n#+end_src".
>
> We can even document how to do it in the manual.

For me, the issue isn't about how the template system is implemented, it
is about backwards compatibility of (org-try-structure-completion).

All the best,
Tom

--
Thomas S. Dye
http://www.tsdye.com



Re: [O] function for inserting a block

2017-11-08 Thread Nicolas Goaziou
Hello,

Takaaki Ishikawa  writes:

> I also support the idea of keeping " Please give importance to the backward compatibility in this case.

I explained why I thought it could be removed. I also suggested
solutions to get an equivalent feature without implementing it in Org.

What is wrong with Abbrev mode, skeletons, tempo.el, expand.el, all
bundled with Emacs, or YASnippet, in the Emacs ecosystem? It sounds like
NIH. Or, to put it differently: why in the world would Org implement its
own template system?

The only argument so far is "<" cannot be expanded since it not word
constituent. Seriously. "<" has no meaning anyway. You can use "@",
which is word constituent and just as meaningless. So, you can define,
e.g., a skeleton, that will expand "@s" to "#+begin_src\n#+end_src".

We can even document how to do it in the manual.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-11-08 Thread Eric Abrahamsen
Perhaps what we can do is re-instate this functionality using the
built-in Emacs mechanisms?




Re: [O] function for inserting a block

2017-11-08 Thread Takaaki Ishikawa
I also support the idea of keeping ":
>
> Berry, Charles writes:
>
>>> On Nov 8, 2017, at 6:07 AM, Rasmus  wrote:
>>>
>>> I think the template expansion a la ">> YASnippet installed for many years, and I never figured out how to use it
>>> properly.  To me, it is a bit too complex a replacement.
>>
>>
>> Me too.  ">
>> I suspect that there will be cries of pain and anger from others who have 
>> not tuned in to this thread if it is removed.
>>
>> Chuck
>
> Agreed.  It would be great if backward compatibility could be
> maintained.  I use (org-try-structure-completion) all the time.
>
> All the best,
> Tom
>
> --
> Thomas S. Dye
> http://www.tsdye.com
>



Re: [O] function for inserting a block

2017-11-08 Thread Thomas S. Dye

Berry, Charles writes:

>> On Nov 8, 2017, at 6:07 AM, Rasmus  wrote:
>>
>> I think the template expansion a la "> YASnippet installed for many years, and I never figured out how to use it
>> properly.  To me, it is a bit too complex a replacement.
>
>
> Me too.  "
> I suspect that there will be cries of pain and anger from others who have not 
> tuned in to this thread if it is removed.
>
> Chuck

Agreed.  It would be great if backward compatibility could be
maintained.  I use (org-try-structure-completion) all the time.

All the best,
Tom

--
Thomas S. Dye
http://www.tsdye.com



Re: [O] function for inserting a block

2017-11-08 Thread Nicolas Goaziou
Hello,

"Berry, Charles"  writes:

>> On Nov 8, 2017, at 6:07 AM, Rasmus  wrote:
>> 
>> I think the template expansion a la "> YASnippet installed for many years, and I never figured out how to use it
>> properly.  To me, it is a bit too complex a replacement.
>
>
> Me too.  "
> I suspect that there will be cries of pain and anger from others who
> have not tuned in to this thread if it is removed.

Emacs provides already a lot of function for that. I don't think "

Re: [O] function for inserting a block

2017-11-08 Thread Berry, Charles

> On Nov 8, 2017, at 6:07 AM, Rasmus  wrote:
> 
> I think the template expansion a la " YASnippet installed for many years, and I never figured out how to use it
> properly.  To me, it is a bit too complex a replacement.


Me too.  "

Re: [O] function for inserting a block

2017-11-08 Thread William Denton

On 8 November 2017, Bastien wrote:


I'm aware of Yasnippet, but it is not in Emacs core and it can
be daunting for newcomers to install, customize and use.

The previous function was very straightforward and simple, the
cost for losing it seems to high to me.  I'd rather have 10 lines
in Org (and in Emacs core) for it, than to ask users to install
something that does a lot more that they don't need.


I just realized that feature.  I hope it comes back so that a little bit of the helpful Org magic is 
built right in.


Bill
--
William Denton :: Toronto, Canada   ---   Listening to Art: 
https://listeningtoart.org/
https://www.miskatonic.org/ ---   GHG.EARTH: http://ghg.earth/
Caveat lector.  ---   STAPLR: http://staplr.org/



Re: [O] function for inserting a block

2017-11-08 Thread Eric Abrahamsen
Bastien Guerry  writes:

> Hi Nicolas,
>
> Nicolas Goaziou  writes:
>
>> Bastien  writes:
>>
>>> is there a simple mechanism in Emacs core to restore the same
>>> functionnality?
>>
>> Of course, it is called Abbrev mode: (info "(emacs)Abbrev
>> Concepts").
>
> Abbrev only expands on words constituents, so "
>> You may also use (info "(autotype)Using Skeletons") for more advanced
>> uses.
>
> I see.  So combining the two, we would have something like this:
>
> (define-skeleton org-skeleton-src-block
>  "" nil
>  >
>  "#+begin_src" \n
>  _ \n
>  "#+end_src")
>
> (define-abbrev org-mode-abbrev-table "   "" 'org-skeleton-src-block)
>
> ... modulo the restriction on "
> I need to think about this and Kaushal proposal to see if we can
> come up with a simple and backward-compatible solution.
>
> Already thanks for the pointers.

I'd be happy to add something to the manual, once we've got a clear way
of replicating the old system.




Re: [O] function for inserting a block

2017-11-08 Thread Bastien Guerry
Hi Nicolas,

Nicolas Goaziou  writes:

> Bastien  writes:
>
>> is there a simple mechanism in Emacs core to restore the same
>> functionnality?
>
> Of course, it is called Abbrev mode: (info "(emacs)Abbrev
> Concepts").

Abbrev only expands on words constituents, so " You may also use (info "(autotype)Using Skeletons") for more advanced
> uses.

I see.  So combining the two, we would have something like this:

(define-skeleton org-skeleton-src-block
 "" nil
 >
 "#+begin_src" \n
 _ \n
 "#+end_src")

(define-abbrev org-mode-abbrev-table "

Re: [O] function for inserting a block

2017-11-08 Thread Rasmus
Hi,

Thanks you, Eric, for providing this new functionality.

Nicolas Goaziou  writes:

> Bastien  writes:
>
>> Maybe I missed something in this thread, but what about making
>> the change backward-compatible by allowing "> be expanded?  For me, I'd rather type 
> I suggested the idea to remove " many other template systems, e.g., YASnippet, already provides this.
> I didn't want to re-implement the wheel and let, instead, various parts
> of Emacs cooperate.

I am only reading the thread now; my apology for coming late to the "party".

I think the template expansion a la "

Re: [O] function for inserting a block

2017-11-08 Thread Nicolas Goaziou
Bastien  writes:

> is there a simple mechanism in Emacs core to restore the same
> functionnality?

Of course, it is called Abbrev mode: (info "(emacs)Abbrev Concepts").

You may also use (info "(autotype)Using Skeletons") for more advanced
uses.

Regards,



Re: [O] function for inserting a block

2017-11-08 Thread Bastien
Hi Nicolas,

> I suggested the idea to remove " many other template systems, e.g., YASnippet, already provides this.
> I didn't want to re-implement the wheel and let, instead, various parts
> of Emacs cooperate.

is there a simple mechanism in Emacs core to restore the same
functionnality?

I'm aware of Yasnippet, but it is not in Emacs core and it can
be daunting for newcomers to install, customize and use.

The previous function was very straightforward and simple, the
cost for losing it seems to high to me.  I'd rather have 10 lines
in Org (and in Emacs core) for it, than to ask users to install
something that does a lot more that they don't need.

Let me know what you think.

-- 
 Bastien



Re: [O] function for inserting a block

2017-11-08 Thread Nicolas Goaziou
Hello,

Bastien  writes:

> Maybe I missed something in this thread, but what about making
> the change backward-compatible by allowing " be expanded?  For me, I'd rather type 

Re: [O] function for inserting a block

2017-11-08 Thread Bastien
Hi Eric,

thanks *a lot* for implementing this change!

Maybe I missed something in this thread, but what about making
the change backward-compatible by allowing "

Re: [O] function for inserting a block

2017-11-06 Thread Kaushal Modi
On Mon, Nov 6, 2017 at 8:48 AM Nicolas Goaziou 
wrote:

> I documented the two changes above in ORG-NEWS.
>

Thanks!
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-11-06 Thread Nicolas Goaziou
Hello,

Kaushal Modi  writes:

> On Sun, Nov 5, 2017 at 9:24 AM Kaushal Modi  wrote:
>
>>  This is a breaking change though (org-try-structure-completion doesn't
>> exist any more.. it breaks at least my config.. could be breaking more).
>>
>
> It could actually be a wider breakage as it changes the structure of
> org-structure-template-alist

I documented the two changes above in ORG-NEWS.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-11-05 Thread Eric Abrahamsen

On 11/05/17 10:06 AM, Nicolas Goaziou wrote:
> Hello,
>
> Eric Abrahamsen  writes:
>
>> From 055af9e9545947b9aeccc3370c8b67a237eea5d8 Mon Sep 17 00:00:00 2001
>> From: Eric Abrahamsen 
>> Date: Mon, 30 Oct 2017 10:55:29 -0700
>> Subject: [PATCH] Replace easy templates with org-insert-structure-template
>>
>> * lisp/org.el (org-insert-structure-template): New function for
>>   wrapping region (or element at point) in a begin/end block.
>>   (org-structure-predefined-blocks): New option holding predefined
>>   blocks, for completion.
>>   (org-try-structure-completion,
>>   org-complete-expand-structure-template): Remove functions.
>> * doc/org.texi (Inserting structure templates): Document.
>> * testing/lisp/test-org.el (test-org/insert-template): New test.
>
> Thank you. I applied your patch with the changes below.

Great!

>> -* Info directory file:: Installing a manual in Info file hierarchy.
>> +* Info directory file:: Installing a manual in Info file hierarchy.
>
> Ignored.
>
>> -* Easy templates::  Quick insertion of structural elements
>> +* Inserting structure templates::  Wrapping text in code blocks
>
> I used "Structure templates" instead of "Inserting structure templates"
> and updated the rest of the "org.texi" accordingly.
>
>> -@node Introduction
>> +@node Introduction, Document structure, Top, Top
>
> There is a lot of noise like this in your patch. I removed it.

I don't know why that happened, I ran the update-menus and update-nodes
menu commands, I figured that was necessary after the section renaming.
I have noticed a bit of churn when producing other info manuals, though.

Thanks a lot for the rest of your edits!

Eric




Re: [O] function for inserting a block

2017-11-05 Thread Kaushal Modi
On Sun, Nov 5, 2017 at 9:24 AM Kaushal Modi  wrote:

>  This is a breaking change though (org-try-structure-completion doesn't
> exist any more.. it breaks at least my config.. could be breaking more).
>

It could actually be a wider breakage as it changes the structure of
org-structure-template-alist
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-11-05 Thread Kaushal Modi
On Sun, Nov 5, 2017 at 4:07 AM Nicolas Goaziou 
wrote:

> Hello,
>
> Eric Abrahamsen  writes:
>
> > From 055af9e9545947b9aeccc3370c8b67a237eea5d8 Mon Sep 17 00:00:00 2001
> > From: Eric Abrahamsen 
> > Date: Mon, 30 Oct 2017 10:55:29 -0700
> > Subject: [PATCH] Replace easy templates with
> org-insert-structure-template
> >
> > * lisp/org.el (org-insert-structure-template): New function for
> >   wrapping region (or element at point) in a begin/end block.
> >   (org-structure-predefined-blocks): New option holding predefined
> >   blocks, for completion.
> >   (org-try-structure-completion,
> >   org-complete-expand-structure-template): Remove functions.
> > * doc/org.texi (Inserting structure templates): Document.
> > * testing/lisp/test-org.el (test-org/insert-template): New test.
>
> Thank you. I applied your patch with the changes below.
>

I'll try this out soon. This is a breaking change though
(org-try-structure-completion doesn't exist any more.. it breaks at least
my config.. could be breaking more). Can you please also mention this
breakage in the ORG-NEWS?
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-11-05 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> From 055af9e9545947b9aeccc3370c8b67a237eea5d8 Mon Sep 17 00:00:00 2001
> From: Eric Abrahamsen 
> Date: Mon, 30 Oct 2017 10:55:29 -0700
> Subject: [PATCH] Replace easy templates with org-insert-structure-template
>
> * lisp/org.el (org-insert-structure-template): New function for
>   wrapping region (or element at point) in a begin/end block.
>   (org-structure-predefined-blocks): New option holding predefined
>   blocks, for completion.
>   (org-try-structure-completion,
>   org-complete-expand-structure-template): Remove functions.
> * doc/org.texi (Inserting structure templates): Document.
> * testing/lisp/test-org.el (test-org/insert-template): New test.

Thank you. I applied your patch with the changes below.

> -* Info directory file:: Installing a manual in Info file hierarchy.
> +* Info directory file:: Installing a manual in Info file hierarchy.

Ignored.

> -* Easy templates::  Quick insertion of structural elements
> +* Inserting structure templates::  Wrapping text in code blocks

I used "Structure templates" instead of "Inserting structure templates"
and updated the rest of the "org.texi" accordingly.

> -@node Introduction
> +@node Introduction, Document structure, Top, Top

There is a lot of noise like this in your patch. I removed it.

> +With just a few keystrokes, it's possible to insert empty structural blocks,
> +such as @code{#+begin_src} and @code{#+end_src}, or to wrap existing text in
> +such a block.

Manual's conventions require to use uppercase keywords: #+BEGIN_SRC,
even though the function inserts them in lowercase.

> +@defun org-insert-structure-template
> +@kindex C-c C-x w

Manual uses

  @table @kbd
  @orgcmd(C-c C-x w, org-insert-structure-template)
  @end table

instead.

> +Prompt for a type of block structure, and insert the block at point.  If the
> +region is active, it will be wrapped in the block.  First prompts the user

"it is wrapped in the block"

GNU Manuals's convention is to favor present tense over future one.

> +@vindex org-structure-template-alist
> +Available structure types are defined in @code{org-structure-template-alist},
> +see the docstring for adding or changing values.
> +
> +@multitable @columnfractions 0.2 0.8
> +@item @kbd{s} @tab @code{src}
> +@item @kbd{e} @tab @code{example}
> +@item @kbd{E} @tab @code{export}
> +@item @kbd{q} @tab @code{quote}
> +@item @kbd{v} @tab @code{verse}
> +@item @kbd{c} @tab @code{center}
> +@item @kbd{C} @tab @code{comment}
> +@item @kbd{l} @tab @code{export latex}
> +@item @kbd{h} @tab @code{export html}
> +@item @kbd{a} @tab @code{export ascii}

@code{src} -> @samp{#+BEGIN_SRC}
...

I also re-ordered the entries.

> +  '((?s . "src")
> +(?e . "example")
> +(?E . "export")
> +(?q . "quote")
> +(?v . "verse")
> +(?c . "center")
> +(?C . "comment")
> +(?l . "export latex")
> +(?h . "export html")
> +(?a . "export ascii"))
>"Structure completion elements.

I re-ordered the entries.

> -This is a list of abbreviation keys and values.  The value gets inserted
> -if you type `<' followed by the key and then press the completion key,
> -usually `TAB'.  %file will be replaced by a file name after prompting
> -for the file using completion.  The cursor will be placed at the position
> -of the `?' in the template.
> -There are two templates for each key, the first uses the original Org syntax,
> -the second uses Emacs Muse-like syntax tags.  These Muse-like tags become
> -the default when the /org-mtags.el/ module has been loaded.  See also the
> -variable `org-mtags-prefer-muse-templates'."
> +This is an alist of characters and values.  When
> +`org-insert-structure-template' is called, an additional key is
> +read.  The key is first looked up in this alist, and the
> +corresponding structure is inserted, with \"#+begin\" and
> +\"#+end\" added automatically."

"#+BEGIN_" and "#+END_".

>:group 'org-completion
>:type '(repeat
> -   (list
> -(string :tag "Key")
> +   (cons
> +(character :tag "Key")
>  (string :tag "Template")))
> -  :version "26.1"
>:package-version '(Org . "8.3"))

"8.3" -> "9.2"

> +(defun org-insert-structure-template (type)
> +  "Insert a block structure of the type #+begin_foo/#+end_foo.
> +First read a character, which can be one of the keys in
> +`org-structure-template-alist'.  When it is , prompt the
> +user for a string to use.  With an active region, wrap the region
> +in the block.  Otherwise, insert an empty block."
> +  (interactive
> +   (list
> +(let* ((key (read-key "Key: "))
> +(struct-string
> + (or (cdr (assq key org-structure-template-alist))
> + (and (= key ?\t)
> +  (read-string "Structure type: "))
> + (error "'%c' has no structure definition" key

(user-error "`%c' ...")

> +  struct-string)))
> +  (let ((s (if 

Re: [O] function for inserting a block

2017-10-30 Thread Eric Abrahamsen
Sorry, last patch had some documentation errors.

>From 055af9e9545947b9aeccc3370c8b67a237eea5d8 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Mon, 30 Oct 2017 10:55:29 -0700
Subject: [PATCH] Replace easy templates with org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
  (org-structure-predefined-blocks): New option holding predefined
  blocks, for completion.
  (org-try-structure-completion,
  org-complete-expand-structure-template): Remove functions.
* doc/org.texi (Inserting structure templates): Document.
* testing/lisp/test-org.el (test-org/insert-template): New test.
---
 doc/org.texi | 756 +++
 etc/ORG-NEWS |   4 +
 lisp/org.el  | 132 -
 testing/lisp/test-org.el |  46 +++
 4 files changed, 491 insertions(+), 447 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 77da6d335..486fe88fe 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -652,7 +652,7 @@ Texinfo export
 * Texinfo specific export settings::  Setting the environment.
 * Texinfo file header:: Generating the header.
 * Texinfo title and copyright page::  Creating preamble pages.
-* Info directory file:: Installing a manual in Info file hierarchy.
+* Info directory file:: Installing a manual in Info file hierarchy.
 * Headings and sectioning structure::  Building document structure.
 * Indices:: Creating indices.
 * Quoting Texinfo code::Incorporating literal Texinfo code.
@@ -749,7 +749,7 @@ Specific header arguments
 Miscellaneous
 
 * Completion::  M-TAB guesses completions
-* Easy templates::  Quick insertion of structural elements
+* Inserting structure templates::  Wrapping text in code blocks
 * Speed keys::  Electric commands at the beginning of a headline
 * Code evaluation security::Org mode files evaluate inline code
 * Customization::   Adapting Org to changing tastes
@@ -796,7 +796,7 @@ MobileOrg
 @end detailmenu
 @end menu
 
-@node Introduction
+@node Introduction, Document structure, Top, Top
 @chapter Introduction
 @cindex introduction
 
@@ -808,7 +808,7 @@ MobileOrg
 * Conventions:: Typesetting conventions in the manual
 @end menu
 
-@node Summary
+@node Summary, Installation, Introduction, Introduction
 @section Summary
 @cindex summary
 
@@ -864,7 +864,7 @@ Network Theory Ltd.}
 
 @page
 
-@node Installation
+@node Installation, Activation, Summary, Introduction
 @section Installation
 @cindex installation
 
@@ -940,7 +940,7 @@ For more detailed explanations on Org's build system, please check the Org
 Build System page on @uref{http://orgmode.org/worg/dev/org-build-system.html,
 Worg}.
 
-@node Activation
+@node Activation, Feedback, Installation, Introduction
 @section Activation
 @cindex activation
 @cindex autoload
@@ -991,7 +991,7 @@ the default.  If you do not like @code{transient-mark-mode}, you can create
 an active region by using the mouse to select a region, or pressing
 @kbd{C-@key{SPC}} twice before moving the cursor.
 
-@node Feedback
+@node Feedback, Conventions, Activation, Introduction
 @section Feedback
 @cindex feedback
 @cindex bug reports
@@ -1091,7 +1091,7 @@ screen.  Save this buffer to a file (for example using @kbd{C-x C-w}) and
 attach it to your bug report.
 @end enumerate
 
-@node Conventions
+@node Conventions,  , Feedback, Introduction
 @section Typesetting conventions used in this manual
 
 @subsubheading TODO keywords, tags, properties, etc.
@@ -1141,7 +1141,7 @@ will be listed to call @code{org-table-move-column-right}.  If you prefer,
 you can compile the manual without the command names by unsetting the flag
 @code{cmdnames} in @file{org.texi}.
 
-@node Document structure
+@node Document structure, Tables, Introduction, Top
 @chapter Document structure
 @cindex document structure
 @cindex structure of document
@@ -1164,7 +1164,7 @@ edit the structure of the document.
 * Org syntax::  Formal description of Org's syntax
 @end menu
 
-@node Outlines
+@node Outlines, Headlines, Document structure, Document structure
 @section Outlines
 @cindex outlines
 @cindex Outline mode
@@ -1178,7 +1178,7 @@ currently being worked on.  Org greatly simplifies the use of
 outlines by compressing the entire show/hide functionality into a single
 command, @command{org-cycle}, which is bound to the @key{TAB} key.
 
-@node Headlines
+@node Headlines, Visibility cycling, Outlines, Document structure
 @section Headlines
 @cindex headlines
 @cindex outline tree
@@ -1220,7 +1220,7 @@ least two empty lines, one empty line will remain visible after folding
 the subtree, in order to structure the collapsed view.  See the
 variable @code{org-cycle-separator-lines} to modify this behavior.
 
-@node Visibility cycling
+@node Visibility cycling, Motion, 

Re: [O] function for inserting a block

2017-10-30 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> This will get there eventually! Because there's likely to be more
>> tweaking, I haven't touched the manual or the tests yet, just reworked
>> the option and function:
>
> Thank you. Some comments follow.
>
>> #+BEGIN_SRC elisp
>> (defcustom org-structure-template-alist
>>   '((?s . "SRC")
>> (?e . "EXAMPLE")
>> (?E . "EXPORT")
>> (?q . "QUOTE")
>> (?v . "VERSE")
>> (?V . "VERBATIM")
>
> This block type doesn't exist. You can remove it.
>
>> (?c . "CENTER")
>> (?C . "COMMENT")
>> (?l . "EXPORT latex")
>> (?L . "#+LaTeX")
>
> #+LATEX or #+latex (see below)
>
>> (?h . "EXPORT html")
>> (?H . "#+HTML")
>> (?a . "EXPORT ascii")
>> (?A . "#+ASCII")
>> (?i . "#+INDEX")
>> (?I . "#+INCLUDE"))
>
> As suggested by Rasmus once, maybe we could get away from FORTRAN touch
> and insert everything lowercase.

Sounds good to me.

>>   "Structure completion elements.
>> This is an alist of characters and values.  When
>> `org-insert-structure-template' is called, an additional key is
>> read.  The key is first looked up in this alist, and the
>> corresponding structure is inserted.  Hitting  will prompt
>> for a structure.
>
> I would remove "Hitting  prompts for a structure." which belongs to
> the function's docstring, not to variable's.
>
>> Structure strings prefixed with a \"#+\" are inserted with no
>> further processing.  Strings without this prefix are used to
>> create a block structure, with \"#+BEGIN\" and \"#+END\" added
>> automatically.
>
> I'm not sure about this part. I understand the backward-compatibility
> concern, but it sounds a bit alien to the purpose of the function, i.e.,
> there is no wrapping around, it is not an "environment" either. WDYT?

It was a hack; I was trying to make the smallest change possible. But
that does turn it into a bit of a Frankenstein, and I would prefer to
remove it. As you mentioned earlier, Emacs already has mechanism for
inserting snippets.

The "include" case is a little different, since it does provide some
extra convenience. I've still taken it out in the next version below, though.

>> WHAT TO DO ABOUT THIS PART?
>> There are two templates for each key, the first uses the original Org
>> syntax,
>
> You can remove it.
>
>> the second uses Emacs Muse-like syntax tags.  These Muse-like tags become
>> the default when the /org-mtags.el/ module has been loaded.  See also the
>> variable `org-mtags-prefer-muse-templates'."
>>   :group 'org-completion
>>   :type '(repeat
>>(cons
>> (character :tag "Key")
>> (string :tag "Template")))
>>   :version "26.1"
>>   :package-version '(Org . "8.3"))
>
> You need to update :version and :package-version. Technically,
> if :package-version is provided, we should only use it, IIUC.

I've never really understood the difference. Current version is 9.1.2,
will this function be present in 9.1.3, or 9.2.0? I've assumed 9.2.0 in
the next version of the patch.

>> (defun org-insert-structure-template ( type)
>>   "Insert a block structure of the type #+BEGIN_FOO/#+END_FOO.
>> This function first reads a character, which can be one of the
>> keys in `org-structure-template-alist'.  It can also be , in
>> which case the user is prompted for a string to use.
>
> "When it is , prompt the user for a string to use."
>
>>   (interactive)
>>   (let* ((key (read-key "Key: "))
>>   (struct-string
>>(or (cdr-safe (assq key org-structure-template-alist))
>
> `cdr-safe' -> `cdr'
>
>>(when (= key ?\t)
>>  (read-string "Structure type: "))
>
> Nitpick: (and (eq key ?\t) (read-string ...))

Out of curiosity is this a style thing, or semantics, or performance?

>>(error "'%c' has no structure definition" key
>> (if (string-prefix-p "#+" struct-string)
>>  (progn
>>(insert (format "%s: " struct-string))
>>(when (string= "#+INCLUDE" struct-string)
>>  (insert
>>   (format "\"%s\""
>>   (abbreviate-file-name
>>(read-file-name "Include file: "))
>

If we take out special handling of "#+" strings, this part is gone
anyway.

Here's a new version with all the above changes made, and the docs
tweaked accordingly.

>From 086733d275ebfb10726144e92ccd30be605c5516 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Mon, 30 Oct 2017 09:19:15 -0700
Subject: [PATCH] Replacing easy templates with function
 org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
  (org-structure-predefined-blocks): New option holding predefined
  blocks, for completion.
  (org-try-structure-completion,
  org-complete-expand-structure-template): Remove functions.
* doc/org.texi (Inserting structure templates): Document.
* 

Re: [O] function for inserting a block

2017-10-30 Thread Eric S Fraga
On Monday, 30 Oct 2017 at 12:05, Nicolas Goaziou wrote:

[...]

> As suggested by Rasmus once, maybe we could get away from FORTRAN touch
> and insert everything lowercase.

+1 (repeat many times)

-- 
: Eric S Fraga via Emacs 27.0.50, Org release_9.1.2-155-gf474c7


signature.asc
Description: PGP signature


Re: [O] function for inserting a block

2017-10-30 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> This will get there eventually! Because there's likely to be more
> tweaking, I haven't touched the manual or the tests yet, just reworked
> the option and function:

Thank you. Some comments follow.

> #+BEGIN_SRC elisp
> (defcustom org-structure-template-alist
>   '((?s . "SRC")
> (?e . "EXAMPLE")
> (?E . "EXPORT")
> (?q . "QUOTE")
> (?v . "VERSE")
> (?V . "VERBATIM")

This block type doesn't exist. You can remove it.

> (?c . "CENTER")
> (?C . "COMMENT")
> (?l . "EXPORT latex")
> (?L . "#+LaTeX")

#+LATEX or #+latex (see below)

> (?h . "EXPORT html")
> (?H . "#+HTML")
> (?a . "EXPORT ascii")
> (?A . "#+ASCII")
> (?i . "#+INDEX")
> (?I . "#+INCLUDE"))

As suggested by Rasmus once, maybe we could get away from FORTRAN touch
and insert everything lowercase.

>   "Structure completion elements.
> This is an alist of characters and values.  When
> `org-insert-structure-template' is called, an additional key is
> read.  The key is first looked up in this alist, and the
> corresponding structure is inserted.  Hitting  will prompt
> for a structure.

I would remove "Hitting  prompts for a structure." which belongs to
the function's docstring, not to variable's.

> Structure strings prefixed with a \"#+\" are inserted with no
> further processing.  Strings without this prefix are used to
> create a block structure, with \"#+BEGIN\" and \"#+END\" added
> automatically.

I'm not sure about this part. I understand the backward-compatibility
concern, but it sounds a bit alien to the purpose of the function, i.e.,
there is no wrapping around, it is not an "environment" either. WDYT?

> WHAT TO DO ABOUT THIS PART?
> There are two templates for each key, the first uses the original Org
> syntax,

You can remove it.

> the second uses Emacs Muse-like syntax tags.  These Muse-like tags become
> the default when the /org-mtags.el/ module has been loaded.  See also the
> variable `org-mtags-prefer-muse-templates'."
>   :group 'org-completion
>   :type '(repeat
> (cons
>  (character :tag "Key")
>  (string :tag "Template")))
>   :version "26.1"
>   :package-version '(Org . "8.3"))

You need to update :version and :package-version. Technically,
if :package-version is provided, we should only use it, IIUC.

> (defun org-insert-structure-template ( type)
>   "Insert a block structure of the type #+BEGIN_FOO/#+END_FOO.
> This function first reads a character, which can be one of the
> keys in `org-structure-template-alist'.  It can also be , in
> which case the user is prompted for a string to use.  

"When it is , prompt the user for a string to use."

>   (interactive)
>   (let* ((key (read-key "Key: "))
>(struct-string
> (or (cdr-safe (assq key org-structure-template-alist))

`cdr-safe' -> `cdr'

> (when (= key ?\t)
>   (read-string "Structure type: "))

Nitpick: (and (eq key ?\t) (read-string ...))

> (error "'%c' has no structure definition" key
> (if (string-prefix-p "#+" struct-string)
>   (progn
> (insert (format "%s: " struct-string))
> (when (string= "#+INCLUDE" struct-string)
>   (insert
>(format "\"%s\""
>(abbreviate-file-name
> (read-file-name "Include file: "))

See above.


Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-28 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> In that case, would you be more in favor of a keymap-plus-subkey system,
>> or a keymap-plus-prompt system?
>
> I have no strong opinion, but a keymap-plus-subkey system (subkeys
> matching current keys in `org-structure-template-alist') with an
> additional key (e.g. ) for "free" seems quite efficient.

This will get there eventually! Because there's likely to be more
tweaking, I haven't touched the manual or the tests yet, just reworked
the option and function:

#+BEGIN_SRC elisp
(defcustom org-structure-template-alist
  '((?s . "SRC")
(?e . "EXAMPLE")
(?E . "EXPORT")
(?q . "QUOTE")
(?v . "VERSE")
(?V . "VERBATIM")
(?c . "CENTER")
(?C . "COMMENT")
(?l . "EXPORT latex")
(?L . "#+LaTeX")
(?h . "EXPORT html")
(?H . "#+HTML")
(?a . "EXPORT ascii")
(?A . "#+ASCII")
(?i . "#+INDEX")
(?I . "#+INCLUDE"))
  "Structure completion elements.
This is an alist of characters and values.  When
`org-insert-structure-template' is called, an additional key is
read.  The key is first looked up in this alist, and the
corresponding structure is inserted.  Hitting  will prompt
for a structure.

Structure strings prefixed with a \"#+\" are inserted with no
further processing.  Strings without this prefix are used to
create a block structure, with \"#+BEGIN\" and \"#+END\" added
automatically.

WHAT TO DO ABOUT THIS PART?
There are two templates for each key, the first uses the original Org syntax,
the second uses Emacs Muse-like syntax tags.  These Muse-like tags become
the default when the /org-mtags.el/ module has been loaded.  See also the
variable `org-mtags-prefer-muse-templates'."
  :group 'org-completion
  :type '(repeat
  (cons
   (character :tag "Key")
   (string :tag "Template")))
  :version "26.1"
  :package-version '(Org . "8.3"))

(defun org-insert-structure-template ( type)
  "Insert a block structure of the type #+BEGIN_FOO/#+END_FOO.
This function first reads a character, which can be one of the
keys in `org-structure-template-alist'.  It can also be , in
which case the user is prompted for a string to use.  With an
active region, wrap the region in the block.  Otherwise, insert
an empty block."
  (interactive)
  (let* ((key (read-key "Key: "))
 (struct-string
  (or (cdr-safe (assq key org-structure-template-alist))
  (when (= key ?\t)
(read-string "Structure type: "))
  (error "'%c' has no structure definition" key
(if (string-prefix-p "#+" struct-string)
(progn
  (insert (format "%s: " struct-string))
  (when (string= "#+INCLUDE" struct-string)
(insert
 (format "\"%s\""
 (abbreviate-file-name
  (read-file-name "Include file: "))
  (let ((s (if (use-region-p)
   (region-beginning)
 (point)))
(e (copy-marker (if (use-region-p)
(region-end)
  (point))
t))
column)
(when (string-match-p
   (concat "\\`"
   (regexp-opt '("example" "export" "src")))
   struct-string)
  (org-escape-code-in-region s e))
(goto-char s)
(setq column (current-indentation))
(beginning-of-line)
(indent-to column)
(insert (format "#+BEGIN_%s\n" struct-string))
(goto-char e)
(if (bolp)
(progn
  (skip-chars-backward " \n\t")
  (forward-line))
  (end-of-line)
  (insert "\n"))
(indent-to column)
(insert (format "#+END_%s\n"
(car (split-string struct-string
(when (or (string-match-p "SRC\\|\\`EXPORT\\'" struct-string)
  (null (use-region-p)))
  (goto-char s)
  (end-of-line))
(set-marker e nil)
#+END_SRC




Re: [O] function for inserting a block

2017-10-24 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> In that case, would you be more in favor of a keymap-plus-subkey system,
> or a keymap-plus-prompt system?

I have no strong opinion, but a keymap-plus-subkey system (subkeys
matching current keys in `org-structure-template-alist') with an
additional key (e.g. ) for "free" seems quite efficient.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-23 Thread Eric Abrahamsen
Eric Abrahamsen  writes:

> Nicolas Goaziou  writes:
>
>> Hello,
>>
>> Eric Abrahamsen  writes:
>>
>>> Kaushal Modi  writes:
>>>
 On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:
>>>
>>> [...]
>>>
 @Eric: I believe you were sort of onboard with this suggestion, just gated 
 by a doubt if this actually can make into master. 
>>>
>>> In principle yes, I agree it's odd to have two completely different
>>> systems for essentially doing the same thing.
>>
>> I agree, but I don't like much the easy template system. We're basically
>> re-inventing the wheel. Better solutions already exist. Some of them
>> even ship with Emacs.
>>
>> I think the function could replace the template system altogether. It's
>> easy enough to mimic current system with, e.g., Yasnippet.
>
> In that case, would you be more in favor of a keymap-plus-subkey system,
> or a keymap-plus-prompt system?
   ^^
I mean, keybinding-plus-prompt...




Re: [O] function for inserting a block

2017-10-23 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> Kaushal Modi  writes:
>>
>>> On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:
>>
>> [...]
>>
>>> @Eric: I believe you were sort of onboard with this suggestion, just gated 
>>> by a doubt if this actually can make into master. 
>>
>> In principle yes, I agree it's odd to have two completely different
>> systems for essentially doing the same thing.
>
> I agree, but I don't like much the easy template system. We're basically
> re-inventing the wheel. Better solutions already exist. Some of them
> even ship with Emacs.
>
> I think the function could replace the template system altogether. It's
> easy enough to mimic current system with, e.g., Yasnippet.

In that case, would you be more in favor of a keymap-plus-subkey system,
or a keymap-plus-prompt system?





Re: [O] function for inserting a block

2017-10-23 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> Kaushal Modi  writes:
>
>> On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:
>
> [...]
>
>> @Eric: I believe you were sort of onboard with this suggestion, just gated 
>> by a doubt if this actually can make into master. 
>
> In principle yes, I agree it's odd to have two completely different
> systems for essentially doing the same thing.

I agree, but I don't like much the easy template system. We're basically
re-inventing the wheel. Better solutions already exist. Some of them
even ship with Emacs.

I think the function could replace the template system altogether. It's
easy enough to mimic current system with, e.g., Yasnippet.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-23 Thread Eric Abrahamsen
Kaushal Modi  writes:

> On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:

[...]

> @Eric: I believe you were sort of onboard with this suggestion, just gated by 
> a doubt if this actually can make into master. 

In principle yes, I agree it's odd to have two completely different
systems for essentially doing the same thing.

> So, checking with Nicolas, and the thread if the proposed behavior change 
> with easy templates (only when region is selected) would be accepted. 




Re: [O] function for inserting a block

2017-10-23 Thread Kaushal Modi
On Mon, Oct 23, 2017 at 10:00 AM Carsten Dominik  wrote:

> I am not sure I understand, at least with transient-region turned on.
> Typing 

You're correct.


> Or do you mean to actually put "<" into a keymap?
>

I actually do that in my personal config .. Maybe binding "<" in org keymap
is the simplest way.

I have this function in my config bound to "<":

(defun modi/org-template-maybe ()
  "Insert org-template if point is at the beginning of the line,
or if a region is selected.  Else call `self-insert-command'."
  (interactive)
  (let ((is-region? (use-region-p)))
(if (or is-region?
(and (not is-region?)
 (looking-back "^[[:blank:]]*")))
(hydra-org-template/body) ;Wrapper function for
`org-try-structure-completion'
  (self-insert-command 1
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-10-23 Thread Carsten Dominik
On Mon, Oct 23, 2017 at 12:52 PM, Kaushal Modi 
wrote:

> On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:
>
>> On Fri, Oct 20, 2017 at 5:15 PM Eric Abrahamsen 
>> wrote:
>>
>>> The template really only inserts the block type, not anything specific
>>> like the source language or export backend.
>>
>>
>> Right, but based on the code you have, you can easily add "SRC org", "SRC
>> emacs-lisp" to the default value of that list. Looking at that, users can
>> also get an idea that they can add their favorite languages to that list
>> too.
>>
>>
>>> I think prompting for
>>> "second-level" information like that might be a little overkill.
>>>
>>
>> I second that. The first-level prompt also feels a bit slow, honestly,
>> compared to using easy templates like "> (just an example) to insert the, well, EXAMPLE block as usual. But *if
>> region is selected*, it does the wrapping as in your code.
>>
>> This is what I mean (GIF animation):
>>
>> https://i.imgur.com/201TISW.gifv
>>
>> I use hydra to provide those awesome hints.. but the same can be done
>> without hydras too.
>>
>
> Hello all,
>
> Above, I suggested merging the template insertion function with the
> current easy template code.
>
> With that, the easy template behavior will remain unchanged if no region
> is selected. But if a region is selected, the same template (example: " Will wrap that region with that template.
>

I Kaustal,

I am not sure I understand, at least with transient-region turned on.
Typing 
> I also have a GIF showing that behavior in action (linked in my previous
> reply).
>
> Benefits:
> - Quicker as there are no prompts.. the user simply types the template
> string as configured by them.
> - Also one doesn't need to do a context switch to use a different binding
> based on if region is selected or not.
>
> @Eric: I believe you were sort of onboard with this suggestion, just gated
> by a doubt if this actually can make into master.
>
> So, checking with Nicolas, and the thread if the proposed behavior change
> with easy templates (only when region is selected) would be accepted.
>
>> --
>
> Kaushal Modi
>


Re: [O] function for inserting a block

2017-10-23 Thread Kaushal Modi
On Fri, Oct 20, 2017, 5:43 PM Kaushal Modi  wrote:

> On Fri, Oct 20, 2017 at 5:15 PM Eric Abrahamsen 
> wrote:
>
>> The template really only inserts the block type, not anything specific
>> like the source language or export backend.
>
>
> Right, but based on the code you have, you can easily add "SRC org", "SRC
> emacs-lisp" to the default value of that list. Looking at that, users can
> also get an idea that they can add their favorite languages to that list
> too.
>
>
>> I think prompting for
>> "second-level" information like that might be a little overkill.
>>
>
> I second that. The first-level prompt also feels a bit slow, honestly,
> compared to using easy templates like " (just an example) to insert the, well, EXAMPLE block as usual. But *if
> region is selected*, it does the wrapping as in your code.
>
> This is what I mean (GIF animation):
>
> https://i.imgur.com/201TISW.gifv
>
> I use hydra to provide those awesome hints.. but the same can be done
> without hydras too.
>

Hello all,

Above, I suggested merging the template insertion function with the current
easy template code.

With that, the easy template behavior will remain unchanged if no region is
selected. But if a region is selected, the same template (example: " --

Kaushal Modi


Re: [O] function for inserting a block

2017-10-22 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> Kaushal Modi  writes:
>
>>> I just tried it out, and it works great!
>>>
>>> I have a comment about
>>>
>>> (when (string-equal (downcase type) "example")
>>>   (org-escape-code-in-region s e)) 
>>>
>>> I have never needed to escape org in example, blocks, but I *have* needed 
>>> to do that in org src blocks. 
>>>
>>> Should type string be also matched with "src org"?
>>>
>>> Actually should the type string be matched only with "src org"? Because I 
>>> see the Org example blocks as   blocks in HTML with no syntax 
>>> highlighting.. so
>>> those can contain code from any language.
>>>
>>> Also as this is part of org and emacs, org-structure-predefined-blocks 
>>> deserves "SRC org" and "SRC emacs-lisp" too? :)
>>
>> The template really only inserts the block type, not anything specific
>> like the source language or export backend. I think prompting for
>> "second-level" information like that might be a little overkill.
>>
>> As for what should be escaped and what shouldn't, I defer to Nicolas,
>> let's see what he says.
>
> "src" (not only with "org" language), "example" and "export", i.e.,
> verbatim, blocks need to be escaped.
>
> You should probably use something like
>
>   (when (string-prefix-p (regexp-opt '("example" "export" "src")) type t)
>...)

string-prefix-p doesn't appear to work with regular expressions, so I
used string-match-p.

>From 1ef3404310f516d1f762501b2bb974220a61da1d Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Sat, 7 Oct 2017 13:01:14 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
  (org-structure-predefined-blocks): New option holding predefined
  blocks, for completion.
* doc/org.texi (Structure of code blocks, Easy templates): And in
  manual.
* testing/lisp/test-org.el (test-org/insert-template): New test.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  4 
 lisp/org.el  | 49 
 testing/lisp/test-org.el | 42 +
 4 files changed, 110 insertions(+), 6 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index c54f2615a..6ad9d1c15 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15242,12 +15242,13 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of src code blocks: a template
+system that can create a new block with just three keystrokes, and a command
+for wrapping existing text in a block (@pxref{Easy templates}).  Org also
+works with other completion systems in Emacs, some of which predate Org and
+have custom domain-specific languages for defining templates.  Regular use of
+templates reduces errors, increases accuracy, and maintains consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17418,6 +17419,14 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+@kindex C-c C-x w
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is bound to @kbd{C-c C-x w} by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1076dd970..190a6b8bc 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -109,6 +109,10 @@ you should expect to see something like:
 #+END_EXAMPLE
 
 ** New functions
+*** ~org-insert-structure-template~
+
+This function can be used to wrap existing text of Org elements in
+a #+BEGIN_FOO/#+END_FOO block.  Bound to C-c C-x w by default.
 
 *** ~org-export-excluded-from-toc-p~
 
diff --git a/lisp/org.el b/lisp/org.el
index 54687abc7..552dd7ec4 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12117,6 +12117,13 @@ keywords relative to each registered export back-end."
 "PRIORITIES:" "SELECT_TAGS:" 

Re: [O] function for inserting a block

2017-10-22 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> Kaushal Modi  writes:

>> I just tried it out, and it works great!
>>
>> I have a comment about
>>
>> (when (string-equal (downcase type) "example")
>>   (org-escape-code-in-region s e)) 
>>
>> I have never needed to escape org in example, blocks, but I *have* needed to 
>> do that in org src blocks. 
>>
>> Should type string be also matched with "src org"?
>>
>> Actually should the type string be matched only with "src org"? Because I 
>> see the Org example blocks as   blocks in HTML with no syntax 
>> highlighting.. so
>> those can contain code from any language.
>>
>> Also as this is part of org and emacs, org-structure-predefined-blocks 
>> deserves "SRC org" and "SRC emacs-lisp" too? :)
>
> The template really only inserts the block type, not anything specific
> like the source language or export backend. I think prompting for
> "second-level" information like that might be a little overkill.
>
> As for what should be escaped and what shouldn't, I defer to Nicolas,
> let's see what he says.

"src" (not only with "org" language), "example" and "export", i.e.,
verbatim, blocks need to be escaped.

You should probably use something like

  (when (string-prefix-p (regexp-opt '("example" "export" "src")) type t)
   ...)

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-21 Thread Eric Abrahamsen
Kaushal Modi  writes:

> On Fri, Oct 20, 2017 at 5:15 PM Eric Abrahamsen  
> wrote:
>
>  The template really only inserts the block type, not anything specific
>  like the source language or export backend.
>
> Right, but based on the code you have, you can easily add "SRC org", "SRC 
> emacs-lisp" to the default value of that list. Looking at that, users can 
> also get an idea that
> they can add their favorite languages to that list too.
>  
>  I think prompting for
>  "second-level" information like that might be a little overkill.
>
> I second that. The first-level prompt also feels a bit slow, honestly, 
> compared to using easy templates like " (just an example) to insert
> the, well, EXAMPLE block as usual. But *if region is selected*, it does the 
> wrapping as in your code.

Perhaps it would make sense to combine the two functionalities; that's
not a decision I can make, though.




Re: [O] function for inserting a block

2017-10-21 Thread Xebar Saram
thx thats perfect

Z

On Sat, Oct 21, 2017 at 2:59 PM, Marco Wahl  wrote:

> The following message is a courtesy copy of an article
> that has been posted to gmane.emacs.orgmode as well.
>
> Hi!
>
> Xebar Saram  writes:
>
> > this looks really cool and would love to try. as a non technical user,
> how
> > does one use the patch to get the functionality? is there a way to
> create a
> > clean function only version from the patch i can try?
>
> You could simply pick the relevant parts from the patch and evaluate
> them.
>
> Concretely you could do:
>
> 1. Copy Eric's code to empty buffer *scratch*.  The relevant code is
>
> #+BEGIN_SRC elisp
> (defcustom org-structure-predefined-blocks
>   '("SRC" "EXAMPLE" "QUOTE" "VERSE" "VERBATIM" "CENTER" "COMMENT" "EXPORT")
>   "Block structure completion names."
>   :group 'org-completion
>   :type '(repeat string)
>   :package-version '(Org . "9.1.3"))
>
> (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 an element under
> point, wrap the element in the block.  Otherwise, insert an empty
> block."
>   (interactive)
>   (setq type (or type (completing-read "Block type: "
>org-structure-predefined-blocks)))
>   (unless (use-region-p)
> (when (org-element-at-point)
>   (org-mark-element)))
>   (let ((s (if (use-region-p)
>(region-beginning)
>  (point)))
> (e (copy-marker (if (use-region-p)
> (region-end)
>   (point))
> t))
> column)
> (when (string-equal (downcase type) "example")
>   (org-escape-code-in-region s e))
> (goto-char s)
> (setq column (current-indentation))
> (beginning-of-line)
> (indent-to column)
> (insert (format "#+BEGIN_%s\n" type))
> (goto-char e)
> (if (bolp)
> (progn
>   (skip-chars-backward " \n\t")
>   (forward-line))
>   (end-of-line)
>   (insert "\n"))
> (indent-to column)
> (insert (format "#+END_%s\n"
> type))
> (set-marker e nil)))
>
> (org-defkey org-mode-map "\C-c\C-xw"'org-insert-structure-template)
> #+END_SRC
>
> 2. In buffer *scratch* do
>
> M-x eval-buffer
>
> 3. Voila!  Check out C-c C-x w in an org mode buffer.
>
>
> Best regards
> Marco
>


Re: [O] function for inserting a block

2017-10-21 Thread Marco Wahl
Hi!

Xebar Saram  writes:

> this looks really cool and would love to try. as a non technical user, how
> does one use the patch to get the functionality? is there a way to create a
> clean function only version from the patch i can try?

You could simply pick the relevant parts from the patch and evaluate
them.

Concretely you could do:

1. Copy Eric's code to empty buffer *scratch*.  The relevant code is

#+BEGIN_SRC elisp
(defcustom org-structure-predefined-blocks
  '("SRC" "EXAMPLE" "QUOTE" "VERSE" "VERBATIM" "CENTER" "COMMENT" "EXPORT")
  "Block structure completion names."
  :group 'org-completion
  :type '(repeat string)
  :package-version '(Org . "9.1.3"))

(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 an element under
point, wrap the element in the block.  Otherwise, insert an empty
block."
  (interactive)
  (setq type (or type (completing-read "Block type: "
   org-structure-predefined-blocks)))
  (unless (use-region-p)
(when (org-element-at-point)
  (org-mark-element)))
  (let ((s (if (use-region-p)
   (region-beginning)
 (point)))
(e (copy-marker (if (use-region-p)
(region-end)
  (point))
t))
column)
(when (string-equal (downcase type) "example")
  (org-escape-code-in-region s e))
(goto-char s)
(setq column (current-indentation))
(beginning-of-line)
(indent-to column)
(insert (format "#+BEGIN_%s\n" type))
(goto-char e)
(if (bolp)
(progn
  (skip-chars-backward " \n\t")
  (forward-line))
  (end-of-line)
  (insert "\n"))
(indent-to column)
(insert (format "#+END_%s\n"
type))
(set-marker e nil)))

(org-defkey org-mode-map "\C-c\C-xw"'org-insert-structure-template)
#+END_SRC

2. In buffer *scratch* do

M-x eval-buffer

3. Voila!  Check out C-c C-x w in an org mode buffer.


Best regards
Marco




Re: [O] function for inserting a block

2017-10-21 Thread Xebar Saram
Hi all

this looks really cool and would love to try. as a non technical user, how
does one use the patch to get the functionality? is there a way to create a
clean function only version from the patch i can try?

thx!

Z

On Sat, Oct 21, 2017 at 12:43 AM, Kaushal Modi 
wrote:

> On Fri, Oct 20, 2017 at 5:15 PM Eric Abrahamsen 
> wrote:
>
>> The template really only inserts the block type, not anything specific
>> like the source language or export backend.
>
>
> Right, but based on the code you have, you can easily add "SRC org", "SRC
> emacs-lisp" to the default value of that list. Looking at that, users can
> also get an idea that they can add their favorite languages to that list
> too.
>
>
>> I think prompting for
>> "second-level" information like that might be a little overkill.
>>
>
> I second that. The first-level prompt also feels a bit slow, honestly,
> compared to using easy templates like " (just an example) to insert the, well, EXAMPLE block as usual. But *if
> region is selected*, it does the wrapping as in your code.
>
> This is what I mean (GIF animation):
>
> https://i.imgur.com/201TISW.gifv
>
> I use hydra to provide those awesome hints.. but the same can be done
> without hydras too.
>
> As for what should be escaped and what shouldn't, I defer to Nicolas,
>> let's see what he says.
>>
> --
>
> Kaushal Modi
>


Re: [O] function for inserting a block

2017-10-20 Thread Kaushal Modi
On Fri, Oct 20, 2017 at 5:15 PM Eric Abrahamsen 
wrote:

> The template really only inserts the block type, not anything specific
> like the source language or export backend.


Right, but based on the code you have, you can easily add "SRC org", "SRC
emacs-lisp" to the default value of that list. Looking at that, users can
also get an idea that they can add their favorite languages to that list
too.


> I think prompting for
> "second-level" information like that might be a little overkill.
>

I second that. The first-level prompt also feels a bit slow, honestly,
compared to using easy templates like " let's see what he says.
>
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-10-20 Thread Eric Abrahamsen
Kaushal Modi  writes:

> Also, if the type is "src", shouldn't the point end up after "#+BEGIN_SRC"? 
> Because the user will anyways need to type something there.

My original version did that. It might be nice to still do that
selectively if the user adds a SRC or EXPORT block.

> Finally, I am trying to understand what this does:
>
> (if (bolp)
> (progn
>   (skip-chars-backward " \n\t")
>   (forward-line))
>   ;; snip
>   )
>
> If the point is at BOL, wouldn't that progn bring the point exactly to where 
> it was, as the same BOL? Also isn't that progn equivalent to (forward-line 0)?
>
> I am probably missing something.. but seems to work the same with 
>
> (unless (bolp)
>   (end-of-line)
>   (insert "\n"))
>
> replacing that whole (if ..) form.

The case this is addressing is if there are multiple newlines after the
org element we're wrapping (one of the clauses in the test shows this).
The skip-chars-backward is there to go back over multiple newlines, so
that the #+END_FOO string always comes right after the end of the text.

Eric




Re: [O] function for inserting a block

2017-10-20 Thread Eric Abrahamsen
Kaushal Modi  writes:

> On Fri, Oct 20, 2017 at 2:07 PM Eric Abrahamsen  
> wrote:
>
>  Okay, here's another version, with a new keybinding and completion. The
>  completion strings are uppercase, which might not always be the right
>  thing, but probably more often than not.
>
> Hi Eric,
>
> I just tried it out, and it works great!
>
> I have a comment about
>
> (when (string-equal (downcase type) "example")
>   (org-escape-code-in-region s e)) 
>
> I have never needed to escape org in example, blocks, but I *have* needed to 
> do that in org src blocks. 
>
> Should type string be also matched with "src org"?
>
> Actually should the type string be matched only with "src org"? Because I see 
> the Org example blocks as   blocks in HTML with no syntax 
> highlighting.. so
> those can contain code from any language.
>
> Also as this is part of org and emacs, org-structure-predefined-blocks 
> deserves "SRC org" and "SRC emacs-lisp" too? :)

The template really only inserts the block type, not anything specific
like the source language or export backend. I think prompting for
"second-level" information like that might be a little overkill.

As for what should be escaped and what shouldn't, I defer to Nicolas,
let's see what he says.

Eric




Re: [O] function for inserting a block

2017-10-20 Thread Kaushal Modi
Also, if the type is "src", shouldn't the point end up after "#+BEGIN_SRC"?
Because the user will anyways need to type something there.

Finally, I am trying to understand what this does:

(if (bolp)
(progn
  (skip-chars-backward " \n\t")
  (forward-line))
  ;; snip
  )

If the point is at BOL, wouldn't that progn bring the point exactly to
where it was, as the same BOL? Also isn't that progn equivalent to
(forward-line 0)?

I am probably missing something.. but seems to work the same with

(unless (bolp)
  (end-of-line)
  (insert "\n"))

replacing that whole (if ..) form.
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-10-20 Thread Kaushal Modi
On Fri, Oct 20, 2017 at 2:07 PM Eric Abrahamsen 
wrote:

> Okay, here's another version, with a new keybinding and completion. The
> completion strings are uppercase, which might not always be the right
> thing, but probably more often than not.
>

Hi Eric,

I just tried it out, and it works great!

I have a comment about

(when (string-equal (downcase type) "example")
  (org-escape-code-in-region s e))

I have never needed to escape org in example, blocks, but I *have* needed
to do that in org src blocks.

Should type string be also matched with "src org"?

Actually should the type string be matched only with "src org"? Because I
see the Org example blocks as   blocks in HTML with no syntax
highlighting.. so those can contain code from any language.

Also as this is part of org and emacs, org-structure-predefined-blocks
deserves "SRC org" and "SRC emacs-lisp" too? :)
-- 

Kaushal Modi


Re: [O] function for inserting a block

2017-10-20 Thread Eric Abrahamsen
Carsten Dominik  writes:

> Hi Eric,
>
> On Wed, Oct 18, 2017 at 4:58 PM, Eric Abrahamsen  
> wrote:
>
>  Carsten Dominik  writes:
>
>  > Dear all,
>  >
>  > this is great added functionality that I have missed a lot myself.  Thanks 
> for this!  Also, I like the key binding.
>
>  I do too, though I also notice it conflicts with inlinetask insertion.
>
> Ooops.  I overlooked that. Hmmm, that is not ideal.
>
> Maybe then C-c C-x w would be better, w can stand for "wrap".
>  
>  
>  > One improvement I can think of it to read the block type with completion 
> (but still allow any word to be used).
>
>  I'd be happy to do that. There would be a tiny bit of redundancy with
>  `org-structure-template-alist', but nothing too terrible.
>
> I agree.  You could pull it would of the alist, but I am not sure it is worth 
> it.

Okay, here's another version, with a new keybinding and completion. The
completion strings are uppercase, which might not always be the right
thing, but probably more often than not.

Eric

>From cb6f1815259094f7f9b68459dcded5dce14d3154 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Sat, 7 Oct 2017 13:01:14 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
  (org-structure-predefined-blocks): New option holding predefined
  blocks, for completion.
* doc/org.texi (Structure of code blocks, Easy templates): And in
  manual.
* testing/lisp/test-org.el (test-org/insert-template): New test.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  4 
 lisp/org.el  | 47 +++
 testing/lisp/test-org.el | 42 ++
 4 files changed, 108 insertions(+), 6 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index c54f2615a..6ad9d1c15 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15242,12 +15242,13 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of src code blocks: a template
+system that can create a new block with just three keystrokes, and a command
+for wrapping existing text in a block (@pxref{Easy templates}).  Org also
+works with other completion systems in Emacs, some of which predate Org and
+have custom domain-specific languages for defining templates.  Regular use of
+templates reduces errors, increases accuracy, and maintains consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17418,6 +17419,14 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+@kindex C-c C-x w
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is bound to @kbd{C-c C-x w} by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1076dd970..190a6b8bc 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -109,6 +109,10 @@ you should expect to see something like:
 #+END_EXAMPLE
 
 ** New functions
+*** ~org-insert-structure-template~
+
+This function can be used to wrap existing text of Org elements in
+a #+BEGIN_FOO/#+END_FOO block.  Bound to C-c C-x w by default.
 
 *** ~org-export-excluded-from-toc-p~
 
diff --git a/lisp/org.el b/lisp/org.el
index 54687abc7..e1cf14cae 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12117,6 +12117,13 @@ keywords relative to each registered export back-end."
 "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
 "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
 
+(defcustom org-structure-predefined-blocks
+  '("SRC" "EXAMPLE" "QUOTE" "VERSE" "VERBATIM" "CENTER" "COMMENT" "EXPORT")
+  "Block structure completion names."
+  :group 'org-completion
+  :type '(repeat string)
+  :package-version '(Org . "9.1.3"))
+
 (defcustom org-structure-template-alist
   '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
 

Re: [O] function for inserting a block

2017-10-19 Thread Carsten Dominik
Hi Eric,



On Wed, Oct 18, 2017 at 4:58 PM, Eric Abrahamsen 
wrote:

> Carsten Dominik  writes:
>
> > Dear all,
> >
> > this is great added functionality that I have missed a lot myself.
> Thanks for this!  Also, I like the key binding.
>
> I do too, though I also notice it conflicts with inlinetask insertion.
>


Ooops.  I overlooked that. Hmmm, that is not ideal.

Maybe then C-c C-x w would be better, w can stand for "wrap".


>
> > One improvement I can think of it to read the block type with completion
> (but still allow any word to be used).
>
> I'd be happy to do that. There would be a tiny bit of redundancy with
> `org-structure-template-alist', but nothing too terrible.
>

I agree.  You could pull it would of the alist, but I am not sure it is
worth it.

Carsten


>
> Eric
>
>
>


Re: [O] function for inserting a block

2017-10-18 Thread Eric Abrahamsen
Carsten Dominik  writes:

> Dear all,
>
> this is great added functionality that I have missed a lot myself.  Thanks 
> for this!  Also, I like the key binding.

I do too, though I also notice it conflicts with inlinetask insertion.

> One improvement I can think of it to read the block type with completion (but 
> still allow any word to be used).

I'd be happy to do that. There would be a tiny bit of redundancy with
`org-structure-template-alist', but nothing too terrible.

Eric




Re: [O] function for inserting a block

2017-10-18 Thread Carsten Dominik
Dear all,

this is great added functionality that I have missed a lot myself.  Thanks
for this!  Also, I like the key binding.

One improvement I can think of it to read the block type with completion
(but still allow any word to be used).

Carsten

On Wed, Oct 18, 2017 at 12:03 AM, Eric Abrahamsen 
wrote:

> Eric Abrahamsen  writes:
>
> > Nicolas Goaziou  writes:
> >
> >> Eric Abrahamsen  writes:
> >>
> >>> I'm still not quite seeing this. This chunk should take care of it:
> >>>
> >>> (goto-char e)
> >>> (if (bolp)
> >>> (progn
> >>>   (skip-chars-backward " \n\t")
> >>>   (forward-line))
> >>>   (end-of-line)
> >>>   (insert "\n"))
> >>>
> >>> If "e" is EOB, we do `end-of-line' and insert a newline, it should be
> >>> taken care of. I added a new clause in the test for this case, and it
> >>> seems to work fine... Am I missing anything?
> >>
> >> I don't think so. It looks correct, indeed.
> >>
> >> However, you sent the wrong patch. Could you send the updated patch
> >> again?
> >
> > Ooof, maybe I need to take a little vacation from the computer. This
> > should be the right one.
>
> Backing away from the keyboard now...
>
>


Re: [O] function for inserting a block

2017-10-17 Thread Eric Abrahamsen
Eric Abrahamsen  writes:

> Nicolas Goaziou  writes:
>
>> Eric Abrahamsen  writes:
>>
>>> I'm still not quite seeing this. This chunk should take care of it:
>>>
>>> (goto-char e)
>>> (if (bolp)
>>> (progn
>>>   (skip-chars-backward " \n\t")
>>>   (forward-line))
>>>   (end-of-line)
>>>   (insert "\n"))
>>>
>>> If "e" is EOB, we do `end-of-line' and insert a newline, it should be
>>> taken care of. I added a new clause in the test for this case, and it
>>> seems to work fine... Am I missing anything?
>>
>> I don't think so. It looks correct, indeed.
>>
>> However, you sent the wrong patch. Could you send the updated patch
>> again?
>
> Ooof, maybe I need to take a little vacation from the computer. This
> should be the right one.

Backing away from the keyboard now...

>From d317f25abafe012e0a678ac0c0cc3b372d22113e Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Sat, 7 Oct 2017 13:01:14 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
* doc/org.texi (Structure of code blocks, Easy templates): And in
  manual.
* testing/lisp/test-org.el (test-org/insert-template): New test.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  4 
 lisp/org.el  | 38 ++
 testing/lisp/test-org.el | 42 ++
 4 files changed, 99 insertions(+), 6 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 728e73f87..3a4068ce5 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15233,12 +15233,13 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of src code blocks: a template
+system that can create a new block with just three keystrokes, and a command
+for wrapping existing text in a block (@pxref{Easy templates}).  Org also
+works with other completion systems in Emacs, some of which predate Org and
+have custom domain-specific languages for defining templates.  Regular use of
+templates reduces errors, increases accuracy, and maintains consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17409,6 +17410,14 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+@kindex C-c C-x t
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is bound to @kbd{C-c C-x t} by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1076dd970..d02148e84 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -109,6 +109,10 @@ you should expect to see something like:
 #+END_EXAMPLE
 
 ** New functions
+*** ~org-insert-structure-template~
+
+This function can be used to wrap existing text of Org elements in
+a #+BEGIN_FOO/#+END_FOO block.  Bound to C-c C-x t by default.
 
 *** ~org-export-excluded-from-toc-p~
 
diff --git a/lisp/org.el b/lisp/org.el
index 99eba2da3..c5f6279d4 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12188,6 +12188,43 @@ expands them."
 (insert rpl)
 (when (re-search-backward "\\?" start t) (delete-char 1
 
+(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 an element under
+point, wrap the element in the block.  Otherwise, insert an empty
+block."
+  (interactive "sBlock type: ")
+  (unless (use-region-p)
+(when (org-element-at-point)
+  (org-mark-element)))
+  (let ((s (if (use-region-p)
+	   (region-beginning)
+	 (point)))
+	(e (copy-marker (if (use-region-p)
+			(region-end)
+			  (point))
+			t))
+	column)
+(when (string-equal (downcase type) "example")
+  (org-escape-code-in-region s e))
+(goto-char s)
+(setq column 

Re: [O] function for inserting a block

2017-10-17 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Eric Abrahamsen  writes:
>
>> I'm still not quite seeing this. This chunk should take care of it:
>>
>> (goto-char e)
>> (if (bolp)
>>  (progn
>>(skip-chars-backward " \n\t")
>>(forward-line))
>>   (end-of-line)
>>   (insert "\n"))
>>
>> If "e" is EOB, we do `end-of-line' and insert a newline, it should be
>> taken care of. I added a new clause in the test for this case, and it
>> seems to work fine... Am I missing anything?
>
> I don't think so. It looks correct, indeed.
>
> However, you sent the wrong patch. Could you send the updated patch
> again?

Ooof, maybe I need to take a little vacation from the computer. This
should be the right one.

Thanks,
Eric




Re: [O] function for inserting a block

2017-10-17 Thread Nicolas Goaziou
Eric Abrahamsen  writes:

> I'm still not quite seeing this. This chunk should take care of it:
>
> (goto-char e)
> (if (bolp)
>   (progn
> (skip-chars-backward " \n\t")
> (forward-line))
>   (end-of-line)
>   (insert "\n"))
>
> If "e" is EOB, we do `end-of-line' and insert a newline, it should be
> taken care of. I added a new clause in the test for this case, and it
> seems to work fine... Am I missing anything?

I don't think so. It looks correct, indeed.

However, you sent the wrong patch. Could you send the updated patch
again?

Thank you.

Regards,



Re: [O] function for inserting a block

2017-10-17 Thread Eric Abrahamsen
Nicolas Goaziou  writes:


[...]

> Then it's `forward-line', not `forward-char', because there could be
> trailing spaces at the end of the paragraph, e.g.
>
>This is a paragraph.

Okay, changed to `forward-line'.

> Also, my question still holds, what about the last paragraph in a buffer
> not ending with a newline character, e.g.
>
>   This is the last paragraph.
>
> You need to insert a newline character in this case.

I'm still not quite seeing this. This chunk should take care of it:

(goto-char e)
(if (bolp)
(progn
  (skip-chars-backward " \n\t")
  (forward-line))
  (end-of-line)
  (insert "\n"))

If "e" is EOB, we do `end-of-line' and insert a newline, it should be
taken care of. I added a new clause in the test for this case, and it
seems to work fine... Am I missing anything?

Eric

>From 7ac8883394bc2979dee731c54ef65c0a9c135d0b Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou 
Date: Tue, 17 Oct 2017 10:11:21 +0200
Subject: [PATCH] Move `org-completing-read' into "org-macs.el"

* lisp/org-macs.el (org-completing-read): Moved here...
* lisp/org.el: ... from there.
---
 lisp/org-macs.el | 35 +--
 lisp/org.el  | 10 --
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 1a2d8a49d..196a2ce22 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -168,6 +168,31 @@ point nowhere."
 
 
 
+;;; Input
+
+(defun org-read-function (prompt  allow-empty?)
+  "Prompt for a function.
+If ALLOW-EMPTY? is non-nil, return nil rather than raising an
+error when the user input is empty."
+  (let ((func (completing-read prompt obarray #'fboundp t)))
+(cond ((not (string= func ""))
+	   (intern func))
+	  (allow-empty? nil)
+	  (t (user-error "Empty input is not valid")
+
+(defun org-completing-read ( args)
+  "Completing-read with SPACE being a normal character."
+  (let ((enable-recursive-minibuffers t)
+	(minibuffer-local-completion-map
+	 (copy-keymap minibuffer-local-completion-map)))
+(org-defkey minibuffer-local-completion-map " " 'self-insert-command)
+(org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
+(org-defkey minibuffer-local-completion-map (kbd "C-c !")
+		'org-time-stamp-inactive)
+(apply #'completing-read args)))
+
+
+
 ;;; Logic
 
 (defsubst org-xor (a b)
@@ -469,16 +494,6 @@ The number of levels is controlled by `org-inlinetask-min-level'"
 			  limit-level)))
 	   (format "\\*\\{1,%d\\} " nstars)
 
-(defun org-read-function (prompt  allow-empty?)
-  "Prompt for a function.
-If ALLOW-EMPTY? is non-nil, return nil rather than raising an
-error when the user input is empty."
-  (let ((func (completing-read prompt obarray #'fboundp t)))
-(cond ((not (string= func ""))
-	   (intern func))
-	  (allow-empty? nil)
-	  (t (user-error "Empty input is not valid")
-
 
 (provide 'org-macs)
 
diff --git a/lisp/org.el b/lisp/org.el
index e3b4ccef1..99eba2da3 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -10322,16 +10322,6 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
 		   (match-string 1 (expand-file-name file
 	  (t (concat "file:" file)
 
-(defun org-completing-read ( args)
-  "Completing-read with SPACE being a normal character."
-  (let ((enable-recursive-minibuffers t)
-	(minibuffer-local-completion-map
-	 (copy-keymap minibuffer-local-completion-map)))
-(org-defkey minibuffer-local-completion-map " " 'self-insert-command)
-(org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
-(org-defkey minibuffer-local-completion-map (kbd "C-c !")
-		'org-time-stamp-inactive)
-(apply #'completing-read args)))
 
 ;;; Opening/following a link
 
-- 
2.14.2



Re: [O] function for inserting a block

2017-10-17 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> Nicolas Goaziou  writes:

>>> +(if (bolp)
>>> +   (progn
>>> + (skip-chars-backward " \n\t")
>>> + (forward-char))
>>> +  (end-of-line)
>>> +  (insert "\n"))
>>
>> I don't understand this part. In particular, the `forward-char' looks
>> wrong. Do you mean `forward-line' ? If so, do you handle the case where
>> buffer doesn't end with a newline character?
>
> This was a bit of finicky code mostly for aesthetic purposes. It has to
> do with this case (the second clause in the test I added):
>
> #+BEGIN_SRC org
> This is a paragraph
>
> This is another paragraph
> #+END_SRC
>
>
> If point is on the first paragraph and the region is not active,
> `org-mark-element' will mark the paragraph and all following whitespace,
> which means we end up with:
>
> #+BEGIN_SRC org
> #+BEGIN_FOO
> This is a paragraph.
>
> #+END_FOO
>
> This is another paragraph.
> #+END_SRC
>
> Which just looked bad to me (even though it probably behaves perfectly
> correctly). So if point is at bol, it skips back over the whitespace and
> then moves forward one char, presumably leaving point right after the
> marked element.

Then it's `forward-line', not `forward-char', because there could be
trailing spaces at the end of the paragraph, e.g.

   This is a paragraph.

Also, my question still holds, what about the last paragraph in a buffer
not ending with a newline character, e.g.

  This is the last paragraph.

You need to insert a newline character in this case.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-16 Thread Eric Abrahamsen
Eric Abrahamsen  writes:


[...]

> #+BEGIN_SRC org
> #+BEGIN_FOO
> This is a paragraph.
>
> #+END_FOO
>
> This is another paragraph.
> #+END_SRC

Make that:

#+BEGIN_FOO
This is a paragraph

#+END_FOO
This is another paragraph




Re: [O] function for inserting a block

2017-10-16 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> How does this look?
>
> Thank you! I have some questions and remarks.
>
>> * etc/ORG-NEWS: Mention in news.
>
> This doesn't need to be added to the commit message.
>
>> +Do not be put off by having to remember the source block syntax.  Org mode
>> +offers two ways of speeding up the creation of @samp{src} code blocks:
>
>   src code blocks
>
> is enough, IMO. There are may of them across the manual, and it just
> makes reading more tedious.
>
>> +@findex org-insert-structure-template
>
> I would also add
>
>   @kindex C-c C-x t
>
>> +  (let ((s (copy-marker (if (use-region-p)
>> +(region-beginning)
>> +  (point
>
> Does it really need to be a marker? AFAICT, nothing really changes this
> position.
>
>> +(back-to-indentation)
n>> +(insert (format "#+BEGIN_%s\n"
>> +type))
>> +(indent-to column)
>
> What about
>
>   (beginning-of-line)
>   (indent-to column)
>   (insert (format "#+BEGIN_%s\n" type))
>
> ?
>
> It avoids `back-to-indentation'.

Cool, I will make all of the above changes, thanks for the notes.

>> +(if (bolp)
>> +(progn
>> +  (skip-chars-backward " \n\t")
>> +  (forward-char))
>> +  (end-of-line)
>> +  (insert "\n"))
>
> I don't understand this part. In particular, the `forward-char' looks
> wrong. Do you mean `forward-line' ? If so, do you handle the case where
> buffer doesn't end with a newline character?

This was a bit of finicky code mostly for aesthetic purposes. It has to
do with this case (the second clause in the test I added):

#+BEGIN_SRC org
This is a paragraph

This is another paragraph
#+END_SRC

If point is on the first paragraph and the region is not active,
`org-mark-element' will mark the paragraph and all following whitespace,
which means we end up with:

#+BEGIN_SRC org
#+BEGIN_FOO
This is a paragraph.

#+END_FOO
This is another paragraph.
#+END_SRC

Which just looked bad to me (even though it probably behaves perfectly
correctly). So if point is at bol, it skips back over the whitespace and
then moves forward one char, presumably leaving point right after the
marked element.

Maybe it should only skip back over newlines (though then indentation
will confuse it). Or maybe I should just not be so picky about
appearances...

Eric




Re: [O] function for inserting a block

2017-10-14 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> How does this look?

Thank you! I have some questions and remarks.

> * etc/ORG-NEWS: Mention in news.

This doesn't need to be added to the commit message.

> +Do not be put off by having to remember the source block syntax.  Org mode
> +offers two ways of speeding up the creation of @samp{src} code blocks:

  src code blocks

is enough, IMO. There are may of them across the manual, and it just
makes reading more tedious.

> +@findex org-insert-structure-template

I would also add

  @kindex C-c C-x t

> +  (let ((s (copy-marker (if (use-region-p)
> + (region-beginning)
> +   (point

Does it really need to be a marker? AFAICT, nothing really changes this
position.

> +(back-to-indentation)
> +(insert (format "#+BEGIN_%s\n"
> + type))
> +(indent-to column)

What about

  (beginning-of-line)
  (indent-to column)
  (insert (format "#+BEGIN_%s\n" type))

?

It avoids `back-to-indentation'.

> +(if (bolp)
> + (progn
> +   (skip-chars-backward " \n\t")
> +   (forward-char))
> +  (end-of-line)
> +  (insert "\n"))

I don't understand this part. In particular, the `forward-char' looks
wrong. Do you mean `forward-line' ? If so, do you handle the case where
buffer doesn't end with a newline character?

> +(set-marker s nil)

See above.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-10-07 Thread Eric Abrahamsen
Nicolas Goaziou  writes:

> Hello,
>
> Eric Abrahamsen  writes:
>
>> Nicolas Goaziou  writes:
>>
>>
>> [...]
>>
>>> C-c C-x t is free, tho.
>>
>> Oops, I think that's what I meant to type. Here's the latest version of
>> the patch -- I removed the bit fooling with the location of point at the
>> end, as it seemed simpler was better. How's this look?
>
> `newline-and-indent' and `mark' shouldn't be used in the function.

This must be a personal record for number of screw-ups in a single
commit... That was an old version from a different computer.

But still, I'm not entirely confident about the indentation handling.
This is my best guess -- it seems to work, and doesn't get confused by
`org-indent-mode'.

> Otherwise, it looks good. Could you provide some tests for that in
> "test-org.el"?

How does this look?

Eric

>From d859c241053aef2dd2f2a9b04f4030de54eb330d Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Sat, 7 Oct 2017 13:01:14 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
* etc/ORG-NEWS: Mention in news.
* doc/org.texi (Structure of code blocks, Easy templates): And in
  manual.
* testing/lisp/test-org.el (test-org/insert-template): New test.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  5 +
 lisp/org.el  | 40 
 testing/lisp/test-org.el | 36 
 4 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index fd26d9790..fd537ded4 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15215,12 +15215,14 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of @samp{src} code blocks: a
+templates system that can create a new block with just three keystrokes, and
+a command for wrapping existing text in a block (@pxref{Easy templates}).
+Org also works with other completion systems in Emacs, some of which predate
+Org and have custom domain-specific languages for defining templates.
+Regular use of templates reduces errors, increases accuracy, and maintains
+consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17391,6 +17393,13 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is bound to @kbd{C-c C-x t} by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 53d604b8c..ed487b20f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -105,6 +105,11 @@ you should expect to see something like:
   ,#+STARTUP: shrink
 #+END_EXAMPLE
 
+** New Functions
+*** ~org-insert-structure-template~
+
+This function can be used to wrap existing text of Org elements in
+a #+BEGIN_FOO/#+END_FOO block.  Bound to C-c C-x t by default.
 ** Miscellaneous
 
 *** ~org-publish-resolve-external-link~ accepts a new optional argument.
diff --git a/lisp/org.el b/lisp/org.el
index 5a60e34cb..f888af09c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12199,6 +12199,45 @@ expands them."
 (insert rpl)
 (when (re-search-backward "\\?" start t) (delete-char 1
 
+(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 an element under
+point, wrap the element in the block.  Otherwise, insert an empty
+block."
+  (interactive "sBlock type: ")
+  (unless (use-region-p)
+(when (org-element-at-point)
+  (org-mark-element)))
+  (let ((s (copy-marker (if (use-region-p)
+			(region-beginning)
+			  (point
+	(e (copy-marker (if (use-region-p)
+			(region-end)
+			  (point))
+			t))
+	column)
+(when 

Re: [O] function for inserting a block

2017-10-05 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> Nicolas Goaziou  writes:
>
>
> [...]
>
>> C-c C-x t is free, tho.
>
> Oops, I think that's what I meant to type. Here's the latest version of
> the patch -- I removed the bit fooling with the location of point at the
> end, as it seemed simpler was better. How's this look?

`newline-and-indent' and `mark' shouldn't be used in the function.

Otherwise, it looks good. Could you provide some tests for that in
"test-org.el"?

Thank you.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-09-30 Thread Eric Abrahamsen
Nicolas Goaziou  writes:


[...]

> C-c C-x t is free, tho.

Oops, I think that's what I meant to type. Here's the latest version of
the patch -- I removed the bit fooling with the location of point at the
end, as it seemed simpler was better. How's this look?

Eric

>From b8636d918e9ff79cac320003361c5a16846f156c Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Sat, 30 Sep 2017 13:23:05 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): New function for
  wrapping region (or element at point) in a begin/end block.
* etc/ORG-NEWS: Mention in news.
* doc/org.texi (Structure of code blocks, Easy templates): And in
  manual.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  5 +
 lisp/org.el  | 25 +
 3 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 1c79d8330..2274e584a 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15215,12 +15215,14 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of @samp{src} code blocks: a
+templates system that can create a new block with just three keystrokes, and
+a command for wrapping existing text in a block (@pxref{Easy templates}).
+Org also works with other completion systems in Emacs, some of which predate
+Org and have custom domain-specific languages for defining templates.
+Regular use of templates reduces errors, increases accuracy, and maintains
+consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17391,6 +17393,13 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is bound to @kbd{C-c C-x t} by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 7c69efa89..1a3aa9e92 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -105,6 +105,11 @@ you should expect to see something like:
   ,#+STARTUP: shrink
 #+END_EXAMPLE
 
+** New Functions
+*** ~org-insert-structure-template~
+
+This function can be used to wrap existing text of Org elements in
+a #+BEGIN_FOO/#+END_FOO block.  Bound to C-c C-x t by default.
 ** Miscellaneous
 
 *** ~org-publish-resolve-external-link~ accepts a new optional argument.
diff --git a/lisp/org.el b/lisp/org.el
index 1ffea80fe..fe7bfd1c5 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12199,6 +12199,30 @@ expands them."
 (insert rpl)
 (when (re-search-backward "\\?" start t) (delete-char 1
 
+(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)
+(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
+
  TODO, DEADLINE, Comments
 
 (defun org-toggle-comment ()
@@ -19660,6 +19684,7 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names."
 (org-defkey org-mode-map "\C-c\C-xE"'org-inc-effort)
 (org-defkey org-mode-map "\C-c\C-xo"'org-toggle-ordered-property)
 (org-defkey org-mode-map "\C-c\C-xi"'org-columns-insert-dblock)
+(org-defkey org-mode-map "\C-c\C-xt"'org-insert-structure-template)
 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
 
 (org-defkey org-mode-map "\C-c\C-x."'org-timer)
-- 
2.14.2



Re: [O] function for inserting a block

2017-09-29 Thread Nicolas Goaziou
Hello,

Eric Abrahamsen  writes:

> 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"?

C-c C-x C-t is `org-toggle-time-stamp-overlays'. I listed all free
keybindings following the scheme 

  C-c C-x C-

or

  C-c C-x 

C-c C-x t is free, tho.

Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-09-10 Thread Eric Abrahamsen

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.




Re: [O] function for inserting a block

2017-09-10 Thread Nicolas Goaziou
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



Re: [O] function for inserting a block

2017-09-08 Thread Eric Abrahamsen
Eric Abrahamsen  writes:

[...]

> 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)))

And here's an actual patch, with docs.

>From 0ffb541d10ff4516a869a8c521683e8a6b0a0577 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen 
Date: Fri, 8 Sep 2017 11:48:45 -0700
Subject: [PATCH] New function org-insert-structure-template

* lisp/org.el (org-insert-structure-template): Wraps the region or
  current element in a #+BEGIN_FOO/#+END_FOO block.
* doc/org.texi: Document.
* etc/ORG-NEWS: Note in news.
---
 doc/org.texi | 21 +++--
 etc/ORG-NEWS |  5 -
 lisp/org.el  | 26 ++
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index f40f458e3..094e9b48b 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15212,12 +15212,14 @@ A @samp{src} block conforms to this structure:
 #+END_SRC
 @end example
 
-Org mode's templates system (@pxref{Easy templates}) speeds up creating
-@samp{src} code blocks with just three keystrokes.  Do not be put-off by
-having to remember the source block syntax.  Org also works with other
-completion systems in Emacs, some of which predate Org and have custom
-domain-specific languages for defining templates.  Regular use of templates
-reduces errors, increases accuracy, and maintains consistency.
+Do not be put off by having to remember the source block syntax.  Org mode
+offers two ways of speeding up the creation of @samp{src} code blocks: a
+templates system that can create a new block with just three keystrokes, and
+a command for wrapping existing text in a block (@pxref{Easy templates}).
+Org also works with other completion systems in Emacs, some of which predate
+Org and have custom domain-specific languages for defining templates.
+Regular use of templates reduces errors, increases accuracy, and maintains
+consistency.
 
 @cindex source code, inline
 An inline code block conforms to this structure:
@@ -17388,6 +17390,13 @@ Org comes with these pre-defined easy templates:
 More templates can added by customizing the variable
 @code{org-structure-template-alist}, whose docstring has additional details.
 
+@findex org-insert-structure-template
+Easy templates are ideal when writing new content, but sometimes it is
+necessary to mark up existing content.  For these cases, Org provides the
+function @code{org-insert-structure-template}, which prompts for a block
+type, and wraps either the active region or the current Org element in that
+block.  This command is not bound to a key by default.
+
 @node Speed keys
 @section Speed keys
 @cindex speed keys
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index e6ad838a6..3b98a7713 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -38,7 +38,6 @@ Archiving headers through ~org-archive-subtree~ and
 #+END_SRC
 
 Will update the status cookie in the top level header.
-
 *** Disable =org-agenda-overriding-header= by setting to empty string
 
 The ~org-agenda-overriding-header~ inserted into agenda views can now
@@ -57,7 +56,11 @@ size.
 #+BEGIN_EXAMPLE
   ,#+STARTUP: shrink
 #+END_EXAMPLE
+** New functions
+*** ~org-insert-structure-template~
 
+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.
 * Version 9.1
 
 ** Incompatible changes
diff --git a/lisp/org.el b/lisp/org.el
index 8fd01ffb2..2c234de48 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -12215,6 +12215,32 @@ expands them."
 (insert rpl)
 (when (re-search-backward "\\?" start t) (delete-char 1
 
+(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)
+(when 

Re: [O] function for inserting a block

2017-09-03 Thread Eric Abrahamsen
Josiah Schwab  writes:

> Hi Eric,
>
>> Erm, I'd be surprised if there's a single Org mode binding that
>> *doesn't* start with C-c. That convention is right out the window with
>> Org...
>
> To be clear, this is referring to C-c and then a single ASCII letter,
> not just any binding that starts with C-c.
>
> The manual /suggests/ globally binding things to C-c a, C-c b, C-c c and
> C-c l, and then proceeds as if you have, but I really don't think it is
> correct to say this convention is out the window.
>
> Josiah

I stand corrected! I hadn't realized the conventions were that narrowly
defined, and now I see that Org indeed doesn't bind "C-c [a-zA-Z]".
Thanks for pointing that out.

Eric




Re: [O] function for inserting a block

2017-09-03 Thread Josiah Schwab
Hi Eric,

> Erm, I'd be surprised if there's a single Org mode binding that
> *doesn't* start with C-c. That convention is right out the window with
> Org...

To be clear, this is referring to C-c and then a single ASCII letter,
not just any binding that starts with C-c.

The manual /suggests/ globally binding things to C-c a, C-c b, C-c c and
C-c l, and then proceeds as if you have, but I really don't think it is
correct to say this convention is out the window.

Josiah



Re: [O] function for inserting a block

2017-09-03 Thread Eric Abrahamsen
Josiah Schwab  writes:

> Hi Eric,
>
>>> 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?
>
> The keybinding convention docs say:
>
> Don't define C-c letter as a key in Lisp programs. Sequences consisting
> of C-c and a letter (either upper or lower case) are reserved for users;
> they are the only sequences reserved for users, so do not block them.
>
> See 
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html

Erm, I'd be surprised if there's a single Org mode binding that
*doesn't* start with C-c. That convention is right out the window with
Org...




Re: [O] function for inserting a block

2017-09-03 Thread Josiah Schwab
Hi Eric,

>> 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?

The keybinding convention docs say:

Don't define C-c letter as a key in Lisp programs. Sequences consisting
of C-c and a letter (either upper or lower case) are reserved for users;
they are the only sequences reserved for users, so do not block them.

See 
https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html

Josiah



Re: [O] function for inserting a block

2017-09-03 Thread Eric Abrahamsen
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




Re: [O] function for inserting a block

2017-09-03 Thread Nicolas Goaziou
Adam Porter  writes:

> "...it is a good idea to make a marker point nowhere if you are sure you
> don’t need it any more.  Markers that can no longer be accessed are
> eventually removed (*note Garbage Collection::)."
>
> I asked on /r/emacs about this but never got a firm answer; some thought
> that they are never collected and require manual clearing, others
> thought that ones that go out-of-scope are eventually collected and that
> it's not typically necessary to clear them manually.  I also recall
> seeing some code recently that didn't manually clear the markers it
> created, so I wonder if that is true.

The are not collected as long as the buffer they point to is alive. If
they are not attached to any buffer, they are marked for garbage
collection.

Regards,



Re: [O] function for inserting a block

2017-09-03 Thread Adam Porter
Nicolas Goaziou  writes:

> 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'.

Not that I oppose cleaning up markers after you're done with them, but
are they actually never collected?  The manual says:

"...it is a good idea to make a marker point nowhere if you are sure you
don’t need it any more.  Markers that can no longer be accessed are
eventually removed (*note Garbage Collection::)."

I asked on /r/emacs about this but never got a firm answer; some thought
that they are never collected and require manual clearing, others
thought that ones that go out-of-scope are eventually collected and that
it's not typically necessary to clear them manually.  I also recall
seeing some code recently that didn't manually clear the markers it
created, so I wonder if that is true.




Re: [O] function for inserting a block

2017-09-03 Thread Nicolas Goaziou
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'.

>(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.

>(goto-char e)
>(goto-char (line-end-position))

(end-of-line) ?

>(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')?

> 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.


Regards,

-- 
Nicolas Goaziou



Re: [O] function for inserting a block

2017-09-02 Thread Eric Abrahamsen
Kaushal Modi  writes:

> On Sat, Sep 2, 2017, 10:23 PM Adam Porter  wrote:
>
>  Hi Eric,
>
>  Thanks for doing this.  I've had some similar code in my config for a
>  while.  I'll share some of it here in case you find it useful in doing
>  this.  You especially might find the org-read-structure-template
>  function useful.
>
> And here's my version, which also uses hydra. But the function 
> modi/org-template-expand to insert the BEGIN_../END_.. blocks can be reused. 
>
> This at least tells that there's a good need to have the easy template do the 
> right thing when it is called with a region selected. 

That's the vast majority of what I wanted. So long as we've got that,
and it doesn't depend on random other packages, I'm happy.




Re: [O] function for inserting a block

2017-09-02 Thread Kaushal Modi
On Sat, Sep 2, 2017, 10:23 PM Adam Porter  wrote:

> Hi Eric,
>
> Thanks for doing this.  I've had some similar code in my config for a
> while.  I'll share some of it here in case you find it useful in doing
> this.  You especially might find the org-read-structure-template
> function useful.
>

And here's my version, which also uses hydra. But the function
modi/org-template-expand to insert the BEGIN_../END_.. blocks can be
reused.

This at least tells that there's a good need to have the easy template do
the right thing when it is called with a region selected.

@Adam: Looks like your code is doing a lot more than just that. I'll put
that to my list to understand once I get to a computer.

>
(defun modi/org-template-expand (str  lang)
  "Expand Org template."
  (let (beg old-beg end content)
;; Save restriction to automatically undo the upcoming
`narrow-to-region'
(save-restriction
  (when (use-region-p)
(setq beg (region-beginning))
(setq end (region-end))
;; Note that regardless of the direction of selection, we
will always
;; have (region-beginning) < (region-end).
(save-excursion
  ;; If `point' is at `end', exchange point and mark so that now the
  ;; `point' is now at `beg'
  (when (> (point) (mark))
(exchange-point-and-mark))
  ;; Insert a newline if `beg' is *not* at beginning of the line.
  ;; Example: You have ^abc$ where ^ is bol and $ is eol.
  ;;  "bc" is selected and 

Re: [O] function for inserting a block

2017-09-02 Thread Adam Porter
Hi Eric,

Thanks for doing this.  I've had some similar code in my config for a
while.  I'll share some of it here in case you find it useful in doing
this.  You especially might find the org-read-structure-template
function useful.

Note that some of this uses s and hydra, which obviously isn't suitable
for Org proper, but that could be fixed.

#+BEGIN_SRC elisp
(defun ap/org-copy-block (prefix)
"Copy current \"#+BEGIN_...\" block to the kill-ring."
(interactive "p")
(kill-new (ap/org-block-contents (>= prefix 4

  (defun ap/org-block-contents ( whole)
"Return contents of current \"BEGIN_...\" block.
When WHOLE is non-nil, include enclosing meta lines."
(let ((bounds (ap/org-block-boundaries (not whole
  (buffer-substring-no-properties (car bounds) (cdr bounds

  (defun ap/org-block-boundaries ( contents)
"Return (BEGINNING . END) of current \"#+BEGIN_...\" block.
If CONTENTS is non-nil, return the boundaries of the block's
contents rather than the entire block."
(let ((case-fold-search t)
  (re "#\\+begin_\\(\\sw+\\)")
  block-beg block-end contents-beg contents-end)
  (save-excursion
;; Get block
(unless (looking-at re)
  ;; If point is in the middle of the "#+BEGIN...",
  ;; `search-backward-regexp' fails, so go to end of line first.
  (end-of-line)
  (condition-case nil
  (search-backward-regexp re)
(error "Not in a block.")))
(setq block-beg (point))
(setq block-end (search-forward-regexp (concat (rx bol (optional (1+ 
space)) "#+end_") (match-string 1
(goto-char block-beg)
(forward-line)
(setq contents-beg (point))
(goto-char block-end)
(end-of-line 0)
(setq contents-end (point)))
  (if contents
  `(,contents-beg . ,contents-end)
`(,block-beg . ,block-end

  (defun ap/org-read-structure-template ()
"Read org-mode structure template with completion.  Returns template 
string."
(let* ((templates (map 'list 'second org-structure-template-alist))
   (prefixes (map 'list (lambda (tp)
  ;; Get template and pre-whitespace prefix for 
completion
  (reverse (s-match (rx (group
 (1+ (not (any "\n" 
space
(1+ anything))
tp)))
  templates))
   (prefix (completing-read "Template: " prefixes nil t))
   (template (second (assoc prefix prefixes
  template))

  (defun ap/org-in-block-p ()
"Non-nil when point belongs to a block.

Return first block name matched, or nil.  Beware that in case of
nested blocks, the returned name may not belong to the closest
block from point."
(save-match-data
  (let ((case-fold-search t)
(lim-up (save-excursion (outline-previous-heading)))
(lim-down (save-excursion (outline-next-heading
(org-between-regexps-p "^[ \t]*#\\+begin_" "^[ \t]*#\\+end_"
   lim-up lim-down

  (defun ap/org-indent-src-block ()
(interactive)
(when (ap/org-in-block-p)
  (org-edit-src-code)
  (insert (replace-regexp-in-string
   " +" " " (delete-and-extract-region (point-min) (point-max
  (ap/indent-whole-buffer)
  (whitespace-cleanup)
  (org-edit-src-exit)))

  (defun ap/org-insert-structure-template-or-enclose-region ()
"Insert structure block template.  When region is active, enclose region in 
block."
(require 's)
(interactive)
(let* ((template (ap/org-read-structure-template))
   (text "")
   enclosed-text)
  (when (use-region-p)
(setq text (buffer-substring-no-properties (region-beginning) 
(region-end)))
(delete-region (region-beginning) (region-end)))
  (setq enclosed-text (s-replace "?" text template))
  (insert enclosed-text)
  (backward-char (- (length enclosed-text) (length (s-shared-start 
enclosed-text template))

  (defun ap/org-change-block-types ()
"Change the type of org-mode block at point, or blocks in region."
(interactive)
(if (use-region-p)
(progn
  (deactivate-mark)
  (goto-char (region-beginning))
  (while (re-search-forward  "^ *#\\+BEGIN_" (region-end) nil)
(ap/org-change-block-type-at-point)))
  (ap/org-change-block-type-at-point)))

  (defun ap/org-change-block-type-at-point ()
"Change type of org-mode block at point."
(interactive)
(unless (ap/org-in-block-p)
  (error "Not in an org-mode block."))
(let* ((template (ap/org-read-structure-template))
   (case-fold-search t)
   (re "#\\+begin_\\(\\sw+\\)")
   (block-bounds (ap/org-block-boundaries))
   (block-beg (car 

[O] function for inserting a block

2017-09-02 Thread Eric Abrahamsen
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?

(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)))
   (goto-char s)
   (goto-char (line-beginning-position))
   (insert (format "#+BEGIN_%s\n" (upcase type)))
   (goto-char e)
   (goto-char (line-end-position))
   (insert (format "\n#+END_%s" (upcase type)

Other possibilities:

- A "close current block" command, a la `sgml-close-tag'. Inserts
  a #+END_FOO tag if needed.
- Provide completion for the block type prompt, though that might create
  some redundancy with `org-structure-template-alist'.

If this is acceptable, I'd like to bind it to "C-c i", and would provide
docs.

Eric