Add new org-insert-source-block' to org-mode-map'

This commit is contained in:
Gerard Vermeulen 2024-01-21 17:27:22 +01:00
parent 2ff850ef72
commit dee56252fc

View File

@ -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" <theonewiththeevillook@yahoo.fr>
@ -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