comparison lib-src/emacstool.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 /*
2 Copyright (C) 1986, 1988, 1990, 1991 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 /*
20 * For Emacs in SunView/Sun-Windows: (supported by Sun Unix v3.2 or greater)
21 * Insert a notifier filter-function to convert all useful input
22 * to "key" sequences that emacs can understand. See: Emacstool(1).
23 *
24 * Author: Jeff Peck, Sun Microsystems, Inc. <peck@eng.sun.com>
25 *
26 * Original Idea: Ian Batten
27 * Updated 15-Mar-88, Jeff Peck: set IN_EMACSTOOL, TERM, TERMCAP
28 * Updated 10-Sep-88, Jeff Peck: add XVIEW and JLE support
29 * Updated 8-Oct-90, Jeff Peck: add Meta-bit for Xview
30 * Updated 6-Mar-91, Jeff Peck: Hack to detect -Wt invocation
31 * [note, TTYSW limitation means you must Click-To-Type in Openwin]
32 * [fixed in OW3 or use local/tty.o]
33 * for better results, this should move to using TERMSW.
34 * Updated 10-Mar-91, Jeff Peck, et al: support for TERMSW (TTERM)
35 * allows point-to-type even in OW2
36 *
37 * [note: xvetool should be started with the "-nw" flag for emacs!]
38 */
39
40 #ifdef XVIEW
41 #include <xview/xview.h>
42 #include <xview/panel.h>
43 #include <xview/attr.h>
44 #include <xview/tty.h>
45 #include <xview/ttysw.h> /* private defines */
46 #include <xview/termsw.h> /* -DTTERM */
47 #include <xview/font.h> /* for testing */
48 #else
49 #include <suntool/sunview.h>
50 #include <suntool/tty.h>
51 #include <suntool/ttysw.h>
52 #endif XVIEW
53
54 #ifdef JLE
55 # include <locale.h>
56 #endif JLE
57
58 #include <stdio.h>
59 #include <sys/file.h>
60
61 #define BUFFER_SIZE 128 /* Size of all the buffers */
62
63 /* define WANT_CAPS_LOCK to make f-key T1 (aka F1) behave as CapsLock */
64 #define WANT_CAPS_LOCK
65 #ifdef WANT_CAPS_LOCK
66 int caps_lock; /* toggle indicator for f-key T1 caps lock */
67 static char *Caps = "[CAPS] "; /* Caps Lock prefix string */
68 #define CAPS_LEN 7 /* strlen (Caps) */
69 #endif
70
71 static char *mouse_prefix = "\030\000"; /* C-x C-@ */
72 static int m_prefix_length = 2; /* mouse_prefix length */
73
74 static char *key_prefix = "\030*"; /* C-x * */
75 static int k_prefix_length = 2; /* key_prefix length */
76
77 #ifdef JLE
78 static char *emacs_name = "nemacs"; /* default run command */
79 static char *title = "NEmacstool - "; /* initial title */
80 #else
81 static char *emacs_name = "emacs"; /* default run command */
82 static char *title = "Emacstool - "; /* initial title */
83 #endif JLE
84
85 static char buffer[BUFFER_SIZE]; /* send to ttysw_input */
86 static char *bold_name = 0; /* for -bold option */
87
88 Frame frame; /* Base frame for system */
89
90 #ifndef TTERM
91 #define SWTYPE TTY
92 Tty tty_win; /* Where emacs is reading */
93 #else
94 #define SWTYPE TERMSW
95 Termsw tty_win; /* Termsw does follow-mouse */
96 #endif TTERM
97
98 #ifdef XVIEW
99 Xv_Window tty_view; /* Where the events are in Xview*/
100 #else
101 Tty tty_view; /* SunView place filler */
102 #endif XVIEW
103
104 int font_width, font_height; /* For translating pixels to chars */
105 int left_margin = 0; /* default window -- frame offset */
106
107 int console_fd = 0; /* for debugging: setenv DEBUGEMACSTOOL */
108 FILE *console; /* for debugging: setenv DEBUGEMACSTOOL */
109
110 Icon frame_icon;
111 /* make an icon_image for the default frame_icon */
112 static short default_image[258] =
113 {
114 #include <images/terminal.icon>
115 };
116 mpr_static(icon_image, 64, 64, 1, default_image);
117
118 /*
119 * Assign a value to a set of keys
120 */
121 int
122 button_value (event)
123 Event *event;
124 {
125 int retval = 0;
126 /*
127 * Code up the current situation:
128 *
129 * 1 = MS_LEFT;
130 * 2 = MS_MIDDLE;
131 * 4 = MS_RIGHT;
132 * 8 = SHIFT;
133 * 16 = CONTROL;
134 * 32 = META;
135 * 64 = DOUBLE;
136 * 128 = UP;
137 */
138
139 if (MS_LEFT == (event_id (event))) retval = 1;
140 if (MS_MIDDLE == (event_id (event))) retval = 2;
141 if (MS_RIGHT == (event_id (event))) retval = 4;
142
143 if (event_shift_is_down (event)) retval += 8;
144 if (event_ctrl_is_down (event)) retval += 16;
145 if (event_meta_is_down (event)) retval += 32;
146 if (event_is_up (event)) retval += 128;
147 return retval;
148 }
149
150 /*
151 * Variables to store the time of the previous mouse event that was
152 * sent to emacs.
153 *
154 * The theory is that to time double clicks while ignoreing UP buttons,
155 * we must keep track of the accumulated time.
156 *
157 * If someone writes a SUN-SET-INPUT-MASK for emacstool,
158 * That could be used to selectively disable UP events,
159 * and then this cruft wouldn't be necessary.
160 */
161 static long prev_event_sec = 0;
162 static long prev_event_usec = 0;
163
164 /*
165 * Give the time difference in milliseconds, where one second
166 * is considered infinite.
167 */
168 int
169 time_delta (now_sec, now_usec, prev_sec, prev_usec)
170 long now_sec, now_usec, prev_sec, prev_usec;
171 {
172 long sec_delta = now_sec - prev_sec;
173 long usec_delta = now_usec - prev_usec;
174
175 if (usec_delta < 0) { /* "borrow" a second */
176 usec_delta += 1000000;
177 --sec_delta;
178 }
179
180 if (sec_delta >= 10)
181 return (9999); /* Infinity */
182 else
183 return ((sec_delta * 1000) + (usec_delta / 1000));
184 }
185
186
187 /*
188 * Filter function to translate selected input events for emacs
189 * Mouse button events become ^X^@(button x-col y-line time-delta) .
190 * Function keys: ESC-*{c}{lrt} l,r,t for Left, Right, Top;
191 * {c} encodes the keynumber as a character [a-o]
192 */
193 static Notify_value
194 input_event_filter_function (window, event, arg, type)
195 #ifdef XVIEW
196 Xv_Window window;
197 #else
198 Window window;
199 #endif XVIEW
200 Event *event;
201 Notify_arg arg;
202 Notify_event_type type;
203 {
204 struct timeval time_stamp;
205
206 if (console_fd) fprintf(console, "Event: %d\n", event_id(event));
207
208 /* UP L1 is the STOP key */
209 if (event_id(event) == WIN_STOP) {
210 ttysw_input(tty_win, "\007\007\007\007\007\007\007", 7);
211 return NOTIFY_IGNORED;
212 }
213
214 /* UP L5 & L7 is Expose & Open, let them pass to sunview */
215 if (event_id(event) == KEY_LEFT(5) || event_id(event) == KEY_LEFT(7))
216 if(event_is_up (event))
217 return notify_next_event_func (window, event, arg, type);
218 else return NOTIFY_IGNORED;
219
220 if (event_is_button (event)) { /* do Mouse Button events */
221 /* Commented out so that we send mouse up events too.
222 if (event_is_up (event))
223 return notify_next_event_func (window, event, arg, type);
224 */
225 time_stamp = event_time (event);
226 ttysw_input (tty_win, mouse_prefix, m_prefix_length);
227 sprintf (buffer, "(%d %d %d %d)\015",
228 button_value (event),
229 (event_x (event) - left_margin) / font_width,
230 event_y (event) / font_height,
231 time_delta (time_stamp.tv_sec, time_stamp.tv_usec,
232 prev_event_sec, prev_event_usec)
233 );
234 ttysw_input (tty_win, buffer, strlen(buffer));
235 prev_event_sec = time_stamp.tv_sec;
236 prev_event_usec = time_stamp.tv_usec;
237 return NOTIFY_IGNORED;
238 }
239
240 { /* Do the function key events */
241 int d;
242 char c = (char) 0;
243 if ((event_is_key_left (event)) ?
244 ((d = event_id(event) - KEY_LEFT(1) + 'a'), c='l') :
245 ((event_is_key_right (event)) ?
246 ((d = event_id(event) - KEY_RIGHT(1) + 'a'), c='r') :
247 ((event_is_key_top (event)) ?
248 ((d = event_id(event) - KEY_TOP(1) + 'a'), c='t') : 0)))
249 {
250 if (event_is_up(event)) return NOTIFY_IGNORED;
251 if (event_shift_is_down (event)) c = c - 32;
252 /* this will give a non-{lrt} for unshifted keys */
253 if (event_ctrl_is_down (event)) c = c - 64;
254 if (event_meta_is_down (event)) c = c + 128;
255 #ifdef WANT_CAPS_LOCK
256 /* set a toggle and relabel window so T1 can act like caps-lock */
257 if (event_id(event) == KEY_TOP(1))
258 {
259 /* make a frame label with and without CAPS */
260 strcpy (buffer, Caps);
261 title = &buffer[CAPS_LEN];
262 strncpy (title, (char *)window_get (frame, FRAME_LABEL),
263 BUFFER_SIZE - CAPS_LEN);
264 buffer[BUFFER_SIZE] = (char) 0;
265 if (strncmp (title, Caps, CAPS_LEN) == 0)
266 title += CAPS_LEN; /* already Caps */
267 caps_lock = (caps_lock ? 0 : CAPS_LEN);
268 window_set(frame, FRAME_LABEL, (title -= caps_lock), 0);
269 return NOTIFY_IGNORED;
270 }
271 #endif
272 ttysw_input (tty_win, key_prefix, k_prefix_length);
273 sprintf (buffer, "%c%c", d, c);
274 ttysw_input(tty_win, buffer, strlen(buffer));
275
276 return NOTIFY_IGNORED;
277 }
278 }
279 if ((event_is_ascii(event) || event_is_meta(event))
280 && event_is_up(event)) return NOTIFY_IGNORED;
281 #ifdef WANT_CAPS_LOCK
282 /* shift alpha chars to upper case if toggle is set */
283 if ((caps_lock) && event_is_ascii(event)
284 && (event_id(event) >= 'a') && (event_id(event) <= 'z'))
285 event_set_id(event, (event_id(event) - 32));
286 /* crufty, but it works for now. is there an UPCASE(event)? */
287 #endif
288 #ifndef NO_META_BIT
289 /* under Openwindows/X, the meta bit is not set in the key event,
290 * emacs expects this so we add it in here:
291 */
292 if (event_is_ascii(event) && event_meta_is_down(event))
293 event_set_id(event, 128 | event_id(event));
294 #endif
295 return notify_next_event_func (window, event, arg, type);
296 }
297
298 main (argc, argv)
299 int argc;
300 char **argv;
301 {
302 int error_code; /* Error codes */
303
304 #ifdef JLE
305 setlocale(LC_ALL, "");
306 #endif JLE
307
308 if(getenv("DEBUGEMACSTOOL"))
309 console = fdopen (console_fd = open("/dev/console",O_WRONLY), "w");
310
311 putenv("IN_EMACSTOOL=t"); /* notify subprocess that it is in emacstool */
312
313 if (putenv("TERM=sun") != 0) /* TTY_WIN will be a TERM=sun window */
314 {fprintf (stderr, "%s: Could not set TERM=sun, using `%s'\n",
315 argv[0], (char *)getenv("TERM")) ;};
316 /*
317 * If TERMCAP starts with a slash, it is the pathname of the
318 * termcap file, not an entry extracted from it, so KEEP it!
319 * Otherwise, it may not relate to the new TERM, so Nuke-It.
320 * If there is no TERMCAP environment variable, don't make one.
321 */
322 {
323 char *termcap ; /* Current TERMCAP value */
324 termcap = (char *)getenv("TERMCAP") ;
325 if (termcap && (*termcap != '/'))
326 {
327 if (putenv("TERMCAP=") != 0)
328 {fprintf (stderr, "%s: Could not clear TERMCAP\n", argv[0]) ;} ;
329 } ;
330 } ;
331
332 /* find command to run as subprocess in window */
333 if (!(argv[0] = (char *)getenv("EMACSTOOL"))) /* Set emacs command name */
334 argv[0] = emacs_name;
335 /* Emacstool recognizes two special args: -rc <file> and -bold <bold-name> */
336 for (argc = 1; argv[argc]; argc++) /* Use last one on line */
337 {
338 if(!(strcmp ("-rc", argv[argc]))) /* Override if -rc given */
339 {int i = argc;
340 argv[argc--]=0; /* kill the -rc argument */
341 if (argv[i+1]) { /* move to argv[0] and squeeze the rest */
342 argv[0]=argv[i+1];
343 for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
344 }
345 }
346
347 if (!(strcmp ("-bold", argv[argc])))
348 {int i = argc;
349 argv[argc--]=0; /* kill the -bold argument */
350 if (argv[i+1]) { /* move to bold_name and squeeze the rest */
351 bold_name = argv[i+1];
352 for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
353 }
354 }
355 };
356
357 strcpy (buffer, title);
358 strncat (buffer, argv[0], /* append run command name */
359 (BUFFER_SIZE - (strlen (buffer)) - (strlen (argv[0]))) - 1);
360
361 error_code = interpose_on_window(argc,argv);
362 if (error_code != 0) { /* Barf */
363 fprintf (stderr, "notify_interpose_event_func returns %d.\n", error_code);
364 exit (1);
365 }
366
367 #ifdef XVIEW
368 xv_main_loop (frame); /* And away we go */
369 #else
370 window_main_loop (frame);
371 #endif XVIEW
372 }
373
374 #ifdef XVIEW
375 int interpose_on_window(argc,argv)
376 int argc;
377 char **argv;
378 {
379 #ifndef TTERM
380 int i, font_width_adjust = 1; /* hackery, and hueristics */
381 /* if -Wt is not supplied, then font comes out as lucida-14 (width=8)
382 * rather than the screen.r.12 (width=7) typically used
383 * this hack attempts to workaround it.
384 * could use a env var EMACSTOOL_DEFAULT_FONT_WIDTH instead */
385 for (i = 1; argv[i]; i++) {
386 if (!(strcmp ("-Wt", argv[i])))
387 {font_width_adjust = 0;
388 if (console_fd) fprintf(console, "-Wt = %d\n", font_width_adjust);
389 break;}
390 }
391 #endif TTERM
392 /* initialize Xview, and strip window args */
393 xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, 0);
394
395 /* do this first, so arglist can override it */
396 frame_icon = icon_create (ICON_LABEL, "Emacstool",
397 ICON_IMAGE, &icon_image,
398 0);
399
400 /* Build a frame to run in */
401 frame = xv_create ((Xv_Window)NULL, FRAME,
402 FRAME_LABEL, buffer,
403 FRAME_ICON, frame_icon,
404 0);
405
406 /* Create a tty with emacs in it */
407 tty_win = xv_create (frame, SWTYPE, WIN_IS_CLIENT_PANE,
408 TTY_QUIT_ON_CHILD_DEATH, TRUE,
409 TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
410 TTY_ARGV, argv,
411 0);
412
413 if (bold_name) {
414 (void)xv_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
415 }
416
417 {
418 Xv_font font; /* declare temp font variable */
419 font = (Xv_font)xv_get (tty_win, XV_FONT);
420 font_height = (int)xv_get (font, FONT_DEFAULT_CHAR_HEIGHT);
421 font_width = (int)xv_get (font, FONT_DEFAULT_CHAR_WIDTH);
422 }
423 if (console_fd) fprintf(console, "Width = %d\n", font_width);
424
425 #ifndef TTERM
426 font_width -= font_width_adjust; /* A guess! font bug in ttysw*/
427 #else
428 /* make the termsw act as a tty */
429 xv_set(tty_win, TERMSW_MODE, TTYSW_MODE_TYPE, 0);
430 /* termsw has variable offset depending on scrollbar size/location */
431 left_margin = (int)xv_get (tty_win, TEXTSW_LEFT_MARGIN);
432 #endif TTERM
433
434 tty_view = (Xv_Window) xv_get (tty_win, OPENWIN_NTH_VIEW, 0);
435 xv_set(tty_view,
436 WIN_CONSUME_EVENTS,
437 WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
438 ACTION_ADJUST, ACTION_MENU,
439 WIN_ASCII_EVENTS,
440 WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
441 0,
442 0);
443 /* Interpose my event function */
444 return (int) notify_interpose_event_func
445 (tty_view, input_event_filter_function, NOTIFY_SAFE);
446 }
447 #else
448 int interpose_on_window (argc, argv)
449 int argc;
450 char **argv;
451 {
452 /* do this first, so arglist can override it */
453 frame_icon = icon_create (ICON_LABEL, "Emacstool",
454 ICON_IMAGE, &icon_image,
455 0);
456
457 /* Build a frame to run in */
458 frame = window_create ((Window)NULL, FRAME,
459 FRAME_LABEL, buffer,
460 FRAME_ICON, frame_icon,
461 FRAME_ARGC_PTR_ARGV, &argc, argv,
462 0);
463
464 /* Create a tty with emacs in it */
465 tty_win = window_create (frame, TTY,
466 TTY_QUIT_ON_CHILD_DEATH, TRUE,
467 TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
468 TTY_ARGV, argv,
469 0);
470
471 if (bold_name) {
472 (void)window_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
473 }
474
475 /* ttysw uses pf_default, one must set WIN_FONT explicitly */
476 window_set (tty_win, WIN_FONT, pf_default(), 0);
477 font_height = (int)window_get (tty_win, WIN_ROW_HEIGHT);
478 font_width = (int)window_get (tty_win, WIN_COLUMN_WIDTH);
479
480 tty_view = tty_win;
481 window_set(tty_view,
482 WIN_CONSUME_PICK_EVENTS,
483 WIN_STOP,
484 WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
485 /* LOC_WINENTER, LOC_WINEXIT, LOC_MOVE, */
486 0,
487 WIN_CONSUME_KBD_EVENTS,
488 WIN_STOP,
489 WIN_ASCII_EVENTS,
490 WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
491 /* WIN_UP_ASCII_EVENTS, */
492 0,
493 0);
494 /* Interpose my event function */
495 return (int) notify_interpose_event_func
496 (tty_view, input_event_filter_function, NOTIFY_SAFE);
497 }
498 #endif XVIEW