Function: ispell-init-process

ispell-init-process is a byte-compiled function defined in ispell.el.gz.

Signature

(ispell-init-process)

Documentation

Check status of Ispell process and start if necessary.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/ispell.el.gz
(defun ispell-init-process ()
  "Check status of Ispell process and start if necessary."
  (let* (;; Basename of dictionary used by the spell-checker
	 (dict-bname (or (car (cdr (member "-d" (ispell-get-ispell-args))))
			 ispell-current-dictionary))
	 ;; The default directory for the process.
	 ;; Use "~/" as default-directory unless using Ispell with per-dir
	 ;; personal dictionaries
	 (default-directory
	   (if (or ispell-really-aspell
		   ispell-really-hunspell
		   ;; Protect against bad default-directory
		   (not (file-accessible-directory-p default-directory))
		   ;; Ispell and per-dir personal dicts available
		   (not (or (file-readable-p (concat default-directory
						     ".ispell_words"))
			    (file-readable-p (concat default-directory
						     ".ispell_"
						     (or dict-bname
							 "default")))))
		   ;; Ispell, in a minibuffer
		   (window-minibuffer-p))
	       (expand-file-name "~/")
	     (expand-file-name default-directory))))
    ;; Check if process needs restart
    (if (and ispell-process
	     (eq (ispell-process-status) 'run)
	     ;; Unless we are using an explicit personal dictionary, ensure
	     ;; we're in the same default directory!  Restart check for
	     ;; personal dictionary is done in
	     ;; `ispell-internal-change-dictionary', called from
	     ;; `ispell-buffer-local-dict'
	     (or (or ispell-local-pdict ispell-personal-dictionary)
		 (equal ispell-process-directory default-directory)))
	(setq ispell-filter nil ispell-filter-continue nil)
      ;; may need to restart to select new personal dictionary.
      (ispell-kill-ispell t)
      (let ((reporter
             (make-progress-reporter
              (format "Starting new Ispell process %s with %s dictionary..."
                      ispell-program-name
                      (or ispell-local-dictionary ispell-dictionary
                          "default")))))
        (sit-for 0)
        (setq ispell-library-directory (ispell-check-version)
              ;; Assign a non-nil value to ispell-process-directory
              ;; before calling ispell-start-process, since that
              ;; function needs it to set default-directory when
              ;; ispell-async-processp is nil.
	      ispell-process-directory default-directory
	      ispell-process (ispell-start-process)
	      ispell-filter nil
	      ispell-filter-continue nil)
        (progress-reporter-done reporter))

      (unless (equal ispell-process-directory (expand-file-name "~/"))
	;; At this point, `ispell-process-directory' will be "~/" unless using
	;; Ispell with directory-specific dicts.
	;; If not, kill ispell process when killing buffer.  It may be in a
	;; removable device that would otherwise become un-mountable.
	(with-current-buffer
	    (if (window-minibuffer-p)                  ;; In minibuffer
		;; In this case kill ispell only when parent buffer is killed
		;; to avoid over and over ispell kill.
		(window-buffer (minibuffer-selected-window))
	      (current-buffer))
          (add-hook 'kill-buffer-hook
		    (lambda () (ispell-kill-ispell t)) nil 'local)))

      (if ispell-async-processp
	  (set-process-filter ispell-process 'ispell-filter))
      (if (and enable-multibyte-characters
               ;; Evidently, some people use the synchronous mode even
               ;; when async subprocesses are supported, in which case
               ;; set-process-coding-system is bound, but
               ;; ispell-process is not a process object.
               ispell-async-processp)
	  (set-process-coding-system ispell-process (ispell-get-coding-system)
				     (ispell-get-coding-system)))
      ;; Get version ID line
      (ispell-accept-output 3)
      ;; get more output if filter empty?
      (if (null ispell-filter) (ispell-accept-output 3))
      (cond ((null ispell-filter)
	     (error "%s did not output version line" ispell-program-name))
	    ((and
	      (stringp (car ispell-filter))
	      (if (string-match "warning: " (car ispell-filter))
		  (progn
		    (ispell-accept-output 3) ; was warn msg.
		    (stringp (car ispell-filter)))
		(null (cdr ispell-filter)))
	      (string-match "^@(#) " (car ispell-filter)))
	     ;; got the version line as expected (we already know it's the right
	     ;; version, so don't bother checking again.)
	     nil)
	    (t
	     ;; Otherwise, it must be an error message.  Show the user.
	     ;; But first wait to see if some more output is going to arrive.
	     ;; Otherwise we get cool errors like "Can't open ".
	     (sleep-for 1)
             ;; Only call `ispell-accept-output' if the Ispell process
             ;; is alive, to avoid showing an unhelpful error message
             ;; about a missing process, instead of the error which
             ;; reports why the Ispell process died.
	     (when (if ispell-async-processp
                         (process-live-p ispell-process)
                       ispell-process)
               (ispell-accept-output 3))
	     (error "%s" (mapconcat #'identity ispell-filter "\n"))))
      (setq ispell-filter nil)		; Discard version ID line
      (let ((extended-char-mode (ispell-get-extended-character-mode)))
	(if extended-char-mode		; ~ extended character mode
	    (ispell-send-string (concat extended-char-mode "\n"))))
      (when ispell-async-processp
        (set-process-query-on-exit-flag ispell-process nil)))))