aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/uzbl/plugins/mode.py
blob: 74e805a802252e16dafe3ef3fc9601e6fef26adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sys
import re

__export__ = ['set_mode', 'get_mode']

UZBLS = {}

DEFAULTS = {
  'mode': '',
  'modes': {
    'insert': {
      'forward_keys': True,
      'keycmd_events': False,
      'modcmd_updates': False,
      'indicator': 'I'},
    'command': {
      'forward_keys': False,
      'keycmd_events': True,
      'modcmd_updates': True,
      'indicator': 'C'}}}

FINDSPACES = re.compile("\s+")
VALID_KEY = re.compile("^[\w_]+$").match

def error(msg):
    sys.stderr.write("mode plugin: error: %s\n" % msg)


def add_instance(uzbl, *args):
    UZBLS[uzbl] = dict(DEFAULTS)


def del_instance(uzbl, *args):
    if uzbl in UZBLS:
        del UZBLS[uzbl]


def get_mode_dict(uzbl):
    if uzbl not in UZBLS:
        add_instance(uzbl)

    return UZBLS[uzbl]


def get_mode_config(uzbl, mode):
    modes = get_mode_dict(uzbl)['modes']
    if mode not in modes:
        modes[mode] = {}

    return modes[mode]


def get_mode(uzbl):
    return get_mode_dict(uzbl)['mode']


def set_mode(uzbl, mode=None):
    mode_dict = get_mode_dict(uzbl)
    config = uzbl.get_config()

    if mode is None:
        if 'default_mode' not in config:
            return

        mode = config['default_mode']

    if not VALID_KEY(mode):
        raise KeyError("invalid mode name: %r" % mode)

    if 'mode' not in config or config['mode'] != mode:
        config['mode'] = mode
        return

    mode_dict['mode'] = mode
    mode_config = get_mode_config(uzbl, mode)

    for (key, value) in mode_config.items():
        if key not in config:
            config[key] = value

        elif config[key] != value:
            config[key] = value

    if 'mode_indicator' not in mode_config:
        config['mode_indicator'] = mode

    uzbl.clear_keycmd()
    uzbl.event("MODE_CHANGED", mode)


def config_changed(uzbl, key, value):
    value = None if not value else value
    if key == 'default_mode':
        if not get_mode(uzbl):
            set_mode(uzbl, value)

    elif key == 'mode':
        set_mode(uzbl, value)


def mode_config(uzbl, args):

    split = map(unicode.strip, FINDSPACES.split(args.lstrip(), 1))
    if len(split) != 2:
        raise SyntaxError('invalid config syntax: %r' % args)

    mode, set = split
    split = map(unicode.strip, set.split('=', 1))
    if len(split) != 2:
        raise SyntaxError('invalid set syntax: %r' % args)

    key, value = split
    if not VALID_KEY(key):
        raise KeyError('invalid config key: %r' % key)

    mode_config = get_mode_config(uzbl, mode)
    mode_config[key] = value

    if get_mode(uzbl) == mode:
        uzbl.set(key, value)


def load_reset(uzbl, *args):
    config = uzbl.get_config()
    if 'reset_on_commit' not in config or config['reset_on_commit'] == '1':
        set_mode(uzbl)


def toggle_modes(uzbl, modes):

    modelist = [s.strip() for s in modes.split(' ') if s]
    if not len(modelist):
        return error("no modes specified to toggle")

    mode_dict = get_mode_dict(uzbl)
    oldmode = mode_dict['mode']
    if oldmode not in modelist:
        return set_mode(uzbl, modelist[0])

    newmode = modelist[(modelist.index(oldmode)+1) % len(modelist)]
    set_mode(uzbl, newmode)


def init(uzbl):

    connects = {'CONFIG_CHANGED': config_changed,
      'INSTANCE_EXIT': del_instance,
      'INSTANCE_START': add_instance,
      'MODE_CONFIG': mode_config,
      'LOAD_START': load_reset,
      'TOGGLE_MODES': toggle_modes}

    uzbl.connect_dict(connects)