+ return Qnil;
+}
+
+DEFUN ("w32-unload-winsock", Fw32_unload_winsock, Sw32_unload_winsock,
+ 0, 0, 0,
+ "Unload the Windows socket library `winsock' if loaded.\n\
+This is provided to allow dial-up socket connections to be disconnected\n\
+when no longer needed. Returns nil without unloading winsock if any\n\
+socket connections still exist.")
+ ()
+{
+ return term_winsock () ? Qt : Qnil;
+}
+
+#endif /* HAVE_SOCKETS */
+
+\f
+/* Some miscellaneous functions that are Windows specific, but not GUI
+ specific (ie. are applicable in terminal or batch mode as well). */
+
+/* lifted from fileio.c */
+#define CORRECT_DIR_SEPS(s) \
+ do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
+ else unixtodos_filename (s); \
+ } while (0)
+
+DEFUN ("w32-short-file-name", Fw32_short_file_name, Sw32_short_file_name, 1, 1, 0,
+ "Return the short file name version (8.3) of the full path of FILENAME.\n\
+If FILENAME does not exist, return nil.\n\
+All path elements in FILENAME are converted to their short names.")
+ (filename)
+ Lisp_Object filename;
+{
+ char shortname[MAX_PATH];
+
+ CHECK_STRING (filename, 0);
+
+ /* first expand it. */
+ filename = Fexpand_file_name (filename, Qnil);
+
+ /* luckily, this returns the short version of each element in the path. */
+ if (GetShortPathName (XSTRING (filename)->data, shortname, MAX_PATH) == 0)
+ return Qnil;
+
+ CORRECT_DIR_SEPS (shortname);
+
+ return build_string (shortname);
+}
+
+
+DEFUN ("w32-long-file-name", Fw32_long_file_name, Sw32_long_file_name,
+ 1, 1, 0,
+ "Return the long file name version of the full path of FILENAME.\n\
+If FILENAME does not exist, return nil.\n\
+All path elements in FILENAME are converted to their long names.")
+ (filename)
+ Lisp_Object filename;
+{
+ char longname[ MAX_PATH ];
+
+ CHECK_STRING (filename, 0);
+
+ /* first expand it. */
+ filename = Fexpand_file_name (filename, Qnil);
+
+ if (!w32_get_long_filename (XSTRING (filename)->data, longname, MAX_PATH))
+ return Qnil;
+
+ CORRECT_DIR_SEPS (longname);
+
+ return build_string (longname);
+}
+
+DEFUN ("w32-set-process-priority", Fw32_set_process_priority, Sw32_set_process_priority,
+ 2, 2, 0,
+ "Set the priority of PROCESS to PRIORITY.\n\
+If PROCESS is nil, the priority of Emacs is changed, otherwise the\n\
+priority of the process whose pid is PROCESS is changed.\n\
+PRIORITY should be one of the symbols high, normal, or low;\n\
+any other symbol will be interpreted as normal.\n\
+\n\
+If successful, the return value is t, otherwise nil.")
+ (process, priority)
+ Lisp_Object process, priority;
+{
+ HANDLE proc_handle = GetCurrentProcess ();
+ DWORD priority_class = NORMAL_PRIORITY_CLASS;
+ Lisp_Object result = Qnil;
+
+ CHECK_SYMBOL (priority, 0);
+
+ if (!NILP (process))
+ {
+ DWORD pid;
+ child_process *cp;
+
+ CHECK_NUMBER (process, 0);
+
+ /* Allow pid to be an internally generated one, or one obtained
+ externally. This is necessary because real pids on Win95 are
+ negative. */
+
+ pid = XINT (process);
+ cp = find_child_pid (pid);
+ if (cp != NULL)
+ pid = cp->procinfo.dwProcessId;
+
+ proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
+ }
+
+ if (EQ (priority, Qhigh))
+ priority_class = HIGH_PRIORITY_CLASS;
+ else if (EQ (priority, Qlow))
+ priority_class = IDLE_PRIORITY_CLASS;
+
+ if (proc_handle != NULL)
+ {
+ if (SetPriorityClass (proc_handle, priority_class))
+ result = Qt;
+ if (!NILP (process))
+ CloseHandle (proc_handle);
+ }
+
+ return result;
+}
+
+
+DEFUN ("w32-get-locale-info", Fw32_get_locale_info, Sw32_get_locale_info, 1, 2, 0,
+ "Return information about the Windows locale LCID.\n\
+By default, return a three letter locale code which encodes the default\n\
+language as the first two characters, and the country or regionial variant\n\
+as the third letter. For example, ENU refers to `English (United States)',\n\
+while ENC means `English (Canadian)'.\n\
+\n\
+If the optional argument LONGFORM is t, the long form of the locale\n\
+name is returned, e.g. `English (United States)' instead; if LONGFORM\n\
+is a number, it is interpreted as an LCTYPE constant and the corresponding\n\
+locale information is returned.\n\
+\n\
+If LCID (a 16-bit number) is not a valid locale, the result is nil.")
+ (lcid, longform)
+ Lisp_Object lcid, longform;
+{
+ int got_abbrev;
+ int got_full;
+ char abbrev_name[32] = { 0 };
+ char full_name[256] = { 0 };
+
+ CHECK_NUMBER (lcid, 0);
+
+ if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
+ return Qnil;
+
+ if (NILP (longform))
+ {
+ got_abbrev = GetLocaleInfo (XINT (lcid),
+ LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
+ abbrev_name, sizeof (abbrev_name));
+ if (got_abbrev)
+ return build_string (abbrev_name);
+ }
+ else if (EQ (longform, Qt))
+ {
+ got_full = GetLocaleInfo (XINT (lcid),
+ LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
+ full_name, sizeof (full_name));
+ if (got_full)
+ return build_string (full_name);
+ }
+ else if (NUMBERP (longform))
+ {
+ got_full = GetLocaleInfo (XINT (lcid),
+ XINT (longform),
+ full_name, sizeof (full_name));
+ if (got_full)
+ return make_unibyte_string (full_name, got_full);
+ }
+
+ return Qnil;
+}
+
+
+DEFUN ("w32-get-current-locale-id", Fw32_get_current_locale_id, Sw32_get_current_locale_id, 0, 0, 0,
+ "Return Windows locale id for current locale setting.\n\
+This is a numerical value; use `w32-get-locale-info' to convert to a\n\
+human-readable form.")
+ ()
+{
+ return make_number (GetThreadLocale ());
+}
+
+DWORD int_from_hex (char * s)
+{
+ DWORD val = 0;
+ static char hex[] = "0123456789abcdefABCDEF";
+ char * p;
+
+ while (*s && (p = strchr(hex, *s)) != NULL)
+ {
+ unsigned digit = p - hex;
+ if (digit > 15)
+ digit -= 6;
+ val = val * 16 + digit;
+ s++;
+ }
+ return val;
+}
+
+/* We need to build a global list, since the EnumSystemLocale callback
+ function isn't given a context pointer. */
+Lisp_Object Vw32_valid_locale_ids;
+
+BOOL CALLBACK enum_locale_fn (LPTSTR localeNum)
+{
+ DWORD id = int_from_hex (localeNum);
+ Vw32_valid_locale_ids = Fcons (make_number (id), Vw32_valid_locale_ids);
+ return TRUE;
+}
+
+DEFUN ("w32-get-valid-locale-ids", Fw32_get_valid_locale_ids, Sw32_get_valid_locale_ids, 0, 0, 0,
+ "Return list of all valid Windows locale ids.\n\
+Each id is a numerical value; use `w32-get-locale-info' to convert to a\n\
+human-readable form.")
+ ()
+{
+ Vw32_valid_locale_ids = Qnil;
+
+ EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
+
+ Vw32_valid_locale_ids = Fnreverse (Vw32_valid_locale_ids);
+ return Vw32_valid_locale_ids;
+}
+
+
+DEFUN ("w32-get-default-locale-id", Fw32_get_default_locale_id, Sw32_get_default_locale_id, 0, 1, 0,
+ "Return Windows locale id for default locale setting.\n\
+By default, the system default locale setting is returned; if the optional\n\
+parameter USERP is non-nil, the user default locale setting is returned.\n\
+This is a numerical value; use `w32-get-locale-info' to convert to a\n\
+human-readable form.")
+ (userp)
+ Lisp_Object userp;
+{
+ if (NILP (userp))
+ return make_number (GetSystemDefaultLCID ());
+ return make_number (GetUserDefaultLCID ());
+}
+
+
+DEFUN ("w32-set-current-locale", Fw32_set_current_locale, Sw32_set_current_locale, 1, 1, 0,
+ "Make Windows locale LCID be the current locale setting for Emacs.\n\
+If successful, the new locale id is returned, otherwise nil.")
+ (lcid)
+ Lisp_Object lcid;
+{
+ CHECK_NUMBER (lcid, 0);
+
+ if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
+ return Qnil;
+
+ if (!SetThreadLocale (XINT (lcid)))
+ return Qnil;
+
+ /* Need to set input thread locale if present. */
+ if (dwWindowsThreadId)
+ /* Reply is not needed. */
+ PostThreadMessage (dwWindowsThreadId, WM_EMACS_SETLOCALE, XINT (lcid), 0);
+
+ return make_number (GetThreadLocale ());
+}
+
+
+/* We need to build a global list, since the EnumCodePages callback
+ function isn't given a context pointer. */
+Lisp_Object Vw32_valid_codepages;
+
+BOOL CALLBACK enum_codepage_fn (LPTSTR codepageNum)
+{
+ DWORD id = atoi (codepageNum);
+ Vw32_valid_codepages = Fcons (make_number (id), Vw32_valid_codepages);
+ return TRUE;
+}
+
+DEFUN ("w32-get-valid-codepages", Fw32_get_valid_codepages, Sw32_get_valid_codepages, 0, 0, 0,
+ "Return list of all valid Windows codepages.")
+ ()
+{
+ Vw32_valid_codepages = Qnil;
+
+ EnumSystemCodePages (enum_codepage_fn, CP_SUPPORTED);
+
+ Vw32_valid_codepages = Fnreverse (Vw32_valid_codepages);
+ return Vw32_valid_codepages;
+}
+
+
+DEFUN ("w32-get-console-codepage", Fw32_get_console_codepage, Sw32_get_console_codepage, 0, 0, 0,
+ "Return current Windows codepage for console input.")
+ ()
+{
+ return make_number (GetConsoleCP ());
+}
+