comparison lisp/mule/mule-cmds.el @ 3707:f6f6fc9eb269

[xemacs-hg @ 2006-11-28 21:20:22 by aidan] Better language behaviour on startup.
author aidan
date Tue, 28 Nov 2006 21:20:37 +0000
parents a2331a8fccb5
children 7a1c4c523603
comparison
equal deleted inserted replaced
3706:4ca1ef2bdb6a 3707:f6f6fc9eb269
240 (setq lang-env (symbol-name lang-env))) 240 (setq lang-env (symbol-name lang-env)))
241 ;; FSF has 30 lines of unbelievably ugly code to set up the menus 241 ;; FSF has 30 lines of unbelievably ugly code to set up the menus
242 ;; appropriately. We just use a filter. 242 ;; appropriately. We just use a filter.
243 (while alist 243 (while alist
244 (set-language-info lang-env (car (car alist)) (cdr (car alist))) 244 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
245 (setq alist (cdr alist)))) 245 (setq alist (cdr alist)))
246 lang-env)
246 247
247 (defun read-language-name (key prompt &optional default) 248 (defun read-language-name (key prompt &optional default)
248 "Read a language environment name which has information for KEY. 249 "Read a language environment name which has information for KEY.
249 If KEY is nil, read any language environment. 250 If KEY is nil, read any language environment.
250 Prompt with PROMPT. DEFAULT is the default choice of language environment. 251 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1010 ;; Init code. 1011 ;; Init code.
1011 1012
1012 ;; auto-language-alist deleted. We have a more sophisticated system, 1013 ;; auto-language-alist deleted. We have a more sophisticated system,
1013 ;; with the locales stored in the language data. 1014 ;; with the locales stored in the language data.
1014 1015
1015 (defconst langenv-to-locale-hash (make-hash-table :test 'equal)) 1016 ;; Initialised with an eval-when-compile in mule/general-late.el, which is
1017 ;; compiled after all the language support--and, thus, minority Chinese
1018 ;; coding systems and so on--has been loaded.
1019 (defvar posix-charset-to-coding-system-hash nil
1020 "A map from the POSIX locale charset versions of the defined coding
1021 systems' names, with all alpha-numeric characters removed, to the actual
1022 coding system names. Used at startup when working out which coding system
1023 should be the default for the locale. ")
1024
1025 (defun parse-posix-locale-string (locale-string)
1026 "Return values \(LANGUAGE REGION CHARSET MODIFIERS\) given LOCALE-STRING.
1027
1028 LOCALE-STRING should be a POSIX locale. If it cannot be parsed as such, this
1029 function returns nil. "
1030 (let (language region charset modifiers locinfo)
1031 (setq locale-string (downcase locale-string))
1032 (cond ((string-match
1033 #r"^\([a-z0-9]\{2,2\}\)\(_[a-z0-9]\{2,2\}\)?\(\.[^@]*\)?\(@.*\)?$"
1034 locale-string)
1035 (setq language (match-string 1 locale-string)
1036 region (match-string 2 locale-string)
1037 charset (match-string 3 locale-string)
1038 modifiers (match-string 4 locale-string)
1039 region (and region (replace-in-string region "^_" ""))
1040 charset (and charset (replace-in-string charset #r"^\." ""))
1041 modifiers (and modifiers
1042 (replace-in-string modifiers "^@" "")))
1043 (when (and modifiers (equal modifiers "euro") (null charset))
1044 ;; Not ideal for Latvian, say, but I don't have any locales
1045 ;; where the @euro modifier doesn't mean ISO-8859-15 in the 956
1046 ;; I have.
1047 (setq charset "iso-8859-15"))
1048 (values language region charset modifiers))
1049 ((and (string-match "^[a-z0-9]+$" locale-string)
1050 (assoc-ignore-case locale-string language-info-alist))
1051 (setq language (get-language-info locale-string 'locale)
1052 language (if (listp language) (car language) language))
1053 (values language region charset modifiers))
1054 ((string-match #r"^\([a-z0-9]+\)\.\([a-z0-9]+\)$" locale-string)
1055 (when (assoc-ignore-case
1056 (setq locinfo (match-string 1 locale-string))
1057 language-info-alist)
1058 (setq language (get-language-info locinfo 'locale)
1059 language (if (listp language) (car language) language)))
1060 (setq charset (match-string 2 locale-string))
1061 (values language region charset modifiers)))))
1062
1063 (defun create-variant-language-environment (langenv coding-system)
1064 "Create a variant of LANGENV with CODING-SYSTEM as its coding systems.
1065
1066 The coding systems in question are those described in the
1067 `set-language-info' docstring with the property names of
1068 `native-coding-system' and `coding-system'. The name of the new language
1069 environment is the name of the old language environment, followed by
1070 CODING-SYSTEM in parentheses. Returns the name of the new language
1071 environment. "
1072 (check-coding-system coding-system)
1073 (if (symbolp langenv) (setq langenv (symbol-name langenv)))
1074 (unless (setq langenv
1075 (assoc-ignore-case langenv language-info-alist))
1076 (error 'wrong-type-argument "Not a known language environment"))
1077 (set-language-info-alist
1078 (if (string-match " ([^)]+)$" (car langenv))
1079 (replace-match (format " (%s)"
1080 (upcase (symbol-name
1081 (coding-system-name coding-system))))
1082 nil nil langenv)
1083 (format "%s (%s)" (car langenv)
1084 (upcase (symbol-name (coding-system-name coding-system)))))
1085 (destructive-plist-to-alist
1086 (plist-put (plist-put (alist-to-plist (cdr langenv)) 'native-coding-system
1087 coding-system) 'coding-system
1088 (cons coding-system
1089 (cdr (assoc 'coding-system (cdr langenv))))))))
1016 1090
1017 (defun get-language-environment-from-locale (locale) 1091 (defun get-language-environment-from-locale (locale)
1018 "Convert LOCALE into a language environment. 1092 "Convert LOCALE into a language environment.
1019 LOCALE is a C library locale string, as returned by `current-locale'. 1093 LOCALE is a C library locale string, as returned by `current-locale'.
1020 Uses the `locale' property of the language environment." 1094 Uses the `locale' property of the language environment."
1021 (or (gethash locale langenv-to-locale-hash) 1095 (block langenv
1022 (let ((retval 1096 (multiple-value-bind (language region charset modifiers)
1023 (block langenv 1097 (parse-posix-locale-string locale)
1024 (dolist (langcons language-info-alist) 1098 (let ((case-fold-search t)
1025 (let* ((lang (car langcons)) 1099 (desired-coding-system
1026 (locs (get-language-info lang 'locale)) 1100 (and charset (gethash (replace-in-string charset "[^a-z0-9]" "")
1027 (case-fold-search t)) 1101 posix-charset-to-coding-system-hash)))
1028 (dolist (loc (if (listp locs) locs (list locs))) 1102 lang locs)
1029 (if (cond ((functionp loc) 1103 (dolist (langcons language-info-alist)
1030 (funcall loc locale)) 1104 (setq lang (car langcons)
1031 ((stringp loc) 1105 locs (get-language-info lang 'locale))
1032 (string-match 1106 (dolist (loc (if (listp locs) locs (list locs)))
1033 (concat "^" loc "\\([^A-Za-z0-9]\\|$\\)") 1107 (cond ((functionp loc)
1034 locale))) 1108 (if (funcall loc locale)
1035 (return-from langenv lang)))))))) 1109 (return-from langenv lang)))
1036 (puthash locale retval langenv-to-locale-hash) 1110 ((stringp loc)
1037 retval))) 1111 (when (or (equal loc language)
1112 (string-match
1113 (format "^%s\\([^A-Za-z0-9]\\|$\\)" loc)
1114 locale))
1115 (if (or (null desired-coding-system)
1116 (and desired-coding-system
1117 (eq desired-coding-system
1118 (get-language-info
1119 lang
1120 'native-coding-system))))
1121 (return-from langenv lang)
1122 (return-from langenv
1123 (create-variant-language-environment
1124 lang desired-coding-system))))))))))))
1038 1125
1039 (defun mswindows-get-language-environment-from-locale (ms-locale) 1126 (defun mswindows-get-language-environment-from-locale (ms-locale)
1040 "Convert MS-LOCALE (an MS Windows locale) into a language environment. 1127 "Convert MS-LOCALE (an MS Windows locale) into a language environment.
1041 MS-LOCALE is in the format recognized by `set-mswindows-current-locale' -- 1128 MS-LOCALE is in the format recognized by `set-mswindows-current-locale' --
1042 i.e. a language string or a cons (LANG . SUBLANG). Note: This is NOT the 1129 i.e. a language string or a cons (LANG . SUBLANG). Note: This is NOT the
1248 )) 1335 ))
1249 1336
1250 ;; set the default buffer coding system from the first element of the 1337 ;; set the default buffer coding system from the first element of the
1251 ;; list in the `coding-priority' property, under Unix. Under Windows, it 1338 ;; list in the `coding-priority' property, under Unix. Under Windows, it
1252 ;; should stay at `mswindows-multibyte', which will reference the current 1339 ;; should stay at `mswindows-multibyte', which will reference the current
1253 ;; code page. (#### Does it really make sense the set the Unix default 1340 ;; code page. ([Does it really make sense to set the Unix default
1254 ;; that way? NOTE also that it's not the same as the native coding 1341 ;; that way? NOTE also that it's not the same as the native coding
1255 ;; system for the locale, which is correct -- the form we choose for text 1342 ;; system for the locale, which is correct -- the form we choose for text
1256 ;; files should not necessarily have any relevant to whether we're in a 1343 ;; files should not necessarily have any relevance to whether we're in a
1257 ;; Shift-JIS, EUC-JP, JIS, or other Japanese locale.) 1344 ;; Shift-JIS, EUC-JP, JIS, or other Japanese locale.])
1345 ;;
1346 ;; On Unix--with the exception of Mac OS X--there is no way to
1347 ;; know for certain what coding system to use for file names, and
1348 ;; the environment is the best guess. If a particular user's
1349 ;; preferences differ from this, then that particular user needs
1350 ;; to edit ~/.xemacs/init.el. Aidan Kehoe, Sun Nov 26 18:11:31 CET
1351 ;; 2006. OS X uses an almost-normal-form version of UTF-8.
1352
1258 (unless (memq system-type '(windows-nt cygwin32)) 1353 (unless (memq system-type '(windows-nt cygwin32))
1259 (set-default-buffer-file-coding-system 1354 (set-default-buffer-file-coding-system
1260 (maybe-change-coding-system-with-eol default-coding eol-type)))) 1355 (maybe-change-coding-system-with-eol default-coding eol-type))))
1261 ;; (setq default-sendmail-coding-system default-coding) 1356 ;; (setq default-sendmail-coding-system default-coding)
1262 1357