1####################################################################
2# SpellChecker.awk                                                 #
3####################################################################
4BEGIN {
5    provides("spell")
6    provides("aspell")
7    provides("hunspell")
8}
9
10# Detect external (ispell -a compatible) spell checker.
11function spellInit() {
12    Ispell = detectProgram("aspell", "--version") ? "aspell" :
13        (detectProgram("hunspell", "--version") ? "hunspell" : "")
14
15    if (!Ispell) {
16        e("[ERROR] Spell checker (aspell or hunspell) not found.")
17        exit 1
18    }
19}
20
21function aspellInit() {
22    if (!(Ispell = detectProgram("aspell", "--version") ? "aspell" : "")) {
23        e("[ERROR] Spell checker (aspell) not found.")
24        exit 1
25    }
26}
27
28function hunspellInit() {
29    if (!(Ispell = detectProgram("hunspell", "--version") ? "hunspell" : "")) {
30        e("[ERROR] Spell checker (hunspell) not found.")
31        exit 1
32    }
33}
34
35# Check a string.
36function spellTranslate(text, sl, tl, hl,
37                        isVerbose, toSpeech, returnPlaylist, returnIl,
38                        ####
39                        args, i, j, r, line, group, word, sug) {
40    args = " -a" (sl != "auto" ? " -d " sl : "")
41    if (system("echo" PIPE Ispell args SUPOUT SUPERR)) {
42        e("[ERROR] No dictionary for language: " sl)
43        exit 1
44    }
45
46    i = 1
47    r = ""
48    while ((("echo " parameterize(text) PIPE Ispell args SUPERR) |& getline line) > 0) {
49        match(line,
50              /^& (.*) [[:digit:]]+ [[:digit:]]+: ([^,]+)(, ([^,]+))?(, ([^,]+))?/,
51              group)
52        if (RSTART) {
53            ExitCode = 1 # found a spelling error
54
55            word = group[1]
56            sug = "[" group[2]
57            if (group[4]) sug = sug "|" group[4]
58            if (group[6]) sug = sug "|" group[6]
59            sug = sug "]"
60
61            j = i + index(substr(text, i), word) - 1
62            r = r substr(text, i, j - i)
63            r = r ansi("bold", ansi("red", word)) ansi("yellow", sug)
64            i = j + length(word)
65        }
66    }
67    r = r substr(text, i)
68    return r
69}
70
71function aspellTranslate(text, sl, tl, hl,
72                         isVerbose, toSpeech, returnPlaylist, returnIl) {
73    return spellTranslate(text, sl, tl, hl)
74}
75
76function hunspellTranslate(text, sl, tl, hl,
77                           isVerbose, toSpeech, returnPlaylist, returnIl) {
78    return spellTranslate(text, sl, tl, hl)
79}
80
81function spellTTSUrl(text, tl,    narrator) {
82    e("[ERROR] Spell checker does not support TTS.")
83    ExitCode = 1
84    return
85}
86
87function aspellTTSUrl(text, tl,    narrator) {
88    return spellTTSUrl(text, tl)
89}
90
91function hunspellTTSUrl(text, tl,    narrator) {
92    return spellTTSUrl(text, tl)
93}
94
95function spellWebTranslateUrl(uri, sl, tl, hl) {
96    e("[ERROR] Spell checker does not support web translation.")
97    ExitCode = 1
98    return
99}
100
101function aspellWebTranslateUrl(uri, sl, tl, hl) {
102    return spellWebTranslateUrl(uri, sl, tl, hl)
103}
104
105function hunspellWebTranslateUrl(uri, sl, tl, hl) {
106    return spellWebTranslateUrl(uri, sl, tl, hl)
107}
108