emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: Docstrings and literate programming (good practices?)
Date: Tue, 01 Nov 2022 14:07:52 +0000	[thread overview]
Message-ID: <87bkpqbwef.fsf@posteo.net> (raw)

Hi all,

I find docstrings very useful. One can learn a great deal about Elisp
just from describe-function and describe-variable. But I don't find the
best way for docstrings to fit into the "precepts" of literate
programming. I try to explain myself: today I am reviewing my Emacs
init, written in Org. I like to document the code neatly, so that my
future self knows what my present self was trying to do. But I realize
that many of those global explanations before a function or a variable
can also be in a docstring. I can duplicate the text, but it seems to be
a bit redundant, right? So what is the best way to proceed when doing
literate programming with any language that supports docstrings?
Apologies in advance if the question is a bit silly, but I'm not a
professional programmer and don't have an academic background in it,
so I don't know if there is any good practice on these things :-)

On the other hand, the following procedure has occurred to me: put the
relevant information in an Org src block. When exporting to a human
readable format (PDF, HTML, etc.), the content is exported as part of
the text. When tangling, the block content is exported as a docstring.

First, I defined this function:

  (defun my-org-format-docstring (cont)
    (with-temp-buffer
      (insert cont)
      (save-excursion
	(goto-char (point-min))
	(while (re-search-forward org-emph-re nil t)
	  (replace-match (concat " " (match-string 4) " ") t nil)))
      (setq cont
	    (string-trim
	     (replace-regexp-in-string
	      "\\(\"\\)"
	      "\\\\\\1"
	      (org-export-string-as (buffer-string) 'ascii t)))))
    (format "\"%s\"" cont))


And an example of use:

#+NAME: format-docstring
#+begin_src emacs-lisp :exports none :var x="" :tangle no
  (if (not org-export-current-backend)
      (my-org-format-docstring x)
    x)
#+end_src

#+NAME: docstring1
#+begin_src org :post format-docstring(*this*) :results replace :exports results :tangle no
  Lorem ipsum dolor sit amet.

  Consectetuer adipiscing elit. "Donec hendrerit tempor tellus". Donec pretium posuere
  tellus. Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus. Cum sociis
  natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
#+end_src

#+begin_src emacs-lisp :noweb strip-export :exports code
  (defun foo ()
   <<docstring1()>>
    (message "hello world"))
#+end_src

The only drawback is that with :noweb strip-export an empty line is
left.

Best regards,

Juan Manuel 


             reply	other threads:[~2022-11-01 17:16 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-01 14:07 Juan Manuel Macías [this message]
2022-11-02  7:13 ` Docstrings and literate programming (good practices?) Ihor Radchenko
2022-11-02  7:53   ` Dr. Arne Babenhauserheide
2022-11-02 10:43     ` Ihor Radchenko
2022-11-02 12:49   ` Juan Manuel Macías
2022-11-02 13:05     ` Ihor Radchenko
2022-11-02 15:20       ` Juan Manuel Macías
2022-11-03  7:38         ` Ihor Radchenko
2022-11-03 20:54 ` Rudolf Adamkovič
2022-11-04  3:03   ` Samuel Wales
2022-11-04  5:45     ` tomas
2022-11-04  6:39       ` Marcin Borkowski
2022-11-04  7:13         ` Samuel Wales
2022-11-04  8:08           ` tomas
2022-11-04  8:06         ` tomas
2022-11-04  8:49     ` Ihor Radchenko
2022-11-05  2:07       ` Samuel Wales
2022-11-08  4:10         ` Samuel Wales
2022-11-04 11:45     ` Max Nikulin

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=87bkpqbwef.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /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).