From 813ca5258f8d5bce88416f54d3beb645535aaa30 Mon Sep 17 00:00:00 2001 From: sebiik <24842260+sebiik@users.noreply.github.com> Date: Sun, 15 Dec 2019 20:43:29 +0800 Subject: [PATCH 1/4] Safety exit when applying macro while still recording If '@' is pressed while still recording a macro, a warning is displayed and recording is stopped. --- lib/vim-mode-plus-macros.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/vim-mode-plus-macros.js b/lib/vim-mode-plus-macros.js index c2be259..6e96128 100644 --- a/lib/vim-mode-plus-macros.js +++ b/lib/vim-mode-plus-macros.js @@ -55,8 +55,20 @@ export default { recording = false atom.notifications.addInfo('Stopped recording macro') }, + + stopCurrentlyRecordingMacro() { + let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) + editor.classList.remove('recording-macro') + recording = false + atom.notifications.addWarning('Currently recording macro! Stopping recording') + }, + applyMacro() { + if (recording === true) { + this.stopCurrentlyRecordingMacro(); + return + } atom.notifications.addInfo('Applying macro') let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) let cursor = editor.querySelector('.cursor') From 6f018b90be88f09513a7926fd607efcc623f5204 Mon Sep 17 00:00:00 2001 From: sebiik Date: Sun, 15 Dec 2019 20:54:35 +0800 Subject: [PATCH 2/4] add 'append' and 'apply and append' modes --- keymaps/macro-keymap.cson | 2 ++ lib/vim-mode-plus-macros.js | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/keymaps/macro-keymap.cson b/keymaps/macro-keymap.cson index 021ffa8..d85cbee 100644 --- a/keymaps/macro-keymap.cson +++ b/keymaps/macro-keymap.cson @@ -1,6 +1,8 @@ 'atom-text-editor.vim-mode-plus.normal-mode': 'q': 'vim-mode-plus-macros:start-recording-macro' '@': 'vim-mode-plus-macros:apply-macro' + 'Q': 'vim-mode-plus-macros:append-to-macro' + 'g @': 'vim-mode-plus-macros:apply-and-append-to-macro' 'atom-text-editor.vim-mode-plus.normal-mode.recording-macro': 'q': 'vim-mode-plus-macros:stop-recording-macro' diff --git a/lib/vim-mode-plus-macros.js b/lib/vim-mode-plus-macros.js index c2be259..64d990c 100644 --- a/lib/vim-mode-plus-macros.js +++ b/lib/vim-mode-plus-macros.js @@ -31,7 +31,9 @@ export default { this.subscriptions.add(atom.commands.add('atom-workspace', Object.assign(characterCommands, { 'vim-mode-plus-macros:start-recording-macro': () => this.startRecordingMacro(), 'vim-mode-plus-macros:stop-recording-macro': () => this.stopRecordingMacro(), - 'vim-mode-plus-macros:apply-macro': () => this.applyMacro() + 'vim-mode-plus-macros:apply-macro': () => this.applyMacro(), + 'vim-mode-plus-macros:append-to-macro': () => this.appendToMacro(), + 'vim-mode-plus-macros:apply-and-append-to-macro': () => this.applyAndAppendToMacro() }))) atom.commands.onDidDispatch(this.logCommand) @@ -46,7 +48,7 @@ export default { editor.classList.add('recording-macro') commandQueue = [] // empty the queue recording = true - atom.notifications.addInfo('Recording macro') + atom.notifications.addInfo('Recording macro...') }, stopRecordingMacro() { @@ -56,13 +58,47 @@ export default { atom.notifications.addInfo('Stopped recording macro') }, + stopCurrentlyRecordingMacro() { + let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) + editor.classList.remove('recording-macro') + recording = false + atom.notifications.addWarning('Currently recording macro! Stopping recording') + }, + applyMacro() { + if (recording === true) { + this.stopCurrentlyRecordingMacro(); + return + } + let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) + let cursor = editor.querySelector('.cursor') atom.notifications.addInfo('Applying macro') + commandQueue.forEach(command => { + atom.commands.dispatch(cursor, command) + }) + }, + + appendToMacro() { + let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) + let cursor = editor.querySelector('.cursor') + editor.classList.add('recording-macro') + recording = true + atom.notifications.addInfo('Appending to macro...') + }, + + applyAndAppendToMacro() { let editor = atom.views.getView(atom.workspace.getActiveTextEditor()) + if (recording === true) { + this.stopCurrentlyRecordingMacro(); + return + } let cursor = editor.querySelector('.cursor') commandQueue.forEach(command => { atom.commands.dispatch(cursor, command) }) + editor.classList.add('recording-macro') + recording = true + atom.notifications.addInfo('Applying then appending to macro...') }, logCommand(event) { @@ -70,7 +106,9 @@ export default { return } const commandName = event.type - if (commandName === 'vim-mode-plus-macros:start-recording-macro') { + if (commandName === 'vim-mode-plus-macros:start-recording-macro' || + commandName === 'vim-mode-plus-macros:append-to-macro' || + commandName === 'vim-mode-plus-macros:apply-and-append-to-macro') { return } commandQueue.push(commandName) From d810d34f4921ac47d00177a4411e765d63e3ec5b Mon Sep 17 00:00:00 2001 From: sebiik Date: Mon, 16 Dec 2019 14:21:21 +0800 Subject: [PATCH 3/4] put list of macro commands --- keymaps/macro-keymap.cson | 3 ++- lib/vim-mode-plus-macros.js | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/keymaps/macro-keymap.cson b/keymaps/macro-keymap.cson index d85cbee..eb537d2 100644 --- a/keymaps/macro-keymap.cson +++ b/keymaps/macro-keymap.cson @@ -2,7 +2,8 @@ 'q': 'vim-mode-plus-macros:start-recording-macro' '@': 'vim-mode-plus-macros:apply-macro' 'Q': 'vim-mode-plus-macros:append-to-macro' - 'g @': 'vim-mode-plus-macros:apply-and-append-to-macro' + 'g Q': 'vim-mode-plus-macros:apply-and-append-to-macro' + 'g @': 'vim-mode-plus-macros:put-macro-as-text'; 'atom-text-editor.vim-mode-plus.normal-mode.recording-macro': 'q': 'vim-mode-plus-macros:stop-recording-macro' diff --git a/lib/vim-mode-plus-macros.js b/lib/vim-mode-plus-macros.js index ad736f5..cf92df8 100644 --- a/lib/vim-mode-plus-macros.js +++ b/lib/vim-mode-plus-macros.js @@ -33,7 +33,8 @@ export default { 'vim-mode-plus-macros:stop-recording-macro': () => this.stopRecordingMacro(), 'vim-mode-plus-macros:apply-macro': () => this.applyMacro(), 'vim-mode-plus-macros:append-to-macro': () => this.appendToMacro(), - 'vim-mode-plus-macros:apply-and-append-to-macro': () => this.applyAndAppendToMacro() + 'vim-mode-plus-macros:apply-and-append-to-macro': () => this.applyAndAppendToMacro(), + 'vim-mode-plus-macros:put-macro-as-text': () => this.putMacroAsText() }))) atom.commands.onDidDispatch(this.logCommand); @@ -101,6 +102,22 @@ export default { atom.notifications.addInfo('Applying then appending to macro...') }, + putMacroAsText() { + if (recording === true) { + this.stopCurrentlyRecordingMacro() + return + } + let editor = atom.workspace.getActiveTextEditor() + if (editor) { + editor.insertText('{\n') + commandQueue.forEach(command => { + editor.insertText(' '+command+'\n') + }) + editor.insertText('}') + } + atom.notifications.addInfo('Putting macro as list of commmands') + }, + logCommand(event) { if (!recording) { return From 265e7b3e5179a0e86d0c01e3d3c075cacc9751c0 Mon Sep 17 00:00:00 2001 From: sebiik Date: Mon, 16 Dec 2019 15:18:52 +0800 Subject: [PATCH 4/4] tweak 'put macro command list as text' --- keymaps/macro-keymap.cson | 2 +- lib/vim-mode-plus-macros.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/keymaps/macro-keymap.cson b/keymaps/macro-keymap.cson index eb537d2..ad0459c 100644 --- a/keymaps/macro-keymap.cson +++ b/keymaps/macro-keymap.cson @@ -3,7 +3,7 @@ '@': 'vim-mode-plus-macros:apply-macro' 'Q': 'vim-mode-plus-macros:append-to-macro' 'g Q': 'vim-mode-plus-macros:apply-and-append-to-macro' - 'g @': 'vim-mode-plus-macros:put-macro-as-text'; + 'g @': 'vim-mode-plus-macros:put-macro-commands-as-text'; 'atom-text-editor.vim-mode-plus.normal-mode.recording-macro': 'q': 'vim-mode-plus-macros:stop-recording-macro' diff --git a/lib/vim-mode-plus-macros.js b/lib/vim-mode-plus-macros.js index cf92df8..f330819 100644 --- a/lib/vim-mode-plus-macros.js +++ b/lib/vim-mode-plus-macros.js @@ -34,7 +34,7 @@ export default { 'vim-mode-plus-macros:apply-macro': () => this.applyMacro(), 'vim-mode-plus-macros:append-to-macro': () => this.appendToMacro(), 'vim-mode-plus-macros:apply-and-append-to-macro': () => this.applyAndAppendToMacro(), - 'vim-mode-plus-macros:put-macro-as-text': () => this.putMacroAsText() + 'vim-mode-plus-macros:put-macro-commands-as-text': () => this.putMacroCommandsAsText() }))) atom.commands.onDidDispatch(this.logCommand); @@ -102,7 +102,7 @@ export default { atom.notifications.addInfo('Applying then appending to macro...') }, - putMacroAsText() { + putMacroCommandsAsText() { if (recording === true) { this.stopCurrentlyRecordingMacro() return @@ -115,7 +115,7 @@ export default { }) editor.insertText('}') } - atom.notifications.addInfo('Putting macro as list of commmands') + atom.notifications.addInfo('Putting macro list of commmands') }, logCommand(event) {