emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Import files to babel blocks
@ 2013-07-21  8:41 Michael Bach
  2013-07-21 10:31 ` Myles English
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Bach @ 2013-07-21  8:41 UTC (permalink / raw)
  To: emacs-orgmode

Dear org-mode Users and Developers,

I am trying to write a simple function for importing files to org-mode
babel code blocks.

My naive approach was

--8<---------------cut here---------------start------------->8---
(defun import-to-org-from-files (path pattern progmode)
  (mapcar #'(lambda (filepath)
              (progn
                (insert (format
                         "\n#+name: %s\n" (file-name-nondirectory
                                           (file-name-sans-extension
                                            filepath))))
                (insert (format "#+begin_src: %s :eval no\n" progmode))
                (insert-file-contents filepath)
                (insert "\n#+end_src\n"))
              )
          (directory-files path t pattern)))
--8<---------------cut here---------------end--------------->8---

to be called e.g. like this

--8<---------------cut here---------------start------------->8---
(import-to-org-from-files "/path/to/scripts/" ".*\.sh$" "sh")
--8<---------------cut here---------------end--------------->8---


This leads to all babel code blocks being inserted empty and all file
content inserted afterwards, i.e. the `insert-file-contents' is somehow
delayed until all `insert's have happened.

Maybe this is just related to my misunderstanding of the involved elisp,
but I am posting it here since I am sure someone must have thought about
this import. Or is there even an org-mode builtin that can be used?

Best Regards,
Michael

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

* Re: Import files to babel blocks
  2013-07-21  8:41 Import files to babel blocks Michael Bach
@ 2013-07-21 10:31 ` Myles English
  2013-07-24 23:00   ` Michael Bach
  0 siblings, 1 reply; 3+ messages in thread
From: Myles English @ 2013-07-21 10:31 UTC (permalink / raw)
  To: Michael Bach; +Cc: emacs-orgmode


Hi Michael,

Michael Bach writes:

> Dear org-mode Users and Developers,
>
> I am trying to write a simple function for importing files to org-mode
> babel code blocks.
>
> My naive approach was
>
> --8<---------------cut here---------------start------------->8---
> (defun import-to-org-from-files (path pattern progmode)
>   (mapcar #'(lambda (filepath)
>               (progn
>                 (insert (format
>                          "\n#+name: %s\n" (file-name-nondirectory
>                                            (file-name-sans-extension
>                                             filepath))))
>                 (insert (format "#+begin_src: %s :eval no\n" progmode))
>                 (insert-file-contents filepath)

From the documentation (C-h f insert-file-contents), it says that it
"returns list of absolute file name and number of characters" but
doesn't move the marker forwards, unlike insert does.  So, I guess you
would have to then move the marker forwards by the number of characters.

>                 (insert "\n#+end_src\n"))
>               )
>           (directory-files path t pattern)))
> --8<---------------cut here---------------end--------------->8---

Myles

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

* Re: Import files to babel blocks
  2013-07-21 10:31 ` Myles English
@ 2013-07-24 23:00   ` Michael Bach
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Bach @ 2013-07-24 23:00 UTC (permalink / raw)
  To: Myles English; +Cc: emacs-orgmode

Hi Myles,

On 7/21/13 12:31 PM, Myles English wrote:
> 
> From the documentation (C-h f insert-file-contents), it says that it
> "returns list of absolute file name and number of characters" but
> doesn't move the marker forwards, unlike insert does.  So, I guess you
> would have to then move the marker forwards by the number of characters.
> 

Erhm, another case of readthedocs... Thanks for the shove! For
reference, I got it to work using a simple let like so:

--8<---------------cut here---------------start------------->8---
(defun import-to-org-from-files (path pattern progmode)
    (mapcar #'(lambda (filepath)
                (progn
                  (insert (format
                           "\n#+name: %s\n" (file-name-nondirectory
                                             (file-name-sans-extension
                                              filepath))))
                  (insert (format "#+begin_src %s :eval no\n" progmode))
                  (let ((res (insert-file-contents filepath)))
                    (forward-char (second res)))
                  (insert "\n#+end_src\n"))
                )
            (directory-files path t pattern)))
--8<---------------cut here---------------end--------------->8---

much obliged,
Michael

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

end of thread, other threads:[~2013-07-24 23:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-21  8:41 Import files to babel blocks Michael Bach
2013-07-21 10:31 ` Myles English
2013-07-24 23:00   ` Michael Bach

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