/usr/share/weechat/python/correction_completion.py is in weechat-scripts 20120603-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | # -*- coding: utf-8 -*-
######################################################################
# Copyright (c) 2011 by Pascal Wittmann <mail@pascal-wittmann.de>
#
# 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/>.
#
# Marked Parts are from Wojciech Muła <wojciech_mula@poczta.onet.pl>
# and are licensed under BSD and are avaliable at
# http://0x80.pl/proj/aspell-python/
########################################################################
# INSTALLTION
# After copying this file into your python plugin directory, start weechat
# load the script and follow futher instructions calling
# /help correction_completion
# You can find these instructions as markdown on
# https://github.com/pSub/weechat-correction-completion/blob/master/README.md
# too.
try:
import ctypes
import ctypes.util
except ImportError:
print "This script depends on ctypes"
try:
import weechat as w
except ImportError:
print "This script must be run under WeeChat."
print "Get WeeChat now at: http://www.weechat.org/"
SCRIPT_NAME = "correction_completion"
SCRIPT_AUTHOR = "Pascal Wittmann <mail@pascal-wittmann.de>"
SCRIPT_VERSION = "0.2.3"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Provides a completion for 's/typo/correct'"
SCRIPT_COMMAND = "correction_completion"
# Default Options
# Your can use all aspell options listed on
# http://aspell.net/man-html/The-Options.html
settings = {
'lang' : 'en',
}
# The Bunch Class is from
# http://code.activestate.com/recipes/52308/
class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
def completion(data, completion_item, buffer, completion):
if state.used == True:
return w.WEECHAT_RC_OK
else:
state.used = True
# Current cursor position
pos = w.buffer_get_integer(buffer, 'input_pos')
# Current input string
input = w.buffer_get_string(buffer, 'input')
fst = input.find("s/")
snd = input.find("/", fst + 2)
# Check for typo or suggestion completion
if pos > 2 and fst >= 0 and fst < pos:
if snd >= 0 and snd < pos:
complete_replacement(pos, input, buffer)
else:
complete_typo(pos, input, buffer)
state.used = False
return w.WEECHAT_RC_OK
def complete_typo(pos, input, buffer):
# Assume that typo changes when doing a completion
state.curRepl = -1
# Get the text of the current buffer
list = []
infolist = w.infolist_get('buffer_lines', buffer, '')
while w.infolist_next(infolist):
list.append(stripcolor(w.infolist_string(infolist, 'message')))
w.infolist_free(infolist)
# Generate a list of words
text = (' '.join(list)).split(' ')
# Remove duplicate elements
text = unify(text)
# Sort by alphabet and length
text.sort(key=lambda item: (item, -len(item)))
i = iter(text)
# Get index of last occurence of "s/" befor cursor position
n = input.rfind("s/", 0, pos)
# Get substring and search the replacement
substr = input[n+2:pos]
replace = search((lambda word : word.startswith(substr)), i)
# If no replacement found, display substring
if replace == "":
replace = substr
# If substring perfectly matched take next replacement
if replace == substr:
try:
replace = next(i)
except StopIteration:
replace = substr
changeInput(substr, replace, input, pos, buffer)
def complete_replacement(pos, input, buffer):
# Start Positions
n = input.rfind("s/", 0, pos)
m = input.rfind("/", n + 2, pos)
repl = input[m + 1 : pos]
typo = input[n + 2 : m]
# Only query new suggestions, when typo changed
if state.curRepl == -1 or typo != state.curTypo:
state.suggestions = suggest(typo)
state.curTypo = typo
if len(state.suggestions) == 0:
return
# Start at begining when reached end of suggestions
if state.curRepl == len(state.suggestions) - 1:
state.curRepl = -1
# Take next suggestion
state.curRepl += 1
# Put suggestion into the input
changeInput(repl, state.suggestions[state.curRepl], input, pos, buffer)
def changeInput(search, replace, input, pos, buffer):
# Put the replacement into the input
n = len(search)
input = '%s%s%s' %(input[:pos-n], replace, input[pos:])
w.buffer_set(buffer, 'input', input)
w.buffer_set(buffer, 'input_pos', str(pos - n + len(replace)))
def stripcolor(string):
return w.string_remove_color(string, '')
def search(p, i):
# Search for item matching the predicate p
while True:
try:
item = next(i)
if p(item):
return item
except StopIteration:
return ""
def unify(list):
# Remove duplicate elements from a list
checked = []
for e in list:
if e not in checked:
checked.append(e)
return checked
# Parts are from Wojciech Muła
def suggest(word):
if type(word) is str:
suggestions = aspell.aspell_speller_suggest(
speller,
word.encode(),
len(word))
elements = aspell.aspell_word_list_elements(suggestions)
list = []
while True:
wordptr = aspell.aspell_string_enumeration_next(elements)
if not wordptr:
break;
else:
word = ctypes.c_char_p(wordptr)
list.append(str(word.value))
aspell.delete_aspell_string_enumeration(elements)
return list
else:
raise TypeError("String expected")
def load_config(data = "", option = "", value = ""):
global speller
config = aspell.new_aspell_config()
for option, default in settings.iteritems():
if not w.config_is_set_plugin(option):
w.config_set_plugin(option, default)
value = w.config_get_plugin(option)
if not aspell.aspell_config_replace(
config,
option.encode(),
value.encode()):
raise Exception("Failed to replace config entry")
# Error checking is from Wojciech Muła
possible_error = aspell.new_aspell_speller(config)
aspell.delete_aspell_config(config)
if aspell.aspell_error_number(possible_error) != 0:
aspell.delete_aspell_can_have_error(possible_error)
raise Exception("Couldn't create speller")
speller = aspell.to_aspell_speller(possible_error)
return w.WEECHAT_RC_OK
if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
# Saving the current completion state
state = Bunch(used = False, curTypo = '', curRepl = -1, suggestions = [])
# Use ctypes to access the apsell library
aspell = ctypes.CDLL(ctypes.util.find_library('aspell'))
speller = 0
# Load configuration
load_config()
template = 'correction_completion'
# Register completion hook
w.hook_completion(template, "Completes after 's/' with words from buffer",
'completion', '')
# Register hook to update config when option is changed with /set
w.hook_config("plugins.var.python." + SCRIPT_NAME + ".*", "load_config", "")
# Register help command
w.hook_command(SCRIPT_COMMAND, SCRIPT_DESC, "",
"""Usage:
If you want to correct yourself, you often do this using the
expression 's/typo/correct'. This plugin allows you to complete the
first part (the typo) by pressing *Tab*. The words from the actual
buffer are used to complet this part. If the word can be perfectly
matched the next word in alphabetical order is shown.
The second part (the correction) can also be completed. Just press
*Tab* after the slash and the best correction for the typo is fetched from aspell.
If you press *Tab* again, it shows the next suggestion.
The lanuage used for suggestions can be set with the option
plugins.var.python.correction_completion.lang
The aspell language pack must be installed for this language.
Setup:
Add the template %%(%(completion)s) to the default completion template.
The best way to set the template is to use the iset-plugin¹, because you can see
there the current value before changing it. Of course you can also use the
standard /set-command e.g.
/set weechat.completion.default_template "%%(nicks)|%%(irc_channels)|%%(%(completion)s)"
Footnotes:
¹ http://weechat.org/scripts/source/stable/iset.pl/
"""
%dict(completion=template), '', '', '')
|