emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* (Maybe) refactor `org-babel-insert-header-arg'?
@ 2014-08-05 16:22 Thorsten Jolitz
  0 siblings, 0 replies; only message in thread
From: Thorsten Jolitz @ 2014-08-05 16:22 UTC (permalink / raw)
  To: emacs-orgmode


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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-08-05 16:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-05 16:22 (Maybe) refactor `org-babel-insert-header-arg'? Thorsten Jolitz

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).