emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Grant Rettke <gcr@wisdomandwonder.com>
To: John Kitchin <jkitchin@andrew.cmu.edu>
Cc: "ZHUO QL (KDr2)" <zhuoql@yahoo.com>,
	Eric S Fraga <esflists@gmail.com>,
	Emacs-org List <emacs-orgmode@gnu.org>
Subject: Re: Proposal: references from code to text.
Date: Fri, 15 Jun 2018 13:07:13 -0500	[thread overview]
Message-ID: <CAAjq1md1mPFo3SvCh+6XSjvrDKtF+iEEx_a5edgtz7pZR_=DCw@mail.gmail.com> (raw)
In-Reply-To: <CAJ51EToGaTD18+x2DZZ7T6hxiVJVtvwsSUcyLCLaJ1C_BunxiA@mail.gmail.com>

On Wed, May 16, 2018 at 9:25 AM, John Kitchin <jkitchin@andrew.cmu.edu> wrote:
> #+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

This thread is great. Got me thinking about "true" one-time
documentation definitions in the file itself. The blog post laid it
all out very nicely.

It got me wondering if I (or anybody) would like something like this
style of use where you write your documentation in-line of the
document using a macro that might look like this:

Add a macro that you could use in the documentation like this both to
define the string and include it in the export

"The document adding function is pretty important to the doc function Add. .

#+MACRO: add-string (eval (add-string "$1" "$2")

Add a file buffer-local variable of key/value pairs for strings named 'strings'.

Add helper functions with the same names here "get-string, add-string"
to use it (pseudo code follows)

#+name: add-string
#+begin_src emacs-lisp :var key="default" :var value="default
;; if the key doesn't exist in the hashmap then add it and return the value
;; otherwise just return the value so it doesn't stomp on the existing value
#+end_src

Add would return the string, and that would get inserted in the export.

Get-string would just get they key's value from the hashmap

#+name: get-string
#+begin_src emacs-lisp :var key="default"
;; if the key exists then return its value
;; otherwise return "UNDEFINED"
#+end_src

To use that documentation in the source block it would work the same as above

It would be easy to add prettification to cut down on the "noise" of
the syntax to use the macro and do the literate call, too that people
might like.

This is all pseudo code but I think it is in the same spirit of this
thread. Do you?

      parent reply	other threads:[~2018-06-15 18:07 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
2018-05-17  9:33                   ` ZHUO QL (KDr2)
2018-06-15 21:44                     ` John Kitchin
2018-06-15 18:07               ` Grant Rettke [this message]

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='CAAjq1md1mPFo3SvCh+6XSjvrDKtF+iEEx_a5edgtz7pZR_=DCw@mail.gmail.com' \
    --to=gcr@wisdomandwonder.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=esflists@gmail.com \
    --cc=jkitchin@andrew.cmu.edu \
    --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).