How to insert text into terminal buffer of nvim by :normal command? - neovim

In a normal buffer of nvim, the following ex command works.
:normal! GAabcd
This adds the text "abcd" to the current normal buffer.
But in a terminal buffer of nvim, the above ex command doesn't work!
:normal! GAabcd
This just converts the terminal buffer into '-- TERMINAL --' mode and the "abcd" isn't added!
Why isn't the text added? How can I accomplish that?

I digged the neovim sources and maybe found the answer by myself.
In edit funtion of edit.c, there is the following comment.
bool edit(int cmdchar, bool startln, long count)
{
if (curbuf->terminal) {
if (ex_normal_busy) {
// Do not enter terminal mode from ex_normal(), which would cause havoc
// (such as terminal-mode recursiveness). Instead set a flag to force-set
// the value of `restart_edit` before `ex_normal` returns.
restart_edit = 'i';
force_restart_edit = true;
} else {
terminal_enter();
}
return false;
}
...
I think it means that it's not possible to enter terminal mode(insert mode) while processing normal commands in ex-mode. Therefore the text 'abcd' wasn't added in terminal mode but was just processed as normal commands.

Your answer is correct in that it's not possible to append text using :normal! GAfoo. However, in the interests of answering your last question, you can do this:
:normal! "0p
So if you happen to have what you need in a register, you can always paste it.

Related

Neovim, how to jump back to a terminal

help terminal-input said
To use `ALT+{h,j,k,l}` to navigate windows from any mode: >
:tnoremap <A-h> <C-\><C-N><C-w>h
:tnoremap <A-j> <C-\><C-N><C-w>j
:tnoremap <A-k> <C-\><C-N><C-w>k
:tnoremap <A-l> <C-\><C-N><C-w>l
:inoremap <A-h> <C-\><C-N><C-w>h
:inoremap <A-j> <C-\><C-N><C-w>j
:inoremap <A-k> <C-\><C-N><C-w>k
:inoremap <A-l> <C-\><C-N><C-w>l
:nnoremap <A-h> <C-w>h
:nnoremap <A-j> <C-w>j
:nnoremap <A-k> <C-w>k
:nnoremap <A-l> <C-w>l
.
Thus I add these shortcut maps into my init file. And when a terminal is open, i type <A-j>, the cursor jumped from the terminal into the normal interface (The terminal interface does not close.). i want jump back to the terminal interface. However, all the shortcuts do not work. They only jump across windows.
:) i wanted the same thing i searched online to learn how to write a custom function around buffers, then came with this:
function SwitchToTerm()
let term_bufs = getbufinfo({'buflisted':1})
let curr_buf = bufnr('%')
call filter(term_bufs, 'v:val.name =~ "^term"')
call filter(term_bufs, 'v:val.bufnr != ' . curr_buf)
call map(term_bufs, { _, e -> ({"nr": e.bufnr, "lu": e.lastused}) })
if bufname('%') =~ "^term"
call sort(term_bufs, { b1, b2 -> b1.lu - b2.lu })
else
call sort(term_bufs, { b1, b2 -> b2.lu - b1.lu })
endif
exec "b " . term_bufs[0].nr
endfunction
nnoremap <silent> <Leader>t :call SwitchToTerm()<cr>
in my ~/.vimrc and using Neovim but not sure if that matters. It is supposed to go to the most recently used buffer, then continuously cycle through terminal buffers if already on at a terminal. I switch to BufExplorer a lot to visit another file, and the last terminal becomes iffy there... but switching buffers directly from the command line works fine.

How to fold all open editors in visual studio code using a script

I would like to be able to create/write a command to fold all code in all open editors within visual studio code.
I believe I am very close.
I am using the "script commands" extension written by Marcel J. Kloubert
When I use the following script with 7 or so open editors in a single group. I achieve the following:
The open editor (at the time of execution) has its code folded
VSC will loop over the open editors
No other editor has its code folded
The script I am using:
// Fold all code in all open editors.
function execute(args) {
// Obtain access to vscode
var vscode = args.require('vscode');
// Set number of open editors... (future: query vscode for number of open editors)
var numOpenEditor = 20;
// Loop for numOpenEditor times
for (var i = 0; i <= numOpenEditor; i++){
// Fold the current open editor
vscode.commands.executeCommand('editor.foldAll');
// Move to the next editor to the right
vscode.commands.executeCommand('workbench.action.nextEditor');
// Loop message
var statusString = 'Loop ->' + i
// print message
vscode.window.showErrorMessage(statusString);
}
}
// Script Commands must have a public execute() function to work.
exports.execute = execute;
I have made an interesting observation, when I use the above script with 7 or so open editors with two groups or more. Something about switching to a new group will allow the command editor.foldAll to work. Note, that if a group has multiple editors, the only editor to fold its code is the open editor in the group. Thus, all other editors will not fold.
I also thought that maybe... the script needed to slow down, so I added a function to pause on every iteration. This did not turn out to work either.
Any help would be great!
You just need to make this function async and wait for the executeCommand calls to complete before moving on:
// Fold all code in all open editors.
async function execute(args) {
// Obtain access to vscode
var vscode = args.require('vscode');
// Set number of open editors... (future: query vscode for number of open editors)
var numOpenEditor = 5;
// Loop for numOpenEditor times
for (var i = 0; i <= numOpenEditor; i++) {
// Fold the current open editor
await vscode.commands.executeCommand('editor.foldAll');
// Move to the next editor to the right
await vscode.commands.executeCommand('workbench.action.nextEditor');
// Loop message
var statusString = 'Loop ->' + i
// print message
vscode.window.showErrorMessage(statusString);
}
}
// Script Commands must have a public execute() function to work.
exports.execute = execute;

how to get vs code `ENTER` to respect/cleanup whitespace

BTW, this is not asking how to format code in VS, or the keybinding, so please don't mark it duplicate of this. It is specifically about formatting during typing.
Given the following code:
if(true) {
console.log('hello');
console.log('world');
}
If I go to the end of the 'log hello' line, hit DEL to bring 'log world' to the end of the line I will get (with | representing my cursor):
if(true) {
console.log('hello');| console.log('world');
}
Now, if I hit ENTER, I will get:
if(true) {
console.log('hello');
| console.log('world');
}
In other editors I would expect the white space to be 'eaten up' and the line to be reformatted, looking like this again:
if(true) {
console.log('hello');
|console.log('world');
}
Any thoughts on how to change this behavior?
This would also (hopefully) handle the pasting in of code that was not properly formatted, for instance, if I wanted to paste the following in:
console.log('arrrgg');
console.log('matey');
To the end of the if, I would see:
if(true) {
console.log('hello');
console.log('world');
console.log('arrrgg');
console.log('matey');
}
Instead of:
if(true) {
console.log('hello');
console.log('world');
console.log('arrrgg');
console.log('matey');
}
Certainly, I can hit Shift-Alt-F and reformat, but I don't always want to reformat an entire file....
I think I have been spoiled with VS and WebStorm automatically cleaning up and reformatting code quickly. Not sure if that is a valid
Is this even possible in Code?

How can I prevent tinyMCE's paste event?

I need to prevent the paste event of tinyMCE if the length of the current content of editor plus the length of the words to be pasted exceed the specified limit. How can I do it? Thank you.
I was wrong. I dont need to prevent or disable paste in tinyMCE to do this. I used their paste plugin and modified the content before it is pasted.
function(pl, o) {
...
if(len > limit) {
o.content = '';
}
}
I think it's better to use the paste_preprocess function.
It's a old question, but others may stumble on this problem.
Here is the solution:
paste_preprocess: function (plugin, args) {
args.content = ''; // modify or do anything with the clipboard data
},
Do it inside, init

Convert GdkX11Window to VTE Terminal class in an focus event

I need to receive a GTK+ focus in event on a Terminal (VTE), but the event returns EventFocus which holds Gtk.Window reference:
http://www.valadoc.org/gdk-3.0/Gdk.EventFocus.html
How can I get the Terminal from the Window reference? I cannot retype it, it looks like it is a container. But I am unable to find which method to call to get the Terminal.
Terminal terminal = new Terminal();
// ...
terminal.focus_in_event.connect((event) =>
{
the_terminal = event.window; // DOES NOT WORK > invalid cast from `GdkX11Window' to `Terminal'
return false;
});
Thanks for pointing out I dont need it. Yeah, my real code is:
for (int i = 0; i < terminal.length; i++) {
this.terminal[i].focus_in_event.connect((event) =>
{
GLib.stdout.printf("Focus event terminal %p\n", this.terminal[i]);
return false;
});
}
Unfortunately it always prints null :-(
Thanks!
I'm not sure there is an easy way to convert a Gdk.Window to a Gtk.Widget as not all widgets have an associated GDK window, necessarily. As I see it, there's no compelling reason to try to extract the terminal from the event. In the context of the callback, you can simply reference the outer variable terminal and Vala will lift it into the callback.
Terminal terminal = new Terminal();
// ...
terminal.focus_in_event.connect((event) =>
{
terminal.queue_draw();
return false;
});