emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] function for inserting a block


From: Eric Abrahamsen
Subject: Re: [O] function for inserting a block
Date: Sat, 28 Oct 2017 15:27:24 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (gnu/linux)

Nicolas Goaziou <address@hidden> writes:

> Hello,
>
> Eric Abrahamsen <address@hidden> 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
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 (&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."
  (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




reply via email to

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