From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Abrahamsen Subject: Re: function for inserting a block Date: Sat, 28 Oct 2017 15:27:24 -0700 Message-ID: <87she2rjqr.fsf@ericabrahamsen.net> References: <877exghblx.fsf@ericabrahamsen.net> <878tgmwwsa.fsf@ericabrahamsen.net> <87po9q2e8k.fsf@nicolasgoaziou.fr> <87tvyyvpst.fsf@ericabrahamsen.net> <87fuaiz069.fsf@nicolasgoaziou.fr> <87lgk9eo4d.fsf@ericabrahamsen.net> <87fuahxxvs.fsf@nicolasgoaziou.fr> <87r2u1cuwj.fsf@ericabrahamsen.net> <87infdctzq.fsf@ericabrahamsen.net> <87k1zsbizs.fsf@ericabrahamsen.net> <87k1zp4rxj.fsf@ericabrahamsen.net> <871slx4j6p.fsf@ericabrahamsen.net> <87r2ttoq5k.fsf@ericabrahamsen.net> <871sltst0t.fsf@nicolasgoaziou.fr> <87y3o14cul.fsf@ericabrahamsen.net> <87po9czqy8.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:32962) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e8ZbW-0007vY-DP for emacs-orgmode@gnu.org; Sat, 28 Oct 2017 18:29:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e8ZbS-0005Vw-0N for emacs-orgmode@gnu.org; Sat, 28 Oct 2017 18:29:22 -0400 Received: from [195.159.176.226] (port=41787 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e8ZbR-0005TJ-PC for emacs-orgmode@gnu.org; Sat, 28 Oct 2017 18:29:17 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1e8ZbF-00085F-0o for emacs-orgmode@gnu.org; Sun, 29 Oct 2017 00:29:05 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org 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 (&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 , 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