emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Proposal: references from code to text.


From: Grant Rettke
Subject: Re: [O] Proposal: references from code to text.
Date: Fri, 15 Jun 2018 13:07:13 -0500

On Wed, May 16, 2018 at 9:25 AM, John Kitchin <address@hidden> 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?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]