;;; speechd-el-extension.el --- 
;; 
;; Project: Extensions 1.0.1 
;;  
;;  
;; Copyright (C) 2009 -- 2018 Pierre L. Nageoire 
;;  
;; Author: Pierre L. Nageoire <devel@pollock-nageoire.net> 
;; X-RCS: $Id$ 
;;  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
;;  
;; This program is free software; you can redistribute it 
;; and/or modify it under the terms of the GNU General Public 
;; License as published by the Free Software Foundation; 
;; either version 2, or (at your option) any later version. 
;;  
;; This program is distributed in the hope that it will be 
;; useful, but WITHOUT ANY WARRANTY; without even the implied 
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
;; PURPOSE. 
;;  
;; See the GNU General Public License for more details. 
;;  
;; You should have received a copy of the GNU General Public 
;; License along with this program; see the file COPYING. 
;;  
;; If not, write to the Free Software Foundation, Inc., 51 
;; Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
;;  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
;;  
;;; Commentary: 
;;  
;;; Code: 
;;  ;

;;
;; Allows to choose automatically a connection according to the
;; context.
;;

(defvar sdx-forced-connection nil)
(make-variable-buffer-local 'sdx-forced-connection)

(defun sdx-force-connection (connection)
  "Forces to use `CONNECTION' for the current buffer. Allows user
to switch manually to a connection when automatic process is not
suitable."
  (interactive (list
		(completing-read "Connection: "
				 (mapcar 'car
					 speechd-connection-voices)
				 nil t)))
  (setq sdx-forced-connection connection))

(defun sdx-match-mode (mode)
  (let ((mode-list (if (listp mode) mode (list mode))))
    (memq major-mode
	  (mapcar (lambda (md)
		    (intern-soft (concat
				  (symbol-name md) "-mode")))
		  mode-list))))

(defvar sdx-coding-system
  '((fr . (utf-8
	   utf-8-unix
	   utf-8-dos
	   iso-latin-1
	   iso-latin-1-unix
	   iso-latin-1-dos
	   iso-latin-9-unix
	   text-unix
	   windows-1252-unix
	   windows-1252-dos)
	)
    )
  )

(defsubst sdx-language-coding-system (language)
  (cdr-safe (assq
	     language sdx-coding-system)))

(defun sdx-guess-coding-system ()
  (if (eq buffer-file-coding-system 'raw-text-unix)
      ;;
      ;; Maybe something better can be found !
      ;;
      (car-safe (find-coding-systems-region (point-min)
					    (point-max)))
    buffer-file-coding-system))

(defsubst sdx-match-language (language)
  (memq (sdx-guess-coding-system)
	(sdx-language-coding-system language)))

(defun sdx-connection-matcher (mode language connection)
  "Returns `t' if current major mode and detected language match
`MODE' and `LANGUAGE'. Check as well if `CONNECTION' was forced."
  (cond
   (sdx-forced-connection
    (and connection
	 (string-equal connection sdx-forced-connection)))
   (t (and (sdx-match-mode mode)
	   (or (sdx-match-language language)
	       (eq language 'en))))))

;;; -- Provide

(provide 'speechd-el-extension)

;;; -- End Provide