Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add emacs yank-pop M-y functionality #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@
"key": "ctrl+x r",
"command": "emacs.C-x_r",
"when": "editorTextFocus"
},{
"key": "alt+y",
"command": "emacs.M-y",
"when": "editorTextFocus"
},{
"key": "backspace",
"command": "emacs.backspace",
"when": "editorTextFocus"
}
]
},
Expand Down
92 changes: 55 additions & 37 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ enum KeybindProgressMode {
};

export class Editor {
private killRing: string;
private isKillRepeated: boolean;
private killRing: string[];
private ringIndex: number;
private keybindProgressMode: KeybindProgressMode;
private registersStorage: { [key:string] : RegisterContent; };
private yanked: boolean;

constructor() {
this.killRing = '';
this.isKillRepeated = false;
this.killRing = [];
this.keybindProgressMode = KeybindProgressMode.None;
this.registersStorage = {};
this.ringIndex = 0;
this.yanked = false;
vscode.window.onDidChangeTextEditorSelection(() => {
this.isKillRepeated = false;
this.yanked = false;
});
}

Expand Down Expand Up @@ -55,65 +57,55 @@ export class Editor {
/** Behave like Emacs kill command
*/
kill(): void {
let saveIsKillRepeated = this.isKillRepeated,
promises = [
let promises = [
vscode.commands.executeCommand("emacs.exitMarkMode"),
vscode.commands.executeCommand("cursorEndSelect")
];

Promise.all(promises).then(() => {
let selection = this.getSelection(),
range = new vscode.Range(selection.start, selection.end);

this.setSelection(range.start, range.start);
this.isKillRepeated = saveIsKillRepeated;
if (range.isEmpty) {
this.killEndOfLine(saveIsKillRepeated, range);
this.killEndOfLine(range);
} else {
this.killText(range);
}
});
}

private killEndOfLine(saveIsKillRepeated: boolean, range: vscode.Range): void {
private killEndOfLine(range: vscode.Range): void {
let doc = vscode.window.activeTextEditor.document,
eof = doc.lineAt(doc.lineCount - 1).range.end;

if (doc.lineCount && !range.end.isEqual(eof) &&
doc.lineAt(range.start.line).rangeIncludingLineBreak) {
this.isKillRepeated ? this.killRing += '\n' : this.killRing = '\n';
saveIsKillRepeated = true;
this.killRing[this.ringIndex++] = vscode.window.activeTextEditor.document.getText(range);
} else {
this.setStatusBarMessage("End of buffer");
}
vscode.commands.executeCommand("deleteRight").then(() => {
this.isKillRepeated = saveIsKillRepeated;
});
vscode.commands.executeCommand("deleteRight");
}

private killText(range: vscode.Range): void {
let text = vscode.window.activeTextEditor.document.getText(range),
promises = [
Editor.delete(range),
vscode.commands.executeCommand("emacs.exitMarkMode")
];

this.isKillRepeated ? this.killRing += text : this.killRing = text;
Promise.all(promises).then(() => {
this.isKillRepeated = true;
});
let text = vscode.window.activeTextEditor.document.getText(range);
this.killRing[this.ringIndex++] = text;
//max 20 items in the ring to avoid excessice memory use
//adjust if desired
this.ringIndex = this.ringIndex % 20;
Editor.delete(range);
vscode.commands.executeCommand("emacs.exitMarkMode");
}

copy(range: vscode.Range = null): boolean {
this.killRing = '';
if (range === null) {
range = this.getSelectionRange();
if (range === null) {
vscode.commands.executeCommand("emacs.exitMarkMode");
return false;
}
}
this.killRing = vscode.window.activeTextEditor.document.getText(range);
this.killRing[this.ringIndex++] = vscode.window.activeTextEditor.document.getText(range);
this.ringIndex = this.ringIndex % 20;
vscode.commands.executeCommand("emacs.exitMarkMode");
return this.killRing !== undefined;
}
Expand All @@ -128,14 +120,40 @@ export class Editor {
return true;
}

yank(): boolean {
async yank(): Promise<boolean> {
if (this.killRing.length === 0) {
return false;
}
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(this.getSelection().active, this.killRing);
await vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(this.getSelection().active, this.killRing[this.ringIndex - 1]);
});
this.yanked = true;
return true;
}
async yankPop(): Promise<boolean> {
if(!this.yanked) {
return new Promise<boolean>((resolve) => {
resolve(false);
});
}
let currentPosition = this.getSelection().active;
let lines = this.killRing[this.ringIndex -1].split("\n");
let linesNumber = lines.length - 1;
let lastLine = lines[linesNumber];
let endPosition = currentPosition.translate(-linesNumber, -lastLine.length);
let deleteThis = new vscode.Range(currentPosition, endPosition);
await vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.delete(deleteThis);
});
this.ringIndex--;
if (this.ringIndex === 0) {
this.ringIndex = this.killRing.length;
}
await vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.replace(this.getSelection().active, this.killRing[this.ringIndex - 1]);
});
this.isKillRepeated = false;
await vscode.commands.executeCommand("cancelSelection");
this.yanked = true;
return true;
}

Expand Down Expand Up @@ -297,11 +315,11 @@ export class Editor {
text: text
});
}
return;
return;
}

SaveTextToRegister(registerName: string): void {
if (null == registerName) {
if (null === registerName) {
return;
}
let range : vscode.Range = this.getSelectionRange();
Expand All @@ -317,7 +335,7 @@ export class Editor {
RestoreTextFromRegister(registerName: string): void {
vscode.commands.executeCommand("emacs.exitMarkMode"); // emulate Emacs
let obj : RegisterContent = this.registersStorage[registerName];
if (null == obj) {
if (null === obj) {
this.setStatusBarMessage("Register does not contain text.");
return;
}
Expand All @@ -331,4 +349,4 @@ export class Editor {
}
return;
}
}
}
16 changes: 9 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function activate(context: vscode.ExtensionContext): void {

// Edit
"C-k", "C-w", "M-w", "C-y", "C-x_C-o",
"C-x_u", "C-/",
"C-x_u", "C-/", "M-y", "backspace",

// R-Mode
"C-x_r"
Expand All @@ -35,17 +35,15 @@ export function activate(context: vscode.ExtensionContext): void {
element
);
})
)
);
});

// 'type' is not an "emacs." command and should be registered separately
context.subscriptions.push(vscode.commands.registerCommand("type", function (args) {
if (!vscode.window.activeTextEditor) {
return;
}
op.onType(args.text);
}));

initMarkMode(context);
}

Expand All @@ -55,9 +53,13 @@ export function deactivate(): void {
function initMarkMode(context: vscode.ExtensionContext): void {
context.subscriptions.push(vscode.commands.registerCommand(
'emacs.enterMarkMode', () => {
initSelection();
inMarkMode = true;
vscode.window.setStatusBarMessage("Mark Set", 1000);
if(!inMarkMode) {
initSelection();
inMarkMode = true;
vscode.window.setStatusBarMessage("Mark Set", 1000);
} else {
vscode.commands.executeCommand("emacs.exitMarkMode");
}
})
);

Expand Down
19 changes: 18 additions & 1 deletion src/operation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Editor} from './editor';

import {commands} from 'vscode';
export class Operation {
private editor: Editor;
private commandList: { [key: string]: (...args: any[]) => any, thisArgs?: any } = {};
Expand Down Expand Up @@ -48,6 +48,23 @@ export class Operation {
"C-x_r": () => {
this.editor.setRMode();
},
"M-y": () => {
let that = this;
Copy link
Contributor

@sammy44nts sammy44nts Jan 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is that for? I mean do we really need yankPop to return a Promise instead of just a boolean? Can you use this to be consistent with the previous code?

this.editor.yankPop().then(function(result) {
if (result) {
that.editor.setStatusBarMessage("Yank Pop");
} else {
that.editor.setStatusBarMessage("Previous command was not a yank");
}
}).catch(function(error) {
console.log(error);
});
},
"backspace": () => {
commands.executeCommand("deleteLeft").then(() => {
commands.executeCommand("emacs.exitMarkMode");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should be in editor.ts to avoid the import of vscode.commands in this file.

});
}
};
}

Expand Down