smart caret in python on underscores in textmate - python

Consider the following line of code in eclipse java editor.
int myFirstVar = 0;
I can use cmd + arrow keys to position my cursor at the beginning of the line, end of line and also at the characters m, F, V, =, 0. I did a bit of searching and found that this is possible in eclipse using smart caret settings. Is this possible in textmate for python code. I generally use underscores in my variable names in python. The smart caret could position the cursor at whitespaces and underscores for instance.

There are several ways to navigate via arrow keys and modifiers, although some might conflict with the latest OS X Spaces switching.
Alt+arrow jumps through "word-like" characters, meaning it'll skip over all letters, numbers, and underscores, but will stop for spaces, punctuation, etc.
Ctrl+arrow jumps through smart detection of naming schemes. It'll stop at various points on MyVarName and my_var_name. This is probably what you want.
Using these in combination helps you navigate very precisely, without making one key combination try to do everything.
If doing Ctrl+arrow makes your desktop flip to a different fullscreen view, visit your System Preferences for Keyboard, go to the Shortcuts tab, and view the Mission Control section from the left sidebar. You'll see a grouping at the bottom of the list which assigns keyboard hotkeys to moving spaces. You can disable or reassign these as you see fit, to avoid conflicts.

Related

How to implement simple terminal line editing similar to input() but handling escape character

How can I implement a Python function to run on Unix systems (minimally, just Linux) that is similar to builtin input(), but supports using the escape key to cancel inputting text? So:
Enter a single line of text at the command line (multiple lines would be OK also)
with simple line editing (left/right arrow keys, backspace to delete a character, control-a to jump to start of line, control-e to jump to end of line, control-w to delete a word)
(and allowing pastes from primary or clipboard selection in X/wayland -- I guess this requires no special support)
submit the text by hitting the return key
or hit the escape key to exit and cancel (it would be OK but not ideal if I couldn't tell the difference between this and the user entering an empty string and hitting return)
How can I achieve that?
I've tried curses, but that is focused on whole-screen input -- it seems hard to implement something that does not clear the screen.
I've tried termios, but I don't know how to implement the backspace key for example, and I suspect I might end up wanting slightly more functionality than is immediately obvious -- for example control-w, which on my linux machine input() implements to delete a whole word. I don't need to reproduce every little detail of input()'s behaviour, but a few things like that (see the list above).
You can use readline, for example
#! /usr/bin/env python3
import readline
input('hello: ')
This takes your ~/.inputrc settings into account. You can configure editing-mode that controls which default set of key bindings is used. By default, Readline starts up in Emacs editing mode, where the keystrokes are most similar to Emacs. This variable can be set to either emacs or vi.

How can I read the character at an x,y position on the terminal (in python?)

I'd rather avoid using curses for this, largely because of how much curses screws with the terminal by default. Frankly, using window.inch() in curses has not worked correctly in most situations, anyway, so it doesn't seem worth working through all the echo, newline, and other issues with curses to get that function.
I need to be able to read the value of a character at some position on the terminal. I don't particularly care if unicode characters work or if I get info about the character attributes (like underline, bold, color, etc.) though it would be nice.
Thanks...

serial ctrl+a escape sequence

I'm using pyserial to create a serial application. I am looking for the ASCII or ANSI escape sequence for CTRL+A and CTRL+E (go to beginning and end of the line, respectively). I cannot seem to find the escape codes anywhere. Does anyone know of a resource that lists the codes? CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys. I have to believe that an escape sequence exists.
Any suggestions would be greatly appreciated!
Neither ASCII characters nor ANSI control codes really quite define a thing called CTRL+A.
However, the ASCII control characters 0-31 (which officially have names from NUL through US) are traditionally mapped as the "control version" of the printable characters 64-95. So, 0 is ^#, 1 is ^A, 2 is ^B, etc.
The Wikipedia pages on ASCII and control character give further information.
So, ^A is 1, and ^E is 5.
Meanwhile:
CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys.
They're not that universal. These are part of the "basic emacs control keys", which a number of Unix terminal emulators and some Unix-based GUI apps (and things like the libreadline CLI editing library and the Qt text widgets) support by convention. However, try hitting ^A in, say, a DOS prompt or Microsoft Word and it won't go to the start of the line.
And, just sending ^A or ^E to an ANSI-compatible terminal won't necessarily move the cursor to the start or end of the line. To do that properly, you want to actually send the corresponding ANSI control sequences. Any list of ANSI escape codes, like Wikipedia's, will show you what's available.
Unfortunately, there is no right answer to what you're trying to do. ANSI treats the entire 80x25 (or whatever) screen as accessible, and doesn't distinguish between character positions that have something in them and those that are blank. So, you can't move to the "end of the line", unless by that you mean "column 79".
And, if what you're looking for is moving to columns 0 and 79, that's easy with the CHA command ('\x1b[0G' and '\x1b[79G')—but not all ANSI terminals support that; in particular, DOS ANSI.SYS and anything built to be compatible with it will ignore it.

Textmate Whitespace/Invisibles - Show Spaces

Is there a way to show "Soft Tabs" (spaces) in TextMate? View → Show Invisibles works well for keeping track of indentation if you're using tabs for indentation. Unfortunately in languages where indentation is semantic you generally have to use spaces.
(Python, YAML, HAML, CoffeeScript)
Any suggestions for showing this whitespace or keeping track of soft indentation in TextMate? Should I keep holding out for Textmate2?
Alternative strategies and suggestions are also welcome.
The latest version of TextMate 2 highlights spaces when Show Invisibles is enabled.
EDIT:
You can even customize which invisibles to show with which character by modifying the invisiblesMap property in .tm_properties file.
From the TextMate changelog:
This can be set to a string which is used to control which glyphs are used for invisible characters. Add either \n, \t, or a space to the string, followed by the glyph that should be used to render the character, or prefix it with ~ to disable rendering of that character. For example to disable rendering of spaces and render tabs as ┊ add this to .tm_properties: invisiblesMap = "~ \t┊".
Sidenote:
THIS IS NOT THE CASE ANYMORE, functionality has been restored.
According to the Log of 2013-10-23 (v2.0-alpha.9489): "Show Invisibles will no longer treat space as an invisible (which was added in previous build) as it was causing issues with right-to-left rendering and combining marks used after spaces. The feature might be back, but needs to be implemented differently."
You can use soft tabs - as described here. I have also problem to find it when I needed this feature;)

Enforce "spaces" or "tabs" only in python files?

In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?
Well, perhaps "enforce" is too strong, more like a "recommendation".
I keep receiving patch files with mixed indentation and this is annoying... (to say the least) Python itself can tell when there is a problem, but I am searching something to do that at the editor level, like it exists for the charset.
Edit : Ok, my question wasn't clear, I am asking this because I keep receiving corrections and patches in any mix of tab/space you can imagine. I am using Mercurial as a DVCS, perhaps something exists at this level ?
Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest LTS Ubuntu, it is provided by the python2.7-examples package.)
If you can set up a Mercurial hook you should be able to run all files through reindent.py.
By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.
Look at the tabnanny module: — Detection of ambiguous indentation.
This is something your editor should do for you. Most editors (try Notepad++ for example, it's free) will let you set whether hitting tab enters a tab character or a number of spaces. I'd recommend using two spaces instead of tab in all files (I find 4 is too much). Using spaces instead of tabs is better as it means that you can indent stuff using both the space & tab keys without worrying about messing up your files.
If you have files that have a mix it isn't hard to write your own script to convert tabs to spaces
As explicited in PEP 8, never mix tabs and space. However, a file with both may just run...
As it says there:
The most popular way of indenting Python is with spaces only. The
second-most popular way is with tabs only. Code indented with a mixture
of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python command line interpreter with the -t option, it issues
warnings about code that illegally mixes tabs and spaces. When using -tt
these warnings become errors. These options are highly recommended!
the solution is therefore to use as a default:
python -t my_mixed_code.py
To answer at the editor level, this depends on the editor, please specify!

Categories

Resources