plugins/aspell_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Wed, 02 May 2007 22:38:53 +0200
changeset 28 f7d12d2d39bc
parent 27 c66d39c6c27b
child 30 33af762962f1
permissions -rw-r--r--
aspell: Default encoding value (ascii)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
#$ neutron_plugin 01
23
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
     2
# 2007-05 Mikael Berthe <mikael@lilotux.net>
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
     4
import subprocess
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
     5
import locale
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
import re
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
def handler_aspell(type, source, parameters):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
    parameters = parameters.lstrip().rstrip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
    splitdata = parameters.split()
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    11
    cmd = ['/usr/bin/aspell', '-a']
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
    reply = ""
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
27
c66d39c6c27b aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    14
    if not parameters:
c66d39c6c27b aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    15
        return
c66d39c6c27b aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    16
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    17
    LC, encoding = locale.getdefaultlocale()
28
f7d12d2d39bc aspell: Default encoding value (ascii)
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    18
    if not encoding:
f7d12d2d39bc aspell: Default encoding value (ascii)
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    19
        encoding = 'ascii'
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    20
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    21
    # Check if the language is specified (two-letter code)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
    if len(splitdata) > 1:
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
        lang = parameters.lower()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
        if re.match(r"\w\w ", lang):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
            parameters = parameters[3:].lstrip()
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    26
            cmd.append("--lang=" + lang)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    28
    # Spawn an aspell process
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    29
    aspell = subprocess.Popen(cmd, stdin=subprocess.PIPE,
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    30
                              stdout=subprocess.PIPE)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    32
    aspell.stdin.write(parameters.encode(encoding))
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    33
    aspell.stdin.close()
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    35
    # Skip the first line (banner)
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    36
    aspell.stdout.readline()
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    37
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    38
    # Process all result lines
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    39
    for r in aspell.stdout.readlines():
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    40
        r = r.decode(encoding)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
        if r == "*\n":
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
            reply += "* Word correctly spelled.\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
        elif r.startswith("& "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
            if r.count(": "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
                reply += "* Let me suggest " + r[r.find(": ")+2:].strip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
                reply += ".\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
            else:
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    48
                # I don't know if it can happen
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
                reply += r
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    51
    aspell.stdout.close()
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    52
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
    if not reply:
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    54
        reply = "Unknown error (no reply)"
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
    smsg(type, source, reply.rstrip())
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
register_command_handler(handler_aspell, 'aspell', 0, 'Check the spelling',
23
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    59
                         'aspell [lang]', ['aspell fr bonjour'])
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    60
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    61
# vim:set et sts=4: