Sublime Forum

Edit cursor off view

#1

I keep getting hit by this because my mind is programmed into believing that the edit cursor position will always be within view but… it isnt.

I mouse scroll down through a 8246976239 page (or more) monster C source file (i didnt write em honest!)… i get to the part where i want to edit and hit cursor down to move the cursor to the line im looking at that i want to edit. The view port instantly scrolls back up to where the cursor is and… bam… i have lost the place i wanted to be editing.

Please PLEASE :smile: Add a switch allowing for the cursor to always remain within the view port on mouse scroll. I.E. once i have scrolled down far enough that the mouse is on the line at the top of the view port the mouse will remain on that line (or bottom if scrolling up).

0 Likes

#2

Are you using ST3? If so you can leverage those command listeners :smile: If not, it’s a bit more involved (but still doable) as you would have to rebind the movement commands. If you have auto pairing enabled and you insert a closing quote, it does issue a move command, so the cursor will move, this may or may not be what you want. Then again, why would you enter a closing quote/brace/etc if you don’t know where the cursor is?

[code]import sublime_plugin
import sublime

class MoveInterceptCommand(sublime_plugin.TextCommand):
def run(self, edit, by, forward, extend=False):
move_cursor_to_visible(self.view)
self.view.run_command(“move”, {“by”: by, “forward”: forward, “extend”: extend})

class MoveToInterceptCommand(sublime_plugin.TextCommand):
def run(self, edit, to, extend=False):
move_cursor_to_visible(self.view)
self.view.run_command(“move_to”, {“to”: to, “extend”: extend})

class MoveInterceptEventListener(sublime_plugin.EventListener):
def on_text_command(self, view, command_name, args):
if len(view.sel()) == 1:
move_by_list = “characters”, “lines”, “words”, “word_ends”, “subwords”, “subword_ends”, “pages”]
move_to_list = “bol”, “eol”]
if command_name == “move”:
if “by” in args and args"by"] in move_by_list:
return (“move_intercept”, args)
elif command_name == “move_to”:
if “to” in args and args"to"] in move_to_list:
return (“move_to_intercept”, args)

def move_cursor_to_visible(view):
vis_region = view.visible_region()
cursors = view.sel()
move_cursor_top = False
move_cursor_bottom = False

for cursor in cursors:
    if cursor.b < vis_region.begin():
        move_cursor_top = True
        break
    if cursor.b > vis_region.end():
        move_cursor_bottom = True
        break
if move_cursor_top:
    cursors.clear()
    cursors.add(sublime.Region(vis_region.begin(), vis_region.begin()))
elif move_cursor_bottom:
    cursors.clear()
    cursors.add(sublime.Region(vis_region.end(), vis_region.end()))

[/code]

0 Likes

#3

erm im not quite follwing you here. what im doing is mouse wheel scrolling and having the cursor not remain within the view port. when i then hit cursor down the view port migrates away from where i specifically located it back to where the cursor is. What i would like to happen would be that when i mouse wheel down that the cursor remain within the view port. if i have mouse wheeled down 5 pages and hit cursor down to position the cursor on the piece of code im LOOKING at… i dont want the view port to suddenly automatically home itself to the cursor - if the cursor was always within the view port it would not have to do so.

Also, im not comfortouble with creating some sort of plugin to achieve this as I dont program python or know the ST3 api enough to do it. Im also working 80 hour weeks for the past 6 weeks and will probably continue to do so for the forseeable future or beyond.

This seems to me like the sort of thing that should be built in as a configurable switch, not some geeky plugin that only a ST3 geek would know how to use :smile:

I did create a syntax highlight module for my forth programming but that was all just regular expressions… none of that API stuff or python coding.

Also, the plugin code you pasted seems to be a “hit this magic keypress and the cursor will home itself to the viewport”. This would not work for me, i need the cursor to ALWAYS be within the view port. IF this magic keypress would mentally work for me then all i would have to do is remember to MOUSE CLICK after doing a mouse scroll and before I hit any cursor keys.

Problem is, my 30+ years of coding has hard wired my brain into expecting the cursor to never go outside the view. Im too old and wrinkly to recode my thought patterns into doing this :smile:

0 Likes

#4

This was more of a temporary work around. Hopefully it will be implemented as a setting but all this does is catch the “move” command (which is called when you press the navigation keys). So the only “magic key press” is the navigation keys (which it sounded like you were doing already?). If the cursor is outside of the view, it moves the cursor to the top/bottom (depending on where the cursor was relative to the view), then executes the move command. Thus, no magic jumping view away from page 7153148520/8246976239. I haven’t been coding for 30+ years, but I completely understand the idea of the cursor staying within the view. At the end of the day, it’s your choice if you want to try it. Just thought I would throw up a short term, temporary solution :smile:

0 Likes

#5

lol what the heck i replied to this hours ago but i guess i musta been assleep at the keyboard and never hit send :confused:

anyway, far be it for me to look a gift horse in the mouth! If this does what im looking for ill give it a shot but im not sure where to put it or how to make it active :smile:

%*$&^!TI! press SEND this time lol (ty :smile:

0 Likes

#6

From the menu select “Tools -> New Plugin”. Copy and paste code contents into the file. Then save the file in your user directory (I believe it should default there, but if not I think you can find it :smile:) as “view_navigation.py”. That’s it! You shouldn’t have to restart the editor, but I’ve seen some odd loading things with plugins in general so you might. Anyways, place the cursor, scroll somewhere, then press a navigation key. See if it does what you expect (or at least something more like what you want). Just as a reminder, this will only work in ST3. Let me know if you run into issues.

0 Likes

#7

!
this solves it tyvm!!!

0 Likes

#8

lol ok this does not quite work. it works for the case when i have mouse wheel scrolled off page but when i am holding shift and using the cursor keys to mark a block of code the cursor jumps back to the top of the page when it reaches the bottom :smile:

That’l learn me for using mouse AND keyboard methods for editing stuff :stuck_out_tongue:

0 Likes

#9

Odd it works for me, unless I’m misunderstanding what you are doing. I’ll assume that’s the case :smile:. I did just find a bug that will break the selections if the beginning if off the view, but I don’t think that is what you are seeing. I also know that this doesn’t handle multiple cursors elegantly. I don’t think either of those are your particular case, so if you could clarify a bit more, I’ll try to fix it :smile:

0 Likes

#10

Nevermind, I neglected to go all the way to the bottom (having the entire view highlighted). I’ve updated the code so it (should) work better. Didn’t anyone tell you you shouldn’t highlight so much code! :smiley:

0 Likes

#11

lol that fixed it from what i can tell :smile:

As for marking so much code, that’s not my fault! the last guy had feature-itis and he liked ALL his features in the same routine. He also did not like to reuse code but instead re-implemented it every time. I.E. External flash chip, I2c driver. LCD, its own I2c driver… LCD backlights… another I2c driver…(with bugs in different places : )

0 Likes

#12

well i discovered the flaw in this module today and i dont know if you can fix it or not but if there are multiple cursors and some of them are off screen moving the cursor removes all but one :confused:

this really needs a fix from the management i think :smile:

0 Likes

#13

Hmm, I was wondering when that would come up. I hadn’t decided the best way to go about managing multiple cursors. My first thought was to keep the cursor positions the same as you scroll. Of course, then I would have to decide how I want to manage multiple cursors that are more than 1 “view region” away from each other. In addition, where would I put cursors that had a column position beyond the new line. For this, I’d probably just put it at the end of that line. Now if only the API would just give the last position in that row rather than moving to the next row. Oh well though, I can do some checks for that. I’ll try to post an update for that soon. :smile:

0 Likes

#14

do you have a way to check if there are multiple cursors and if there are not do anything special? Im not really sure how multiple cursors should be handled but this might be the best plan. I.E. only move the cursor into the view if there are only one of them. otherwise let the editor do whatever it would do without this plugin. I dont even know if the api allows this either.

0 Likes

#15

That’s the easiest thing to do. I’ve updated the plugin so it should only move the cursor into the view if there is a single cursor. Otherwise, it should retain the default editor behavior.

0 Likes

#16

tyvm :smile:

0 Likes