Textmate Whitespace/Invisibles - Show Spaces - python

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;)

Related

How to change the characters used for QTextOption.ShowTabsAndSpaces?

Is there a way to change which character is used for QT's QTextOption.ShowTabsAndSpaces flag?
I find that the default character that's used for viewing whitespace (specifically spaces) stands out a little too much. I'd like to change the font or character used so that it's less distinct.
It looks like the character used is unicode "Middle Dot", · (U+00B7) and I'd like to use, say, U+02D1 ˑ.
Ideally I'd like to be able to set it to whatever the user wants.
I've been searching through the Qt docs and have only been able to find how to turn this flag on (here).
EDIT:
I guess I should show some code... Here's how I'm currently adding the whitespace indicators:
opts = self.document().defaultTextOption()
opts.setFlags(opts.flags() | QTextOption.ShowTabsAndSpaces)
self.document().setDefaultTextOption(opts)
Running Python 3.4 and PyQt4, but should be able to port C++ code over.
EDIT2:
Thanks to Andrei Shikalev's answer below, I've posted a feature request for this on the QT tracker: https://bugreports.qt.io/browse/QTBUG-46072
Currently we could not change characters for tabs and white space. This characters hardcoded in Qt source for QTextLayout:
QChar visualTab(0x2192);
...
QChar visualSpace((ushort)0xb7);
More info in source for QTextLayout on GitHub.
You can create feature request for tabs and white spaces custom characters. IMHO this feature will be useful for custom-looking editors, based on Qt.

Easy way to set indentation on whole file

I screwed up the formatting on a few files using VIM. What I thought was tabs was something that looked like tabs. Is there an easy to change the formatting back to standard tabs?
I can find a lot of info on doing from tabs to spaces, but not the other way around.
You can do this by disabling expanding tabs to spaces, and than replacing every sequence of whitespace with tabs:
:set expandtab
:%retab!
Note: Retab command takes range, so you can execute it on the part of the file. Also, before executing this command, set proper size of tab, depending on your preferences, for example:
:set tabstop=2
This means that every two spaces will be replaced by tab.

smart caret in python on underscores in textmate

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.

page separator in Kate editor

PEP 8 says:
Python accepts the control-L (i.e. ^L)
form feed character as whitespace;
Many tools treat these characters as
page separators, so you may use them
to separate pages of related sections
of your file
This look like a great idea for me, but in the text editor I use(kate) "control+L" is for save all files. Someone have any solution?
... or I'm losing something here?
Ctrl-L simply refers to the character with ASCII code 12 (form feed, new page). It is called Ctrl-L only because some editors allow you to enter it with Ctrl-L. (For instance, in vim, one can type Ctrl-Q Ctrl-L to enter that character, and it also appears as ^L). In Kate, Ctrl-L is a shortcut for saving all files, so you cannot type it that way and I'm not sure there is any way of entering that character easily.
As a Kate developer, I unfortunately have to tell you that such control sequences are not supported. In fact, Kate often treats these files as binary files, since such characters are not human readable text. So in short: Try to avoid ^L.
You can create a plugin, and disable the shortcut Ctrl + L from the menu: Settings -> Configure shortcuts

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