Sublime Forum

Always centered cursor

#1

I’ve been trying to make a simple (I think) plugin but have utterly failed so far.

Basically I want to execute view.show_at_center whenever on_modified is triggered in the EventListener.

I’ve read the online docs and looked at WordCount’s source code (it seemed close), but I cannot for the life of my work this out. Can someone please help me or at least tell me if this should be a lot longer than the three lines I imagine it to be?

(I should mention that I’m not a programmer, but in the past have managed to puzzle out how to do small things.)

Thanks,
Alex

0 Likes

Key Down Event
Customizing ctrl+click
Typewriter: a plugin for writers
#2

[code]import sublime_plugin

class AlwaysCenterCommand(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()
pt = sel[0].begin() if len(sel) == 1 else None
if pt != None:
view.show_at_center(pt)[/code]

Center if not multi selection and modifying. That is it.

Or alternatively you could just use the region.

[code]import sublime_plugin

class AlwaysCenterCommand(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()
region = sel[0] if len(sel) == 1 else None
if region != None:
view.show_at_center(region)[/code]

0 Likes

#3

Thank you for the quick response!

It works exactly as expected and it’s even more robust than what I asked for. I was actually planning to break multi-selection. (So why it hadn’t occured to me that sel is an array?)

I’ve tinkered with it to make it center the cursor when selection is modified:

import sublime_plugin

class AlwaysCenterCommand(sublime_plugin.EventListener):

	def on_selection_modified(self, view):
		self.center_cursor(view)
	
	def center_cursor(self, view):
		sel = view.sel()
		region = sel[0] if len(sel) == 1 else None
		if region != None:		
			view.show_at_center(region)

This may be a bit overkill. It’s nice that now the cursor is always centered, but it’s very difficult to select text with the mouse. (I think I won’t mind if I am writing prose, but I have yet to try it out at length.)

I’ve had a couple ideas about this (but I haven’t looked into them, yet):

]Differentiate between mouseclicks and keypresses. I know FocusWriter uses this strategy, but I don’t quite like it. (I found myself having scrolled with the mouse to a location and then, when I started typing, the view reverted to where the cursor was.)/]
]Differentiate between regular and distraction-free mode, centering on_modified in the former case and on_selection_modified in the latter./]

If anyone has used WriteRoom or iA’s Writer or any other software which centers the cursor, could you please share strategies for keeping the cursor centered while not breaking (too much) the editor’s functionality?

(And thanks again to facelessuser.)

Alex

0 Likes

#4

Pardon my ignorance, but what is a typical use case for a centered cursor? And by “centered”, do you mean always centered on the current line, ie, in the center of the text input area? I know this may sound dumb, but I don’t understand how this would be helpful. Not trying to suggest it should/could not be, just that I’m drawing a blank.

Thanks.

0 Likes

#5

He meant centered vertically in the view-able screen, not centered horizontally on the line.

So when he says he modified it to center during the on_selection_modified, so when moving the cursors, the lines would move up and down instead of the cursor.

0 Likes

#6

Thanks for the explanation. Interesting. I don’t think I’ve ever considered that as a possibility. Do many people use/desire such functionality? I’d like to hear some user stories about how this has made a difference in their day to day use.

0 Likes

#7

Or you could just try it out for yourself…

0 Likes

#8

Everyone is different and likes different things. I personally don’t care for the functionality, but quodlibet does; that is good enough for me. But give it a try if your curious as COD312 says :smile: .

0 Likes

#9

Short Answer

I use ST2 for two different tasks. Writing HTML, CSS & the occasional Javascript and writing prose. I would neither use nor recommend this for the former task or coding in general; I find it very useful for the latter.

Long Answer

Centering the cursor is a feature of several “distraction free environments”. Although the whole idea of such software is a little gimmicky, keeping the cursor centered is actually a useful feature. When writing prose, especially on first drafts, you treat the screen like a page: you start on the top left and work your way down and to the right. But screens, unlike pages, do not turn, so most of the time you end up looking at the bottom of your screen. The solution (or a solution) is to treat the screen like a typewriter, by centering the cursor. This keeps your sight focused on a single line so that you can focus on typing and let the editor worry about scrolling.

The current implementation, as posted above, is a little glitchy and makes mouse-use very difficult. But I have used editors dedicated to writing prose, and I spent too much time missing ST2 features. As I use this in my writing projects, I hope I will be able to improve the problems which will inevitably arise. (I wrote this comment in ST2 with cursor-centering enabled, and I got a “slow plugin” as I was editing this paragraph, so there’s surely work to be done.)

If you happen to write prose (or blog or whatever), you can try it and see if it works for you. I should repeat that I think it would be a bad idea to write code in this mode.

Cheers,
Alex

Edit: a theme that takes cursor-centering into account is important. (I.e., centering text, 67 character wrap, 16px+ font-size, etc.) I will shape up what I am using and share it soon-ish.

0 Likes

#10

You might get a cheap improvement by using the on_modified event instead.

if view.match_selector(0, 'text.plain'): ... can restrict to only text files.

Check out the Idle Watcher example at sublimetext.com/docs/plugin-examples, though you’ll need to translate the example from ST1 to ST2.

0 Likes

#11

Thanks for the explanation, quodlibet. I wasn’t so much interested in how well it works out for people, so the notion of trying it out for myself is really sort of moot. I was more interested in why someone would use it. The idea of writing prose makes sense. I don’t write prose or any type of literature, so the concept of distraction-free writing is not something I’ve ever given much thought to.

To be fair to the suggestions that I try it out myself, I did put the plugin in place. I quickly noticed the issues with selection. That alone would be enough to keep me from using it while trying to work. It would drive me bonkers and be a major distraction.

Neat stuff. One learns a lot by asking questions. Thanks for the answers.

0 Likes

#12

Thanks for the input.

Using the on_modified event is what I originally was planning to use and the code that facelessuser provided. I switched to on_selection_modified because that was closer to the behavior I wanted. I knew that mouse-selection would be a disaster, but it doesn’t bother me; I love scrolling with the keys with on_selection_modified. I have thought that it might be possible to put together a function (or series of functions) that centers the cursor on_modified, as well as every command that potentially moves the cursor up or down. But maybe that would be rewriting the on_selection_modified function :smile:

Restricting it to text files or related syntaxes (markdown, textile etc.) is in the TODO, but it’s a little more complicated than that. For this plugin to “work” properly, it needs a specific theme as well. And, as far as I can, tell plugins can’t override a user’s theme settings. (I derive this information from the fact that the “org mode” plugin doesn’t do this, either.)

I’ll take a look at ‘Idle Watcher’, but I think a second is maybe half-a-second too slow for this purpose.

Writing code with this plugin drives me bonkers. But writing prose is a whole different beast.

To put it another way: what I call a line when I write code, I call a paragraph when I write prose. The semantic units are utterly different.

Alex

0 Likes

#13

I’m a just a writer, not a coder at all. This functionality is very useful. However, I have been wondering …

I recently added BufferScroll to my install of ST2 so I can have a single file cloned over multiple pane which will all scroll with me. One of the points of this behavior for my work is so I could, for example, set up with 3 columns and focus on the middle one while I work which would give me a column worth of text front and back of the point I’m working at. Very useful for me most of the time as I’m trying to make things flow into each other, agree with each other, etc.

Now the typewriter scrolling you have with this code works only to center the cursor in the first panel. If you try typing in any other panel, things get all wonkey, similarly to how things go when you try to make mouse selections (which, incidentally, is no problem for me, just thought I’d try it out to see for myself since reading of it in this thread). The most ideal situation would be for it to work in any panel you chose to place you’re cursor, for a couple of reasons. First, I think the optimal default mode here is to be able to input text in the second panel and have 3 or 4 columns of sync scrolled material presenting a good panorama of the surrounding text. Second, I know there will be times when you’ll see something in one of the panes you aren’t currently inputting to that needs to be changed or expanded, and it will often be a great deal more useful to simply be able to click over to a spot and just start typing without things going crazy.

Regardless, this is useful as is, thank you. Just not optimal yet.

Kensai

0 Likes