emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* user control of source block header line exporting formats
@ 2010-07-31 20:54 David O'Toole
  2010-08-27 15:13 ` Eric Schulte
  0 siblings, 1 reply; 2+ messages in thread
From: David O'Toole @ 2010-07-31 20:54 UTC (permalink / raw)
  To: emacs-orgmode Mailinglist

[-- Attachment #1: Type: text/plain, Size: 173 bytes --]

I've attached a diff of my apparently-working changes to allow better
control of the way ob-exp.el formats the name and arguments of source
code blocks.

What do you think?

[-- Attachment #2: caption-formatting.diff --]
[-- Type: text/x-patch, Size: 3700 bytes --]

diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 796812c..0c34431 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -206,42 +206,66 @@ The function respects the value of the :exports header argument."
 		     (org-babel-exp-results info type))))))
 
 (defvar backend)
+
+(defvar org-babel-format-code-functions
+  '(inline org-babel-format-code-inline
+    block org-babel-format-code-block
+    lob org-babel-format-code-lob))
+
+(defun org-babel-format-code-inline (lang body args switches name) 
+  (format "=%s=" body))
+
+(defvar org-babel-format-code-block-control-string "%s(%s)")
+
+(defun org-babel-format-code-block-default-style (info)
+  (destructuring-bind (lang body args0 switches name &rest ignore) info
+    (format org-babel-format-code-block-control-string
+	    name
+	    (mapconcat #'identity args ", "))))
+
+(defun org-babel-format-code-block-alternate-style (info)
+  (destructuring-bind (lang body args0 switches name &rest ignore) info
+    (format "SOURCE: %s ARGUMENTS: %s LANGUAGE: %s"
+	    name (if (null args) "None."
+		     (mapconcat #'identity args ", "))
+	    lang)))
+
+(defvar org-babel-format-code-block-caption-function
+  'org-babel-format-code-block-default-style) 
+
+(defun org-babel-format-code-block (lang body args switches name) 
+  (let ((str
+	 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
+		 (if (and body (string-match "\n$" body))
+		     "" "\n"))))
+    (when name
+      (add-text-properties
+       0 (length str)
+       (list 'org-caption
+	     (funcall org-babel-format-code-block-caption-function 
+		      (list lang body args switches name)))
+       str))
+    str))
+
+(defun org-babel-format-code-lob (lang body args switches name) 
+  (let ((call-line (and (string-match "results=" (car args))
+			(substring (car args) (match-end 0)))))
+    (cond
+      ((eq backend 'html)
+       (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
+	       call-line))
+      ((format ": %s\n" call-line)))))
+
 (defun org-babel-exp-code (info type)
   "Prepare and return code in the current code block for export.
-Code is prepared in a manner suitable for exportat by
+Code is prepared in a manner suitable for export by
 org-mode.  This function is called by `org-babel-exp-do-export'.
 The code block is not evaluated."
-  (let ((lang (nth 0 info))
-        (body (nth 1 info))
-        (switches (nth 3 info))
-        (name (nth 4 info))
-        (args (mapcar
-	       #'cdr
-	       (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
-    (case type
-      ('inline (format "=%s=" body))
-      ('block
-	  (let ((str
-		 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
-			 (if (and body (string-match "\n$" body))
-			     "" "\n"))))
-	    (when name
-	      (add-text-properties
-	       0 (length str)
-	       (list 'org-caption
-		     (format "%s(%s)"
-			     name
-			     (mapconcat #'identity args ", ")))
-	       str))
-	    str))
-      ('lob
-       (let ((call-line (and (string-match "results=" (car args))
-			     (substring (car args) (match-end 0)))))
-	 (cond
-	  ((eq backend 'html)
-	   (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
-		   call-line))
-	  ((format ": %s\n" call-line))))))))
+  (destructuring-bind (lang body args0 switches name &rest ignore) info
+    (let ((args (mapcar #'cdr
+			(org-remove-if-not (lambda (el) (eq :var (car el))) args0))))
+      (let ((formatter (getf org-babel-format-code-functions type)))
+	(funcall formatter lang body args switches name)))))
 
 (defun org-babel-exp-results (info type &optional silent)
   "Evaluate and return the results of the current code block for export.

[-- Attachment #3: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: user control of source block header line exporting formats
  2010-07-31 20:54 user control of source block header line exporting formats David O'Toole
@ 2010-08-27 15:13 ` Eric Schulte
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Schulte @ 2010-08-27 15:13 UTC (permalink / raw)
  To: David O'Toole; +Cc: emacs-orgmode Mailinglist

Hi David,

I agree we would like to allow more control over the appearance of
exported code blocks.  Since this patch admits better control over the
format of name and arguments of code blocks on export, could you include
a couple of example customizations along with their results?

I've just applied this patch to my system, and although it works in most
cases, it raises an error when exporting the following subtree to html

--8<---------------cut here---------------start------------->8---
** lob -- writing results out to files
#+source: table
#+begin_src emacs-lisp
  (mapcar
   (lambda (el) (number-sequence el (+ el 3)))
   (number-sequence 0 4))
#+end_src

writes the results out as csv file
#+call: write(data=table, file="~/Desktop/example.csv") :results silent

writes the results out as tab separated file
#+call: write(data=table, file="~/Desktop/example.tsv") :results silent

write the results out as a normal org-mode file
#+call: write(data=table, file="~/Desktop/example.org") :results silent
--8<---------------cut here---------------end--------------->8---

Cheers -- Eric

"David O'Toole" <dto1138@gmail.com> writes:

> I've attached a diff of my apparently-working changes to allow better
> control of the way ob-exp.el formats the name and arguments of source
> code blocks.
>
> What do you think?
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-08-27 15:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-31 20:54 user control of source block header line exporting formats David O'Toole
2010-08-27 15:13 ` Eric Schulte

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).