aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/history.py
blob: 4467e5d073a40f2e605ac73c9e67897896a54b55 (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
__export__ = ['get_history']
UZBLS = {}

import random

shared_history = {'':[]}

class History(object):
    def __init__(self):
        self._temporary = []
        self.prompt = ''
        self.cursor = None
        self.__temp_tail = False
        self.search_key = None

    def prev(self):
        if self.cursor is None:
            self.cursor = len(self) - 1
        else:
            self.cursor -= 1

        if self.search_key:
            while self.cursor >= 0 and self.search_key not in self[self.cursor]:
                self.cursor -= 1

        if self.cursor < 0 or len(self) == 0:
            self.cursor = -1
            return random.choice(end_messages)

        return self[self.cursor]

    def next(self):
        if self.cursor is None:
            return ''

        self.cursor += 1

        if self.search_key:
            while self.cursor < len(self) and self.search_key not in self[self.cursor]:
                self.cursor += 1

        if self.cursor >= len(shared_history[self.prompt]):
            self.cursor = None
            self.search_key = None

            if self._temporary:
                print 'popping temporary'
                return self._temporary.pop()
            return ''

        return self[self.cursor]

    def change_prompt(self, prompt):
        self.prompt = prompt
        self._temporary = []
        self.__temp_tail = False
        if prompt not in shared_history:
            shared_history[prompt] = []

    def search(self, key):
        self.search_key = key
        return self.prev()

    def add(self, cmd):
        if self._temporary:
            self._temporary.pop()

        shared_history[self.prompt].append(cmd)
        self.cursor = None
        self.search_key = None

    def add_temporary(self, cmd):
        assert not self._temporary

        self._temporary.append(cmd)
        self.cursor = len(self) - 1

        print 'adding temporary', self

    def __getitem__(self, i):
        if i < len(shared_history[self.prompt]):
            return shared_history[self.prompt][i]
        return self._temporary[i-len(shared_history)+1]

    def __len__(self):
        return len(shared_history[self.prompt]) + len(self._temporary)

    def __str__(self):
        return "(History %s)" % (self.cursor)

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

    return UZBLS[uzbl]

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

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

def keycmd_exec(uzbl, keylet):
    history = get_history(uzbl)
    cmd = keylet.get_keycmd()
    if cmd:
        history.add(cmd)

def config_changed(uzbl, key, value):
    if key == 'keycmd_prompt':
        history = get_history(uzbl)
        history.change_prompt(value)

def history_prev(uzbl, _x):
    history = get_history(uzbl)
    cmd = uzbl.get_keylet().get_keycmd()
    if history.cursor is None and cmd:
        history.add_temporary(cmd)

    uzbl.set_keycmd(history.prev())
    print 'PREV', history

def history_next(uzbl, _x):
    history = get_history(uzbl)
    cmd = uzbl.get_keylet().get_keycmd()

    uzbl.set_keycmd(history.next())
    print 'NEXT', history

def history_search(uzbl, key):
    history = get_history(uzbl)
    uzbl.set_keycmd(history.search(key))
    print 'SEARCH', history

end_messages = ('Look behind you, A three-headed monkey!', 'error #4: static from nylon underwear.', 'error #5: static from plastic slide rules.', 'error #6: global warming.', 'error #9: doppler effect.', 'error #16: somebody was calculating pi on the server.', 'error #19: floating point processor overflow.', 'error #21: POSIX compliance problem.', 'error #25: Decreasing electron flux.', 'error #26: first Saturday after first full moon in Winter.', 'error #64: CPU needs recalibration.', 'error #116: the real ttys became pseudo ttys and vice-versa.', 'error #229: wrong polarity of neutron flow.', 'error #330: quantum decoherence.', 'error #388: Bad user karma.', 'error #407: Route flapping at the NAP.', 'error #435: Internet shut down due to maintenance.')

def init(uzbl):
    connects = {'INSTANCE_START': add_instance,
        'INSTANCE_EXIT': del_instance,
        'KEYCMD_EXEC': keycmd_exec,
        'CONFIG_CHANGED': config_changed,
        'HISTORY_PREV': history_prev,
        'HISTORY_NEXT': history_next,
        'HISTORY_SEARCH': history_search
    }

    uzbl.connect_dict(connects)