Home Download Buy Blog Forum Support

Edit cursor off view...

Edit cursor off view...

Postby mark4 on Sun Apr 14, 2013 4:05 pm

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*** :) 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).
mark4
 
Posts: 98
Joined: Sat Feb 11, 2012 3:33 am

Re: Edit cursor off view...

Postby skuroda on Sun Apr 14, 2013 7:09 pm

Are you using ST3? If so you can leverage those command listeners :) 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: Select all
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()))
Last edited by skuroda on Fri Apr 19, 2013 6:21 am, edited 5 times in total.
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: Edit cursor off view...

Postby mark4 on Sun Apr 14, 2013 10:28 pm

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

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 :)
mark4
 
Posts: 98
Joined: Sat Feb 11, 2012 3:33 am

Re: Edit cursor off view...

Postby skuroda on Sun Apr 14, 2013 10:53 pm

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 :)
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: Edit cursor off view...

Postby mark4 on Mon Apr 15, 2013 3:16 am

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

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

%*$&^!TI! press SEND this time lol (ty :)
mark4
 
Posts: 98
Joined: Sat Feb 11, 2012 3:33 am

Re: Edit cursor off view...

Postby skuroda on Mon Apr 15, 2013 4:25 am

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 :)) 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.
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: Edit cursor off view...

Postby mark4 on Mon Apr 15, 2013 2:55 pm

!
this solves it tyvm!!!
mark4
 
Posts: 98
Joined: Sat Feb 11, 2012 3:33 am

Re: Edit cursor off view...

Postby mark4 on Tue Apr 16, 2013 1:22 pm

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

That'l learn me for using mouse AND keyboard methods for editing stuff :P
mark4
 
Posts: 98
Joined: Sat Feb 11, 2012 3:33 am

Re: Edit cursor off view...

Postby skuroda on Tue Apr 16, 2013 5:05 pm

Odd it works for me, unless I'm misunderstanding what you are doing. I'll assume that's the case :). 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 :)
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: Edit cursor off view...

Postby skuroda on Tue Apr 16, 2013 8:45 pm

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! :D
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Next

Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot] and 23 guests