diff options
| author | Eli Zaretskii | 2015-08-22 17:16:58 +0300 |
|---|---|---|
| committer | Eli Zaretskii | 2015-08-22 17:16:58 +0300 |
| commit | ff42c71d054019dacace429204dfdf54111cb3d4 (patch) | |
| tree | 75adbe0de8323f308aa3a974919a7e43fe0274dd /lisp | |
| parent | 19efb9db0a03c453475f3b735d4f2e6fdfb853e4 (diff) | |
| download | emacs-ff42c71d054019dacace429204dfdf54111cb3d4.tar.gz emacs-ff42c71d054019dacace429204dfdf54111cb3d4.zip | |
Support invocation of Hunspell with multiple dictionaries
* lisp/textmodes/ispell.el (ispell-parse-hunspell-affix-file): Support
lists of dictionaries of the form "DICT1,DICT2,...".
(ispell-hunspell-add-multi-dic): New command. (Bug#20495)
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/textmodes/ispell.el | 113 |
1 files changed, 75 insertions, 38 deletions
diff --git a/lisp/textmodes/ispell.el b/lisp/textmodes/ispell.el index 5831fc82ac3..8627404ddfe 100644 --- a/lisp/textmodes/ispell.el +++ b/lisp/textmodes/ispell.el | |||
| @@ -1195,44 +1195,81 @@ all uninitialized dicts using that affix file." | |||
| 1195 | 1195 | ||
| 1196 | (defun ispell-parse-hunspell-affix-file (dict-key) | 1196 | (defun ispell-parse-hunspell-affix-file (dict-key) |
| 1197 | "Parse Hunspell affix file to extract parameters for DICT-KEY. | 1197 | "Parse Hunspell affix file to extract parameters for DICT-KEY. |
| 1198 | Return a list in `ispell-dictionary-alist' format." | 1198 | Return a list in `ispell-dictionary-alist' format. |
| 1199 | (let ((affix-file (cadr (assoc dict-key ispell-hunspell-dict-paths-alist)))) | 1199 | |
| 1200 | (unless affix-file | 1200 | DICT_KEY can be in the \"DICT1,DICT2,DICT3\" format, to invoke Hunspell |
| 1201 | (error "ispell-phaf: No matching entry for %s.\n" dict-key)) | 1201 | with a list of dictionaries. The first dictionary in the list must have |
| 1202 | (if (not (file-exists-p affix-file)) | 1202 | a corresponding .aff affix file; the rest are allowed to have no affix |
| 1203 | (error "ispell-phaf: File \"%s\" not found.\n" affix-file)) | 1203 | files, and will then use the affix file of the preceding dictionary that |
| 1204 | (let ((dict-name (file-name-sans-extension | 1204 | did." |
| 1205 | (file-name-nondirectory affix-file))) | 1205 | (let ((dict-list (split-string dict-key "," t)) |
| 1206 | otherchars-string otherchars-list) | 1206 | (first-p t) |
| 1207 | (with-temp-buffer | 1207 | (dict-arg "") |
| 1208 | (insert-file-contents affix-file) | 1208 | otherchars-list) |
| 1209 | (setq otherchars-string | 1209 | (dolist (dict-key dict-list) |
| 1210 | (save-excursion | 1210 | (let ((affix-file |
| 1211 | (goto-char (point-min)) | 1211 | (cadr (assoc dict-key ispell-hunspell-dict-paths-alist)))) |
| 1212 | (if (search-forward-regexp "^WORDCHARS +" nil t ) | 1212 | (unless affix-file |
| 1213 | (buffer-substring (point) | 1213 | (error "ispell-phaf: No matching entry for %s in `ispell-hunspell-dict-paths-alist'.\n" dict-key)) |
| 1214 | (progn (end-of-line) (point)))))) | 1214 | (if (and first-p (not (file-exists-p affix-file))) |
| 1215 | ;; Remove trailing whitespace and extra stuff. Make list if | 1215 | (error "ispell-phaf: File \"%s\" not found.\n" affix-file)) |
| 1216 | ;; non-nil. | 1216 | (and first-p (setq first-p nil)) |
| 1217 | (setq otherchars-list | 1217 | (let ((dict-name (file-name-sans-extension |
| 1218 | (if otherchars-string | 1218 | (file-name-nondirectory affix-file))) |
| 1219 | (split-string | 1219 | otherchars-string) |
| 1220 | (if (string-match " +.*$" otherchars-string) | 1220 | (with-temp-buffer |
| 1221 | (replace-match "" nil nil otherchars-string) | 1221 | (insert-file-contents affix-file) |
| 1222 | otherchars-string) | 1222 | (setq otherchars-string |
| 1223 | "" t))) | 1223 | (save-excursion |
| 1224 | 1224 | (goto-char (point-min)) | |
| 1225 | ;; Fill dict entry | 1225 | (if (search-forward-regexp "^WORDCHARS +" nil t ) |
| 1226 | (list dict-key | 1226 | (buffer-substring (point) |
| 1227 | "[[:alpha:]]" | 1227 | (progn (end-of-line) (point)))))) |
| 1228 | "[^[:alpha:]]" | 1228 | ;; Remove trailing whitespace and extra stuff. Make list |
| 1229 | (if otherchars-list | 1229 | ;; if non-nil. |
| 1230 | (regexp-opt otherchars-list) | 1230 | (if otherchars-string |
| 1231 | "") | 1231 | (let* ((otherchars-string |
| 1232 | t ; many-otherchars-p: We can't tell, set to t. | 1232 | ;; Remove trailing junk. |
| 1233 | (list "-d" dict-name) | 1233 | (substring otherchars-string |
| 1234 | nil ; extended-char-mode: not supported by hunspell! | 1234 | 0 (string-match " +" otherchars-string))) |
| 1235 | 'utf-8))))) | 1235 | (chars-list (append otherchars-string nil))) |
| 1236 | (setq chars-list (delq ?\ chars-list)) | ||
| 1237 | (dolist (ch chars-list) | ||
| 1238 | (add-to-list 'otherchars-list ch))))) | ||
| 1239 | ;; Cons the argument for the -d switch. | ||
| 1240 | (setq dict-arg (concat dict-arg | ||
| 1241 | (if (> (length dict-arg) 0) ",") | ||
| 1242 | dict-name))))) | ||
| 1243 | |||
| 1244 | ;; Fill dict entry | ||
| 1245 | (list dict-key | ||
| 1246 | "[[:alpha:]]" | ||
| 1247 | "[^[:alpha:]]" | ||
| 1248 | (if otherchars-list | ||
| 1249 | (regexp-opt (mapcar 'char-to-string otherchars-list)) | ||
| 1250 | "") | ||
| 1251 | t ; many-otherchars-p: We can't tell, set to t. | ||
| 1252 | (list "-d" dict-arg) | ||
| 1253 | nil ; extended-char-mode: not supported by hunspell! | ||
| 1254 | 'utf-8))) | ||
| 1255 | |||
| 1256 | (defun ispell-hunspell-add-multi-dic (dict) | ||
| 1257 | "Add DICT of the form \"DICT1,DICT2,...\" to `ispell-dictionary-alist'. | ||
| 1258 | |||
| 1259 | Invoke this command before you want to start Hunspell for the first time | ||
| 1260 | with a particular combination of dictionaries. The first dictionary | ||
| 1261 | in the list must have an affix file where Hunspell affix files are kept." | ||
| 1262 | (interactive "sMulti-dictionary combination: ") | ||
| 1263 | ;; Make sure the first dictionary in the list is known to us. | ||
| 1264 | (let ((first-dict (car (split-string dict "," t)))) | ||
| 1265 | (unless ispell-hunspell-dictionary-alist | ||
| 1266 | (ispell-find-hunspell-dictionaries) | ||
| 1267 | (setq ispell-dictionary-alist ispell-hunspell-dictionary-alist)) | ||
| 1268 | (or (assoc first-dict ispell-local-dictionary-alist) | ||
| 1269 | (assoc first-dict ispell-dictionary-alist) | ||
| 1270 | (error "Unknown dictionary: %s" first-dict))) | ||
| 1271 | (add-to-list 'ispell-dictionary-alist (list dict '())) | ||
| 1272 | (ispell-hunspell-fill-dictionary-entry dict)) | ||
| 1236 | 1273 | ||
| 1237 | (defun ispell-find-hunspell-dictionaries () | 1274 | (defun ispell-find-hunspell-dictionaries () |
| 1238 | "Look for installed Hunspell dictionaries. | 1275 | "Look for installed Hunspell dictionaries. |