function Log2InitState() if !exists("b:log_2_cursor") || b:log_2_cursor != col(".") || b:log_2_line != line(".") let b:log_2_cursor = col(".") let b:log_2_line = line(".") let b:log_2_left = 0 let b:log_2_right = strlen(getline(b:log_2_line)) let b:log_2_call_stack = "" endif endfunction function Log2MoveIt(i) let b:log_2_cursor = a:i + b:log_2_left + ( (b:log_2_right - b:log_2_left) / 2 ) call cursor(b:log_2_line, b:log_2_cursor) endfunction function Log2StackPush (val) let b:log_2_call_stack = b:log_2_call_stack . a:val endfunction function Log2StackPop () let ret = strpart(b:log_2_call_stack, strlen(b:log_2_call_stack)-1, 1) let b:log_2_call_stack = substitute(b:log_2_call_stack, ".$", "", "") return ret endfunction function Log2Left() call Log2InitState() if b:log_2_right - b:log_2_left > 1 let b:log_2_right = b:log_2_cursor call Log2StackPush("l") call Log2MoveIt(0) endif endfunction function Log2Right() call Log2InitState() if b:log_2_right - b:log_2_left > 1 let b:log_2_left = b:log_2_cursor call Log2StackPush("r") call Log2MoveIt(1) endif endfunction function Log2Backtrack() call Log2InitState() let popped = Log2StackPop() if popped == "r" let b:log_2_left = b:log_2_left - (b:log_2_right - b:log_2_left) if b:log_2_left < 0 let b:log_2_left = 0 endif elseif popped == "l" let b:log_2_right = b:log_2_right + ( b:log_2_right - b:log_2_left) if b:log_2_left > strlen(getline(b:log_2_line)) let b:log_2_line = strlen(getline(b:log_2_line)) endif else let b:log_2_left = 0 let b:log_2_right = strlen(getline(b:log_2_line)) endif call Log2MoveIt(0) endfunction