emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Including source code *from* a file in #+BEGIN_SRC blocks
@ 2011-12-11  5:56 Sankalp
  2011-12-11  6:53 ` Puneeth Chaganti
  2011-12-11 16:00 ` Eric Schulte
  0 siblings, 2 replies; 6+ messages in thread
From: Sankalp @ 2011-12-11  5:56 UTC (permalink / raw)
  To: emacs-orgmode

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

Hi,
I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
display source code.

I was wondering if there's a way to include code from an external file.
Like the *\lstinputlisting* feature in the LaTeX *listings* package.

I have a source code file that keeps getting modified, and each time I
publish my project to the web (htmlize), I'd like to have the latest
version of it in my source code block instead of having to copy-paste its
contents each time.

-------
Sankalp

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

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

* Re: Including source code *from* a file in #+BEGIN_SRC blocks
  2011-12-11  5:56 Including source code *from* a file in #+BEGIN_SRC blocks Sankalp
@ 2011-12-11  6:53 ` Puneeth Chaganti
  2011-12-11 16:00 ` Eric Schulte
  1 sibling, 0 replies; 6+ messages in thread
From: Puneeth Chaganti @ 2011-12-11  6:53 UTC (permalink / raw)
  To: Sankalp; +Cc: emacs-orgmode

On 12/11/11, Sankalp <sankalpkhare@gmail.com> wrote:
> Hi,
> I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
> display source code.
>
> I was wondering if there's a way to include code from an external file.
> Like the *\lstinputlisting* feature in the LaTeX *listings* package.

You could use the #+INCLUDE directive.
http://orgmode.org/manual/Include-files.html

HTH,
Puneeth

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

* Re: Including source code *from* a file in #+BEGIN_SRC blocks
  2011-12-11  5:56 Including source code *from* a file in #+BEGIN_SRC blocks Sankalp
  2011-12-11  6:53 ` Puneeth Chaganti
@ 2011-12-11 16:00 ` Eric Schulte
  2011-12-11 17:08   ` Sankalp
  2011-12-11 19:22   ` Nick Dokos
  1 sibling, 2 replies; 6+ messages in thread
From: Eric Schulte @ 2011-12-11 16:00 UTC (permalink / raw)
  To: Sankalp; +Cc: emacs-orgmode

Sankalp <sankalpkhare@gmail.com> writes:

> Hi,
> I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
> display source code.
>
> I was wondering if there's a way to include code from an external file.
> Like the *\lstinputlisting* feature in the LaTeX *listings* package.
>

You could write a code block which when executed wraps the file's
contents in a code block which is then inserted into the Org-mode
buffer.  For example if your external file is named foo.c and holds C
code the following

#+begin_src sh :exports results :results raw output
  echo "#+BEGIN_SRC C"
  cat foo.c
  echo "#+END_SRC"
#+end_src

will insert its contents into the buffer during every export.

Best -- Eric

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

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

* Re: Including source code *from* a file in #+BEGIN_SRC blocks
  2011-12-11 16:00 ` Eric Schulte
@ 2011-12-11 17:08   ` Sankalp
  2011-12-11 19:22   ` Nick Dokos
  1 sibling, 0 replies; 6+ messages in thread
From: Sankalp @ 2011-12-11 17:08 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

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

On 11 December 2011 21:30, Eric Schulte <eric.schulte@gmx.com> wrote:

> Sankalp <sankalpkhare@gmail.com> writes:
>
> > Hi,
> > I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
> > display source code.
> >
> > I was wondering if there's a way to include code from an external file.
> > Like the *\lstinputlisting* feature in the LaTeX *listings* package.
> >
>
> You could write a code block which when executed wraps the file's
> contents in a code block which is then inserted into the Org-mode
> buffer.  For example if your external file is named foo.c and holds C
> code the following
>
> #+begin_src sh :exports results :results raw output
>  echo "#+BEGIN_SRC C"
>  cat foo.c
>  echo "#+END_SRC"
> #+end_src
>
> will insert its contents into the buffer during every export.
>
> Best -- Eric
>
> --
> Eric Schulte
> http://cs.unm.edu/~eschulte/
>

Thanks a lot Eric, though I got it working with what Puneeth suggested
earlier :)

--
Sankalp

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

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

* Re: Including source code *from* a file in #+BEGIN_SRC blocks
  2011-12-11 16:00 ` Eric Schulte
  2011-12-11 17:08   ` Sankalp
@ 2011-12-11 19:22   ` Nick Dokos
  2011-12-11 19:25     ` Eric Schulte
  1 sibling, 1 reply; 6+ messages in thread
From: Nick Dokos @ 2011-12-11 19:22 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Sankalp, nicholas.dokos, emacs-orgmode

Eric Schulte <eric.schulte@gmx.com> wrote:

> Sankalp <sankalpkhare@gmail.com> writes:
> 
> > Hi,
> > I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
> > display source code.
> >
> > I was wondering if there's a way to include code from an external file.
> > Like the *\lstinputlisting* feature in the LaTeX *listings* package.
> >
> 
> You could write a code block which when executed wraps the file's
> contents in a code block which is then inserted into the Org-mode
> buffer.  For example if your external file is named foo.c and holds C
> code the following
> 
> #+begin_src sh :exports results :results raw output
>   echo "#+BEGIN_SRC C"
>   cat foo.c
>   echo "#+END_SRC"
> #+end_src
> 
> will insert its contents into the buffer during every export.
> 

That might be necessary in more complex situations, but in the OP's case
(at least, to the extent that I understand it), Puneeth's suggestion of

#+begin_src c :exports code
#+include: foo.c
#+end_src

seems to work fine.

Nick

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

* Re: Including source code *from* a file in #+BEGIN_SRC blocks
  2011-12-11 19:22   ` Nick Dokos
@ 2011-12-11 19:25     ` Eric Schulte
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Schulte @ 2011-12-11 19:25 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: Sankalp, emacs-orgmode

Nick Dokos <nicholas.dokos@hp.com> writes:

> Eric Schulte <eric.schulte@gmx.com> wrote:
>
>> Sankalp <sankalpkhare@gmail.com> writes:
>> 
>> > Hi,
>> > I've been using org mode's #+BEGIN_SRC ... <code> #+END_SRC feature to
>> > display source code.
>> >
>> > I was wondering if there's a way to include code from an external file.
>> > Like the *\lstinputlisting* feature in the LaTeX *listings* package.
>> >
>> 
>> You could write a code block which when executed wraps the file's
>> contents in a code block which is then inserted into the Org-mode
>> buffer.  For example if your external file is named foo.c and holds C
>> code the following
>> 
>> #+begin_src sh :exports results :results raw output
>>   echo "#+BEGIN_SRC C"
>>   cat foo.c
>>   echo "#+END_SRC"
>> #+end_src
>> 
>> will insert its contents into the buffer during every export.
>> 
>
> That might be necessary in more complex situations, but in the OP's case
> (at least, to the extent that I understand it), Puneeth's suggestion of
>
> #+begin_src c :exports code
> #+include: foo.c
> #+end_src
>

Oh, I didn't notice the code block surrounding the include call in
Puneeth's suggestion.  I didn't realize include working inside of
blocks, very cool.  I agree that is preferable to my code block
execution base solution.

Best,

>
> seems to work fine.
>
> Nick
>

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

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

end of thread, other threads:[~2011-12-11 19:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-11  5:56 Including source code *from* a file in #+BEGIN_SRC blocks Sankalp
2011-12-11  6:53 ` Puneeth Chaganti
2011-12-11 16:00 ` Eric Schulte
2011-12-11 17:08   ` Sankalp
2011-12-11 19:22   ` Nick Dokos
2011-12-11 19:25     ` 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).