home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1910 < prev    next >
Encoding:
Text File  |  1990-12-28  |  14.9 KB  |  485 lines

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