Hello,
Kaushal Modi <[email protected]> writes:
> Can we have {{{kbd(v SPC)}}} export to below for HTML?
>
> <kbd>v <span class="key">SPC</span></kbd>
Good idea. New patch follows.
> *Also stuff within angle brackets will hide in HTML if you choose to leave
> the export as "v <SPC>" for HTML too!*
Indeed. This is fixed per the change above.
Regards,
--
Nicolas Goaziou
>From 7caa7028ac496354fbe8a20eb98f63e306b9e021 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <[email protected]>
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 | 47 +++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 45 insertions(+), 2 deletions(-)
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 3453e5e07..3da09910a 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,16 @@ 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")
+ 'words)
+ "Regexp matching special keyboard keys.")
+
(defun org-macro--vc-modified-time (file)
(save-window-excursion
(when (vc-backend file)
@@ -318,8 +333,36 @@ 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 (org-trim wrap)
+ ("code" "~%s~")
+ ("verbatim" "=%s=")
+ (_ "%s"))))
+ (format template
+ (cond
+ ((org-export-derived-backend-p org-export-current-backend 'texinfo)
+ (format "@@texinfo:@kbd{%s}@@"
+ (replace-regexp-in-string
+ org-macro--special-keys-re "@key{\\&}" kbd t)))
+ ((org-export-derived-backend-p org-export-current-backend 'html)
+ (format "@@html:<kbd>%s</kbd>@@"
+ (replace-regexp-in-string
+ org-macro--special-keys-re
+ "<span class=\"key\">\\&</span>" kbd t)))
+ (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