emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: "ZHUO QL (KDr2)" <zhuoql@yahoo.com>
Cc: Eric S Fraga <esflists@gmail.com>,
	Emacs-org List <emacs-orgmode@gnu.org>
Subject: Re: Proposal: references from code to text.
Date: Wed, 16 May 2018 20:10:57 -0700	[thread overview]
Message-ID: <CAJ51ETorpicBZu_pir5PZzhFTWrAEcEeadPkWcQ6a=-Bzo_S0g@mail.gmail.com> (raw)
In-Reply-To: <458799504.2216018.1526525935772@mail.yahoo.com>

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

I believe you can do that with the Library of Babel (
https://orgmode.org/worg/org-contrib/babel/library-of-babel.html)

John

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


On Wed, May 16, 2018 at 7:58 PM, ZHUO QL (KDr2) <zhuoql@yahoo.com> wrote:

>
> 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?
>
> Greetings.
>
> ZHUO QL (KDr2, http://kdr2.com)
>
>
>
> On Wednesday, May 16, 2018, 10:26:41 PM GMT+8, John Kitchin <
> jkitchin@andrew.cmu.edu> wrote:
>
>
> * Main document
>
> See [[id:BAD97113-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 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="add-options"
> (let ((el (org-element-map (org-element-parse-buffer)
> org-element-all-elements
>     (lambda (el)
>       (when (string= (org-element-property :name el) name)
> el))
>     nil t)))
>   (let ((s (buffer-substring (org-element-property :contents-begin el)
>      (org-element-property :contents-end el))))
>     (replace-regexp-in-string "\\\"" "\\\\\"" 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 add (x y)
>   "One line description of adding X and Y.
>   <<get-string("DOC-OF-ADD")>> ;; code to code
>   <<get-string("add-options")>>"
>   ;; [[id:BAD97113-3561-4A4A-BA07-0CD5BF6BA35F][There is a reason we only
> support two args]] code to text
>   (+ x y) (ref:add)
>   ;; 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.
>   )
> #+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.
>    ;; code to code
>   - one
>   - two
>   - three
>   - and of course \"optional things\"
>   "
>   ;; [[id:BAD97113-3561-4A4A-BA07-0CD5BF6BA35F][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 coderef: add when you click on the link.
>   )
>
> #+END_SRC
>
>
> ** Notes
>
> *** There is a reason we only support two args
>     :PROPERTIES:
>     :ID:       BAD97113-3561-4A4A-BA07-0CD5BF6BA35F
>     :END:
>
>
> John
>
> -----------------------------------
> 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
>
>
> 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 will try to
> implement it, but I'm not very familiar with the code base and don't have
> much time, so I can't give any commitment. But once I have anything to
> show, I'll be back here  :)
>
> Greetings.
>
> ZHUO QL (KDr2, http://kdr2.com)
>
>
>
> On Tuesday, May 15, 2018, 6:05:31 PM GMT+8, Eric S Fraga <
> esflists@gmail.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 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.  This allows 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
>
>
>

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

  reply	other threads:[~2018-05-17  3:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <800808596.465327.1526131788117.ref@mail.yahoo.com>
2018-05-12 13:29 ` Proposal: references from code to text ZHUO QL (KDr2)
2018-05-14  5:48   ` Eric S Fraga
2018-05-14 15:05     ` John Kitchin
2018-05-14 17:06       ` Eric S Fraga
2018-05-14 17:11       ` Eric S Fraga
2018-05-15  4:49       ` ZHUO QL (KDr2)
2018-05-15 10:04         ` Eric S Fraga
2018-05-16  7:04           ` ZHUO QL (KDr2)
2018-05-16 14:25             ` John Kitchin
2018-05-16 19:37               ` Samuel Wales
2018-05-17  2:29                 ` John Kitchin
2018-05-17  2:40                   ` Samuel Wales
2018-05-17  2:58               ` ZHUO QL (KDr2)
2018-05-17  3:10                 ` John Kitchin [this message]
2018-05-17  9:33                   ` ZHUO QL (KDr2)
2018-06-15 21:44                     ` John Kitchin
2018-06-15 18:07               ` Grant Rettke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJ51ETorpicBZu_pir5PZzhFTWrAEcEeadPkWcQ6a=-Bzo_S0g@mail.gmail.com' \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=esflists@gmail.com \
    --cc=zhuoql@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).