From mboxrd@z Thu Jan 1 00:00:00 1970 From: Grant Rettke Subject: Re: Proposal: references from code to text. Date: Fri, 15 Jun 2018 13:07:13 -0500 Message-ID: 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> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35992) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fTt81-0002Q0-9e for emacs-orgmode@gnu.org; Fri, 15 Jun 2018 14:07:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fTt80-0003R6-3r for emacs-orgmode@gnu.org; Fri, 15 Jun 2018 14:07:17 -0400 Received: from mail-lf0-x229.google.com ([2a00:1450:4010:c07::229]:33602) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fTt7z-0003Pw-PI for emacs-orgmode@gnu.org; Fri, 15 Jun 2018 14:07:16 -0400 Received: by mail-lf0-x229.google.com with SMTP id y20-v6so15893811lfy.0 for ; Fri, 15 Jun 2018 11:07:15 -0700 (PDT) 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: "ZHUO QL (KDr2)" , Eric S Fraga , Emacs-org List On Wed, May 16, 2018 at 9:25 AM, John Kitchin 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. > <> ;; code to code > <>" > ;; [[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?