#706 [Patch] Extend "sticky caret" feature.

Completed
closed
Neil Hodgson
Scintilla (355)
5
2010-07-30
2010-07-09
Jason Oster
No

I've implemented a simple patch which changes the "sticky caret" feature from a boolean (on/off) to an enumerated list of "sticky types".

The first two types are exactly the same as in previous implementations:

val SC_CARETSTICKY_OFF=0
val SC_CARETSTICKY_ON=1
val SC_CARETSTICKY_WHITESPACE=2

When set to 0, the sticky flag is off; all text changes (and all caret position changes) will remember the caret's new horizontal position when moving to different lines. This is the default; this functionality remains unchanged with the patch.

When set to 1, the sticky flag is on, and the only thing which will cause the editor to remember the horizontal caret position is moving the caret with mouse or keyboard (left/right arrow keys, home/end keys, etc). This functionality remains unchanged with the patch.

When set to 2 (the new mode added with the patch), the caret acts like mode 0 (sticky off) except under one special case; when space or tab characters are inserted. (Including pasting *only space/tabs* -- undo, redo, etc. do not exhibit this behavior..)

SCI_TOGGLECARETSTICKY retains its old behavior; with one exception. When the sticky mode has been set to 2 (sticky whitespace), the toggle will set the mode to 0 (off). Toggle will then continue toggling between modes 0 and 1 as normal.

The use-case behind this is pretty simple. Take this code as an example:
http://pastebin.com/zP1HHA3S

Assuming we want our code to be very pretty, and align the comments together, the usual way to do that is one line at a time. The caret is placed before the '//' and the tab key is pressed until the comment is in the desired position. Then the down arrow (or up arrow) is used to get to the next line.

Wouldn't it be nice if the editor would move the caret to the next '//' so you can indent it, as well? That would make it super easy to adjust the indentation in the middle of a line ... when you have more than one line that needs this work; just down-arrow, tab-tab-tab, down-arrow, tab-tab-tab, ...

That's what mode 2 "sticky caret for whitespace" does. I've used this feature in other editors (Komodo, for example) and it is very useful in certain situations. It can also be confusing and unexpected, at times. Good reason to make it a new option, rather than changing current behavior. I believe Komodo implements this outside of Scintilla, but it would be nice to upstream the feature, so other editors can consume it if they like..

Discussion

  • Neil Hodgson
    Neil Hodgson
    2010-07-10

    • assigned_to: nobody --> nyamatongwe
     
  • Neil Hodgson
    Neil Hodgson
    2010-07-12

    Committed with a minor change to using an unsigned int loop index to avoid warning.

     
  • Neil Hodgson
    Neil Hodgson
    2010-07-12

    • milestone: --> Completed
     
  • Neil Hodgson
    Neil Hodgson
    2010-07-30

    • status: open --> closed