From mboxrd@z Thu Jan 1 00:00:00 1970 From: James Harkins Subject: Re: Extract source code /with/ captions Date: Sat, 18 Jan 2014 03:04:09 +0000 (UTC) Message-ID: References: <3cbde2bd-2dba-4c2f-85b7-62fd2a9daffc@dewdrop-world.net> <87ob3fn75e.fsf@alphaville.bos.redhat.com> <87d2jvn4m3.fsf@alphaville.bos.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W4MDC-0003Wj-5S for emacs-orgmode@gnu.org; Fri, 17 Jan 2014 22:04:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W4MD4-00021t-S1 for emacs-orgmode@gnu.org; Fri, 17 Jan 2014 22:04:42 -0500 Received: from plane.gmane.org ([80.91.229.3]:41307) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W4MD4-00021F-LW for emacs-orgmode@gnu.org; Fri, 17 Jan 2014 22:04:34 -0500 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1W4MD3-0000oy-0r for emacs-orgmode@gnu.org; Sat, 18 Jan 2014 04:04:33 +0100 Received: from mail.klingt.org ([86.59.21.178]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Jan 2014 04:04:33 +0100 Received: from jamshark70 by mail.klingt.org with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Jan 2014 04:04:33 +0100 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.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)))