[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[O] (Maybe) refactor `org-babel-insert-header-arg'?
From: |
Thorsten Jolitz |
Subject: |
[O] (Maybe) refactor `org-babel-insert-header-arg'? |
Date: |
Tue, 05 Aug 2014 18:22:22 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Hi,
maybe make parts of `org-babel-insert-header-arg' reusable by a
refactoring like this?
;; refactored org-babel-insert-header-arg (part 1)
#+begin_src emacs-lisp
(defun tj/prompt-for-header-arg (&optional lang)
"Prompt for header argument selecting from lists of common args and values.
Optional argument LANG specifies the Org Babel language."
(interactive)
(let* ((curr-block (ignore-errors
(org-babel-goto-src-block-head)
(org-element-src-block-parser
(point-max) nil)))
(lang (or lang
(condition-case err
(org-element-property :language curr-block)
(error "Cannot determine language"))))
(lang-headers (intern
(concat "org-babel-header-args:" lang)))
(headers (org-babel-combine-header-arg-lists
org-babel-common-header-args-w-values
(when (boundp lang-headers)
(eval lang-headers))))
(header-arg (org-icompleting-read
"Header Arg: "
(mapcar
(lambda (header-spec)
(symbol-name (car header-spec)))
headers)))
(vals (cdr (assoc (intern header-arg) headers)))
(value (cond
((eq vals :any)
(read-from-minibuffer "value: "))
((listp vals)
(mapconcat
(lambda (group)
(let ((arg (org-icompleting-read
"Value: "
(cons "default"
(mapcar #'symbol-name
group)))))
(if (and arg
(not (string= "default" arg)))
(concat arg " ")
"")))
vals "")))))
(cons header-arg value)))
#+end_src
;; refactored org-babel-insert-header-arg (part 2)
#+begin_src emacs-lisp
(defun tj/insert-header-arg (header-arg &optional value keyword-p)
"Insert HEADER-ARG with (optional) VALUE on header line.
If optional arg KEYWORD-P is non-nil, insert as separate
'#+header:' line on top of src-block."
(when (ignore-errors (org-babel-goto-src-block-head))
(if keyword-p
(progn
(newline)
(forward-line -1)
(insert (format "#+header: :%s%s"
header-arg
(if value (concat " " value) ""))))
(save-excursion
(goto-char (point-at-eol))
(unless (= (char-before (point)) ?\ ) (insert " "))
(insert ":" header-arg)
(when value (insert " " value))))))
#+end_src
--
cheers,
Thorsten
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [O] (Maybe) refactor `org-babel-insert-header-arg'?,
Thorsten Jolitz <=