* lisp/emacs-lisp/lisp.el (lisp-completion-at-point): Don't use
[emacs.git] / src / xrdb.c
blob32ad3c7f01e65caaca4376dcc358565b4d10d684
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2014 Free Software Foundation,
3 Inc.
5 Author: Joseph Arceneaux
6 Created: 4/90
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <epaths.h>
28 #include <stdlib.h>
29 #include <stdio.h>
31 #include "lisp.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.h"
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/X.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
42 #ifdef HAVE_PWD_H
43 #include <pwd.h>
44 #endif
46 #ifdef USE_MOTIF
47 /* For Vdouble_click_time. */
48 #include "keyboard.h"
49 #endif
51 /* X file search path processing. */
54 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
55 and friends, or zero if none was specified. */
56 static char *x_customization_string;
59 /* Return the value of the emacs.customization (Emacs.Customization)
60 resource, for later use in search path decoding. If we find no
61 such resource, return zero. */
62 static char *
63 x_get_customization_string (XrmDatabase db, const char *name,
64 const char *class)
66 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
67 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
68 char *result;
70 sprintf (full_name, "%s.%s", name, "customization");
71 sprintf (full_class, "%s.%s", class, "Customization");
73 result = x_get_string_resource (db, full_name, full_class);
74 return result ? xstrdup (result) : NULL;
77 /* Expand all the Xt-style %-escapes in STRING, whose length is given
78 by STRING_LEN. Here are the escapes we're supposed to recognize:
80 %N The value of the application's class name
81 %T The value of the type parameter ("app-defaults" in this
82 context)
83 %S The value of the suffix parameter ("" in this context)
84 %L The language string associated with the specified display
85 (We use the "LANG" environment variable here, if it's set.)
86 %l The language part of the display's language string
87 (We treat this just like %L. If someone can tell us what
88 we're really supposed to do, dandy.)
89 %t The territory part of the display's language string
90 (This never gets used.)
91 %c The codeset part of the display's language string
92 (This never gets used either.)
93 %C The customization string retrieved from the resource
94 database associated with display.
95 (This is x_customization_string.)
97 Return the resource database if its file was read successfully, and
98 refers to %L only when the LANG environment variable is set, or
99 otherwise provided by X.
101 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
102 %-escapes in ESCAPED_SUFFIX are expanded.
104 Return NULL otherwise. */
106 static XrmDatabase
107 magic_db (const char *string, ptrdiff_t string_len, const char *class,
108 const char *escaped_suffix)
110 XrmDatabase db;
111 char *lang = getenv ("LANG");
113 ptrdiff_t path_size = 100;
114 char *path = xmalloc (path_size);
115 ptrdiff_t path_len = 0;
117 const char *p = string;
119 while (p < string + string_len)
121 /* The chunk we're about to stick on the end of result. */
122 const char *next = NULL;
123 ptrdiff_t next_len;
125 if (*p == '%')
127 p++;
129 if (p >= string + string_len)
130 next_len = 0;
131 else
132 switch (*p)
134 case '%':
135 next = "%";
136 next_len = 1;
137 break;
139 case 'C':
140 next = (x_customization_string
141 ? x_customization_string
142 : "");
143 next_len = strlen (next);
144 break;
146 case 'N':
147 next = class;
148 next_len = strlen (class);
149 break;
151 case 'T':
152 next = "app-defaults";
153 next_len = strlen (next);
154 break;
156 default:
157 case 'S':
158 next_len = 0;
159 break;
161 case 'L':
162 case 'l':
163 if (! lang)
165 xfree (path);
166 return NULL;
169 next = lang;
170 next_len = strlen (next);
171 break;
173 case 't':
174 case 'c':
175 xfree (path);
176 return NULL;
179 else
180 next = p, next_len = 1;
182 /* Do we have room for this component followed by a '\0'? */
183 if (path_size - path_len <= next_len)
185 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
186 memory_full (SIZE_MAX);
187 path_size = (path_len + next_len + 1) * 2;
188 path = xrealloc (path, path_size);
191 memcpy (path + path_len, next, next_len);
192 path_len += next_len;
194 p++;
196 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
197 if (p >= string + string_len && escaped_suffix)
199 string = escaped_suffix;
200 string_len = strlen (string);
201 p = string;
202 escaped_suffix = NULL;
206 path[path_len] = '\0';
207 db = XrmGetFileDatabase (path);
208 xfree (path);
209 return db;
213 static char *
214 gethomedir (void)
216 struct passwd *pw;
217 char *ptr;
218 char *copy;
220 if ((ptr = getenv ("HOME")) == NULL)
222 if ((ptr = getenv ("LOGNAME")) != NULL
223 || (ptr = getenv ("USER")) != NULL)
224 pw = getpwnam (ptr);
225 else
226 pw = getpwuid (getuid ());
228 if (pw)
229 ptr = pw->pw_dir;
232 if (ptr == NULL)
233 return xstrdup ("/");
235 copy = xmalloc (strlen (ptr) + 2);
236 strcpy (copy, ptr);
237 return strcat (copy, "/");
241 /* Find the first element of SEARCH_PATH which exists and is readable,
242 after expanding the %-escapes. Return 0 if we didn't find any, and
243 the path name of the one we found otherwise. */
245 static XrmDatabase
246 search_magic_path (const char *search_path, const char *class,
247 const char *escaped_suffix)
249 const char *s, *p;
251 for (s = search_path; *s; s = p)
253 for (p = s; *p && *p != ':'; p++)
256 if (p > s)
258 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
259 if (db)
260 return db;
262 else if (*p == ':')
264 static char const ns[] = "%N%S";
265 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
266 if (db)
267 return db;
270 if (*p == ':')
271 p++;
274 return 0;
277 /* Producing databases for individual sources. */
279 static XrmDatabase
280 get_system_app (const char *class)
282 const char *path;
284 path = getenv ("XFILESEARCHPATH");
285 if (! path) path = PATH_X_DEFAULTS;
287 return search_magic_path (path, class, 0);
291 static XrmDatabase
292 get_fallback (Display *display)
294 return NULL;
298 static XrmDatabase
299 get_user_app (const char *class)
301 XrmDatabase db = 0;
302 const char *path;
304 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
305 names, not directories. */
306 path = getenv ("XUSERFILESEARCHPATH");
307 if (path)
308 db = search_magic_path (path, class, 0);
310 if (! db)
312 /* Check for APPLRESDIR; it is a path of directories. In each,
313 we have to search for LANG/CLASS and then CLASS. */
314 path = getenv ("XAPPLRESDIR");
315 if (path)
317 db = search_magic_path (path, class, "/%L/%N");
318 if (!db)
319 db = search_magic_path (path, class, "/%N");
323 if (! db)
325 /* Check in the home directory. This is a bit of a hack; let's
326 hope one's home directory doesn't contain any %-escapes. */
327 char *home = gethomedir ();
328 db = search_magic_path (home, class, "%L/%N");
329 if (! db)
330 db = search_magic_path (home, class, "%N");
331 xfree (home);
334 return db;
338 static XrmDatabase
339 get_user_db (Display *display)
341 XrmDatabase db;
342 char *xdefs;
344 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
345 xdefs = XResourceManagerString (display);
346 #else
347 xdefs = display->xdefaults;
348 #endif
350 if (xdefs != NULL)
351 db = XrmGetStringDatabase (xdefs);
352 else
354 char *home;
355 char *xdefault;
357 home = gethomedir ();
358 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
359 strcpy (xdefault, home);
360 strcat (xdefault, ".Xdefaults");
361 db = XrmGetFileDatabase (xdefault);
362 xfree (home);
363 xfree (xdefault);
366 #ifdef HAVE_XSCREENRESOURCESTRING
367 /* Get the screen-specific resources too. */
368 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
369 if (xdefs != NULL)
371 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
372 XFree (xdefs);
374 #endif
376 return db;
379 static XrmDatabase
380 get_environ_db (void)
382 XrmDatabase db;
383 char *p;
384 char *path = 0;
386 if ((p = getenv ("XENVIRONMENT")) == NULL)
388 static char const xdefaults[] = ".Xdefaults-";
389 char *home = gethomedir ();
390 char const *host = SSDATA (Vsystem_name);
391 ptrdiff_t pathsize = (strlen (home) + sizeof xdefaults
392 + SBYTES (Vsystem_name));
393 path = xrealloc (home, pathsize);
394 strcat (strcat (path, xdefaults), host);
395 p = path;
398 db = XrmGetFileDatabase (p);
400 xfree (path);
402 return db;
405 /* External interface. */
407 /* Types of values that we can find in a database */
409 #define XrmStringType "String" /* String representation */
410 static XrmRepresentation x_rm_string; /* Quark representation */
412 /* Load X resources based on the display and a possible -xrm option. */
414 XrmDatabase
415 x_load_resources (Display *display, const char *xrm_string,
416 const char *myname, const char *myclass)
418 XrmDatabase user_database;
419 XrmDatabase rdb;
420 XrmDatabase db;
421 char line[256];
423 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
424 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
425 #endif
427 #ifdef USE_MOTIF
428 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
429 #endif
431 x_rm_string = XrmStringToQuark (XrmStringType);
432 #ifndef USE_X_TOOLKIT
433 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
434 I suspect it's because the toolkit version does this elsewhere. */
435 XrmInitialize ();
436 #endif
437 rdb = XrmGetStringDatabase ("");
439 /* Add some font defaults. If the font `helv' doesn't exist, widgets
440 will use some other default font. */
441 #ifdef USE_MOTIF
443 sprintf (line, "%s.pane.background: grey75", myclass);
444 XrmPutLineResource (&rdb, line);
445 sprintf (line, "%s*fontList: %s", myclass, helv);
446 XrmPutLineResource (&rdb, line);
447 sprintf (line, "%s*menu*background: grey75", myclass);
448 XrmPutLineResource (&rdb, line);
449 sprintf (line, "%s*menubar*background: grey75", myclass);
450 XrmPutLineResource (&rdb, line);
451 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
452 XrmPutLineResource (&rdb, line);
453 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
454 XrmPutLineResource (&rdb, line);
455 sprintf (line, "%s*horizontalScrollBar.background: grey75", myclass);
456 XrmPutLineResource (&rdb, line);
457 sprintf (line, "%s*horizontalScrollBar.troughColor: grey75", myclass);
458 XrmPutLineResource (&rdb, line);
459 sprintf (line, "%s.dialog*.background: grey75", myclass);
460 XrmPutLineResource (&rdb, line);
461 sprintf (line, "%s*fsb.Text.background: white", myclass);
462 XrmPutLineResource (&rdb, line);
463 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
464 XrmPutLineResource (&rdb, line);
465 sprintf (line, "%s*fsb*DirList.background: white", myclass);
466 XrmPutLineResource (&rdb, line);
467 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
468 XrmPutLineResource (&rdb, line);
469 sprintf (line, "%s*fsb*background: grey75", myclass);
470 XrmPutLineResource (&rdb, line);
471 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
472 XrmPutLineResource (&rdb, line);
473 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
474 XrmPutLineResource (&rdb, line);
475 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
476 XrmPutLineResource (&rdb, line);
477 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
478 XrmPutLineResource (&rdb, line);
480 /* Set double click time of list boxes in the file selection
481 dialog from `double-click-time'. */
482 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
484 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
485 myclass, XFASTINT (Vdouble_click_time));
486 XrmPutLineResource (&rdb, line);
487 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
488 myclass, XFASTINT (Vdouble_click_time));
489 XrmPutLineResource (&rdb, line);
492 #else /* not USE_MOTIF */
494 sprintf (line, "Emacs.dialog*.background: grey75");
495 XrmPutLineResource (&rdb, line);
496 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
497 sprintf (line, "Emacs.dialog*.font: %s", helv);
498 XrmPutLineResource (&rdb, line);
499 sprintf (line, "*XlwMenu*font: %s", helv);
500 XrmPutLineResource (&rdb, line);
501 #endif
502 sprintf (line, "*XlwMenu*background: grey75");
503 XrmPutLineResource (&rdb, line);
504 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
505 XrmPutLineResource (&rdb, line);
506 sprintf (line, "Emacs*horizontalScrollBar.background: grey75");
507 XrmPutLineResource (&rdb, line);
509 #endif /* not USE_MOTIF */
511 user_database = get_user_db (display);
513 /* Figure out what the "customization string" is, so we can use it
514 to decode paths. */
515 xfree (x_customization_string);
516 x_customization_string
517 = x_get_customization_string (user_database, myname, myclass);
519 /* Get application system defaults */
520 db = get_system_app (myclass);
521 if (db != NULL)
522 XrmMergeDatabases (db, &rdb);
524 /* Get Fallback resources */
525 db = get_fallback (display);
526 if (db != NULL)
527 XrmMergeDatabases (db, &rdb);
529 /* Get application user defaults */
530 db = get_user_app (myclass);
531 if (db != NULL)
532 XrmMergeDatabases (db, &rdb);
534 /* get User defaults */
535 if (user_database != NULL)
536 XrmMergeDatabases (user_database, &rdb);
538 /* Get Environment defaults. */
539 db = get_environ_db ();
540 if (db != NULL)
541 XrmMergeDatabases (db, &rdb);
543 /* Last, merge in any specification from the command line. */
544 if (xrm_string != NULL)
546 db = XrmGetStringDatabase (xrm_string);
547 if (db != NULL)
548 XrmMergeDatabases (db, &rdb);
551 return rdb;
555 /* Retrieve the value of the resource specified by NAME with class CLASS
556 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
558 static int
559 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
560 XrmRepresentation expected_type, XrmValue *ret_value)
562 XrmValue value;
563 XrmName namelist[100];
564 XrmClass classlist[100];
565 XrmRepresentation type;
567 XrmStringToNameList (name, namelist);
568 XrmStringToClassList (class, classlist);
570 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
571 && (type == expected_type))
573 if (type == x_rm_string)
574 ret_value->addr = (char *) value.addr;
575 else
576 memcpy (ret_value->addr, value.addr, ret_value->size);
578 return value.size;
581 return 0;
584 /* Retrieve the string resource specified by NAME with CLASS from
585 database RDB. */
587 char *
588 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
590 XrmValue value;
592 if (inhibit_x_resources)
593 /* --quick was passed, so this is a no-op. */
594 return NULL;
596 if (x_get_resource (rdb, name, class, x_rm_string, &value))
597 return (char *) value.addr;
599 return 0;
602 /* Stand-alone test facilities. */
604 #ifdef TESTRM
606 typedef char **List;
607 #define arg_listify(len, list) (list)
608 #define car(list) (*(list))
609 #define cdr(list) (list + 1)
610 #define NIL(list) (! *(list))
611 #define free_arglist(list)
613 static List
614 member (char *elt, List list)
616 List p;
618 for (p = list; ! NIL (p); p = cdr (p))
619 if (! strcmp (elt, car (p)))
620 return p;
622 return p;
625 static void
626 fatal (char *msg, char *prog)
628 fprintf (stderr, msg, prog);
629 exit (1);
633 main (int argc, char **argv)
635 Display *display;
636 char *displayname, *resource_string, *class, *name;
637 XrmDatabase xdb;
638 List arg_list, lp;
640 arg_list = arg_listify (argc, argv);
642 lp = member ("-d", arg_list);
643 if (!NIL (lp))
644 displayname = car (cdr (lp));
645 else
646 displayname = "localhost:0.0";
648 lp = member ("-xrm", arg_list);
649 resource_string = NIL (lp) ? 0 : car (cdr (lp));
651 lp = member ("-c", arg_list);
652 if (! NIL (lp))
653 class = car (cdr (lp));
654 else
655 class = "Emacs";
657 lp = member ("-n", arg_list);
658 if (! NIL (lp))
659 name = car (cdr (lp));
660 else
661 name = "emacs";
663 free_arglist (arg_list);
665 if (!(display = XOpenDisplay (displayname)))
666 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
668 xdb = x_load_resources (display, resource_string, name, class);
670 /* In a real program, you'd want to also do this: */
671 display->db = xdb;
673 while (1)
675 char query_name[90];
676 char query_class[90];
678 printf ("Name: ");
679 gets (query_name);
681 if (strlen (query_name))
683 char *value;
685 printf ("Class: ");
686 gets (query_class);
688 value = x_get_string_resource (xdb, query_name, query_class);
690 if (value != NULL)
691 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
692 else
693 printf ("\tNo Value.\n\n");
695 else
696 break;
698 printf ("\tExit.\n\n");
700 XCloseDisplay (display);
702 return 0;
704 #endif /* TESTRM */