From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Eric Schulte" Subject: Re: Re: Babel, Python and UTF-8 Date: Fri, 03 Dec 2010 07:30:51 -0700 Message-ID: <8739qf2ade.fsf@gmail.com> References: <87fwuhas6t.fsf@gmail.com> <80bp54a693.fsf@missioncriticalit.com> <87hbew7yvi.fsf@gmail.com> <80k4jsszq4.fsf@missioncriticalit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=49457 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POWii-0000ha-Cy for emacs-orgmode@gnu.org; Fri, 03 Dec 2010 09:34:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1POWig-0001jz-Sm for emacs-orgmode@gnu.org; Fri, 03 Dec 2010 09:34:44 -0500 Received: from mail-yw0-f41.google.com ([209.85.213.41]:41842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1POWig-0001hA-N9 for emacs-orgmode@gnu.org; Fri, 03 Dec 2010 09:34:42 -0500 Received: by mail-yw0-f41.google.com with SMTP id 9so667961ywk.0 for ; Fri, 03 Dec 2010 06:34:42 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Vincent Beffara Cc: emacs-orgmode@gnu.org Hi, Vincent Beffara writes: > Hi, > > "preamble" sounds fine As this seems to be the general consensus I've replaced "prefix" with "preamble" as the header argument name > (and it would be excellent to allow for a code block as a preamble, > instead of a string in the header or as an alternative, because > preambles once they are allowed tend to grow uncontrollably ;->) > This is currently possible using the `sbe' function. Arbitrary emacs lisp can be placed inside of header arguments, and the `sbe' take the name of a code block and returns its result. e.g. ** Python requires a utf-8 coding prefix #+source: my-preamble #+begin_src python :return preamble preamble =3D "# -*- coding: utf-8 -*-" #+end_src #+begin_src python :preamble (sbe my-preamble) :return s s =3D "=C3=A9" #+end_src #+results: : =C3=A9 #+begin_src python :preamble # -*- coding: utf-8 -*- :results output s =3D "=C3=A9" print(s) #+end_src #+results: : =C3=A9 > >>> The :shebang header argument is only used for tangling, not during >>> evaluation, however if you ever needed to tangle python code blocks with >>> non-ASCII encodings this would be the appropriate solution. >> >> Sorry, reading this after the other post... >> >> But, does it still make sense to make that distinction: >> >> - some sort of preamble only for tangling? >> - a preamble for evaluation (and tangling)? > > One naive question : why is the code path different for tangling and > evaluation ? One would think that a natural way for evaluation would be > to tangle the current block (plus included noweb stuff etc) into a > temporary file and eval that file ... and that would enable shebang for > evaluation as well. There must be something I am missing here. > Tangling works for *any* programming language, even those which have yet to be created and have no explicit Emacs or Org-mode support, this is because on tangling the code block is simply treated as text. Evaluation requires language specific support for both performing the evaluation and collecting the result. Evaluation can (for example) be performed using an external file or using a persistent session. Even when using an external file the contents of the file are often very different when tangling and exporting, e.g. #+source: my-preamble #+begin_src python :return preamble preamble =3D "# -*- coding: utf-8 -*-" #+end_src tangles to the following (possibly with comments) --8<---------------cut here---------------start------------->8--- preamble =3D "# -*- coding: utf-8 -*-" --8<---------------cut here---------------end--------------->8--- and is evaluated as the following --8<---------------cut here---------------start------------->8--- def main(): preamble =3D "# -*- coding: utf-8 -*-" return preamble open('temporary-result-collection-file', 'w').write( pprint.pformat(main())= ) --8<---------------cut here---------------end--------------->8--- Best -- Eric > > /v > > > _______________________________________________ > Emacs-orgmode mailing list > Please use `Reply All' to send replies to the list. > Emacs-orgmode@gnu.org > http://lists.gnu.org/mailman/listinfo/emacs-orgmode