emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: James Harkins <jamshark70@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Extract source code /with/ captions
Date: Sat, 18 Jan 2014 03:04:09 +0000 (UTC)	[thread overview]
Message-ID: <loom.20140118T040247-329@post.gmane.org> (raw)
In-Reply-To: loom.20140118T031939-696@post.gmane.org

For posterity, this is doing what I want.

It's with some shock that I note, I'm actually starting to understand LISP.

hjh


(defun hjh-get-string-from-nested-thing (thing)
  "Peel off 'car's from a nested list until the car is a string."
  (while (and thing (not (stringp thing)))
    (setq thing (car thing)))
  thing
)

(defun hjh-src-blocks-to-string ()
  "Iterate src blocks from org-element and add them to a string."
  (interactive)
  (let ((tree (org-element-parse-buffer))
	(string "")
	(counter 0))
    (org-element-map tree 'src-block
      (lambda (element)
	(setq element (car (cdr element)))
	(let ((caption (hjh-get-string-from-nested-thing (plist-get element :caption)))
	      (source (hjh-get-string-from-nested-thing (plist-get element :value))))
	  (when caption
	    (setq counter (1+ counter))
	    (setq string (concat string (format "/*********
Listing %d. %s
 *********/

%s\n\n"
					  counter
					  (substring-no-properties caption)
					  (substring-no-properties source))))))))
    string))

(defun hjh-src-blocks-to-buffer ()
  "Put all the captioned source blocks from a buffer into another buffer."
  (interactive)
  (let* ((contents (hjh-src-blocks-to-string))
	 (bufpath (buffer-file-name))
	 (newname (concat (file-name-sans-extension bufpath) ".scd"))
	 (bufname (file-name-nondirectory newname))
	 (newbuf (get-buffer-create bufname)))
    (with-current-buffer newbuf
      (erase-buffer)
      (insert contents)
      (set-visited-file-name newname))
    (switch-to-buffer-other-window newbuf)))

  reply	other threads:[~2014-01-18  3:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-12 14:49 Extract source code /with/ captions James Harkins
2014-01-12 17:19 ` John Kitchin
2014-01-13  2:52   ` James Harkins
2014-01-13 17:06     ` Nick Dokos
2014-01-13 17:46       ` Nick Dokos
2014-01-13 18:01       ` Nick Dokos
2014-01-18  2:20         ` James Harkins
2014-01-18  3:04           ` James Harkins [this message]
2014-01-12 18:34 ` Charles Berry

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=loom.20140118T040247-329@post.gmane.org \
    --to=jamshark70@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).