import os
import util

# default callbacks
def none(w):
    return None

def last_buffer(w):
    bl = w.application.bufferlist
    if bl.hidden_buffers:
        return bl.hidden_buffers[0].name()
    else:
        return ''

def current_buffer(w):
    return w.buffer.name()

def last_replace_before(w):
    a = w.application
    if a.config.get('use_last_replace') and a.last_replace_before:
        return a.last_replace_before
    return None
def last_replace_after(w):
    a = w.application
    if a.config.get('use_last_replace') and a.last_replace_after:
        return w.application.last_replace_after
    return None

def current_working_dir(w):
    return util.normal_path(os.getcwd())

def path_dirname(w):
    if hasattr(w.buffer, 'path'):
        return util.normal_path(os.path.dirname(w.buffer.path))
    else:
        return current_working_dir(w)

# default callback builders
def build_constant(c):
    return lambda w: c

def build_mode_var(name):
    return lambda w: hasattr(w.mode, name) and getattr(w.mode, name) or ''