From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: [RFC] New "kbd" macro? Date: Wed, 13 Sep 2017 15:22:16 +0200 Message-ID: <87377qpwc7.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:38045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds7cT-0002cZ-Hx for emacs-orgmode@gnu.org; Wed, 13 Sep 2017 09:22:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ds7cQ-0000WR-S4 for emacs-orgmode@gnu.org; Wed, 13 Sep 2017 09:22:21 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:41372) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ds7cQ-0000Vo-Ks for emacs-orgmode@gnu.org; Wed, 13 Sep 2017 09:22:18 -0400 Received: from saiph.selenimh (000043010000000000000469.ipv6.commingeshautdebit.fr [IPv6:2a03:a0a0:0:4301::469]) (Authenticated sender: mail@nicolasgoaziou.fr) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id AE02E1720E2 for ; Wed, 13 Sep 2017 15:22:16 +0200 (CEST) Received: from ngz by saiph.selenimh with local (Exim 4.89) (envelope-from ) id 1ds7cO-0003QX-10 for emacs-orgmode@gnu.org; Wed, 13 Sep 2017 15:22:16 +0200 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: Org Mode List --=-=-= Content-Type: text/plain Hello, I would like to submit a new minor macro for integration within Org: the "kbd" macro. The "kbd" macro focuses on normalizing keybinding during export. For example, during Texinfo export, {{{kbd(v SPC)}}} becomes @kbd{v @key{SPC}} whereas in another back-end, it becomes v More specifically: Within {{{kbd(...)}}}, the following case-sensitive keys are wrapped within angle brackets: SPC, RET, LFD, TAB, BS, ESC, DELETE, SHIFT, CTRL, META, up, down, left, right. With an optional argument, it can wrap the key-binding within verbatim or code markup: {{{kbd(v SPC,code)}}} => ~v ~ {{{kbd(v SPC,verbatim)}}} => =v = Other markup is not implemented because, in that case, you can wrap appropriate characters around the macro. Granted, this is probably only useful for Texinfo export, but defining it as a global macro makes Org documents a bit more portable across export back-ends. If there is any interest in it, I will add tests and documentation. I attach a proof of concept. WDYT? Regards, -- Nicolas Goaziou 0x80A93738 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-org-macro-Implement-kbd-macro.patch Content-Description: implement kbd macro >From be6192293c41a3d252572a0fdbed7ac5b7a0c6bd Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 13 Sep 2017 13:01:59 +0200 Subject: [PATCH] org-macro: Implement kbd macro * lisp/org-macro.el (org-macro--special-keys-re): New variable. (org-macro--kbd): New function. (org-macro-initialize-templates): Use new function. --- lisp/org-macro.el | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 3453e5e07..cad38cdd2 100644 --- a/lisp/org-macro.el +++ b/lisp/org-macro.el @@ -64,6 +64,8 @@ (declare-function vc-call "vc-hooks" (fun file &rest args) t) (declare-function vc-exec-after "vc-dispatcher" (code)) +(defvar org-export-current-backend) + ;;; Variables (defvar-local org-macro-templates nil @@ -164,6 +166,9 @@ function installs the following ones: \"property\", (org-macro--counter-initialize) (funcall update-templates (cons "n" "(eval (org-macro--counter-increment \"$1\" \"$2\"))")) + ;; Install "kbd" macro. + (funcall update-templates + (cons "kbd" "(eval (org-macro--kbd \"$1\" \"$2\"))")) (setq org-macro-templates templates))) (defun org-macro-expand (macro templates) @@ -294,6 +299,15 @@ Return a list of arguments, as strings. This is the opposite of ;;; Helper functions and variables for internal macros +(defvar org-macro--counter-table nil + "Hash table containing counter value per name.") + +(defconst org-macro--special-keys-re + (regexp-opt + '("SPC" "RET" "LFD" "TAB" "BS" "ESC" "DELETE" "SHIFT" "CTRL" "META" + "up" "left" "right" "down")) + "Regexp matching special keyboard keys.") + (defun org-macro--vc-modified-time (file) (save-window-excursion (when (vc-backend file) @@ -318,8 +332,31 @@ Return a list of arguments, as strings. This is the opposite of (kill-buffer buf)) date)))) -(defvar org-macro--counter-table nil - "Hash table containing counter value per name.") +(defun org-macro--kbd (kbd wrap) + "Return normalized keyboard sequence KBD. + +KBD is a string. Within KBD, the following case-sensitive keys +are wrapped within angle brackets: SPC, RET, LFD, TAB, BS, ESC, +DELETE, SHIFT, CTRL, META, up, down, left, right. + +When WRAP is \"code\", the whole sequence is surrounded with +\"~\" markers. If WRAP is \"verbatim\", the sequence is +surrounded with \"=\" markers." + (let* ((case-fold-search nil) + (template + (pcase wrap + ("code" "~%s~") + ("verbatim" "=%s=") + (_ "%s")))) + (format template + (pcase org-export-current-backend + (`texinfo + (format "@@texinfo:@kbd{%s}@@" + (replace-regexp-in-string + org-macro--special-keys-re "@key{\\&}" kbd t))) + (_ + (replace-regexp-in-string + org-macro--special-keys-re "<\\&>" kbd t)))))) (defun org-macro--counter-initialize () "Initialize `org-macro--counter-table'." -- 2.14.1 --=-=-=--