(in-package "KPML")

;;; File: language-range-defs.lisp

;;; The language-range.lisp file should contain a restriction on
;;; the languages that are to be accepted during loading of the
;;; resource set at issue. A predefined way of achieving this is
;;; provided by the following macro. This may be called in two
;;; forms:
;;;  (i)  (ensure-language-range () :lg1 :lg2 ... :lgN)
;;;  (ii) (ensure-language-range (:only T) :lg1 :lg2 ... :lgN)
;;;
;;; The first makes sure that at least the specified languages are
;;; on the all_languages list. The second makes sure that *only*
;;; the specified languages are on the all_languages list.
;;; Resources created with KPML automatically have language range
;;; assured for at least the language varieties for which the
;;; resources are requested to be saved.
;;;
;;; However...
;;;
;;; 2.0.60
;;; There was a problem introduced by the trick of giving resource sets
;;; arbitrary names and then loading them with multilingual loading. 
;;; When the spl-keyword types are set up, it was not clear what curlan
;;; should be set to, making it possible that setup-spl was called
;;; with curlan set to an inappropriate language variety that messes
;;; up the spl-keyword type allocation. 
;;;
;;; To fix this, the last set of languages defined to be relevant
;;; in an ensure-language-range declaration is now saved and this is
;;; used as the range of languages over which the spl-keyword types
;;; must be set up.
;;; 
;;; However, it is also the case that the SPL default environments 
;;; cannot be guaranteed *even to parse correctly* if curlan is not
;;; set to an appropriate value when they are read in. This is clearly
;;; problematic for their multilinguality. So we have just at this point
;;; to say that if a user defined default environments with a multilingual
;;; resource, then they must ensure that the definition is OK for all
;;; languages of that resource, which is, I think, fair enough. The
;;; actual behaviour is that when loading default environments for
;;; a multilingual resource, curlan will be set to the first of the
;;; concerned language varieties during loading of the default definitions.


;;; ----------------------------------------------------------------------
;;; 2.1
;;; ----------------------------------------------------------------------

(defvar *current-language-range* nil)       


(defmacro ENSURE-LANGUAGE-RANGE 
    ((&key (only nil)) &body languages)
  (if only
      `(progn
	 (setf kpml::all_languages (quote ,languages))
	 (setf kpml::*current-language-range* kpml::all_languages))  ;; 2.1
    `(progn
       (setf kpml::*current-language-range* nil)
       (loop for lg in (quote ,languages)
	   do (pushnew lg kpml::all_languages)
	      (pushnew lg kpml::*current-language-range*)            ;; 2.1
	      ))))
		       

 ;; ----------------------------------------------------------------------
 ;; 3.2.22
 ;; font information was still being written to properties.lisp which
 ;; is wrong (because it is then loaded too late).
 ;; ----------------------------------------------------------------------

(defun SAVE-LANGUAGE-RANGE-TO-DIRECTORY (directory ml inheriting-to)
  (let* ((concerned-languages (if *languages-in-focus* *languages-in-focus*
				all_languages))
	 (lg-specified (list (if inheriting-to inheriting-to curlan)))
	 (lg-range (if ml concerned-languages lg-specified)))
    (with-open-file (stream (merge-pathnames (pathname directory)
					     (pathname "language-range.lisp"))
		     :direction :output
		     :if-exists :new-version)
      (format stream "(in-package \"KPML\")")
      (format stream "~%(ensure-language-range ()~{ ~S~})~%" lg-range)
      ;; 3.2.22 moved here from properties file...
      ;; 3.2.57 ... but forgot that it cannot be passed a list! ...
      ;; 1.0.24 (was forgotten at 0.9.59?)
      (write-language-font-declarations stream (first lg-specified)))
    (format *trace-output* 
	    "~%Created evaluable language range file ~S." lg-range)))

