Parcourir la source

Add smart copy/paste and update docs

Steven Silvester il y a 6 ans
Parent
commit
901efa0d5c

+ 12 - 13
docs/source/user/terminal.rst

@@ -47,20 +47,19 @@ re-open it using the Running tab in the left sidebar:
 Copy/Paste
 ~~~~~~~~~~~~
 
-For both Windows and Mac users, press ``shift`` + ``right-click`` to get to the right-click menu from inside Jupyterlab terminal. Or by using shortcut keys as follows:
+For all platforms, JupyterLab will interpret ``Ctrl+C`` as a copy if there is text selected.
+In addition, ``Ctrl+V`` will be interpreted as a paste command unless the ``pasteWithCtrlV``
+setting is disabled.  One may want to disable ``pasteWithCtrlV`` if the shortcut is needed
+for something else such as the vi editor.
 
-* **For Mac users:**
-
-	**Copy** : ``Cmd`` + ``c`` 
-
-	**Paste**: ``Cmd`` + ``v``
-
-
-* **For Windows users (Power shell):**
-
-	**Copy** : ``Ctrl`` + ``Insert``
-
-	**Paste** : ``Shift`` + ``Insert``
+To use the native browser Copy/Paste menu, hold ``Shift`` and right click to bring up the
+context menu.
 
+For MacOS users, ``Cmd+C`` and ``Cmd+V`` work as usual.
 
+For Windows users using ``PowerShell``, ``Ctrl+Insert`` and ``Shift+Insert`` work as usual.
 
+For anyone using a *nix shell, the default ``Ctrl+Shift+C`` conflicts with the default
+shortcut for toggling the command palette (``apputils:activate-command-palette``).
+If desired, that shortcut can be changed by editing the keyboard shortcuts in setttings.
+Using ``Ctrl+Shift+V`` for paste works as usual.

+ 9 - 0
packages/terminal-extension/schema/plugin.json

@@ -21,6 +21,9 @@
     },
     "scrollback": {
       "type": "number"
+    },
+    "pasteWithCtrlV": {
+      "type": "boolean"
     }
   },
   "properties": {
@@ -59,6 +62,12 @@
       "description": "Whether to shut down or not the session when closing the terminal.",
       "type": "boolean",
       "default": false
+    },
+    "pasteWithCtrlV": {
+      "title": "Paste with Ctrl+V",
+      "description": "Whether to enable pasting with Ctrl+V.  This can be disabled to use Ctrl+V in the vi editor, for instance",
+      "type": "boolean",
+      "default": true
     }
   },
   "additionalProperties": false,

+ 8 - 2
packages/terminal/src/tokens.ts

@@ -96,11 +96,16 @@ export namespace ITerminal {
     initialCommand: string;
 
     /**
-     * Wether to enable screen reader support.
+     * Whether to enable screen reader support.
      *
      * Set to false if you run into performance problems from DOM overhead
      */
     screenReaderMode: boolean;
+
+    /**
+     * Whether to enable using Ctrl+V to paste.
+     */
+    pasteWithCtrlV: boolean;
   }
 
   /**
@@ -115,7 +120,8 @@ export namespace ITerminal {
     shutdownOnClose: false,
     cursorBlink: true,
     initialCommand: '',
-    screenReaderMode: true
+    screenReaderMode: true,
+    pasteWithCtrlV: true
   };
 
   /**

+ 27 - 3
packages/terminal/src/widget.ts

@@ -237,7 +237,8 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
    * Initialize the terminal object.
    */
   private _initializeTerm(): void {
-    this._term.on('data', (data: string) => {
+    const term = this._term;
+    term.on('data', (data: string) => {
       if (this.isDisposed) {
         return;
       }
@@ -247,9 +248,32 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
       });
     });
 
-    this._term.on('title', (title: string) => {
+    term.on('title', (title: string) => {
       this.title.label = title;
     });
+
+    term.attachCustomKeyEventHandler(event => {
+      if (
+        (event.ctrlKey || event.metaKey) &&
+        event.key === 'c' &&
+        term.hasSelection()
+      ) {
+        // Return so that the usual OS copy happens
+        // instead of interrupt signal.
+        return false;
+      }
+
+      if (
+        (event.ctrlKey || event.metaKey) &&
+        event.key === 'v' &&
+        this._options.pasteWithCtrlV
+      ) {
+        // Return so that the usual paste happens.
+        return false;
+      }
+
+      return true;
+    });
   }
 
   /**
@@ -303,7 +327,7 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
     }
   }
 
-  private _term: Xterm;
+  private readonly _term: Xterm;
   private _needsResize = true;
   private _termOpened = false;
   private _offsetWidth = -1;