import os, commands, re, tempfile
from subprocess import Popen, PIPE, STDOUT

import buffer, default, dirutil, regex, util, window
from point import Point

from method import Method, Argument

class StartOfLine(Method):
    '''Move the cursor to the start of the current line'''
    def _execute(self, w, **vargs):
        w.start_of_line()
class EndOfLine(Method):
    '''Move the cursor to the end of the current line'''
    def _execute(self, w, **vargs):
        w.end_of_line()

class Forward(Method):
    '''Move the cursor right one character'''
    def _execute(self, w, **vargs):
        w.forward()
class Backward(Method):
    '''Move the cursor left one character'''
    def _execute(self, w, **vargs):
        w.backward()

class NextLine(Method):
    '''Move the cursor down one line'''
    def _execute(self, w, **vargs):
        w.next_line()
class PreviousLine(Method):
    '''Move the cursor up one line'''
    def _execute(self, w, **vargs):
        w.previous_line()

class PageUp(Method):
    '''Move the cursor up one page'''
    def _execute(self, w, **vargs):
        w.page_up()
class PageDown(Method):
    '''Move the cursor down one page'''
    def _execute(self, w, **vargs):
        w.page_down()

class GotoBeginning(Method):
    '''Move the cursor to the beginning of the buffer'''
    def _execute(self, w, **vargs):
        w.set_mark()
        w.goto_beginning()
class GotoEnd(Method):
    '''Move the cursor to the end of the buffer'''
    def _execute(self, w, **vargs):
        w.set_mark()
        w.goto_end()

class RightWord(Method):
    '''Move the cursor to the start of the word to the right'''
    def _execute(self, w, **vargs):
        w.right_word()
class LeftWord(Method):
    '''Move the cursor to the start of the word to the left'''
    def _execute(self, w, **vargs):
        w.left_word()

class NextSection(Method):
    '''Move the cursor to the next section'''
    def _execute(self, w, **vargs):
        cursor = w.logical_cursor()
        i = cursor.y + 1
        seen_null_line = False
        while i < len(w.buffer.lines):
            if seen_null_line:
                w.goto_line(i)
                break
            seen_null_line = regex.whitespace.match(w.buffer.lines[i])
            i += 1
class PreviousSection(Method):
    '''Move the cursor to the previous section'''
    def _execute(self, w, **vargs):
        cursor = w.logical_cursor()
        i = cursor.y - 1
        seen_null_line = False
        while i >= 0:
            if seen_null_line:
                w.goto_line(i)
                break
            seen_null_line = regex.whitespace.match(w.buffer.lines[i])
            i -= 1

class GotoChar(Method):
    '''Jump to the specified character'''
    args = [Argument("charno", type=type(0), prompt="Goto char: ")]
    def _execute(self, w, **vargs):
        w.goto_char(vargs["charno"])

class ForwardChars(Method):
    '''Move forward the specified number of characters'''
    args = [Argument("charno", type=type(0), prompt="Forward chars: ")]
    def _execute(self, w, **vargs):
        w.forward_chars(vargs["charno"])

class GotoLine(Method):
    '''Jump to the specified line number'''
    args = [Argument("lineno", type=type(0), prompt="Goto line: ")]
    def _execute(self, w, **vargs):
        n = vargs["lineno"]
        if n < 0:
            n = len(w.buffer.lines) + n + 1
        if n > len(w.buffer.lines):
            n = len(w.buffer.lines)
        elif n < 1:
            n = 1
        w.goto_line(n)

class ForwardLines(Method):
    '''Move forward the specified number of lines'''
    args = [Argument("lineno", type=type(0), prompt="Forward lines: ")]
    def _execute(self, w, **vargs):
        w.forward_lines(vargs["lineno"])