* net/tramp-compat.el (tramp-compat-pop-to-buffer-same-window): New defun.
[emacs.git] / src / xrdb.c
blobe18ff65f799787e91b0ca69ac85cb2fe766ac07a
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2011 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>
28 #include <stdio.h>
29 #include <setjmp.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
45 #include <sys/stat.h>
47 #ifdef USE_MOTIF
48 /* For Vdouble_click_time. */
49 #include "keyboard.h"
50 #endif
52 extern char *getenv (const char *);
54 extern struct passwd *getpwuid (uid_t);
55 extern struct passwd *getpwnam (const char *);
57 char *x_get_string_resource (XrmDatabase rdb, const char *name,
58 const char *class);
59 static int file_p (const char *filename);
62 /* X file search path processing. */
65 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
66 and friends, or zero if none was specified. */
67 static char *x_customization_string;
70 /* Return the value of the emacs.customization (Emacs.Customization)
71 resource, for later use in search path decoding. If we find no
72 such resource, return zero. */
73 static char *
74 x_get_customization_string (XrmDatabase db, const char *name,
75 const char *class)
77 char *full_name
78 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
79 char *full_class
80 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
81 char *result;
83 sprintf (full_name, "%s.%s", name, "customization");
84 sprintf (full_class, "%s.%s", class, "Customization");
86 result = x_get_string_resource (db, full_name, full_class);
88 if (result)
90 char *copy = (char *) xmalloc (strlen (result) + 1);
91 strcpy (copy, result);
92 return copy;
94 else
95 return 0;
99 /* Expand all the Xt-style %-escapes in STRING, whose length is given
100 by STRING_LEN. Here are the escapes we're supposed to recognize:
102 %N The value of the application's class name
103 %T The value of the type parameter ("app-defaults" in this
104 context)
105 %S The value of the suffix parameter ("" in this context)
106 %L The language string associated with the specified display
107 (We use the "LANG" environment variable here, if it's set.)
108 %l The language part of the display's language string
109 (We treat this just like %L. If someone can tell us what
110 we're really supposed to do, dandy.)
111 %t The territory part of the display's language string
112 (This never gets used.)
113 %c The codeset part of the display's language string
114 (This never gets used either.)
115 %C The customization string retrieved from the resource
116 database associated with display.
117 (This is x_customization_string.)
119 Return the expanded file name if it exists and is readable, and
120 refers to %L only when the LANG environment variable is set, or
121 otherwise provided by X.
123 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
124 %-escapes in ESCAPED_SUFFIX are expanded.
126 Return NULL otherwise. */
128 static char *
129 magic_file_p (const char *string, EMACS_INT string_len, const char *class,
130 const char *escaped_suffix)
132 char *lang = getenv ("LANG");
134 ptrdiff_t path_size = 100;
135 char *path = (char *) xmalloc (path_size);
136 ptrdiff_t path_len = 0;
138 const char *p = string;
140 while (p < string + string_len)
142 /* The chunk we're about to stick on the end of result. */
143 const char *next = NULL;
144 ptrdiff_t next_len;
146 if (*p == '%')
148 p++;
150 if (p >= string + string_len)
151 next_len = 0;
152 else
153 switch (*p)
155 case '%':
156 next = "%";
157 next_len = 1;
158 break;
160 case 'C':
161 next = (x_customization_string
162 ? x_customization_string
163 : "");
164 next_len = strlen (next);
165 break;
167 case 'N':
168 next = class;
169 next_len = strlen (class);
170 break;
172 case 'T':
173 next = "app-defaults";
174 next_len = strlen (next);
175 break;
177 default:
178 case 'S':
179 next_len = 0;
180 break;
182 case 'L':
183 case 'l':
184 if (! lang)
186 xfree (path);
187 return NULL;
190 next = lang;
191 next_len = strlen (next);
192 break;
194 case 't':
195 case 'c':
196 xfree (path);
197 return NULL;
200 else
201 next = p, next_len = 1;
203 /* Do we have room for this component followed by a '\0' ? */
204 if (path_size - path_len <= next_len)
206 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
207 memory_full (SIZE_MAX);
208 path_size = (path_len + next_len + 1) * 2;
209 path = (char *) xrealloc (path, path_size);
212 memcpy (path + path_len, next, next_len);
213 path_len += next_len;
215 p++;
217 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
218 if (p >= string + string_len && escaped_suffix)
220 string = escaped_suffix;
221 string_len = strlen (string);
222 p = string;
223 escaped_suffix = NULL;
227 path[path_len] = '\0';
229 if (! file_p (path))
231 xfree (path);
232 return NULL;
235 return path;
239 static char *
240 gethomedir (void)
242 struct passwd *pw;
243 char *ptr;
244 char *copy;
246 if ((ptr = getenv ("HOME")) == NULL)
248 if ((ptr = getenv ("LOGNAME")) != NULL
249 || (ptr = getenv ("USER")) != NULL)
250 pw = getpwnam (ptr);
251 else
252 pw = getpwuid (getuid ());
254 if (pw)
255 ptr = pw->pw_dir;
258 if (ptr == NULL)
259 return xstrdup ("/");
261 copy = (char *) xmalloc (strlen (ptr) + 2);
262 strcpy (copy, ptr);
263 strcat (copy, "/");
265 return copy;
269 static int
270 file_p (const char *filename)
272 struct stat status;
274 return (access (filename, 4) == 0 /* exists and is readable */
275 && stat (filename, &status) == 0 /* get the status */
276 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
280 /* Find the first element of SEARCH_PATH which exists and is readable,
281 after expanding the %-escapes. Return 0 if we didn't find any, and
282 the path name of the one we found otherwise. */
284 static char *
285 search_magic_path (const char *search_path, const char *class,
286 const char *escaped_suffix)
288 const char *s, *p;
290 for (s = search_path; *s; s = p)
292 for (p = s; *p && *p != ':'; p++)
295 if (p > s)
297 char *path = magic_file_p (s, p - s, class, escaped_suffix);
298 if (path)
299 return path;
301 else if (*p == ':')
303 char *path;
305 s = "%N%S";
306 path = magic_file_p (s, strlen (s), class, escaped_suffix);
307 if (path)
308 return path;
311 if (*p == ':')
312 p++;
315 return 0;
318 /* Producing databases for individual sources. */
320 static XrmDatabase
321 get_system_app (const char *class)
323 XrmDatabase db = NULL;
324 const char *path;
325 char *p;
327 path = getenv ("XFILESEARCHPATH");
328 if (! path) path = PATH_X_DEFAULTS;
330 p = search_magic_path (path, class, 0);
331 if (p)
333 db = XrmGetFileDatabase (p);
334 xfree (p);
337 return db;
341 static XrmDatabase
342 get_fallback (Display *display)
344 return NULL;
348 static XrmDatabase
349 get_user_app (const char *class)
351 const char *path;
352 char *file = 0;
353 char *free_it = 0;
355 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
356 names, not directories. */
357 if (((path = getenv ("XUSERFILESEARCHPATH"))
358 && (file = search_magic_path (path, class, 0)))
360 /* Check for APPLRESDIR; it is a path of directories. In each,
361 we have to search for LANG/CLASS and then CLASS. */
362 || ((path = getenv ("XAPPLRESDIR"))
363 && ((file = search_magic_path (path, class, "/%L/%N"))
364 || (file = search_magic_path (path, class, "/%N"))))
366 /* Check in the home directory. This is a bit of a hack; let's
367 hope one's home directory doesn't contain any %-escapes. */
368 || (free_it = gethomedir (),
369 ((file = search_magic_path (free_it, class, "%L/%N"))
370 || (file = search_magic_path (free_it, class, "%N")))))
372 XrmDatabase db = XrmGetFileDatabase (file);
373 xfree (file);
374 xfree (free_it);
375 return db;
378 xfree (free_it);
379 return NULL;
383 static XrmDatabase
384 get_user_db (Display *display)
386 XrmDatabase db;
387 char *xdefs;
389 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
390 xdefs = XResourceManagerString (display);
391 #else
392 xdefs = display->xdefaults;
393 #endif
395 if (xdefs != NULL)
396 db = XrmGetStringDatabase (xdefs);
397 else
399 char *home;
400 char *xdefault;
402 home = gethomedir ();
403 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
404 strcpy (xdefault, home);
405 strcat (xdefault, ".Xdefaults");
406 db = XrmGetFileDatabase (xdefault);
407 xfree (home);
408 xfree (xdefault);
411 #ifdef HAVE_XSCREENRESOURCESTRING
412 /* Get the screen-specific resources too. */
413 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
414 if (xdefs != NULL)
416 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
417 XFree (xdefs);
419 #endif
421 return db;
424 static XrmDatabase
425 get_environ_db (void)
427 XrmDatabase db;
428 char *p;
429 char *path = 0, *home = 0;
430 const char *host;
432 if ((p = getenv ("XENVIRONMENT")) == NULL)
434 home = gethomedir ();
435 host = get_system_name ();
436 path = (char *) xmalloc (strlen (home)
437 + sizeof (".Xdefaults-")
438 + strlen (host));
439 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
440 p = path;
443 db = XrmGetFileDatabase (p);
445 xfree (path);
446 xfree (home);
448 return db;
451 /* External interface. */
453 /* Types of values that we can find in a database */
455 #define XrmStringType "String" /* String representation */
456 static XrmRepresentation x_rm_string; /* Quark representation */
458 /* Load X resources based on the display and a possible -xrm option. */
460 XrmDatabase
461 x_load_resources (Display *display, const char *xrm_string,
462 const char *myname, const char *myclass)
464 XrmDatabase user_database;
465 XrmDatabase rdb;
466 XrmDatabase db;
467 char line[256];
469 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
470 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
471 #endif
473 #ifdef USE_MOTIF
474 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
475 #endif
477 x_rm_string = XrmStringToQuark (XrmStringType);
478 #ifndef USE_X_TOOLKIT
479 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
480 I suspect it's because the toolkit version does this elsewhere. */
481 XrmInitialize ();
482 #endif
483 rdb = XrmGetStringDatabase ("");
485 /* Add some font defaults. If the font `helv' doesn't exist, widgets
486 will use some other default font. */
487 #ifdef USE_MOTIF
489 sprintf (line, "%s.pane.background: grey75", myclass);
490 XrmPutLineResource (&rdb, line);
491 sprintf (line, "%s*fontList: %s", myclass, helv);
492 XrmPutLineResource (&rdb, line);
493 sprintf (line, "%s*menu*background: grey75", myclass);
494 XrmPutLineResource (&rdb, line);
495 sprintf (line, "%s*menubar*background: grey75", myclass);
496 XrmPutLineResource (&rdb, line);
497 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
498 XrmPutLineResource (&rdb, line);
499 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
500 XrmPutLineResource (&rdb, line);
501 sprintf (line, "%s.dialog*.background: grey75", myclass);
502 XrmPutLineResource (&rdb, line);
503 sprintf (line, "%s*fsb.Text.background: white", myclass);
504 XrmPutLineResource (&rdb, line);
505 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
506 XrmPutLineResource (&rdb, line);
507 sprintf (line, "%s*fsb*DirList.background: white", myclass);
508 XrmPutLineResource (&rdb, line);
509 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
510 XrmPutLineResource (&rdb, line);
511 sprintf (line, "%s*fsb*background: grey75", myclass);
512 XrmPutLineResource (&rdb, line);
513 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
514 XrmPutLineResource (&rdb, line);
515 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
516 XrmPutLineResource (&rdb, line);
517 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
518 XrmPutLineResource (&rdb, line);
519 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
520 XrmPutLineResource (&rdb, line);
522 /* Set double click time of list boxes in the file selection
523 dialog from `double-click-time'. */
524 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
526 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
527 myclass, XFASTINT (Vdouble_click_time));
528 XrmPutLineResource (&rdb, line);
529 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
530 myclass, XFASTINT (Vdouble_click_time));
531 XrmPutLineResource (&rdb, line);
534 #else /* not USE_MOTIF */
536 sprintf (line, "Emacs.dialog*.background: grey75");
537 XrmPutLineResource (&rdb, line);
538 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
539 sprintf (line, "Emacs.dialog*.font: %s", helv);
540 XrmPutLineResource (&rdb, line);
541 sprintf (line, "*XlwMenu*font: %s", helv);
542 XrmPutLineResource (&rdb, line);
543 #endif
544 sprintf (line, "*XlwMenu*background: grey75");
545 XrmPutLineResource (&rdb, line);
546 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
547 XrmPutLineResource (&rdb, line);
549 #endif /* not USE_MOTIF */
551 user_database = get_user_db (display);
553 /* Figure out what the "customization string" is, so we can use it
554 to decode paths. */
555 xfree (x_customization_string);
556 x_customization_string
557 = x_get_customization_string (user_database, myname, myclass);
559 /* Get application system defaults */
560 db = get_system_app (myclass);
561 if (db != NULL)
562 XrmMergeDatabases (db, &rdb);
564 /* Get Fallback resources */
565 db = get_fallback (display);
566 if (db != NULL)
567 XrmMergeDatabases (db, &rdb);
569 /* Get application user defaults */
570 db = get_user_app (myclass);
571 if (db != NULL)
572 XrmMergeDatabases (db, &rdb);
574 /* get User defaults */
575 if (user_database != NULL)
576 XrmMergeDatabases (user_database, &rdb);
578 /* Get Environment defaults. */
579 db = get_environ_db ();
580 if (db != NULL)
581 XrmMergeDatabases (db, &rdb);
583 /* Last, merge in any specification from the command line. */
584 if (xrm_string != NULL)
586 db = XrmGetStringDatabase (xrm_string);
587 if (db != NULL)
588 XrmMergeDatabases (db, &rdb);
591 return rdb;
595 /* Retrieve the value of the resource specified by NAME with class CLASS
596 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
598 static int
599 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
600 XrmRepresentation expected_type, XrmValue *ret_value)
602 XrmValue value;
603 XrmName namelist[100];
604 XrmClass classlist[100];
605 XrmRepresentation type;
607 XrmStringToNameList(name, namelist);
608 XrmStringToClassList(class, classlist);
610 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
611 && (type == expected_type))
613 if (type == x_rm_string)
614 ret_value->addr = (char *) value.addr;
615 else
616 memcpy (ret_value->addr, value.addr, ret_value->size);
618 return value.size;
621 return 0;
624 /* Retrieve the string resource specified by NAME with CLASS from
625 database RDB. */
627 char *
628 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
630 XrmValue value;
632 if (inhibit_x_resources)
633 /* --quick was passed, so this is a no-op. */
634 return NULL;
636 if (x_get_resource (rdb, name, class, x_rm_string, &value))
637 return (char *) value.addr;
639 return (char *) 0;
642 /* Stand-alone test facilities. */
644 #ifdef TESTRM
646 typedef char **List;
647 #define arg_listify(len, list) (list)
648 #define car(list) (*(list))
649 #define cdr(list) (list + 1)
650 #define NIL(list) (! *(list))
651 #define free_arglist(list)
653 static List
654 member (elt, list)
655 char *elt;
656 List list;
658 List p;
660 for (p = list; ! NIL (p); p = cdr (p))
661 if (! strcmp (elt, car (p)))
662 return p;
664 return p;
667 static void
668 fatal (msg, prog, x1, x2, x3, x4, x5)
669 char *msg, *prog;
670 int x1, x2, x3, x4, x5;
672 if (errno)
673 perror (prog);
675 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
676 exit (1);
679 main (argc, argv)
680 int argc;
681 char **argv;
683 Display *display;
684 char *displayname, *resource_string, *class, *name;
685 XrmDatabase xdb;
686 List arg_list, lp;
688 arg_list = arg_listify (argc, argv);
690 lp = member ("-d", arg_list);
691 if (!NIL (lp))
692 displayname = car (cdr (lp));
693 else
694 displayname = "localhost:0.0";
696 lp = member ("-xrm", arg_list);
697 if (! NIL (lp))
698 resource_string = car (cdr (lp));
699 else
700 resource_string = (char *) 0;
702 lp = member ("-c", arg_list);
703 if (! NIL (lp))
704 class = car (cdr (lp));
705 else
706 class = "Emacs";
708 lp = member ("-n", arg_list);
709 if (! NIL (lp))
710 name = car (cdr (lp));
711 else
712 name = "emacs";
714 free_arglist (arg_list);
716 if (!(display = XOpenDisplay (displayname)))
717 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
719 xdb = x_load_resources (display, resource_string, name, class);
721 /* In a real program, you'd want to also do this: */
722 display->db = xdb;
724 while (1)
726 char query_name[90];
727 char query_class[90];
729 printf ("Name: ");
730 gets (query_name);
732 if (strlen (query_name))
734 char *value;
736 printf ("Class: ");
737 gets (query_class);
739 value = x_get_string_resource (xdb, query_name, query_class);
741 if (value != NULL)
742 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
743 else
744 printf ("\tNo Value.\n\n");
746 else
747 break;
749 printf ("\tExit.\n\n");
751 XCloseDisplay (display);
753 #endif /* TESTRM */