Navigation enhancements

2010-10-14
2013-01-25
  • I wrote a few small scripts to (a) move the cursor to the top/bottom of the window, and (b) scroll the current line to the top/bottom of the window.

    Scroll to top (Ctrl+Alt+Up)

    editor.setFirstVisibleLine(editor.lineFromPosition(editor.getCurrentPos()))
    

    Scroll to bottom (Ctrl+Alt+Down)

    editor.setFirstVisibleLine(editor.lineFromPosition(editor.getCurrentPos()) - editor.linesOnScreen() + 1)
    

    Cursor to top (Ctrl+Up)

    line = editor.getFirstVisibleLine()
    end = editor.getLineEndPosition(line)
    pos = editor.findColumn(line, editor.getColumn(editor.getCurrentPos()))
    editor.gotoPos(min(pos, end))
    if end < pos:
        editor.lineEnd()  # hack: gotoPos() doesn't change lineUp/lineDown target column
    

    Cursor to bottom (Ctrl+Down)

    line = editor.getFirstVisibleLine() + editor.linesOnScreen() - 1
    end = editor.getLineEndPosition(line)
    pos = editor.findColumn(line, editor.getColumn(editor.getCurrentPos()))
    editor.gotoPos(min(pos, end))
    if end < pos:
        editor.lineEnd()  # hack: gotoPos() doesn't change lineUp/lineDown target column
    

    The keystrokes I've used were chosen based on this scheme: Right-Arrow moves forward one char, Ctrl+Right moves forward one word, so: Up-Arrow moves up one line, Ctrl+Up moves to top of window. (Maybe "top of paragraph" would be more semantically correct, but I don't really use "paragraph" as an organizing concept in my code.)

    I've also assigned, per my own logic, Alt+Up to scroll up one line, so Ctrl+Alt+Up scrolls to the top.