Skip to content

Commit

Permalink
Add Windows Terminal support
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzy112 committed Sep 6, 2024
1 parent ef56674 commit 19531eb
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 94 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This package allows configuring Emacs and a supported terminal emulator to handl
* [Linux console](#linux-console)
* [st](#st)
* [macOS Terminal](#macos-terminal)
* [Windows Terminal](#windows-terminal)
* [Unsupported terminals](#unsupported-terminals)
* [Similar projects](#similar-projects)

Expand Down Expand Up @@ -400,6 +401,18 @@ These entries will appear (displayed with the key's hex code instead of the key

You can customize the mapping of Terminal.app modifiers to Emacs modifiers using the respective `customize` group, i.e.: <kbd>M-:</kbd>`(progn (require 'term-keys-terminal-app) (customize-group 'term-keys/terminal-app))`

#### Windows Terminal

To configure Windows Terminal for `term-keys`, use `term-keys/windows-terminal-json` to generate a json file.

```elisp
(require 'term-keys-windows-terminal)
(with-temp-file "~/windows-terminal.json"
(insert (term-keys/windows-terminal-json)))
```

Then, add the contents to Windows Terminal settings.

#### Unsupported terminals

These terminals don't (directly) support customizing key bindings, and thus cannot be used with `term-keys`:
Expand Down
92 changes: 92 additions & 0 deletions term-keys-windows-terminal.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
;;; term-keys-windows-terminal.el --- term-keys support for Windows Terminal -*- lexical-binding: t; -*-

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This file contains supplementary code for aiding in the
;; configuration of Windows Terminal terminal emulator to interoperate
;; with the term-keys package.

;; For more information, please see the accompanying README.me file.

;;; Code:

(require 'term-keys)
(require 'json)

(defgroup term-keys/windows-terminal nil
"`term-keys' options for Windows Terminal."
:group 'term-keys)

(define-widget 'term-keys/windows-terminal-modifier 'lazy
"Choice for Windows Terminal key binding modifiers and state flags."
:type '(choice (const :tag "Shift" "shift+")
(const :tag "Ctrl" "ctrl+")
(const :tag "Alt" "alt+")
(const :tag "Win" "win+")
(const :tag "(none)" nil)))

(defcustom term-keys/windows-terminal-modifier-map ["shift+" "ctrl+" "alt+" "win+" nil nil]
"Modifier keys for Windows Terminal key bindings.
This should be a vector of 6 elements, with each element being a
string indicating the name of the Windows Terminal modifier
corresponding to the Emacs modifiers Shift, Control, Meta, Super,
Hyper and Alt respectively, as they should appear in generated
Windows Terminal settings JSON files. nil indicates that there
is no mapping for this modifier."
:type '(vector
(term-keys/windows-terminal-modifier :tag "Shift")
(term-keys/windows-terminal-modifier :tag "Control")
(term-keys/windows-terminal-modifier :tag "Meta")
(term-keys/windows-terminal-modifier :tag "Super")
(term-keys/windows-terminal-modifier :tag "Hyper")
(term-keys/windows-terminal-modifier :tag "Alt")))

(defun term-keys/windows-terminal-json ()
"Construct Windows Terminal settings.
This function returns, as a string, the Windows Terminal settings
in JSON format used to configure Windows Terminal to encode
term-keys key sequences, according to the term-keys
configuration."
(let ((actions nil))
(term-keys/iterate-keys
(lambda (index keymap mods)
(unless (cl-reduce (lambda (x y) (or x y))
(mapcar (lambda (n)
(and (elt mods n)
(not (elt term-keys/windows-terminal-modifier-map n))))
(number-sequence 0 (1- (length mods)))))
(let ((input (concat term-keys/prefix
(term-keys/encode-key index mods)
term-keys/suffix))
(keys (concat (mapconcat
(lambda (n)
(if (elt mods n)
(elt term-keys/windows-terminal-modifier-map n)))
(number-sequence 0 (1- (length mods))))
(elt keymap 14))))
(push `((:command . ((:action . "sendInput")
(:input . ,input)))
(:keys . ,keys))
actions)))))
(let ((json-encoding-pretty-print t))
(json-encode `((:actions . ,(nreverse actions)))))))

(provide 'term-keys-windows-terminal)
;;; term-keys-windows-terminal.el ends here
Loading

0 comments on commit 19531eb

Please sign in to comment.