From mboxrd@z Thu Jan 1 00:00:00 1970 From: "ZHUO QL (KDr2)" Subject: Re: Proposal: references from code to text. Date: Thu, 17 May 2018 02:58:55 +0000 (UTC) Message-ID: <458799504.2216018.1526525935772@mail.yahoo.com> References: <800808596.465327.1526131788117.ref@mail.yahoo.com> <800808596.465327.1526131788117@mail.yahoo.com> <871seebxdt.fsf@gmail.com> <385386647.1304827.1526359774932@mail.yahoo.com> <87wow5s0a4.fsf@gmail.com> <408787171.1793110.1526454275347@mail.yahoo.com> Reply-To: "ZHUO QL \(KDr2\)" Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2216017_990791109.1526525935765" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55747) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fJ98A-00025j-L3 for emacs-orgmode@gnu.org; Wed, 16 May 2018 22:59:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fJ987-0004H3-Hm for emacs-orgmode@gnu.org; Wed, 16 May 2018 22:59:02 -0400 Received: from sonic304-23.consmr.mail.gq1.yahoo.com ([98.137.68.204]:36777) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fJ987-0004Gs-6Q for emacs-orgmode@gnu.org; Wed, 16 May 2018 22:58:59 -0400 In-Reply-To: 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" To: John Kitchin Cc: Eric S Fraga , Emacs-org List ------=_Part_2216017_990791109.1526525935765 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hooray! Do we have a way to make that piece of code a predefined and callable code = block that can be seen and called in any Org file?=C2=A0 Greetings. ZHUO QL (KDr2, http://kdr2.com) =20 On Wednesday, May 16, 2018, 10:26:41 PM GMT+8, John Kitchin wrote: =20 =20 * Main document See [[id:BAD97113-3561-4A4A-BA07-0CD5BF6BA35F][There is a reason we only su= pport two args]] (text to text) for notes about this function. The addition= is done in line [[(add)]] (text to code). Here we put names on different kinds of elements so we can put them into a = tangled file later. #+NAME: DOC-OF-ADDWe use the function add to calculate the sum of two numbe= rs. #+NAME: add-options- one- two- three- and of course "optional things" We use a block like this to get the contents of an org-element by name as a= string, and possibly transform it some how, e.g. in this case I escape quo= tes. I guess you could also use an exporter to convert it to what ever form= you want. You might bury this block at the end in an appendix so it isn't = in the middle of your document like this. #+name: get-string#+BEGIN_SRC emacs-lisp :var name=3D"add-options"(let ((el= (org-element-map (org-element-parse-buffer) org-element-all-elements =C2= =A0 =C2=A0 (lambda (el) =C2=A0 =C2=A0 =C2=A0 (when (string=3D (org-element-= property :name el) name) el)) =C2=A0 =C2=A0 nil t)))=C2=A0 (let ((s (buffer= -substring (org-element-property :contents-begin el) =C2=A0 =C2=A0 =C2=A0(o= rg-element-property :contents-end el))))=C2=A0 =C2=A0 (replace-regexp-in-st= ring "\\\"" "\\\\\"" s)))#+END_SRC Now, we can use those elements in a src-block like this. #+NAME: ADD#+BEGIN_SRC emacs-lisp -n -r :noweb yes :tangle test.el(defun ad= d (x y)=C2=A0 "One line description of adding X and Y.=C2=A0 <> ;; code to code=C2=A0 <>"=C2=A0 ;= ; [[id:BAD97113-3561-4A4A-BA07-0CD5BF6BA35F][There is a reason we only supp= ort two args]] code to text=C2=A0 (+ x y) (ref:add)=C2=A0 ;; it appears the= coderef needs to be on it's own line, otherwise you get a org-link-search:= No match for coderef: add when you click on the link.=C2=A0 )#+END_SRC #+RESULTS: ADD: add The above block tangles to this for me: #+BEGIN_SRC emacs-lisp(defun add (x y)=C2=A0 "One line description of addin= g X and Y.=C2=A0 We use the function add to calculate the sum of two number= s.=C2=A0 =C2=A0;; code to code=C2=A0 - one=C2=A0 - two=C2=A0 - three=C2=A0 = - and of course \"optional things\"=C2=A0 "=C2=A0 ;; [[id:BAD97113-3561-4A4= A-BA07-0CD5BF6BA35F][There is a reason we only support two args]] code to t= ext=C2=A0 (+ x y)=C2=A0=C2=A0 ;; it appears the coderef needs to be on it's= own line, otherwise you get a org-link-search: No match for coderef: add w= hen you click on the link.=C2=A0 ) #+END_SRC ** Notes *** There is a reason we only support two args=C2=A0 =C2=A0 :PROPERTIES:=C2= =A0 =C2=A0 :ID:=C2=A0 =C2=A0 =C2=A0 =C2=A0BAD97113-3561-4A4A-BA07-0CD5BF6BA= 35F=C2=A0 =C2=A0 :END: John ----------------------------------- Professor John Kitchin=C2=A0 Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803@johnkitchin http://kitchingroup.cheme.cmu.edu On Wed, May 16, 2018 at 12:04 AM, ZHUO QL (KDr2) wrote: Oh, that makes sense. So in this way, we must put the text into quotes, and then into a txt code = block. And also we don't have a chance to do any transformation of the text= (e.g. org-mode list to=C2=A0javadoc format).=C2=A0=C2=A0 I think the direct reference from code to text is better. I will try to imp= lement=C2=A0it, but I'm not very familiar=C2=A0with the code base and don't= have much time, so I can't give any commitment. But once I have anything t= o show, I'll be back here=C2=A0 :) Greetings. ZHUO QL (KDr2, http://kdr2.com) =20 On Tuesday, May 15, 2018, 6:05:31 PM GMT+8, Eric S Fraga wrote:=20 =20 On Tuesday, 15 May 2018 at 04:49, ZHUO QL (KDr2) wrote: > 2. The bug Eric just found while putting a <> link within > quotes, although, it may be easy to fix.=C2=A0 I realised afterwards that it is not a bug but is a feature: org assumes that anything before the << start of the link should be repeated on each line of the incorporated src block.=C2=A0 This allows for easy incorporatio= n of code into a comment block, e.g. for C or sh etc. --=20 Eric S Fraga via Emacs 27.0.50, Org release_9.1.6-591-gee336b =20 =20 ------=_Part_2216017_990791109.1526525935765 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hooray!

Do we have a way to make that piece of code a predefin= ed and callable code block that can be seen and called in any Org file?&nbs= p;

Greetings.

ZHUO QL (KDr2, http://kdr2.com)



=20
=20
On Wednesday, May 16, 2018, 10:26:41 PM GMT+8, John= Kitchin <jkitchin@andrew.cmu.edu> wrote:


* Main document

See [[id:BAD9711= 3-3561-4A4A-BA07-0CD5BF6BA35F][There is a reason we only support two args]]= (text to text) for notes about this function. The addition is done in line= [[(add)]] (text to code).

Here we = put names on different kinds of elements so we can put them into a tangled = file later.

#+NAME: DOC-OF-ADD
We use the function add to calculate the sum of two numbers.

#+NAME: add-options
- one
- two
- three
- and of course "optional things"


We use a = block like this to get the contents of an org-element by name as a string, = and possibly transform it some how, e.g. in this case I escape quotes. I gu= ess you could also use an exporter to convert it to what ever form you want= . You might bury this block at the end in an appendix so it isn't in the mi= ddle of your document like this.

#+= name: get-string
#+BEGIN_SRC emacs-lisp :var name=3D"add-options"=
(let ((el (org-element-map (org-element-parse-buffer) org-elemen= t-all-elements
=09  =   (lambda (el)
=09&n= bsp;     (when (string=3D (org-element-property :name el) name)
=09=09el))
=09    nil t)))
&nbs= p; (let ((s (buffer-substring (org-element-property :contents-begin el)
=09=09=09     = ;(org-element-property :contents-end el))))
    (replac= e-regexp-in-string "\\\"" "\\\\\"" s)))
#+END_SRC


Now, we can use thos= e elements in a src-block like this.

#+NAME: ADD
#+BEGIN_SRC emacs-lisp -n -r :noweb yes :tangle tes= t.el
(defun add (x y)
  "One line description of a= dding X and Y.
  <<get-string("DOC-OF-ADD")>> ;;= code to code
  <<get-string("add-options")>>"
  ;; [[id:BAD97113-3561-4A4A-BA07-0CD5BF6BA35F][There is a re= ason we only support two args]] code to text
  (+ x y) (ref:= add)
  ;; it appears the coderef needs to be on it's own lin= e, otherwise you get a org-link-search: No match for coderef: add when you = click on the link.
  )
#+END_SRC

#+RESULTS: ADD
: add

The above block tangles to this for me:

#+BEGIN_SRC emacs-lisp
(defun add (x y)=
  "One line description of adding X and Y.
 = We use the function add to calculate the sum of two numbers.
&nb= sp;  ;; code to code
  - one
  - two
  - three
  - and of course \"optional things\"<= /div>
  "
  ;; [[id:BAD97113-3561-4A4A-BA07-0CD5BF6= BA35F][There is a reason we only support two args]] code to text
=   (+ x y) 
  ;; it appears the coderef needs to be= on it's own line, otherwise you get a org-link-search: No match for codere= f: add when you click on the link.
  )

#+END_SRC


** Notes

= *** There is a reason we only support two args
    :PRO= PERTIES:
    :ID:       BAD97113-35= 61-4A4A-BA07-0CD5BF6BA35F
    :END:


John

---------------= --------------------
Professor John Kitchin 
Doherty Hall A207F
Department of Chemical Engi= neering
Carnegie Mellon University
Pitt= sburgh, PA 15213
412-268-7803

On Wed, May 16, 2018= at 12:04 AM, ZHUO QL (KDr2) <zhuoql@yahoo.com> wrote:
Oh, that makes sense.

So in this= way, we must put the text into quotes, and then into a txt code block. And= also we don't have a chance to do any transformation of the text(e.g. org-= mode list to javadoc format).  

I think the direct reference from code to text is better. I wil= l try to implement it, but I'm not very familiar with the code ba= se and don't have much time, so I can't give any commitment. But once I hav= e anything to show, I'll be back here  :)

Greetings.

ZHUO QL (KDr2, http://kdr2.com= )



=20
=20
On Tuesday, May 15, 2018, 6:05:31 PM GMT+8, Eric S= Fraga <esflists@gmai= l.com> wrote:


On Tuesday, 15 May 2018 at 04:49,= ZHUO QL (KDr2) wrote:

> 2. The bug Eric just found while putting= a <> link within
> quotes, although, it may be = easy to fix. 


I realised af= terwards that it is not a bug but is a feature: org assumes
that anything before the << start of the link should be repeated o= n each
line of the incorporated src block.  This all= ows for easy incorporation
of code into a comment block, = e.g. for C or sh etc.

--
Eric S Fraga via Emacs 27.0.50, Org release_9.1.6-591-gee336b

------=_Part_2216017_990791109.1526525935765--