From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id UGmnOhTi5mH4wQAAgWs5BA (envelope-from ) for ; Tue, 18 Jan 2022 16:51:48 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id IIQeOBTi5mGYJAAA9RJhRA (envelope-from ) for ; Tue, 18 Jan 2022 16:51:48 +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 05E4242D40 for ; Tue, 18 Jan 2022 16:51:48 +0100 (CET) Received: from localhost ([::1]:53222 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n9qlv-0000te-06 for larch@yhetil.org; Tue, 18 Jan 2022 10:51:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:41918) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n9q9F-00040c-Cl for emacs-orgmode@gnu.org; Tue, 18 Jan 2022 10:11:55 -0500 Received: from mail.hostpark.net ([212.243.197.30]:35416) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n9q92-0007NS-BH for emacs-orgmode@gnu.org; Tue, 18 Jan 2022 10:11:40 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.hostpark.net (Postfix) with ESMTP id C1D2716BAF for ; Tue, 18 Jan 2022 16:11:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=bernoul.li; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=sel2011a; t=1642518690; bh=lwPPLM/vBKMThPb VDYJSPK1An6dhp3Hrp9pYtpRe8zY=; b=VY5tSneP/f25skKe2kG6Hs6BpmoMGR6 gfSighkElD1z7CDo2NG8aZHlwYEB4fz3MrVpt6xwWOL7EoH8wHmCzv6p9WNU7N6l DN9wuSh9zQxz02AjiT/IBP4OHavj4G/45Xs3hpYH2G1UWsYV7QOVj5k1pJpKYWDN mhwc/ewDBuG0= X-Virus-Scanned: by Hostpark/NetZone Mailprotection at hostpark.net Received: from mail.hostpark.net ([127.0.0.1]) by localhost (mail0.hostpark.net [127.0.0.1]) (amavisd-new, port 10224) with ESMTP id STsdB0TZzVYO for ; Tue, 18 Jan 2022 16:11:30 +0100 (CET) Received: from customer (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.hostpark.net (Postfix) with ESMTPSA id 78C1E16BBC for ; Tue, 18 Jan 2022 16:11:30 +0100 (CET) From: Jonas Bernoulli To: emacs-orgmode@gnu.org Subject: [PATCH v3 3/3] ox-texinfo: Define definition commands using description lists Date: Tue, 18 Jan 2022 16:11:29 +0100 Message-Id: <20220118151129.19646-4-jonas@bernoul.li> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220118151129.19646-1-jonas@bernoul.li> References: <20211218214038.9459-1-jonas@bernoul.li> <20220118151129.19646-1-jonas@bernoul.li> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: none client-ip=212.243.197.30; envelope-from=jonas@bernoul.li; helo=mail.hostpark.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=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: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" 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=1642521108; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=Rs+Yt5WzqB/w/gvgZVMgPL8GeF7ArWjFVT9Olhibsik=; b=NZ9PTpYMwW0o3c/ajpD4+sGBZrAxa8mZWH3s8eQg36AAhjfIPxfyGDtZM+mam8R84mBMFz kz4bOiKVAS/Bazm7BsJxRpQvAc5480+UrkKugU0l0qxLzdgae/M3pMv5odXPyZjMSjpyG3 W6vSJ8b12S5RYnA9vTpiPnbKOnmRupQv4eZL5OcG6K6OpeNEnmrKXF7G+wn1ZOSQ2DlzRM 9T/J7q6J8xcmuz/lCB6BotpgDXI0osi/Y/5AxnrYk79/SyjHdWWBnqHt/ztpqZufZhgjan QC9ef+Zq0TTTn20u9vPCb0pWSgjqzXMKwhoXYkGhC0IBm6zEwCxQx0X+0zevnQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1642521108; a=rsa-sha256; cv=none; b=PIk9girkoRubN7Uw2P6Bu4MN9rnB3cwn5VjmiyqF3MjnpYqGIlrvL10mZNFrUHL7lwJLch 9d97uan2c7X/qmCX5okqT49A6S59Xe6ERe+l0zSH/z1XtlzdRO1FkP/fXbrZZXLYpAc1op z0bhG6qXIVHD9rvl+7i8HmgqdoWqCc0oy4oHpQ6AnQNBI7EhqWEF5GLBRXjn528+tuRwer 0+w+SQVFBG2pF4DmEKzKaMkvI6ZEezp8DwHyJ8wtkAnuSGbGir5Kjkk/3hALLmo/5qGCE2 0EOzTalg1PB1SAsJH69GFWsBNAWY6hrpdpwDJGP3OMM6YIA6CwvQasCQdO89kQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=bernoul.li header.s=sel2011a header.b=VY5tSneP; dmarc=none; 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: -2.42 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=bernoul.li header.s=sel2011a header.b=VY5tSneP; dmarc=none; 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: 05E4242D40 X-Spam-Score: -2.42 X-Migadu-Scanner: scn0.migadu.com X-TUID: TwpamGiyY1Ai * doc/org-manual.org (Plain lists in Texinfo export): Document use of definition command prefixes in description lists. * lisp/ox-texinfo.el: Add org-texinfo--separate-definitions to the list of :filter-parse-tree functions of the texinfo backend. * lisp/ox-texinfo.el (org-texinfo--quoted-keys-regexp) (org-texinfo--definition-command-alist) (org-texinfo--definition-command-regexp): New variables. * lisp/ox-texinfo.el (org-texinfo--filter-parse-tree): Call org-texinfo--separate-definitions. * lisp/ox-texinfo.el (org-texinfo--separate-definitions) (org-texinfo--match-definition, org-texinfo--split-definition) (org-texinfo--split-plain-list, org-texinfo--massage-key-item): New functions. --- doc/org-manual.org | 70 +++++++++++++++++++++++ lisp/ox-texinfo.el | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) diff --git a/doc/org-manual.org b/doc/org-manual.org index b3c4a9bef..daa207a5d 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -15307,6 +15307,72 @@ example is transcoded to the same output as above. This is the common text for variables foo and bar. #+end_example +Likewise, the Texinfo export back-end supports two approaches to +writing Texinfo definition commands (see [[info:texinfo::Definition +Commands]]). One of them uses description lists and is describe below, +the other is described in [[*Special blocks in Texinfo export]]. + +Items in a description list in a Org file that begin with =Function:= +or certain other prefixes are converted using Texinfo definition +commands. This works even if other items in the same list do not have +such a prefix; if necessary a single description list is converted +using multiple tables (such as =@vtable=) and definition commands +(such as =@defun=). + +#+begin_example +- Function: org-texinfo-drawer drawer contents info :: + Transcode a DRAWER element from Org to Texinfo. +#+end_example + +#+texinfo: @noindent +becomes + +#+begin_example +@defun org-texinfo-drawer drawer contents info :: + Transcode a DRAWER element from Org to Texinfo. +@end defun +#+end_example + +The recognized prefixes are =Command:=, =Function:=, =Macro:=, +=Special Form:=, =Variable:= and =User Option:=. These are the same +prefixes that appear in the Info file for the respective definition +commands. For example a =Function:= item in the Org file is converted +to a =@defun= command in the Texinfo file, which in turn is converted +to a definition prefixed with =-- Function:= in the Info file. + +As a special case the prefix =Key:= is also recognized. No Texinfo +definition command exists for key bindings and the output in Info +files also lacks the =Key:= prefix. Even so this special case is +supported because it provides a convenient shorthand, as illustrated +here: + +#+begin_example +- Key: C-c C-c (do-something) :: + This command does something. + +- User Option: do-something-somehow :: + This option controls how exactly ~do-something~ does its thing. +#+end_example + +#+texinfo: @noindent +becomes + +#+begin_example +@table @asis +@item @kbd{C-c C-c} (@code{do-something}) +@kindex C-c C-c +@findex do-something +This command does something. +@end table + +@defopt do-something-somehow +This option controls how exactly @code{do-something} does its thing. +@end defopt +#+end_example + +#+texinfo: @noindent +Command in parenthesis, as done above, is optional. + *** Tables in Texinfo export :PROPERTIES: :DESCRIPTION: Table attributes. @@ -15401,6 +15467,10 @@ Type @kbd{C-c @key{SPC}}. :DESCRIPTION: Special block attributes. :END: +The Texinfo export back-end supports two approaches to writing Texinfo +definition commands. One of them is describe here, the other in +[[*Plain lists in Texinfo export]]. + #+cindex: @samp{ATTR_TEXINFO}, keyword The Texinfo export back-end converts special blocks to commands with diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el index 36e1436d7..751ad1126 100644 --- a/lisp/ox-texinfo.el +++ b/lisp/ox-texinfo.el @@ -84,6 +84,7 @@ (org-export-define-backend 'texinfo :filters-alist '((:filter-headline . org-texinfo--filter-section-blank-lines) (:filter-parse-tree . (org-texinfo--normalize-headlines + org-texinfo--separate-definitions org-texinfo--combine-items)) (:filter-section . org-texinfo--filter-section-blank-lines) (:filter-final-output . org-texinfo--untabify)) @@ -408,6 +409,30 @@ (defconst org-texinfo-inline-image-rules (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg")))) "Rules characterizing image files that can be inlined.") +(defvar org-texinfo--quoted-keys-regexp + (regexp-opt '("BS" "TAB" "RET" "ESC" "SPC" "DEL" + "LFD" "DELETE" "SHIFT" "Ctrl" "Meta" "Alt" + "Cmd" "Super" "UP" "LEFT" "RIGHT" "DOWN") + 'words) + "Regexp matching keys that have to be quoted using @key{KEY}.") + +(defconst org-texinfo--definition-command-alist + '(("deffn Command" . "Command") + ("defun" . "Function") + ("defmac" . "Macro") + ("defspec" . "Special Form") + ("defvar" . "Variable") + ("defopt" . "User Option") + (nil . "Key")) + "Alist mapping Texinfo definition commands to output in Info files.") + +(defconst org-texinfo--definition-command-regexp + (format "\\`%s: \\(.+\\)" + (regexp-opt + (delq nil (mapcar #'cdr org-texinfo--definition-command-alist)) + t)) + "Regexp used to match definition commands in descriptive lists.") + ;;; Internal Functions @@ -605,6 +630,120 @@ (defun org-texinfo--sectioning-structure (info) (`(,_ ,_ . ,sections) sections) (_ (user-error "Unknown Texinfo class: %S" class))))) +(defun org-texinfo--separate-definitions (tree _backend info) + "Split up descriptive lists in TREE that contain Texinfo definition commands. + +_BACKEND is the symbol `texinfo'; the back-end used for export. +INFO is a plist used as a communication channel. + +Return new tree." + (org-element-map tree 'plain-list + (lambda (plain-list) + (when (eq (org-element-property :type plain-list) 'descriptive) + (let ((contents (org-element-contents plain-list)) + (items nil)) + (dolist (item contents) + (pcase-let ((`(,cmd . ,args) (org-texinfo--match-definition item))) + (cond + (cmd + (when items + (org-texinfo--split-plain-list plain-list (nreverse items)) + (setq items nil)) + (org-texinfo--split-definition plain-list item cmd args)) + (t + (when args + (org-texinfo--massage-key-item plain-list item args)) + (push item items))))) + (unless (org-element-contents plain-list) + (org-element-extract-element plain-list))))) + info) + tree) + +(defun org-texinfo--match-definition (item) + "Return a cons-cell if ITEM specifies a Texinfo definition command. +The car is the command and the cdr is its arguments." + (let ((tag (car-safe (org-element-property :tag item)))) + (and tag + (stringp tag) + (string-match org-texinfo--definition-command-regexp tag) + (pcase-let* + ((cmd (car (rassoc (match-string-no-properties 1 tag) + org-texinfo--definition-command-alist))) + (`(,cmd ,category) + (and cmd (save-match-data (split-string cmd " ")))) + (args (match-string-no-properties 2 tag))) + (cons cmd (if category (concat category " " args) args)))))) + +(defun org-texinfo--split-definition (plain-list item cmd args) + "Insert a definition command before list PLAIN-LIST. +Replace list item ITEM with a special-block that inherits the +contents of ITEM and whose type and Texinfo attributes are +specified by CMD and ARGS." + (let ((contents (org-element-contents item))) + (org-element-insert-before + (apply #'org-element-create 'special-block + (list :type cmd + :attr_texinfo (list (format ":options %s" args)) + :post-blank (if contents 1 0)) + (mapc #'org-element-extract-element contents)) + plain-list)) + (org-element-extract-element item)) + +(defun org-texinfo--split-plain-list (plain-list items) + "Insert a new plain list before the plain list PLAIN-LIST. +Remove ITEMS from PLAIN-LIST and use them as the contents of the +new plain list." + (org-element-insert-before + (apply #'org-element-create 'plain-list + (list :type 'descriptive :post-blank 1) + (mapc #'org-element-extract-element items)) + plain-list)) + +(defun org-texinfo--massage-key-item (plain-list item args) + "In PLAIN-LIST modify ITEM based on ARGS. +Reformat ITEM's tag property and add findex and kindex entries to +its content. If the bullet is \"+\" then use \"@itemx\" and deal with +data from preceeding siblings that use such a bullet." + (let (key cmd) + (if (string-match (rx (+ " ") + "(" (group (+ (not (any "()")))) ")" + (* " ") + eos) + args) + (setq key (substring args 0 (match-beginning 0)) + cmd (match-string 1 args)) + (setq key args)) + (org-element-put-property + item :tag + (cons (org-export-raw-string (org-texinfo-kbd-macro key t)) + (and cmd `(" (" (code (:value ,cmd :post-blank 0)) ")")))) + (let ((findex (org-element-property :findex item)) + (kindex (org-element-property :kindex item)) + (next-item (cadr (memq item (org-element-contents plain-list)))) + (mx (string-prefix-p "M-x " key))) + (when (and (not cmd) mx) + (setq cmd (substring key 4))) + (when (and cmd (not (member cmd findex))) + (setq findex (nconc findex (list cmd)))) + (unless mx + (setq kindex (nconc kindex (list key)))) + (cond + ((and next-item + (org-not-nil + (org-export-read-attribute :attr_texinfo plain-list :compact)) + (not (org-element-contents item)) + (= (org-element-property :post-blank item) 1)) + (org-element-put-property next-item :findex findex) + (org-element-put-property next-item :kindex kindex) + (org-element-put-property item :findex nil) + (org-element-put-property item :kindex nil)) + (t + (org-element-set-contents + item (nconc + (mapcar (lambda (key) `(keyword (:key "KINDEX" :value ,key))) kindex) + (mapcar (lambda (cmd) `(keyword (:key "FINDEX" :value ,cmd))) findex) + (org-element-contents item)))))))) + ;;; Template (defun org-texinfo-template (contents info) -- 2.34.1