Grammalecte  Check-in [ce4a3cc724]

Overview
Comment:[tb] spellchecker: tabulations to spaces
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tb | tbnext
Files: files | file ages | folders
SHA3-256: ce4a3cc724ee1361bb16f96779d82334ea81ef0c514b44ad0fd4ba7f0f1ab1da
User & Date: olr on 2018-03-14 19:50:26
Other Links: branch diff | manifest | tags
Context
2018-03-14
20:52
[tb] remove useless console.log() check-in: 9acf544aa6 user: olr tags: tb, tbnext
19:50
[tb] spellchecker: tabulations to spaces check-in: ce4a3cc724 user: olr tags: tb, tbnext
19:49
[tb] new API: fix dictionary loading + remove obsolete echo function check-in: 1dd9310e28 user: olr tags: tb, tbnext
Changes

Modified gc_lang/fr/tb/content/spellchecker.js from [7f146a3994] to [bf36be002a].

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
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



-
+

-
-
+
+

-
+

-
-
-
-
+
+
+
+


















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

// JavaScript

/*
	Hunspell wrapper
    Hunspell wrapper

	XPCOM obsolete (?), but there is nothing else...
	Overly complicated and weird. To throw away ASAP if possible.
    XPCOM obsolete (?), but there is nothing else...
    Overly complicated and weird. To throw away ASAP if possible.

	And you can’t access to this from a PromiseWorker (it sucks).
    And you can’t access to this from a PromiseWorker (it sucks).

	https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/mozISpellCheckingEngine
	https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Using_spell_checking_in_XUL
	https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile
	https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm
    https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/mozISpellCheckingEngine
    https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Using_spell_checking_in_XUL
    https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile
    https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm
*/

"use strict";

/*
// Loaded in another file
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const { require } = Cu.import("resource://gre/modules/commonjs/toolkit/require.js", {});
*/


const FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils;
const AddonManager = Cu.import("resource://gre/modules/AddonManager.jsm").AddonManager;


var oSpellControl = {
	xSCEngine: null,
	init: function () {
		if (this.xSCEngine === null) {
			try {
				let sSpellchecker = "@mozilla.org/spellchecker/myspell;1";
				if ("@mozilla.org/spellchecker/hunspell;1" in Cc) {
					sSpellchecker = "@mozilla.org/spellchecker/hunspell;1";
				}
				if ("@mozilla.org/spellchecker/engine;1" in Cc) {
					sSpellchecker = "@mozilla.org/spellchecker/engine;1";
				}
				this.xSCEngine = Cc[sSpellchecker].getService(Ci.mozISpellCheckingEngine);
			}
			catch (e) {
				console.log("Can’t initiate the spellchecker.");
				Cu.reportError(e);
			}
		}
	},
	getDictionariesList: function () {
		this.init();
		try {
			let l = {};
			let c = {};
			this.xSCEngine.getDictionaryList(l, c);
			return l.value;
		}
		catch (e) {
			Cu.reportError(e);
			return [];
		}
	},
	setDictionary: function (sLocale) {
		if (this.getDictionariesList().includes(sLocale)) {
			try {
				this.xSCEngine.dictionary = sLocale; // en-US, fr, etc.
				return true;
			}
			catch (e) {
				Cu.reportError(e);
				return false;
			}
		} else {
			console.log("Warning. No dictionary for locale: " + sLocale);
			console.log("Existing dictionaries: " + this.getDictionariesList().join(" | "));
		}
		return false;
	},
	check: function (sWord) {
		// todo: check in personal dict?
		try {
			return this.xSCEngine.check(sWord);
		}
		catch (e) {
			Cu.reportError(e);
			return false;
		}
	},
	suggest: function (sWord) {
		try {
			let lSugg = {};
			this.xSCEngine.suggest(sWord, lSugg, {});
			return lSugg.value;
			// lSugg.value is a JavaScript Array of strings
		}
		catch (e) {
			Cu.reportError(e);
			return ['#Erreur.'];
		}
	},
	addDirectory: function (sFolder) {
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.addDirectory(xNsiFolder);
		}
		catch (e) {
			console.log("Unable to add directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	removeDirectory: function (sFolder) {
		// does not work but no exception raised (bug?)
		try {
			let xNsiFolder = new FileUtils.File(sFolder);
			this.xSCEngine.removeDirectory(xNsiFolder);
		}
		catch (e) {
			console.log("Unable to remove directory: " + sFolder);
			Cu.reportError(e);
		}
	},
	setExtensionDictFolder: function (sDictName, bActivate) {
		try {
			let that = this;
			let sPath = "/content/dictionaries/" + sDictName;
			AddonManager.getAddonByID("French-GC-TB@grammalecte.net", function (addon) {
				let xURI = addon.getResourceURI(sPath);
				//console.log(xURI);
				let sFolder = xURI.filePath;
				if (sFolder !== undefined) {
					if (/^\/[A-Z]:\//.test(sFolder)) {
						// Windows path
						sFolder = sFolder.slice(1).replace(/\//g, "\\\\");
					}
					console.log("folder: " + sFolder);
					if (bActivate) {
						that.addDirectory(sFolder);
					} else {
						that.removeDirectory(sFolder);
					}
				}
			});
		}
		catch (e) {
			console.log("Unable to add extension folder");
			Cu.reportError(e);
		}
	}
    xSCEngine: null,
    init: function () {
        if (this.xSCEngine === null) {
            try {
                let sSpellchecker = "@mozilla.org/spellchecker/myspell;1";
                if ("@mozilla.org/spellchecker/hunspell;1" in Cc) {
                    sSpellchecker = "@mozilla.org/spellchecker/hunspell;1";
                }
                if ("@mozilla.org/spellchecker/engine;1" in Cc) {
                    sSpellchecker = "@mozilla.org/spellchecker/engine;1";
                }
                this.xSCEngine = Cc[sSpellchecker].getService(Ci.mozISpellCheckingEngine);
            }
            catch (e) {
                console.log("Can’t initiate the spellchecker.");
                Cu.reportError(e);
            }
        }
    },
    getDictionariesList: function () {
        this.init();
        try {
            let l = {};
            let c = {};
            this.xSCEngine.getDictionaryList(l, c);
            return l.value;
        }
        catch (e) {
            Cu.reportError(e);
            return [];
        }
    },
    setDictionary: function (sLocale) {
        if (this.getDictionariesList().includes(sLocale)) {
            try {
                this.xSCEngine.dictionary = sLocale; // en-US, fr, etc.
                return true;
            }
            catch (e) {
                Cu.reportError(e);
                return false;
            }
        } else {
            console.log("Warning. No dictionary for locale: " + sLocale);
            console.log("Existing dictionaries: " + this.getDictionariesList().join(" | "));
        }
        return false;
    },
    check: function (sWord) {
        // todo: check in personal dict?
        try {
            return this.xSCEngine.check(sWord);
        }
        catch (e) {
            Cu.reportError(e);
            return false;
        }
    },
    suggest: function (sWord) {
        try {
            let lSugg = {};
            this.xSCEngine.suggest(sWord, lSugg, {});
            return lSugg.value;
            // lSugg.value is a JavaScript Array of strings
        }
        catch (e) {
            Cu.reportError(e);
            return ['#Erreur.'];
        }
    },
    addDirectory: function (sFolder) {
        try {
            let xNsiFolder = new FileUtils.File(sFolder);
            this.xSCEngine.addDirectory(xNsiFolder);
        }
        catch (e) {
            console.log("Unable to add directory: " + sFolder);
            Cu.reportError(e);
        }
    },
    removeDirectory: function (sFolder) {
        // does not work but no exception raised (bug?)
        try {
            let xNsiFolder = new FileUtils.File(sFolder);
            this.xSCEngine.removeDirectory(xNsiFolder);
        }
        catch (e) {
            console.log("Unable to remove directory: " + sFolder);
            Cu.reportError(e);
        }
    },
    setExtensionDictFolder: function (sDictName, bActivate) {
        try {
            let that = this;
            let sPath = "/content/dictionaries/" + sDictName;
            AddonManager.getAddonByID("French-GC-TB@grammalecte.net", function (addon) {
                let xURI = addon.getResourceURI(sPath);
                //console.log(xURI);
                let sFolder = xURI.filePath;
                if (sFolder !== undefined) {
                    if (/^\/[A-Z]:\//.test(sFolder)) {
                        // Windows path
                        sFolder = sFolder.slice(1).replace(/\//g, "\\\\");
                    }
                    console.log("folder: " + sFolder);
                    if (bActivate) {
                        that.addDirectory(sFolder);
                    } else {
                        that.removeDirectory(sFolder);
                    }
                }
            });
        }
        catch (e) {
            console.log("Unable to add extension folder");
            Cu.reportError(e);
        }
    }
};