From 33141b51c3121b93f625c76b996b17ec8de97419 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sun, 8 May 2022 15:03:59 +0200 Subject: [PATCH] Allow term-mode to send function keys to the underlying shell * lisp/term.el (term-bind-function-keys): New user option. (term-raw-map): Bind f keys. (term-send-function-key): Send the function key to the underlying shell (bug#29920). --- etc/NEWS | 5 +++++ lisp/term.el | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index ee7a127af0e..5c13b72c29b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1137,6 +1137,11 @@ filters and displayed with the specified color. ** term-mode +--- +*** New user option 'term-bind-function-keys'. +If non-nil, 'term-mode' will pass the function keys on to the +underlying shell instead of using the normal Emacs bindings. + --- *** Support for ANSI 256-color and 24-bit colors, italic and other fonts. Term-mode can now display 256-color and 24-bit color codes. It can diff --git a/lisp/term.el b/lisp/term.el index 3e05d529cd7..640478b59a2 100644 --- a/lisp/term.el +++ b/lisp/term.el @@ -918,6 +918,13 @@ is buffer-local." :type 'integer :version "27.1") +(defcustom term-bind-function-keys nil + "If nil, don't alter , and so on. +If non-nil, bind these keys in `term-mode' and send them to the +underlying shell." + :type 'boolean + :version "29.1") + ;; Set up term-raw-map, etc. @@ -958,6 +965,10 @@ is buffer-local." (define-key map [next] 'term-send-next) (define-key map [xterm-paste] #'term--xterm-paste) (define-key map [?\C-/] #'term-send-C-_) + + (when term-bind-function-keys + (dotimes (key 21) + (keymap-set map (format "" key) #'term-send-function-key))) map) "Keyboard map for sending characters directly to the inferior process.") @@ -1411,6 +1422,26 @@ Entry to this mode runs the hooks on `term-mode-hook'." (defun term-send-del () (interactive) (term-send-raw-string "\e[3~")) (defun term-send-backspace () (interactive) (term-send-raw-string "\C-?")) (defun term-send-C-_ () (interactive) (term-send-raw-string "\C-_")) + +(defun term-send-function-key () + "If bound to a function key, this will send that key to the underlying shell." + (interactive) + (let ((key (this-command-keys-vector))) + (when (and (= (length key) 1) + (symbolp (elt key 0))) + (let ((name (symbol-name (elt key 0)))) + (when (string-match "\\`f\\([0-9]++\\)\\'" name) + (let* ((num (string-to-number (match-string 1 name))) + (ansi + (cond + ((<= num 5) (+ num 10)) + ((<= num 10) (+ num 11)) + ((<= num 14) (+ num 12)) + ((<= num 16) (+ num 13)) + ((<= num 20) (+ num 14))))) + (when ansi + (term-send-raw-string (format "\e[%d~" ansi))))))))) + (defun term-char-mode () "Switch to char (\"raw\") sub-mode of term mode. -- 2.39.2