Subject: bug#24439: ispell.el; cutom setting `ispell-library-directory'; patch

From: Laimonas Vėbra <[email-masked]>
Date: Sat, 24 Sep 2016 20:47:15 +0300

Eli Zaretskii wrote:

>> Sorry, that is not true (mistakenly thought, that fopen won't work with
>> windows paths). Then i should really rebuild ispell with LIBDIR set to
>> native path and it will work with Emacs.
>
> I think you don't even need that.  You need to set ISPELL_DICTDIR in
> the environment to point to that directory.
>
> (The reason I didn't suggest this earlier is that I didn't know Cygwin
> can fopen Windows-style absolute file names.)

Actually that won't work with current ispell.el, because LIBRARYVAR is 
completely ignored and dictionaries searched only in LIBDIR (though it 
can be overridden by LIBRARYVAR).

Fixed this and other (-d with an absolute path) issues in attached patch.

Below is a explanatory test:

(require 'ispell)
ispell

;; cygwin ispell reports its LIBDIR as
(ispell-check-version)
"/usr/local/lib"

;; and it's not a correct/existing windows path
(file-exists-p "/usr/local/lib")
nil

(shell-command-to-string "cygpath -w /usr/local/lib")
"C:\\cygwin64\\usr\\local\\lib
"

;; so ispell.el still can not see valid/installed dictionaries
(shell-command-to-string "ls -l /usr/local/lib")
"total 1126
lrwxrwxrwx 1 Admin None      16 Jul 29 01:38 american.hash -> 
americanmed.hash
-rw-r--r-- 1 Admin None 1125408 Jul 29 01:38 americanmed.hash
-rw-r--r-- 1 Admin None   24095 Jul 29 01:38 english.aff
lrwxrwxrwx 1 Admin None      16 Jul 29 01:38 english.hash -> 
americanmed.hash
"
(ispell-valid-dictionary-list)
("default")

;; Though now we can add dictionaries explicitly (-d with an absolute path)
ispell-local-dictionary-alist
nil

(add-to-list 'ispell-local-dictionary-alist
	     '("american" "[A-Za-z]" "[^A-Za-z]" "[']" nil
	       ("-d" "c:/cygwin64/usr/local/lib/american.hash") nil iso-8859-1))
(("american" "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-d" 
"c:/cygwin64/usr/local/lib/american.hash") nil iso-8859-1))

(add-to-list 'ispell-local-dictionary-alist
	     '("english" "[A-Za-z]" "[^A-Za-z]" "[']" nil
	       ("-d" "c:/cygwin64/usr/local/lib/english.hash") nil iso-8859-1))
(("english" "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-d" 
"c:/cygwin64/usr/local/lib/english.hash") nil iso-8859-1) ("american" 
"[A-Za-z]" "[^A-Za-z]" "[']" nil ("-d" 
"c:/cygwin64/usr/local/lib/american.hash") nil iso-8859-1))

(ispell-valid-dictionary-list)
("american" "english" "default")

;; or we can set ispell LIBRARYVAR (default: ISPELL_DICTDIR) environment 
variable
(setq ispell-local-dictionary-alist nil)
nil
(ispell-valid-dictionary-list)
("default")

(setenv "ISPELL_DICTDIR" "c:/cygwin64/usr/local/lib")
"c:/cygwin64/usr/local/lib"

(setq ispell-library-directory (ispell-check-version))
"c:/cygwin64/usr/local/lib"

(ispell-valid-dictionary-list)
("english" "american" "default")

--- lisp/textmodes/ispell.el.orig	2015-04-02 10:23:06.000000000 +0300
+++ lisp/textmodes/ispell.el	2016-09-24 17:58:21.637841100 +0300
@@ -848,7 +848,12 @@
   (let ((default-directory (or (and (boundp 'temporary-file-directory)
 				    temporary-file-directory)
 			       default-directory))
-	result status ispell-program-version)
+	(get-config-var
+	 (lambda (var)
+	   (when (re-search-forward
+		  (concat var " = \\\"\\(.+?\\)\\\"") nil t)
+	     (match-string 1))))
+	result libvar status ispell-program-version)
 
     (with-temp-buffer
       (setq status (ispell-call-process
@@ -870,9 +875,13 @@
 				 ", "
 				 ispell-version))
 	    (message "%s" result))
-	;; return library directory.
-	(if (re-search-forward "LIBDIR = \\\"\\([^ \t\n]*\\)\\\"" nil t)
-	    (setq result (match-string 1))))
+	;; return LIBDIR or LIBRARYVAR (overrides LIBDIR) env.
+	(progn
+	  (setq result (funcall get-config-var "LIBDIR")
+		libvar (funcall get-config-var "LIBRARYVAR"))
+	  (when libvar
+	    (setq libvar (getenv libvar))
+	    (unless (member libvar '(nil "")) (setq result libvar)))))
       (goto-char (point-min))
       (if (not (memq status '(0 nil)))
 	  (error "%s exited with %s %s" ispell-program-name
@@ -1463,23 +1472,29 @@
 
   (let ((dicts (append ispell-local-dictionary-alist ispell-dictionary-alist))
 	(dict-list (cons "default" nil))
-	name dict-bname)
+	(dict-locate
+	 (lambda (dict &optional dir)
+	   (locate-file (file-name-nondirectory dict)
+			`(,(or dir (file-name-directory dict)))
+			(unless (file-name-extension dict) '(".hash" ".has")))))
+	name dict-explt dict-bname)
     (dolist (dict dicts)
       (setq name (car dict)
-	    dict-bname (or (car (cdr (member "-d" (nth 5 dict))))
-			   name))
-      ;; Include if the dictionary is in the library, or dir not defined.
-      (if (and
-	   name
-	   ;; For Aspell, we already know which dictionaries exist.
-	   (or ispell-really-aspell
-	       ;; Include all dictionaries if lib directory not known.
-	       ;; Same for Hunspell, where ispell-library-directory is nil.
-	       (not ispell-library-directory)
-	       (file-exists-p (concat ispell-library-directory
-				      "/" dict-bname ".hash"))
-	       (file-exists-p (concat ispell-library-directory
-				      "/" dict-bname ".has"))))
+	    ;; Explicitly (via ispell-args) specified dictionary.
+	    dict-explt (car (cdr (member "-d" (nth 5 dict))))
+	    dict-bname (or dict-explt name))
+      (if (and name
+	       (or
+		;; Include all for Aspell (we already know existing dicts)
+		ispell-really-aspell
+		;; Include all if `ispell-library-directory' is nil (Hunspell)
+		(not ispell-library-directory)
+		;; If explicit (-d with an absolute path) and existing dict.
+		(and dict-explt
+		     (file-name-absolute-p dict-explt)
+		     (funcall dict-locate dict-explt))
+		;; If dict located in `ispell-library-directory'.
+		(funcall dict-locate dict-bname ispell-library-directory)))
 	  (push name dict-list)))
     dict-list))