From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id ME2ZNIlUYWNhfgEAbAwnHQ (envelope-from ) for ; Tue, 01 Nov 2022 18:16:57 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id SB+gNIlUYWN2SgAAauVa8A (envelope-from ) for ; Tue, 01 Nov 2022 18:16:57 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9206425CC4 for ; Tue, 1 Nov 2022 18:16:57 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oprvw-0007Ml-5y; Tue, 01 Nov 2022 10:08:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oprvu-0007M3-HJ for emacs-orgmode@gnu.org; Tue, 01 Nov 2022 10:08:02 -0400 Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oprvs-0000BF-KG for emacs-orgmode@gnu.org; Tue, 01 Nov 2022 10:08:02 -0400 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id BD132240026 for ; Tue, 1 Nov 2022 15:07:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1667311678; bh=jnnrhckDpk9Ty3oYv7uzHMx+AgY5HkiK9G1pI7rcUc4=; h=From:To:Subject:Date:From; b=AZltwt+hVqPaGdd4jbL6x6VporU55cVtTmC2eYb96Ll2VSR6HEaBS42YO9Aq2kYA+ Wyr33jwLsZzkdfUndDsqhjq52z4o7v7kN3nZ7JRdmttmc6lA2JrFyAT/f/cm2V2EXK cUcxcAc4z5fkjmCdtuOIch6ZkR0o38HWjr/s4S6ZKt0WqOVPNn5rndWSiQgM47u9eP lo2X99hwoy/aGL5sXM09mMf8zOFnnpWm7+NITjCmDt0mNqoNlQLwnPImPceODebRSy JYOKY6VbBEs2C4e1cURBsIrxGOk0aZzrOh9X3CvsYtGVpyslwb55Yt/VpbLxGQHOVA cPAxVZI+LfVoA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4N1sL51mQwz9rxB for ; Tue, 1 Nov 2022 15:07:54 +0100 (CET) From: =?utf-8?Q?Juan_Manuel_Mac=C3=ADas?= To: orgmode Subject: Docstrings and literate programming (good practices?) Date: Tue, 01 Nov 2022 14:07:52 +0000 Message-ID: <87bkpqbwef.fsf@posteo.net> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=185.67.36.65; envelope-from=maciaschain@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Emacs-orgmode" Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1667323017; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=oNA/juNaht85ZFR13GO/OP3io95ngVPBc6RBaGxU3O4=; b=cUv3Uoh6hfEqOBquvyMjftPsqSa538AnMg/R/xWMs8ZXwve8kDtLBNHhFgdDCm/zxDgdmZ 1JlDpT3ySLHZGLvCj1B9/GKJdVZrpQEfoRbOUXRUDXY+Q7O61DYV/kQCy7duRgr+V1Jcxo 5O00ppEvL09meH5Xmf5KjjqObVX3VdXO80cI6gidzrwz9g51Tcgjd2tpsElDzW9iIR8k+t ALddZZKSKyB9/bdSYsJf5upTeqt/Nruy9ThXciHQ5Be6gByZtv80w0oessvJPrgwhDZHUA Jzs2eVlJP6e6IVUmDkrDm31DFGwBb3WyZt3pLm1OPJo9j8cVdgNBuDMCKgU+Eg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1667323017; a=rsa-sha256; cv=none; b=PFDVn/m+6iLiORzjldNgw+fE8Na05i3yNq0MypSSNXmKxb+pw+RdKVqiYQ+1JnMB+sVa9X BElIKBXKB2khQHhu5MGOUwUjyhJrCny23+pNG+AtOSzVEU2GMNLYDYyoIVDcbfrtJl4o0w 53XQLkpCh7mMPzF//ZreDw8iYIPOf4++3fNNNn6GLWMPATNNJdLJ/0au4igdsqM861sX1+ JzUNAZjk+K5x8yCjFm/G5LAEwqgdZjHurfx9Sg+oz7tA3lj04joL7vKByDpH7hucKri94D sc+r6Pcht+PZRPwRJ54tloUt4bqH/k/+DWQcPU5Zy3VmnQDfoQKfjGN3+cmBUw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=AZltwt+h; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.98 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=AZltwt+h; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 9206425CC4 X-Spam-Score: -3.98 X-Migadu-Scanner: scn1.migadu.com X-TUID: URC/Pe7v2uZ/ 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 () <> (message "hello world")) #+end_src The only drawback is that with :noweb strip-export an empty line is left. Best regards, Juan Manuel