* src/xdisp.c (select_frame_for_redisplay): Keep selected_window and
[emacs.git] / src / xrdb.c
blob59b0876ebf88751d58bf5e57d92956603aaaaa3c
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2012 Free Software Foundation, Inc.
4 Author: Joseph Arceneaux
5 Created: 4/90
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <epaths.h>
27 #include <stdlib.h>
28 #include <stdio.h>
30 #include "lisp.h"
32 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
34 #include "xterm.h"
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/X.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
41 #ifdef HAVE_PWD_H
42 #include <pwd.h>
43 #endif
45 #ifdef USE_MOTIF
46 /* For Vdouble_click_time. */
47 #include "keyboard.h"
48 #endif
50 char *x_get_string_resource (XrmDatabase rdb, const char *name,
51 const char *class);
54 /* X file search path processing. */
57 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
58 and friends, or zero if none was specified. */
59 static char *x_customization_string;
62 /* Return the value of the emacs.customization (Emacs.Customization)
63 resource, for later use in search path decoding. If we find no
64 such resource, return zero. */
65 static char *
66 x_get_customization_string (XrmDatabase db, const char *name,
67 const char *class)
69 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
70 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
71 char *result;
73 sprintf (full_name, "%s.%s", name, "customization");
74 sprintf (full_class, "%s.%s", class, "Customization");
76 result = x_get_string_resource (db, full_name, full_class);
78 if (result)
80 char *copy = xmalloc (strlen (result) + 1);
81 strcpy (copy, result);
82 return copy;
84 else
85 return 0;
89 /* Expand all the Xt-style %-escapes in STRING, whose length is given
90 by STRING_LEN. Here are the escapes we're supposed to recognize:
92 %N The value of the application's class name
93 %T The value of the type parameter ("app-defaults" in this
94 context)
95 %S The value of the suffix parameter ("" in this context)
96 %L The language string associated with the specified display
97 (We use the "LANG" environment variable here, if it's set.)
98 %l The language part of the display's language string
99 (We treat this just like %L. If someone can tell us what
100 we're really supposed to do, dandy.)
101 %t The territory part of the display's language string
102 (This never gets used.)
103 %c The codeset part of the display's language string
104 (This never gets used either.)
105 %C The customization string retrieved from the resource
106 database associated with display.
107 (This is x_customization_string.)
109 Return the resource database if its file was read successfully, and
110 refers to %L only when the LANG environment variable is set, or
111 otherwise provided by X.
113 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
114 %-escapes in ESCAPED_SUFFIX are expanded.
116 Return NULL otherwise. */
118 static XrmDatabase
119 magic_db (const char *string, ptrdiff_t string_len, const char *class,
120 const char *escaped_suffix)
122 XrmDatabase db;
123 char *lang = getenv ("LANG");
125 ptrdiff_t path_size = 100;
126 char *path = xmalloc (path_size);
127 ptrdiff_t path_len = 0;
129 const char *p = string;
131 while (p < string + string_len)
133 /* The chunk we're about to stick on the end of result. */
134 const char *next = NULL;
135 ptrdiff_t next_len;
137 if (*p == '%')
139 p++;
141 if (p >= string + string_len)
142 next_len = 0;
143 else
144 switch (*p)
146 case '%':
147 next = "%";
148 next_len = 1;
149 break;
151 case 'C':
152 next = (x_customization_string
153 ? x_customization_string
154 : "");
155 next_len = strlen (next);
156 break;
158 case 'N':
159 next = class;
160 next_len = strlen (class);
161 break;
163 case 'T':
164 next = "app-defaults";
165 next_len = strlen (next);
166 break;
168 default:
169 case 'S':
170 next_len = 0;
171 break;
173 case 'L':
174 case 'l':
175 if (! lang)
177 xfree (path);
178 return NULL;
181 next = lang;
182 next_len = strlen (next);
183 break;
185 case 't':
186 case 'c':
187 xfree (path);
188 return NULL;
191 else
192 next = p, next_len = 1;
194 /* Do we have room for this component followed by a '\0' ? */
195 if (path_size - path_len <= next_len)
197 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
198 memory_full (SIZE_MAX);
199 path_size = (path_len + next_len + 1) * 2;
200 path = xrealloc (path, path_size);
203 memcpy (path + path_len, next, next_len);
204 path_len += next_len;
206 p++;
208 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
209 if (p >= string + string_len && escaped_suffix)
211 string = escaped_suffix;
212 string_len = strlen (string);
213 p = string;
214 escaped_suffix = NULL;
218 path[path_len] = '\0';
219 db = XrmGetFileDatabase (path);
220 xfree (path);
221 return db;
225 static char *
226 gethomedir (void)
228 struct passwd *pw;
229 char *ptr;
230 char *copy;
232 if ((ptr = getenv ("HOME")) == NULL)
234 if ((ptr = getenv ("LOGNAME")) != NULL
235 || (ptr = getenv ("USER")) != NULL)
236 pw = getpwnam (ptr);
237 else
238 pw = getpwuid (getuid ());
240 if (pw)
241 ptr = pw->pw_dir;
244 if (ptr == NULL)
245 return xstrdup ("/");
247 copy = xmalloc (strlen (ptr) + 2);
248 strcpy (copy, ptr);
249 strcat (copy, "/");
251 return copy;
255 /* Find the first element of SEARCH_PATH which exists and is readable,
256 after expanding the %-escapes. Return 0 if we didn't find any, and
257 the path name of the one we found otherwise. */
259 static XrmDatabase
260 search_magic_path (const char *search_path, const char *class,
261 const char *escaped_suffix)
263 const char *s, *p;
265 for (s = search_path; *s; s = p)
267 for (p = s; *p && *p != ':'; p++)
270 if (p > s)
272 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
273 if (db)
274 return db;
276 else if (*p == ':')
278 static char const ns[] = "%N%S";
279 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
280 if (db)
281 return db;
284 if (*p == ':')
285 p++;
288 return 0;
291 /* Producing databases for individual sources. */
293 static XrmDatabase
294 get_system_app (const char *class)
296 const char *path;
298 path = getenv ("XFILESEARCHPATH");
299 if (! path) path = PATH_X_DEFAULTS;
301 return search_magic_path (path, class, 0);
305 static XrmDatabase
306 get_fallback (Display *display)
308 return NULL;
312 static XrmDatabase
313 get_user_app (const char *class)
315 XrmDatabase db = 0;
316 const char *path;
318 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
319 names, not directories. */
320 path = getenv ("XUSERFILESEARCHPATH");
321 if (path)
322 db = search_magic_path (path, class, 0);
324 if (! db)
326 /* Check for APPLRESDIR; it is a path of directories. In each,
327 we have to search for LANG/CLASS and then CLASS. */
328 path = getenv ("XAPPLRESDIR");
329 if (path)
331 db = search_magic_path (path, class, "/%L/%N");
332 if (!db)
333 db = search_magic_path (path, class, "/%N");
337 if (! db)
339 /* Check in the home directory. This is a bit of a hack; let's
340 hope one's home directory doesn't contain any %-escapes. */
341 char *home = gethomedir ();
342 db = search_magic_path (home, class, "%L/%N");
343 if (! db)
344 db = search_magic_path (home, class, "%N");
345 xfree (home);
348 return db;
352 static XrmDatabase
353 get_user_db (Display *display)
355 XrmDatabase db;
356 char *xdefs;
358 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
359 xdefs = XResourceManagerString (display);
360 #else
361 xdefs = display->xdefaults;
362 #endif
364 if (xdefs != NULL)
365 db = XrmGetStringDatabase (xdefs);
366 else
368 char *home;
369 char *xdefault;
371 home = gethomedir ();
372 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
373 strcpy (xdefault, home);
374 strcat (xdefault, ".Xdefaults");
375 db = XrmGetFileDatabase (xdefault);
376 xfree (home);
377 xfree (xdefault);
380 #ifdef HAVE_XSCREENRESOURCESTRING
381 /* Get the screen-specific resources too. */
382 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
383 if (xdefs != NULL)
385 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
386 XFree (xdefs);
388 #endif
390 return db;
393 static XrmDatabase
394 get_environ_db (void)
396 XrmDatabase db;
397 char *p;
398 char *path = 0;
400 if ((p = getenv ("XENVIRONMENT")) == NULL)
402 static char const xdefaults[] = ".Xdefaults-";
403 char *home = gethomedir ();
404 char const *host = SSDATA (Vsystem_name);
405 ptrdiff_t pathsize = (strlen (home) + sizeof xdefaults
406 + SBYTES (Vsystem_name));
407 path = xrealloc (home, pathsize);
408 strcat (strcat (path, xdefaults), host);
409 p = path;
412 db = XrmGetFileDatabase (p);
414 xfree (path);
416 return db;
419 /* External interface. */
421 /* Types of values that we can find in a database */
423 #define XrmStringType "String" /* String representation */
424 static XrmRepresentation x_rm_string; /* Quark representation */
426 /* Load X resources based on the display and a possible -xrm option. */
428 XrmDatabase
429 x_load_resources (Display *display, const char *xrm_string,
430 const char *myname, const char *myclass)
432 XrmDatabase user_database;
433 XrmDatabase rdb;
434 XrmDatabase db;
435 char line[256];
437 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
438 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
439 #endif
441 #ifdef USE_MOTIF
442 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
443 #endif
445 x_rm_string = XrmStringToQuark (XrmStringType);
446 #ifndef USE_X_TOOLKIT
447 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
448 I suspect it's because the toolkit version does this elsewhere. */
449 XrmInitialize ();
450 #endif
451 rdb = XrmGetStringDatabase ("");
453 /* Add some font defaults. If the font `helv' doesn't exist, widgets
454 will use some other default font. */
455 #ifdef USE_MOTIF
457 sprintf (line, "%s.pane.background: grey75", myclass);
458 XrmPutLineResource (&rdb, line);
459 sprintf (line, "%s*fontList: %s", myclass, helv);
460 XrmPutLineResource (&rdb, line);
461 sprintf (line, "%s*menu*background: grey75", myclass);
462 XrmPutLineResource (&rdb, line);
463 sprintf (line, "%s*menubar*background: grey75", myclass);
464 XrmPutLineResource (&rdb, line);
465 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
466 XrmPutLineResource (&rdb, line);
467 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
468 XrmPutLineResource (&rdb, line);
469 sprintf (line, "%s.dialog*.background: grey75", myclass);
470 XrmPutLineResource (&rdb, line);
471 sprintf (line, "%s*fsb.Text.background: white", myclass);
472 XrmPutLineResource (&rdb, line);
473 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
474 XrmPutLineResource (&rdb, line);
475 sprintf (line, "%s*fsb*DirList.background: white", myclass);
476 XrmPutLineResource (&rdb, line);
477 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
478 XrmPutLineResource (&rdb, line);
479 sprintf (line, "%s*fsb*background: grey75", myclass);
480 XrmPutLineResource (&rdb, line);
481 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
482 XrmPutLineResource (&rdb, line);
483 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
484 XrmPutLineResource (&rdb, line);
485 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
486 XrmPutLineResource (&rdb, line);
487 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
488 XrmPutLineResource (&rdb, line);
490 /* Set double click time of list boxes in the file selection
491 dialog from `double-click-time'. */
492 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
494 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
495 myclass, XFASTINT (Vdouble_click_time));
496 XrmPutLineResource (&rdb, line);
497 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
498 myclass, XFASTINT (Vdouble_click_time));
499 XrmPutLineResource (&rdb, line);
502 #else /* not USE_MOTIF */
504 sprintf (line, "Emacs.dialog*.background: grey75");
505 XrmPutLineResource (&rdb, line);
506 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
507 sprintf (line, "Emacs.dialog*.font: %s", helv);
508 XrmPutLineResource (&rdb, line);
509 sprintf (line, "*XlwMenu*font: %s", helv);
510 XrmPutLineResource (&rdb, line);
511 #endif
512 sprintf (line, "*XlwMenu*background: grey75");
513 XrmPutLineResource (&rdb, line);
514 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
515 XrmPutLineResource (&rdb, line);
517 #endif /* not USE_MOTIF */
519 user_database = get_user_db (display);
521 /* Figure out what the "customization string" is, so we can use it
522 to decode paths. */
523 xfree (x_customization_string);
524 x_customization_string
525 = x_get_customization_string (user_database, myname, myclass);
527 /* Get application system defaults */
528 db = get_system_app (myclass);
529 if (db != NULL)
530 XrmMergeDatabases (db, &rdb);
532 /* Get Fallback resources */
533 db = get_fallback (display);
534 if (db != NULL)
535 XrmMergeDatabases (db, &rdb);
537 /* Get application user defaults */
538 db = get_user_app (myclass);
539 if (db != NULL)
540 XrmMergeDatabases (db, &rdb);
542 /* get User defaults */
543 if (user_database != NULL)
544 XrmMergeDatabases (user_database, &rdb);
546 /* Get Environment defaults. */
547 db = get_environ_db ();
548 if (db != NULL)
549 XrmMergeDatabases (db, &rdb);
551 /* Last, merge in any specification from the command line. */
552 if (xrm_string != NULL)
554 db = XrmGetStringDatabase (xrm_string);
555 if (db != NULL)
556 XrmMergeDatabases (db, &rdb);
559 return rdb;
563 /* Retrieve the value of the resource specified by NAME with class CLASS
564 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
566 static int
567 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
568 XrmRepresentation expected_type, XrmValue *ret_value)
570 XrmValue value;
571 XrmName namelist[100];
572 XrmClass classlist[100];
573 XrmRepresentation type;
575 XrmStringToNameList (name, namelist);
576 XrmStringToClassList (class, classlist);
578 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
579 && (type == expected_type))
581 if (type == x_rm_string)
582 ret_value->addr = (char *) value.addr;
583 else
584 memcpy (ret_value->addr, value.addr, ret_value->size);
586 return value.size;
589 return 0;
592 /* Retrieve the string resource specified by NAME with CLASS from
593 database RDB. */
595 char *
596 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
598 XrmValue value;
600 if (inhibit_x_resources)
601 /* --quick was passed, so this is a no-op. */
602 return NULL;
604 if (x_get_resource (rdb, name, class, x_rm_string, &value))
605 return (char *) value.addr;
607 return (char *) 0;
610 /* Stand-alone test facilities. */
612 #ifdef TESTRM
614 typedef char **List;
615 #define arg_listify(len, list) (list)
616 #define car(list) (*(list))
617 #define cdr(list) (list + 1)
618 #define NIL(list) (! *(list))
619 #define free_arglist(list)
621 static List
622 member (char *elt, List list)
624 List p;
626 for (p = list; ! NIL (p); p = cdr (p))
627 if (! strcmp (elt, car (p)))
628 return p;
630 return p;
633 static void
634 fatal (char *msg, char *prog)
636 if (errno)
637 perror (prog);
639 (void) fprintf (stderr, msg, prog);
640 exit (1);
644 main (int argc, char **argv)
646 Display *display;
647 char *displayname, *resource_string, *class, *name;
648 XrmDatabase xdb;
649 List arg_list, lp;
651 arg_list = arg_listify (argc, argv);
653 lp = member ("-d", arg_list);
654 if (!NIL (lp))
655 displayname = car (cdr (lp));
656 else
657 displayname = "localhost:0.0";
659 lp = member ("-xrm", arg_list);
660 if (! NIL (lp))
661 resource_string = car (cdr (lp));
662 else
663 resource_string = (char *) 0;
665 lp = member ("-c", arg_list);
666 if (! NIL (lp))
667 class = car (cdr (lp));
668 else
669 class = "Emacs";
671 lp = member ("-n", arg_list);
672 if (! NIL (lp))
673 name = car (cdr (lp));
674 else
675 name = "emacs";
677 free_arglist (arg_list);
679 if (!(display = XOpenDisplay (displayname)))
680 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
682 xdb = x_load_resources (display, resource_string, name, class);
684 /* In a real program, you'd want to also do this: */
685 display->db = xdb;
687 while (1)
689 char query_name[90];
690 char query_class[90];
692 printf ("Name: ");
693 gets (query_name);
695 if (strlen (query_name))
697 char *value;
699 printf ("Class: ");
700 gets (query_class);
702 value = x_get_string_resource (xdb, query_name, query_class);
704 if (value != NULL)
705 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
706 else
707 printf ("\tNo Value.\n\n");
709 else
710 break;
712 printf ("\tExit.\n\n");
714 XCloseDisplay (display);
716 return 0;
718 #endif /* TESTRM */