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

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
           (character :tag "Key")
           (string :tag "Template")))
  :version "26.1"
  :package-version '(Org . "8.3"))

(defun org-insert-structure-template (&optional 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 <TAB>, 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."
  (let* ((key (read-key "Key: "))
          (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)
          (insert (format "%s: " struct-string))
          (when (string= "#+INCLUDE" struct-string)
             (format "\"%s\""
                      (read-file-name "Include file: "))))))
      (let ((s (if (use-region-p)
            (e (copy-marker (if (use-region-p)
        (when (string-match-p
               (concat "\\`"
                       (regexp-opt '("example" "export" "src")))
          (org-escape-code-in-region s e))
        (goto-char s)
        (setq column (current-indentation))
        (indent-to column)
        (insert (format "#+BEGIN_%s\n" struct-string))
        (goto-char e)
        (if (bolp)
              (skip-chars-backward " \n\t")
          (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)
        (set-marker e nil)))))

Reply via email to