Home Download Buy Blog Forum Support

Always centered cursor

Always centered cursor

Postby quodlibet on Sat Jan 21, 2012 2:12 pm

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
quodlibet
 
Posts: 547
Joined: Sat Jun 18, 2011 5:53 pm

Re: Always centered cursor

Postby facelessuser on Sat Jan 21, 2012 5:08 pm

Code: Select all
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)


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

Or alternatively you could just use the region.
Code: Select all
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)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Always centered cursor

Postby quodlibet on Sun Jan 22, 2012 11:19 am

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:

Code: Select all
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
quodlibet
 
Posts: 547
Joined: Sat Jun 18, 2011 5:53 pm

Re: Always centered cursor

Postby phillip.koebbe on Sun Jan 22, 2012 4:09 pm

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Always centered cursor

Postby facelessuser on Sun Jan 22, 2012 8:35 pm

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.
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Always centered cursor

Postby phillip.koebbe on Sun Jan 22, 2012 8:43 pm

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Always centered cursor

Postby C0D312 on Sun Jan 22, 2012 8:59 pm

phillip.koebbe wrote:I'd like to hear some user stories about how this has made a difference in their day to day use.

Or you could just try it out for yourself...
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Always centered cursor

Postby facelessuser on Sun Jan 22, 2012 9:08 pm

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 :) .
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Always centered cursor

Postby quodlibet on Mon Jan 23, 2012 5:56 am

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.
quodlibet
 
Posts: 547
Joined: Sat Jun 18, 2011 5:53 pm

Re: Always centered cursor

Postby sublimator on Mon Jan 23, 2012 2:34 pm

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 http://www.sublimetext.com/docs/plugin-examples, though you'll need to translate the example from ST1 to ST2.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests