emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Thorsten Jolitz <tjolitz@gmail.com>
To: emacs-orgmode@gnu.org
Subject: (Maybe) refactor `org-babel-insert-header-arg'?
Date: Tue, 05 Aug 2014 18:22:22 +0200	[thread overview]
Message-ID: <87ppgevrcx.fsf@gmail.com> (raw)


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

                 reply	other threads:[~2014-08-05 16:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ppgevrcx.fsf@gmail.com \
    --to=tjolitz@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).