update.
[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 "character.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 (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 (Lisp_Color_Instance *c)
172 {
173   mark_object (COLOR_INSTANCE_TTY_SYMBOL (c));
174 }
175
176 static void
177 tty_print_color_instance (Lisp_Color_Instance *c,
178                           Lisp_Object printcharfun,
179                           int escapeflag)
180 {
181 }
182
183 static void
184 tty_finalize_color_instance (Lisp_Color_Instance *c)
185 {
186   if (c->data)
187     xfree (c->data);
188 }
189
190 static int
191 tty_color_instance_equal (Lisp_Color_Instance *c1,
192                           Lisp_Color_Instance *c2,
193                           int depth)
194 {
195   return (EQ (COLOR_INSTANCE_TTY_SYMBOL (c1),
196               COLOR_INSTANCE_TTY_SYMBOL (c2)));
197 }
198
199 static unsigned long
200 tty_color_instance_hash (Lisp_Color_Instance *c, int depth)
201 {
202   return LISP_HASH (COLOR_INSTANCE_TTY_SYMBOL (c));
203 }
204
205 static int
206 tty_valid_color_name_p (struct device *d, Lisp_Object color)
207 {
208   return (!NILP (assoc_no_quit (Fintern (color, Qnil), Vtty_color_alist)));
209 #if 0
210           || STRINGP (Vtty_dynamic_color_fg)
211           || STRINGP (Vtty_dynamic_color_bg)
212 #endif
213 }
214
215 \f
216 static int
217 tty_initialize_font_instance (Lisp_Font_Instance *f, Lisp_Object name,
218                               Lisp_Object device, Error_behavior errb)
219 {
220   Bufbyte *str = XSTRING_DATA (name);
221   Lisp_Object charset = Qnil;
222
223   if (strncmp ((const char *) str, "normal", 6))
224     return 0;
225   str += 6;
226   if (*str)
227     {
228 #ifdef MULE
229       if (*str != '/')
230         return 0;
231       str++;
232       charset = Ffind_charset (intern ((const char *) str));
233       if (NILP (charset))
234         return 0;
235 #else
236       return 0;
237 #endif
238     }
239
240   /* Don't allocate the data until we're sure that we will succeed. */
241   f->data = xnew (struct tty_font_instance_data);
242   FONT_INSTANCE_TTY_CHARSET (f) = charset;
243 #ifdef MULE
244   if (CHARSETP (charset))
245     f->width = CHARSET_COLUMNS (XCHARSET (charset));
246   else
247 #endif
248     f->width = 1;
249
250   f->proportional_p = 0;
251   f->ascent = f->height = 1;
252   f->descent = 0;
253
254   return 1;
255 }
256
257 static void
258 tty_mark_font_instance (Lisp_Font_Instance *f)
259 {
260   mark_object (FONT_INSTANCE_TTY_CHARSET (f));
261 }
262
263 static void
264 tty_print_font_instance (Lisp_Font_Instance *f,
265                          Lisp_Object printcharfun,
266                          int escapeflag)
267 {
268 }
269
270 static void
271 tty_finalize_font_instance (Lisp_Font_Instance *f)
272 {
273   if (f->data)
274     xfree (f->data);
275 }
276
277 static Lisp_Object
278 tty_list_fonts (Lisp_Object pattern, Lisp_Object device)
279 {
280   return list1 (build_string ("normal"));
281 }
282
283 #ifdef MULE
284
285 static int
286 tty_font_spec_matches_charset (struct device *d, Lisp_Object charset,
287                                const Bufbyte *nonreloc, Lisp_Object reloc,
288                                Bytecount offset, Bytecount length)
289 {
290   const Bufbyte *the_nonreloc = nonreloc;
291
292   if (!the_nonreloc)
293     the_nonreloc = XSTRING_DATA (reloc);
294   fixup_internal_substring (nonreloc, reloc, offset, &length);
295   the_nonreloc += offset;
296
297   if (UNBOUNDP (charset))
298     return !memchr (the_nonreloc, '/', length);
299   the_nonreloc = (const Bufbyte *) memchr (the_nonreloc, '/', length);
300   if (!the_nonreloc)
301     return 0;
302   the_nonreloc++;
303   {
304     Lisp_String *s = symbol_name (XSYMBOL (XCHARSET_NAME (charset)));
305     return !strcmp ((const char *) the_nonreloc,
306                     (const char *) string_data (s));
307   }
308 }
309
310 /* find a font spec that matches font spec FONT and also matches
311    (the registry of) CHARSET. */
312 static Lisp_Object
313 tty_find_charset_font (Lisp_Object device, Lisp_Object font,
314                        Lisp_Object charset)
315 {
316   Bufbyte *fontname = XSTRING_DATA (font);
317
318   if (strchr ((const char *) fontname, '/'))
319     {
320       if (tty_font_spec_matches_charset (XDEVICE (device), charset, 0,
321                                          font, 0, -1))
322         return font;
323       return Qnil;
324     }
325
326   if (UNBOUNDP (charset))
327     return font;
328
329   return concat3 (font, build_string ("/"),
330                   Fsymbol_name (XCHARSET_NAME (charset)));
331 }
332
333 #endif /* MULE */
334
335 \f
336 /************************************************************************/
337 /*                            initialization                            */
338 /************************************************************************/
339
340 void
341 syms_of_objects_tty (void)
342 {
343   DEFSUBR (Fregister_tty_color);
344   DEFSUBR (Funregister_tty_color);
345   DEFSUBR (Ffind_tty_color);
346   DEFSUBR (Ftty_color_list);
347 #if 0
348   DEFSUBR (Fset_tty_dynamic_color_specs);
349   DEFSUBR (Ftty_dynamic_color_specs);
350 #endif
351 }
352
353 void
354 console_type_create_objects_tty (void)
355 {
356   /* object methods */
357   CONSOLE_HAS_METHOD (tty, initialize_color_instance);
358   CONSOLE_HAS_METHOD (tty, mark_color_instance);
359   CONSOLE_HAS_METHOD (tty, print_color_instance);
360   CONSOLE_HAS_METHOD (tty, finalize_color_instance);
361   CONSOLE_HAS_METHOD (tty, color_instance_equal);
362   CONSOLE_HAS_METHOD (tty, color_instance_hash);
363   CONSOLE_HAS_METHOD (tty, valid_color_name_p);
364
365   CONSOLE_HAS_METHOD (tty, initialize_font_instance);
366   CONSOLE_HAS_METHOD (tty, mark_font_instance);
367   CONSOLE_HAS_METHOD (tty, print_font_instance);
368   CONSOLE_HAS_METHOD (tty, finalize_font_instance);
369   CONSOLE_HAS_METHOD (tty, list_fonts);
370 #ifdef MULE
371   CONSOLE_HAS_METHOD (tty, font_spec_matches_charset);
372   CONSOLE_HAS_METHOD (tty, find_charset_font);
373 #endif
374 }
375
376 void
377 vars_of_objects_tty (void)
378 {
379   staticpro (&Vtty_color_alist);
380   Vtty_color_alist = Qnil;
381
382 #if 0
383   staticpro (&Vtty_dynamic_color_fg);
384   Vtty_dynamic_color_fg = Qnil;
385
386   staticpro (&Vtty_dynamic_color_bg);
387   Vtty_dynamic_color_bg = Qnil;
388 #endif
389 }