Skip to content

Commit

Permalink
support source command for using .vimrc in home directory esm7#157
Browse files Browse the repository at this point in the history
  • Loading branch information
shengxuanwei committed Jan 4, 2023
1 parent 9bd6370 commit cc12724
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,9 @@ export default class VimrcPlugin extends Plugin {
this.defineSurround(this.codeMirrorVimObject);
this.defineJsCommand(this.codeMirrorVimObject);
this.defineJsFile(this.codeMirrorVimObject);
this.defineSource(this.codeMirrorVimObject);

vimCommands.split("\n").forEach(
function (line: string, index: number, arr: [string]) {
if (line.trim().length > 0 && line.trim()[0] != '"') {
let split = line.split(" ")
if (mappingCommands.includes(split[0])) {
// Have to do this because "vim-command-done" event doesn't actually work properly, or something.
this.customVimKeybinds[split[1]] = true
}
this.codeMirrorVimObject.handleEx(cmEditor, line);
}
}.bind(this) // Faster than an arrow function. https://stackoverflow.com/questions/50375440/binding-vs-arrow-function-for-react-onclick-event
)
this.loadVimCommands(vimCommands);

this.prepareChordDisplay();
this.prepareVimModeDisplay();
Expand All @@ -275,6 +265,26 @@ export default class VimrcPlugin extends Plugin {
}
}

loadVimCommands(vimCommands: string) {
let view = this.getActiveView();
if (view) {
var cmEditor = this.getCodeMirror(view);

vimCommands.split("\n").forEach(
function (line: string, index: number, arr: [string]) {
if (line.trim().length > 0 && line.trim()[0] != '"') {
let split = line.split(" ")
if (mappingCommands.includes(split[0])) {
// Have to do this because "vim-command-done" event doesn't actually work properly, or something.
this.customVimKeybinds[split[1]] = true
}
this.codeMirrorVimObject.handleEx(cmEditor, line);
}
}.bind(this) // Faster than an arrow function. https://stackoverflow.com/questions/50375440/binding-vs-arrow-function-for-react-onclick-event
)
}
}

defineBasicCommands(vimObject: any) {
vimObject.defineOption('clipboard', '', 'string', ['clip'], (value: string, cm: any) => {
if (value) {
Expand Down Expand Up @@ -632,6 +642,21 @@ export default class VimrcPlugin extends Plugin {
});
}

defineSource(vimObject: any) {
vimObject.defineEx('source', '', async (cm: any, params: any) => {
console.log(params);
const fileName = params.argString.trim();
let vimrcContent = '';
try {
this.app.vault.adapter.read(fileName).then(vimrcContent => {
this.loadVimCommands(vimrcContent);
});
} catch (e) {
console.log('Error loading vimrc file', fileName, 'from the vault root', e.message)
}
});
}

}

class SettingsTab extends PluginSettingTab {
Expand Down

0 comments on commit cc12724

Please sign in to comment.