Lightweight event wrapper for the terminal - macos

I believe this is the realm of the ncurses library. I'm trying to avoid having to get down and dirty with it though.
I'm looking for a program that I can configure to run a command while performing terminal mouse reporting translation to keypresses.
This is for use with pagers like less.
For example the MouseTerm SIMBL plugin for Terminal.app does exactly this.
But iTerm2 does not. And I want it.
I think the answer may be as simple as directly remapping the codes.
It looks like there are escape codes to switch the terminal into and out of mouse-listening mode, and mouse click escape codes actually seem to include the character coordinates. I can look at them with Ctrl+V inside of Vim because I have told vim to turn on the mouse.
It looks like this:
Note ^[ denotes escape (you can type escape by typing ctrl+[)
left click: ^[[M !!
right click: ^[[M"!!
middle click: ^[[M!!!
scroll up: ^[[M`!!
scroll down: ^[[Ma!!
So that does match up with the mouse wheel button codes being 64 more than the mouse button ones according to documentation (I like this page).
Now that I'm armed with the knowledge of what codes I need to map to what I just need to find out how to get a layer that lets me filter the input.
This has apparently led me to an epiphany. I simply need a simple non-line-buffering program that listens for mouse escape codes and replaces them with key codes. Surely Perl Term::ReadKey will let me set raw mode and do this nearly trivial task.

This stuff is difficult. I've been making do by configuring Tmux to handle things.

Related

Some questions related the autocompletion of the fish shell

When typing the beginning of a command in the fish shell the most recent (or frequently?) possible completion of the command is visible in dark grey.
Say I type:
fish
in dark grey: _config is appended.
at this time it is not yet evident, what I'm about to do. So the TAB key shows me all possible completions of 'fish'
I can keep on typing characters, until it's clear what I want. E.g: _con
Now there is only one option to which this could be completed. So I can hit the tab key to see fish_config. However: this was not indicated somehow. In other words: After typing fish_con nothing really tells me that I don't have to keep on typing. Is this the case? Wouldn't this be extremely helpful?
Second question: What is the actual sense of the grey characters? I'd only understand their purpose if there was a way to accept this propose. After typing f, I'd expect a key combination that immediately fully completes to the propose in grey: fish_config.
Even better would be the option let the grey letters cycle through all options, or possible completions based on the history.
The characters to the right of the cursor are called the autosuggestion. They are gray to indicate that they are not actually part of the command, just a suggested completion of what you've typed so far.
So I can hit the tab key to see fish_config. However: this was not indicated somehow. In other words: After typing fish_con nothing really tells me that I don't have to keep on typing
This sounds like you have an idea for an indication when the partial command is a unique prefix of another command. I am not sure what UI you have in mind - what would the indication look like? Please feel welcome to open an issue with your UI ideas.
However if your command is unique, the autosuggestion will always contain it.
Second question: What is the actual sense of the grey characters? I'd only understand their purpose if there was a way to accept this propose. After typing f, I'd expect a key combination that immediately fully completes to the propose in grey: fish_config.
You can accept the autosuggestion by hitting right arrow or control-F. Tab shows you all possible completions, and up arrow lets you cycle through matching history.
You may want to read the fish tutorial, which covers autosuggestions here: http://fishshell.com/docs/current/tutorial.html#tut_autosuggestions

Prefered keystroke for ending NSTextView edit

Is there prefered keystroke for ending edit of an NSTextVIew? Obvious candidates are ESCape and Return with modifier key. I'd like this to be fairly intuitive and easy to type. Of course for people raised on Vim, Escape is the obvious choice.
Since I mentioned Vim, I thought I'd add my emacsish version:
For emacs I guess it might be C-x C-S. I would find this more convenient typing than ESC. But what would be the cocoa way?
Command+return is quite common for confirming multi-line input (this is used for example in Apple's iWork when editing a text frame).
In standard Cocoa text views, Esc triggers auto-completion suggestions (when applicable).
tab is the most popular to switch between different textfields and textviews and is considered as end of editing.
Also you can use any combination as per your likings, e.g. cmd+enter etc to move out of the textView.
But no to esc, it sends as something you are cancelling. However this is used in CUI as vim, but surely not in GUI for OSX and even for Windows.

Getting "complete" and "menu-complete" to work together

I found out that the Bash shell supports a type of autocompletion that is different from the "traditional" autocompletion, where all possibilities get listed on the following line.
With the "traditional" autocompletion, if I type ch and then press the Tab key, I get something like:
$ ch
chacl chgrp chmod chown chvt
But if I add the following line to my /etc/inputrc (which remaps the Tab key to the built-in menu-complete function):
Tab: menu-complete
then the behavior of the shell changes: the word to be completed is replaced "inline" with a single match from the list of possible completions, and if I press the Tab key again, the word gets replaced with the next match.
I found this useful, but I still wanted to keep the traditional autocompletion and have it bound to the key combination Ctrl + Tab. So I added the following line to my /etc/inputrc file, according to what the readline library documentation suggests:
Ctrl-Tab: complete
However, adding this line only seems to make both Tab and Ctrl-Tab call the traditional complete function.
Does anyone know what I am doing wrong?
Thanks in advance!
To start with, I'm not a massive expert in this area, but I think I can answer your question. First of all, while you are using Bash, Bash is a shell which interprets keyboard commands that it receives from a terminal / console. While you are informing Bash how to react to specific key combinations in the inputrc file, your Terminal determines precisely which character is 'sent' to the Shell before the inputrc file even enters the equation.
Unfortunately, on my system (granted, it's OSX - but I don't think this is strange behaviour when compared to Linux), both Tab and Ctrl-Tab send the same keyboard input to the shell. Infact, both Tab and Ctrl-Tab send a Ctrl-I command to the shell, and indeed, if I enter Ctrl-I when using the terminal, it performs the completion as if I hit Tab.
The software (installed on most Linux systems by default), showkey will tell you what keys the shell is receiving when you press specific keyboard inputs as you push them.
Anyway, my suggestion to you is to use Shift-Tab, which does appear to send it's own key-code to the shell. Shift-Tab on my computer shows up (using showkey) as '<ESC>[Z', which I think is pretty standard across the board. As such, your inputrc file with the following bindings should allow you to use shift-tab instead of ctrl-tab to achieve what you desire:
Tab: menu-complete
"\e[Z": complete
The \e in the second binding represents the escape character, and the [Z are simply the characters as shown using showkey. You can get a similar effect on OSX by simply using cat, running cat from within a terminal and pressing Shift-Tab will show you "^[[Z", where ^[ represents the escape character and the other characters are as before.
I know this doesn't resolve your question precisely, but as I don't think you are able to use Ctrl-Tab as a key combination, without re-mapping Ctrl-Tab to another keybinding within your terminal (more likely to be easier if you are using a GUI terminal), this is likely as close as you can get without significant effort!
I have ShiftTab bound to menu-complete-backward, so it goes back one step if I skipped the right completion, and I've mapped Ctrlq to complete, so if there are several possible completions I hit Ctrlq to list them without having to cycle through them.
# Make Tab cycle between possible completions
# Cycle forward: Tab
# Cycle backward: Shift-Tab
TAB: menu-complete
"\e[Z": menu-complete-backward
# Make C-q display the list of possible completions
Control-q: complete
# Display the list of matches when no further completion is possible
set show-all-if-unmodified on
Edit: Ctrlq is bound to quoted-insert by default, that is, it tells the shell to take the next key literally. quoted-insert is also bound to Ctrlv, so you don't lose that functionality if you rebind Ctrlq. Anyway, I've found that AltESC also works, by default, for showing the possible completions (as far as I can tell it is equivalent to TAB); note that it may be seized by Gnome, then either double press ESC or rebind "Switch windows directly" in Settings → Devices → Keyboard → Navigation.
The following should achieve what you're looking for (if I understand correctly!)
In your .inputrc
# display all possible matches for an ambiguous pattern at first tab
set show-all-if-ambiguous on
# next tab(s) will cycle through matches
TAB: menu-complete
# shift tab cycles backward
"\e[Z": menu-complete-backward
Where to start, if you can or cant do this is dependent your keyboard and your drivers and there isn't one catch all answer. Each key press and release generates a sequenced key pair (key down and release) (scancode) these codes are then translated by the kernel into keycodes for example on my laptop keyboard 0x3a 0xba are translated to keycode 15 (down and up) these are then translated into actions such as return letter c a / you can assign actions to keysyms using the keycode/hex/binary/octal notation which codes match which letters is determined by the kernel translation table which is fairly standardized, however the first part signal that's translated to keycodes is different for most keyboards.
Continuing with the earlier example for me shift tab (and alt and control and any combination) produce keycode 15 however in hex it produces 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a and this is because shift alt and control are special keys (modifiers) these multiply out against the keycodes and fill out the dumpkeys table the kernel is limited to the number of assignments as well this is determined by your choice of keymap and shares resources with your terminal colors (if your char set its defined above the threshold it limits your terminal color scope). And this all goes out the window if your in an xserver and has a whole new system. Most of these things can be changed,modified and manipulated by the user and programs installed. My point to all this is to emphasize that there is no catch all for the mapping of the tab key and its going to vary keyboard drivers to kbd drivers (now if you find a solution that happens to work for you excellent :)) but chances are it won't be portable and might not work if you change keyboards and might not translate between xserver and tui. What i recommend is learning the steps to modify your kbd on the go.
will give you the decimal octal hex notation for a key press on the same line
--full-table -1 >> keytable
will give you a documented with your full list of keycode->keysym pairing in a format that will give you a better picture of your layout and from there you can either use loadkey to change a keys value or ad an entry in .inputrc or your main rc file. You can also create a custom key.map file.
Further escape sequence translation is determined by the "$TERM" variable and each virtual terminal emulator can be different
infocmp "$TERM"
will give you a list of your terminal escape sequences
Resources:
https://man7.org/linux/man-pages/man4/console_codes.4.html
https://www.gnu.org/software/screen/manual/html_node/Input-Translation.html
http://kbd-project.org/docs/scancodes/scancodes.html
https://www.vt100.net/
So to sum up.
Your keyboard drivers
Your kemap choice
Your virtual terminal emulator
and your kernel
form the backbone of remapping dificult keys (tab/s-tab/a-tab)
I'm not sure Ctrl-Tab is a real character; my terminal, for instance, ignores the combination. I think the only way to use Ctrl-Tab is to use your terminal emulator to map it to some otherwise unused escape sequence, then bind that sequence to complete.

How Do ncurses et. al. Work?

There are several libraries like ncurses that assist in making command-line GUIs.
Simply put, how do they work?
My first thought was that ncurses intercepts all keyboard input, and draws each "frame" by outputting it line-by-line normally. Closer inspection, however, reveals that each new frame overwrites the previous one. How does it modify lines that have already been outputted? Furthermore, how does it handle color?
EDIT: The same question applies to anything with a "fancy" interface, like vim and emacs.
Text terminals have command sequences that do things like move the cursor to a particular position on the screen, insert characters, delete lines etc.
Each terminal type is different and has its own set of command sequences. ncurses has a databse (see terminfo for details)
Internally ncurses maintains 2 views of the screen: the current contents and what the screen should look like after the current pending changes are applied. Once the program requests a screen redraw, ncurses calculates an efficient way to update the screen to look like the desired view. The exact characters/command sequences output depend on what terminal type is in use.
curses (and ncurses, too, I think) works by moving the cursor around on the screen. There are control sequences to do such things. Take a look at the code again and you'll see them. These sequences are not ASCII control characters, they are strings starting with (umm...) ESC, maybe. Have a look here for a higher-level explanation.

Remap Caps lock key to Esc in Mma 7

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?
I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.
Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.
I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...
Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]
but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.
Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.
edit: I did not see you say which operating system you had, so I've added Mac instructions.
Windows
For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:
How can a hotkey or hotstring be made exclusive to certain program(s)?
In other words, I want a certain key to act as it normally does except when a specific window is active.
In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":
$NumpadEnter::
IfWinNotActive, CAD Editor
{
Send, {NumpadEnter}
return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.
~NumpadEnter::
IfWinNotActive, CAD Editor
return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
To quote from "MRCS" in this forum post, you may find the following useful:
The first one I named CapsLockR.ahk and contains the following script:
CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk
The second one is named CapsLock.ahk and has this script:
GetKeyState, state, CapsLock, T
if state = D
SetCapsLockState, off
else
SetCapsLockState, on
exit
Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.
Linux
I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)
Mac
Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...
If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

Resources