Make a keybinding that adds CLI arguments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
(defun org-babel-change-block-type ()
  (interactive)
  (if (or (org-in-src-block-p)
          (org-in-block-p '("src" "example" "verbatim" "clocktable")))
      (progn
        (call-interactively 'org-babel-raise)
        (call-interactively 'hydra-org-template/body))
    (self-insert-command 1)))

(define-key org-mode-map (kbd "M-.") 'org-babel-change-block-type)
1
(org-babel-insert-header-arg "args" (read "arguments:"))
1
2
3
4
5
6
(defun org-babel-add-src-args ()
  (interactive)
  (if (org-in-src-block-p)
      (org-babel-insert-header-arg "args" (read-string "arguments:"))))

(define-key org-mode-map (kbd "M-@") 'org-babel-add-src-args)

asciinema recording

Make a keybinding that adds previous block as stdin

Get previous src block name

1
(org-babel-previous-src-block &optional ARG)
1
2
3
4
(defun org-babel-previous-src-name ()
  (save-excursion
    (org-babel-previous-src-block 1)
    (org-element-property :name (org-element-at-point))))

How to get the name of the current block?

1
(org-babel-get-src-block-info)
1
(org-element-property :name (org-element-at-point))

Get previous results block name

1
2
3
4
(defun org-babel-previous-results-name ()
  (save-excursion
    (org-babel-previous-src-block 1)
    (car (org-element-property :results (org-element-at-point)))))

Get previous block name

1
2
3
(defun org-babel-previous-results-name ()
  (or (org-babel-previous-block-name)
      (org-babel-previous-results-name)))

Putting it all together

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(defun org-babel-add-src-args ()
  (interactive)
  (if (org-in-src-block-p)
      (org-babel-insert-header-arg "args" (read-string "arguments:"))))

(define-key org-mode-map (kbd "M-@") 'org-babel-add-src-args)


(defun org-babel-previous-src-name ()
  (save-excursion
    (org-babel-previous-src-block 1)
    (org-element-property :name (org-element-at-point))))

(defun org-babel-previous-results-name ()
  (save-excursion
    (org-babel-previous-src-block 1)
    (car (org-element-property :results (org-element-at-point)))))

;; (defun org-babel-previous-block-name ()
;;   (or (org-babel-previous-block-name)
;;       (org-babel-previous-results-name)))

(defun org-babel-add-stdin-arg-for-previous-block ()
  (interactive)
  (if (org-in-src-block-p)
      (let ((prevsrc (org-babel-previous-src-name))
            (prevres (org-babel-previous-results-name)))
        (if prevres
            (org-babel-insert-header-arg "inr" prevres)
          (if prevsrc
              (org-babel-insert-header-arg "inb" prevsrc))))))

(define-key org-mode-map (kbd "M-!") 'org-babel-add-stdin-arg-for-previous-block)

Demonstration

asciinema recording