|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import * as os from 'os'; |
| 3 | +import { |
| 4 | + commands, |
| 5 | + Disposable, |
| 6 | + ExtensionContext, |
| 7 | + TextEditorRevealType, |
| 8 | + window, |
| 9 | + workspace, |
| 10 | +} from 'vscode'; |
| 11 | + |
| 12 | +export function deactivate() {} |
| 13 | + |
| 14 | +export function activate(context: ExtensionContext) { |
| 15 | + let nemacs = new Nemacs(); |
| 16 | + context.subscriptions.push( |
| 17 | + commands.registerCommand("nemacs.mark", () => { |
| 18 | + nemacs.mark(); |
| 19 | + }), |
| 20 | + ); |
| 21 | + for (let cmd of [ |
| 22 | + "cursorUp", |
| 23 | + "cursorDown", |
| 24 | + "cursorLeft", |
| 25 | + "cursorRight", |
| 26 | + "cursorHome", |
| 27 | + "cursorEnd", |
| 28 | + "cursorPageUp", |
| 29 | + "cursorPageDown", |
| 30 | + ]) { |
| 31 | + context.subscriptions.push( |
| 32 | + commands.registerCommand(`nemacs.${cmd}`, () => { |
| 33 | + nemacs.cursorMove(cmd); |
| 34 | + }), |
| 35 | + ); |
| 36 | + } |
| 37 | + context.subscriptions.push( |
| 38 | + commands.registerCommand("nemacs.killLine", () => { |
| 39 | + nemacs.killLine(); |
| 40 | + }), |
| 41 | + ); |
| 42 | + context.subscriptions.push( |
| 43 | + commands.registerCommand("nemacs.yank", () => { |
| 44 | + nemacs.yank(); |
| 45 | + }), |
| 46 | + ); |
| 47 | + context.subscriptions.push( |
| 48 | + commands.registerCommand("nemacs.copy", () => { |
| 49 | + nemacs.copy(); |
| 50 | + }), |
| 51 | + ); |
| 52 | + context.subscriptions.push( |
| 53 | + commands.registerCommand("nemacs.centerVertically", () => { |
| 54 | + nemacs.centerVertically(); |
| 55 | + }), |
| 56 | + ); |
| 57 | + context.subscriptions.push(nemacs); |
| 58 | +} |
| 59 | + |
| 60 | +class Nemacs { |
| 61 | + private disposable: Disposable; |
| 62 | + private marked: boolean = false; |
| 63 | + private moving: boolean = false; |
| 64 | + private ringBuffer: string = ""; |
| 65 | + |
| 66 | + constructor() { |
| 67 | + let subscriptions: Disposable[] = []; |
| 68 | + window.onDidChangeTextEditorSelection( |
| 69 | + this.onDidChangeTextEditorSelection, |
| 70 | + this, |
| 71 | + subscriptions, |
| 72 | + ); |
| 73 | + this.disposable = Disposable.from(...subscriptions); |
| 74 | + } |
| 75 | + |
| 76 | + public mark() { |
| 77 | + this.marked = true; |
| 78 | + } |
| 79 | + |
| 80 | + public cursorMove(command: string) { |
| 81 | + if (this.marked) { |
| 82 | + this.moving = true; |
| 83 | + commands.executeCommand(`${command}Select`).then(() => { |
| 84 | + this.moving = false; |
| 85 | + }); |
| 86 | + } else { |
| 87 | + commands.executeCommand(command); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private copyRingBuffer() { |
| 92 | + if (this.ringBuffer) { |
| 93 | + if (os.platform() === "darwin") { |
| 94 | + execSync("pbcopy", { input: this.ringBuffer }); |
| 95 | + } |
| 96 | + this.ringBuffer = ""; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private onDidChangeTextEditorSelection() { |
| 101 | + if (this.marked && !this.moving) { |
| 102 | + this.cancelSelection(); |
| 103 | + } |
| 104 | + this.copyRingBuffer(); |
| 105 | + } |
| 106 | + |
| 107 | + public cancelSelection() { |
| 108 | + this.marked = false; |
| 109 | + this.moving = false; |
| 110 | + commands.executeCommand("cancelSelection"); |
| 111 | + } |
| 112 | + |
| 113 | + public killLine() { |
| 114 | + if (!window.activeTextEditor) return; |
| 115 | + let sel = window.activeTextEditor.selection; |
| 116 | + let line = window.activeTextEditor.document.lineAt(sel.active.line); |
| 117 | + if (this.marked) { |
| 118 | + commands.executeCommand("editor.action.clipboardCutAction"); |
| 119 | + } else if (line.range.end.character === sel.active.character) { |
| 120 | + if (line.lineNumber < window.activeTextEditor.document.lineCount - 1) { |
| 121 | + this.ringBuffer = |
| 122 | + this.ringBuffer + workspace.getConfiguration("files").get("eol"); |
| 123 | + commands.executeCommand("deleteRight"); |
| 124 | + } |
| 125 | + } else { |
| 126 | + this.ringBuffer = |
| 127 | + this.ringBuffer + |
| 128 | + line.text.substring(sel.active.character, line.range.end.character); |
| 129 | + commands.executeCommand("deleteAllRight"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public yank() { |
| 134 | + this.copyRingBuffer(); |
| 135 | + commands.executeCommand("editor.action.clipboardPasteAction"); |
| 136 | + } |
| 137 | + |
| 138 | + public copy() { |
| 139 | + commands.executeCommand("editor.action.clipboardCopyAction"); |
| 140 | + this.cancelSelection(); |
| 141 | + } |
| 142 | + |
| 143 | + public centerVertically() { |
| 144 | + window.activeTextEditor?.revealRange( |
| 145 | + window.activeTextEditor.selection.with(), |
| 146 | + TextEditorRevealType.InCenter, |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + dispose() { |
| 151 | + this.disposable.dispose(); |
| 152 | + } |
| 153 | +} |
0 commit comments