From dee56252fceab94060277ca47ec166a97f513fb4 Mon Sep 17 00:00:00 2001 From: Gerard Vermeulen Date: Sun, 21 Jan 2024 17:27:22 +0100 Subject: [PATCH] Add new `org-insert-source-block' to `org-mode-map' --- README.org | 57 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/README.org b/README.org index 7c10e96..2ca5e07 100644 --- a/README.org +++ b/README.org @@ -2304,8 +2304,8 @@ list detailing and motivating each listing: tangle-dir: ANY-PLACE~ near the beginning an Org file. 4. Listing [[lst:set-org-link-options]] handles [[https://orgmode.org/][Org-mode]] options specific to [[info:org#Hyperlinks][hyperlinks (info)]]. -5. Listing [[lst:setup-org-mode-map]] extends the =org-mode-map= with useful - key-bindings. +5. Listing [[lst:setup-org-mode-map-1]] and [[lst:setup-org-mode-map-2]] extend the + =org-mode-map= with useful key-bindings. 6. Listing [[lst:setup-org-src]] facilitates [[info:org#Editing Source Code][editing source code blocks]], and it provides functions to change the indentation of all =org-src-mode= blocks. 7. Listing [[lst:setup-ob-python]] allows to pretty-print Python session source @@ -2442,9 +2442,9 @@ May be changed by `toggle-post-tangle-hook-dir-usage'." org-link-file-path-type 'relative)) #+end_src -#+caption[Setup =org-mode-map=]: -#+caption: Setup =org-mode-map=. -#+name: lst:setup-org-mode-map +#+caption[Setup =org-mode-map= 1]: +#+caption: Setup =org-mode-map= 1. +#+name: lst:setup-org-mode-map-1 #+begin_src emacs-lisp -n (with-eval-after-load 'emacs ;; From: "Nicolas Richard" @@ -2487,6 +2487,53 @@ When called twice, replace the previously inserted \\(\\) by one $." (keymap-set org-mode-map "M-q" #'org-fill-paragraph))) #+end_src +#+caption[Setup =org-mode-map= 2]: +#+caption: Setup =org-mode-map= 2. +#+name: lst:setup-org-mode-map-2 +#+begin_src emacs-lisp :results silent +(with-eval-after-load 'emacs + ;; Stolen from `org-insert-structure-template'. + ;; Note: `org-tempo' does not require `tempo' at all. + (defcustom org-insert-source-block-default '("emacs-lisp -n :results silent" + "python -i -n :results silent" + "org") + "Default value for `org-insert-source-block' (`M-n' and `M-p')." + :group 'org) + + (defun org-insert-source-block () + "Insert a source block #+begin_src/SPEC/BODY/#+end_src. +Prompt for the source block SPEC. With an active region, the +region becomes the block BODY. Otherwise, insert an empty block." + (interactive) + (let* ((spec (read-string "Block spec: " nil nil + org-insert-source-block-default)) + (region? (use-region-p)) + (region-start (and region? (region-beginning))) + (region-end (and region? (copy-marker (region-end))))) + (when region? (goto-char region-start)) + (let ((column (current-indentation))) + (if (save-excursion (skip-chars-backward " \t") (bolp)) + (forward-line 0) + (insert "\n")) + (save-excursion + (indent-to column) + (insert (format "#+begin_src %s\n" spec)) + (when region? + (org-escape-code-in-region (point) region-end) + (goto-char region-end) + (skip-chars-backward " \n\t\r") + (end-of-line)) + (unless (bolp) (insert "\n")) + (indent-to column) + (insert "#+end_src") + (if (looking-at "[ \t]*$") (replace-match "") (insert "\n")) + (when (and (eobp) (not (bolp))) (insert "\n"))) + (end-of-line))))) + +(with-eval-after-load 'org + (keymap-set org-mode-map "C-c C-;" #'org-insert-source-block)) +#+end_src + #+caption[Setup =org-src=]: #+caption: Setup =org-src=. #+name: lst:setup-org-src