emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [RFC] New "kbd" macro?
@ 2017-09-13 13:22 Nicolas Goaziou
  2017-09-13 14:03 ` Kaushal Modi
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Nicolas Goaziou @ 2017-09-13 13:22 UTC (permalink / raw)
  To: Org Mode List

[-- Attachment #1: Type: text/plain, Size: 1132 bytes --]

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 <SPC>

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 <SPC>~

  {{{kbd(v SPC,verbatim)}}}  =>  =v <SPC>=

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: implement kbd macro --]
[-- Type: text/x-diff, Size: 2969 bytes --]

From be6192293c41a3d252572a0fdbed7ac5b7a0c6bd Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
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
 \f
 ;;; 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


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2017-09-17  7:20 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-13 13:22 [RFC] New "kbd" macro? Nicolas Goaziou
2017-09-13 14:03 ` Kaushal Modi
2017-09-13 14:05   ` Kaushal Modi
2017-09-13 19:25   ` Nicolas Goaziou
2017-09-14  4:45   ` Marcin Borkowski
2017-09-14 13:49   ` Oleh Krehel
2017-09-13 19:24 ` Eric S Fraga
2017-09-14  8:07 ` Rasmus
2017-09-14 22:02   ` Nicolas Goaziou
2017-09-15  7:36     ` Rasmus
2017-09-15 10:16       ` Nicolas Goaziou
2017-09-15 11:20         ` Rasmus
2017-09-15 11:52           ` Nicolas Goaziou
2017-09-15 12:54     ` Kaushal Modi
2017-09-15 13:08       ` Kaushal Modi
2017-09-15 15:53         ` Nicolas Goaziou
2017-09-17  7:20           ` Nicolas Goaziou

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).