File: completion.py

package info (click to toggle)
weechat-scripts 20100422-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,184 kB
  • ctags: 1,634
  • sloc: python: 8,545; perl: 8,472; ruby: 147; tcl: 137; sh: 8; makefile: 2
file content (203 lines) | stat: -rw-r--r-- 6,817 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
###
# Copyright (c) 2010 by Elián Hanisch <lambdae2@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
###

###
#
#   This scripts adds word completion, like irssi's /completion
#
#   Commands:
#   * /completion: see /help completion
#
#
#   Settings:
#   * plugins.var.python.completion.replace_values:
#     Completion list, it shouldn't be edited by hand.
#
#
#   History:
#   2010-01-26
#   version 0.1: release
#
###

try:
    import weechat
    WEECHAT_RC_OK = weechat.WEECHAT_RC_OK
    import_ok = True
except ImportError:
    print "This script must be run under WeeChat."
    print "Get WeeChat now at: http://www.weechat.org/"
    import_ok = False

SCRIPT_NAME    = "completion"
SCRIPT_AUTHOR  = "Elián Hanisch <lambdae2@gmail.com>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Word completions for WeeChat"
SCRIPT_COMMAND = "completion"

completion_template = 'completion_script'

### Config ###
settings = {
'replace_values':''
}

### Messages ###
def debug(s, prefix='', buffer=None):
    """Debug msg"""
    if not weechat.config_get_plugin('debug'): return
    if buffer is None:
        buffer_name = 'DEBUG_' + SCRIPT_NAME
        buffer = weechat.buffer_search('python', buffer_name)
        if not buffer:
            buffer = weechat.buffer_new(buffer_name, '', '', '', '')
            weechat.buffer_set(buffer, 'nicklist', '0')
            weechat.buffer_set(buffer, 'time_for_each_line', '0')
            weechat.buffer_set(buffer, 'localvar_set_no_log', '1')
    weechat.prnt(buffer, '%s\t%s' %(prefix, s))

def error(s, prefix=None, buffer='', trace=''):
    """Error msg"""
    prefix = prefix or script_nick
    weechat.prnt(buffer, '%s%s %s' %(weechat.prefix('error'), prefix, s))
    if weechat.config_get_plugin('debug'):
        if not trace:
            import traceback
            if traceback.sys.exc_type:
                trace = traceback.format_exc()
        not trace or weechat.prnt('', trace)

def say(s, prefix=None, buffer=''):
    """normal msg"""
    prefix = prefix or script_nick
    weechat.prnt(buffer, '%s\t%s' %(prefix, s))

print_replace = lambda k,v : say('%s %s=>%s %s' %(k, color_delimiter, color_reset, v))

### Config functions ###
def get_config_dict(config):
    value = weechat.config_get_plugin(config)
    if not value:
        return {}
    values = value.split(';;')
    values = map(lambda s: s.split('=>'), values)
    #debug(values)
    return dict(values)

def load_replace_table():
    global replace_table
    replace_table = get_config_dict('replace_values')

def save_replace_table():
    global replace_table
    values = [ '%s=>%s' %(k, v) for k, v in replace_table.iteritems() ]
    weechat.config_set_plugin('replace_values', ';;'.join(values))

### Commands ###
def cmd_completion(data, buffer, args):
    global replace_table
    if not args:
        if replace_table:
            for k, v in replace_table.iteritems():
                print_replace(k, v)
        else:
            say('No completions.')
        return WEECHAT_RC_OK
    cmd, space, args = args.partition(' ')
    if cmd == 'add':
        word, space, text = args.partition(' ')
        k, v = word.strip(), text.strip()
        replace_table[k] = v
        save_replace_table()
        say('added: %s %s=>%s %s' %(k, color_delimiter, color_reset, v))
    elif cmd == 'del':
        k = args.strip()
        try:
            del replace_table[k]
            save_replace_table()
            say("completion for '%s' deleted." %k)
            save_replace_table()
        except KeyError:
            error("completion for '%s' not found." %k)
    return WEECHAT_RC_OK

### Completion ###
def completion_replacer(data, completion_item, buffer, completion):
    global replace_table
    input = weechat.buffer_get_string(buffer, 'input')
    input, space, last_word = input.rpartition(' ')
    if last_word in replace_table:
        weechat.buffer_set(buffer, 'input', '%s%s%s ' %(input, space, replace_table[last_word]))
    return WEECHAT_RC_OK

def completion_keys(data, completion_item, buffer, completion):
    global replace_table
    for k in replace_table:
        weechat.hook_completion_list_add(completion, k, 0, weechat.WEECHAT_LIST_POS_SORT)
    return WEECHAT_RC_OK

### Main ###
if __name__ == '__main__' and import_ok and \
        weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, \
        SCRIPT_DESC, '', ''):
    
    # colors
    color_delimiter   = weechat.color('chat_delimiters')
    color_script_nick = weechat.color('chat_nick')
    color_reset   = weechat.color('reset')
    
    # pretty [SCRIPT_NAME]
    script_nick = '%s[%s%s%s]%s' %(color_delimiter, color_script_nick, SCRIPT_NAME, color_delimiter,
            color_reset)

    # settings
    for opt, val in settings.iteritems():
        if not weechat.config_is_set_plugin(opt):
            weechat.config_set_plugin(opt, val)

    load_replace_table()
    
    completion_template = 'completion_script'
    weechat.hook_completion(completion_template,
            "Replaces last word in input by its configured value.", 'completion_replacer', '')
    weechat.hook_completion('completion_keys', "Words in completion list.", 'completion_keys', '')
    
    weechat.hook_command(SCRIPT_COMMAND, SCRIPT_DESC , "[add <word> <text>|del <word>]",
"""
add: adds a new completion, <word> => <text>.
del: deletes a completion.
Without arguments it displays current completions.

<word> will be replaced by <text> when pressing tab,
note that only the last word in input line is completed,
not where the cursor is or in all matching words.

Setup:
For this script to work, you must add the template
%%(%(completion)s) to the default completion template, use:
/set weechat.completion.default_template "%%(nicks)|%%(irc_channels)|%%(%(completion)s)"

Examples:
/%(command)s add wee WeeChat (typing wee<tab> will replace 'wee' by 'WeeChat')
/%(command)s add weeurl http://www.weechat.org/
/%(command)s add test This is a test!
""" %dict(completion=completion_template, command=SCRIPT_COMMAND),
            'add|del %(completion_keys)', 'cmd_completion', '')

# vim:set shiftwidth=4 tabstop=4 softtabstop=4 expandtab textwidth=100: