Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / src / objects-tty.c
1 /* TTY-specific Lisp objects.
2    Copyright (C) 1995 Board of Trustees, University of Illinois.
3    Copyright (C) 1995, 1996 Ben Wing.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 #include <config.h>
25 #include "lisp.h"
26
27 #include "console-tty.h"
28 #include "insdel.h"
29 #include "objects-tty.h"
30 #ifdef MULE
31 #include "device.h"
32 #include "mule-charset.h"
33 #endif
34
35 /* An alist mapping from color names to a cons of (FG-STRING, BG-STRING). */
36 Lisp_Object Vtty_color_alist;
37 #if 0 /* This stuff doesn't quite work yet */
38 Lisp_Object Vtty_dynamic_color_fg;
39 Lisp_Object Vtty_dynamic_color_bg;
40 #endif
41
42 DEFUN ("register-tty-color", Fregister_tty_color, 3, 3, 0, /*
43 Register COLOR as a recognized TTY color.
44 COLOR should be a string.
45 Strings FG-STRING and BG-STRING should specify the escape sequences to
46  set the foreground and background to the given color, respectively.
47 */
48        (color, fg_string, bg_string))
49 {
50   CHECK_STRING (color);
51   CHECK_STRING (fg_string);
52   CHECK_STRING (bg_string);
53
54   color = Fintern (color, Qnil);
55   Vtty_color_alist = Fremassq (color, Vtty_color_alist);
56   Vtty_color_alist = Fcons (Fcons (color, Fcons (fg_string, bg_string)),
57                             Vtty_color_alist);
58
59   return Qnil;
60 }
61
62 DEFUN ("unregister-tty-color", Funregister_tty_color, 1, 1, 0, /*
63 Unregister COLOR as a recognized TTY color.
64 */
65        (color))
66 {
67   CHECK_STRING (color);
68
69   color = Fintern (color, Qnil);
70   Vtty_color_alist = Fremassq (color, Vtty_color_alist);
71   return Qnil;
72 }
73
74 DEFUN ("find-tty-color", Ffind_tty_color, 1, 1, 0, /*
75 Look up COLOR in the list of registered TTY colors.
76 If it is found, return a list (FG-STRING BG-STRING) of the escape
77 sequences used to set the foreground and background to the color, respectively.
78 If it is not found, return nil.
79 */
80        (color))
81 {
82   Lisp_Object result;
83
84   CHECK_STRING (color);
85
86   result = Fassq (Fintern (color, Qnil), Vtty_color_alist);
87   if (!NILP (result))
88     return list2 (Fcar (Fcdr (result)), Fcdr (Fcdr (result)));
89   else
90     return Qnil;
91 }
92
93 DEFUN ("tty-color-list", Ftty_color_list, 0, 0, 0, /*
94 Return a list of the registered TTY colors.
95 */
96        ())
97 {
98   Lisp_Object result = Qnil;
99   Lisp_Object rest;
100
101   LIST_LOOP (rest, Vtty_color_alist)
102     {
103       result = Fcons (Fsymbol_name (XCAR (XCAR (rest))), result);
104     }
105
106   return Fnreverse (result);
107 }
108
109 #if 0
110
111 /* This approach is too simplistic.  The problem is that the
112    dynamic color settings apply to *all* text in the default color,
113    not just the text output after the escape sequence has been given. */
114
115 DEFUN ("set-tty-dynamic-color-specs", Fset_tty_dynamic_color_specs, 2, 2, 0, /*
116 Set the dynamic color specifications for TTY's.
117 FG and BG should be either nil or vaguely printf-like strings,
118 where each occurrence of %s is replaced with the color name and each
119 occurrence of %% is replaced with a single % character.
120 */
121        (fg, bg))
122 {
123   if (!NILP (fg))
124     CHECK_STRING (fg);
125   if (!NILP (bg))
126     CHECK_STRING (bg);
127
128   Vtty_dynamic_color_fg = fg;
129   Vtty_dynamic_color_bg = bg;
130
131   return Qnil;
132 }
133
134 DEFUN ("tty-dynamic-color-specs", Ftty_dynamic_color_specs, 0, 0, 0, /*
135 Return the dynamic color specifications for TTY's as a list of (FG BG).
136 See `set-tty-dynamic-color-specs'.
137 */
138        ())
139 {
140   return list2 (Vtty_dynamic_color_fg, Vtty_dynamic_color_bg);
141 }
142
143 #endif /* 0 */
144
145 static int
146 tty_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
147                                Lisp_Object device, Error_behavior errb)
148 {
149   Lisp_Object result;
150
151   name = Fintern (name, Qnil);
152   result = assq_no_quit (name, Vtty_color_alist);
153
154   if (NILP (result))
155     {
156 #if 0
157       if (!STRINGP (Vtty_dynamic_color_fg)
158           && !STRINGP (Vtty_dynamic_color_bg))
159 #endif
160         return 0;
161     }
162
163   /* Don't allocate the data until we're sure that we will succeed. */
164   c->data = xnew (struct tty_color_instance_data);
165   COLOR_INSTANCE_TTY_SYMBOL (c) = name;
166
167   return 1;
168 }
169
170 static void
171 tty_mark_color_instance (struct Lisp_Color_Instance *c,
172                          void (*markobj) (Lisp_Object))
173 {
174   markobj (COLOR_INSTANCE_TTY_SYMBOL (c));
175 }
176
177 static void
178 tty_print_color_instance (struct Lisp_Color_Instance *c,
179                           Lisp_Object printcharfun,
180                           int escapeflag)
181 {
182 }
183
184 static void
185 tty_finalize_color_instance (struct Lisp_Color_Instance *c)
186 {
187   if (c->data)
188     xfree (c->data);
189 }
190
191 static int
192 tty_color_instance_equal (struct Lisp_Color_Instance *c1,
193                           struct Lisp_Color_Instance *c2,
194                           int depth)
195 {
196   return (EQ (COLOR_INSTANCE_TTY_SYMBOL (c1),
197               COLOR_INSTANCE_TTY_SYMBOL (c2)));
198 }
199
200 static unsigned long
201 tty_color_instance_hash (struct Lisp_Color_Instance *c, int depth)
202 {
203   return LISP_HASH (COLOR_INSTANCE_TTY_SYMBOL (c));
204 }
205
206 static int
207 tty_valid_color_name_p (struct device *d, Lisp_Object color)
208 {
209   return (!NILP (assoc_no_quit (Fintern (color, Qnil), Vtty_color_alist)));
210 #if 0
211           || STRINGP (Vtty_dynamic_color_fg)
212           || STRINGP (Vtty_dynamic_color_bg)
213 #endif
214 }
215
216 \f
217 static int
218 tty_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
219                               Lisp_Object device, Error_behavior errb)
220 {
221   Bufbyte *str = XSTRING_DATA (name);
222   Lisp_Object charset = Qnil;
223
224   if (strncmp ((CONST char *) str, "normal", 6))
225     return 0;
226   str += 6;
227   if (*str)
228     {
229 #ifdef MULE
230       if (*str != '/')
231         return 0;
232       str++;
233       charset = Ffind_charset (intern ((CONST char *) str));
234       if (NILP (charset))
235         return 0;
236 #else
237       return 0;
238 #endif
239     }
240
241   /* Don't allocate the data until we're sure that we will succeed. */
242   f->data = xnew (struct tty_font_instance_data);
243   FONT_INSTANCE_TTY_CHARSET (f) = charset;
244 #ifdef MULE
245   if (CHARSETP (charset))
246     f->width = XCHARSET_COLUMNS (charset);
247   else
248 #endif
249     f->width = 1;
250
251   f->proportional_p = 0;
252   f->ascent = f->height = 1;
253   f->descent = 0;
254
255   return 1;
256 }
257
258 static void
259 tty_mark_font_instance (struct Lisp_Font_Instance *f,
260                         void (*markobj) (Lisp_Object))
261 {
262   markobj (FONT_INSTANCE_TTY_CHARSET (f));
263 }
264
265 static void
266 tty_print_font_instance (struct Lisp_Font_Instance *f,
267                          Lisp_Object printcharfun,
268                          int escapeflag)
269 {
270 }
271
272 static void
273 tty_finalize_font_instance (struct Lisp_Font_Instance *f)
274 {
275   if (f->data)
276     xfree (f->data);
277 }
278
279 static Lisp_Object
280 tty_list_fonts (Lisp_Object pattern, Lisp_Object device)
281 {
282   return list1 (build_string ("normal"));
283 }
284
285 #ifdef MULE
286
287 static int
288 tty_font_spec_matches_charset (struct device *d, Lisp_Object charset,
289                                CONST Bufbyte *nonreloc, Lisp_Object reloc,
290                                Bytecount offset, Bytecount length)
291 {
292   CONST Bufbyte *the_nonreloc = nonreloc;
293
294   if (!the_nonreloc)
295     the_nonreloc = XSTRING_DATA (reloc);
296   fixup_internal_substring (nonreloc, reloc, offset, &length);
297   the_nonreloc += offset;
298
299   if (UNBOUNDP (charset))
300     return !memchr (the_nonreloc, '/', length);
301   the_nonreloc = (CONST Bufbyte *) memchr (the_nonreloc, '/', length);
302   if (!the_nonreloc)
303     return 0;
304   the_nonreloc++;
305   {
306     struct Lisp_String *s = symbol_name (XSYMBOL (XCHARSET_NAME (charset)));
307     return !strcmp ((CONST char *) the_nonreloc,
308                     (CONST char *) string_data (s));
309   }
310 }
311
312 /* find a font spec that matches font spec FONT and also matches
313    (the registry of) CHARSET. */
314 static Lisp_Object
315 tty_find_charset_font (Lisp_Object device, Lisp_Object font,
316                        Lisp_Object charset)
317 {
318   Bufbyte *fontname = XSTRING_DATA (font);
319
320   if (strchr ((CONST char *) fontname, '/'))
321     {
322       if (tty_font_spec_matches_charset (XDEVICE (device), charset, 0,
323                                          font, 0, -1))
324         return font;
325       return Qnil;
326     }
327
328   if (UNBOUNDP (charset))
329     return font;
330
331   return concat3 (font, build_string ("/"),
332                   Fsymbol_name (XCHARSET_NAME (charset)));
333 }
334
335 #endif /* MULE */
336
337 \f
338 /************************************************************************/
339 /*                            initialization                            */
340 /************************************************************************/
341
342 void
343 syms_of_objects_tty (void)
344 {
345   DEFSUBR (Fregister_tty_color);
346   DEFSUBR (Funregister_tty_color);
347   DEFSUBR (Ffind_tty_color);
348   DEFSUBR (Ftty_color_list);
349 #if 0
350   DEFSUBR (Fset_tty_dynamic_color_specs);
351   DEFSUBR (Ftty_dynamic_color_specs);
352 #endif
353 }
354
355 void
356 console_type_create_objects_tty (void)
357 {
358   /* object methods */
359   CONSOLE_HAS_METHOD (tty, initialize_color_instance);
360   CONSOLE_HAS_METHOD (tty, mark_color_instance);
361   CONSOLE_HAS_METHOD (tty, print_color_instance);
362   CONSOLE_HAS_METHOD (tty, finalize_color_instance);
363   CONSOLE_HAS_METHOD (tty, color_instance_equal);
364   CONSOLE_HAS_METHOD (tty, color_instance_hash);
365   CONSOLE_HAS_METHOD (tty, valid_color_name_p);
366
367   CONSOLE_HAS_METHOD (tty, initialize_font_instance);
368   CONSOLE_HAS_METHOD (tty, mark_font_instance);
369   CONSOLE_HAS_METHOD (tty, print_font_instance);
370   CONSOLE_HAS_METHOD (tty, finalize_font_instance);
371   CONSOLE_HAS_METHOD (tty, list_fonts);
372 #ifdef MULE
373   CONSOLE_HAS_METHOD (tty, font_spec_matches_charset);
374   CONSOLE_HAS_METHOD (tty, find_charset_font);
375 #endif
376 }
377
378 void
379 vars_of_objects_tty (void)
380 {
381   staticpro (&Vtty_color_alist);
382   Vtty_color_alist = Qnil;
383
384 #if 0
385   staticpro (&Vtty_dynamic_color_fg);
386   Vtty_dynamic_color_fg = Qnil;
387
388   staticpro (&Vtty_dynamic_color_bg);
389   Vtty_dynamic_color_bg = Qnil;
390 #endif
391 }