plugins/aspell_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Wed, 02 May 2007 20:39:20 +0200
changeset 26 8a1893b6b113
parent 24 7d7edae239c9
child 27 c66d39c6c27b
permissions -rw-r--r--
Switch to using module "subprocess" (Suggested by Loic Berthe)
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
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    14
    LC, encoding = locale.getdefaultlocale()
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    15
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    16
    # Check if the language is specified (two-letter code)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
    if len(splitdata) > 1:
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
        lang = parameters.lower()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
        if re.match(r"\w\w ", lang):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
            parameters = parameters[3:].lstrip()
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    21
            cmd.append("--lang=" + lang)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    23
    # Spawn an aspell process
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    24
    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
    25
                              stdout=subprocess.PIPE)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    27
    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
    28
    aspell.stdin.close()
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    30
    # 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
    31
    aspell.stdout.readline()
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    32
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    33
    # Process all result lines
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    34
    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
    35
        r = r.decode(encoding)
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
        if r == "*\n":
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
            reply += "* Word correctly spelled.\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
        elif r.startswith("& "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
            if r.count(": "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
                reply += "* Let me suggest " + r[r.find(": ")+2:].strip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
                reply += ".\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
            else:
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    43
                # I don't know if it can happen
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
                reply += r
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
26
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    46
    aspell.stdout.close()
8a1893b6b113 Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    47
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
    if not reply:
24
7d7edae239c9 Update comments
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    49
        reply = "Unknown error (no reply)"
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
    smsg(type, source, reply.rstrip())
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
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
    54
                         'aspell [lang]', ['aspell fr bonjour'])
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    55
f34555473aaf Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    56
# vim:set et sts=4: