emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How does org mode write to a file?
@ 2015-11-21 17:27 Lawrence Bottorff
       [not found] ` <m2mvu7dxq8.fsf@andrew.cmu.edu>
  0 siblings, 1 reply; 2+ messages in thread
From: Lawrence Bottorff @ 2015-11-21 17:27 UTC (permalink / raw)
  To: emacs-orgmode Mailinglist

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

Excuse my elisp ignorance, but I've got this:

#+name: elisp-unordered-ast
#+begin_src emacs-lisp :file myastfile.ast
(org-element-parse-buffer)
#+end_src

which works fine. Question: Where is the elisp code that does this writing
of `org-element-parse-buffer` output to file? I'm stumped on how to do just
simple file read-write in elisp. It seems to only want to work with live
buffers. For example, how would I redirect `(org-element-parse-buffer)`
into, say,  `(append-to-file 1 (buffer-size) "test-ast.txt")`

LB

[-- Attachment #2: Type: text/html, Size: 672 bytes --]

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

* Re: How does org mode write to a file?
       [not found]   ` <CAFAhFSV21t2doDHLb+VNGJg4w1GdNKm9LvV8_jP=AXHfK1mNjA@mail.gmail.com>
@ 2015-11-21 21:22     ` John Kitchin
  0 siblings, 0 replies; 2+ messages in thread
From: John Kitchin @ 2015-11-21 21:22 UTC (permalink / raw)
  To: Lawrence Bottorff; +Cc: org-mode mailing list

Your method doesn't work because you parse an empty buffer (the temp
one).

Try:

(let ((data (org-element-parse-buffer)))
 (with-temp-file "test.txt"
   (insert (format "%s" data))))

or, if you like you could try:

(eval `(with-temp-file "test.txt"
   (insert (format "%s" (quote ,(org-element-parse-buffer))))))

Lawrence Bottorff writes:

> I tracked down the ob-core.el code that also used the `with-temp-file. . .`
> method. Yes, thanks, your code works, e.g.,
>
> (write-file "test2015-11-21T20:51:59.ast" (org-element-parse-buffer))
>
> but my stab in the dark didn't
>
> (with-temp-file "test.txt"
>   (insert (format "%s" (org-element-parse-buffer))))
>
> The file created only contains
>
> (org-data nil)
>
> Not sure why.
>
> BTW, thanks for your org-babel-load-file tip from before. However, it seems
> to tangle everything in the file, then does load-file. I do want the
> finer-grained control of LOB where I can specify which code blocks to make
> live. But then maybe I should just roll my own.. . . .
>
> LB
>
>
>
>
> On Sat, Nov 21, 2015 at 6:16 PM, John Kitchin <jkitchin@andrew.cmu.edu>
> wrote:
>
>> You do work with buffers, e.g.
>>
>> (defun write-file (fname data &optional append)
>>   "Open FNAME and write DATA to it. If APPEND is non-nil, append DATA to
>> the end of the file"
>>   (with-temp-file fname
>>     (when append
>>       (insert-file-contents fname)
>>       (goto-char (point-max))
>>       (insert "\n"))
>>     (insert (format "%s" data))))
>>
>> (defun read-file (fname)
>>   "Return the contents of FNAME in a string."
>>   (with-temp-buffer
>>     (insert-file-contents fname)
>>     (buffer-string)))
>>
>> (write-file "test-ast.txt" "test4" t)
>>
>> (read-file "test-ast.txt")
>>
>> I don't know a better way to do it.
>>
>> Lawrence Bottorff writes:
>>
>> > Excuse my elisp ignorance, but I've got this:
>> >
>> > #+name: elisp-unordered-ast
>> > #+begin_src emacs-lisp :file myastfile.ast
>> > (org-element-parse-buffer)
>> > #+end_src
>> >
>> > which works fine. Question: Where is the elisp code that does this
>> writing
>> > of `org-element-parse-buffer` output to file? I'm stumped on how to do
>> just
>> > simple file read-write in elisp. It seems to only want to work with live
>> > buffers. For example, how would I redirect `(org-element-parse-buffer)`
>> > into, say,  `(append-to-file 1 (buffer-size) "test-ast.txt")`
>> >
>> > LB
>>
>> --
>> Professor John Kitchin
>> Doherty Hall A207F
>> Department of Chemical Engineering
>> Carnegie Mellon University
>> Pittsburgh, PA 15213
>> 412-268-7803
>> @johnkitchin
>> http://kitchingroup.cheme.cmu.edu
>>

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

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

end of thread, other threads:[~2015-11-21 21:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-21 17:27 How does org mode write to a file? Lawrence Bottorff
     [not found] ` <m2mvu7dxq8.fsf@andrew.cmu.edu>
     [not found]   ` <CAFAhFSV21t2doDHLb+VNGJg4w1GdNKm9LvV8_jP=AXHfK1mNjA@mail.gmail.com>
2015-11-21 21:22     ` John Kitchin

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