home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume10 / xsel / patch2 next >
Encoding:
Text File  |  1990-12-08  |  11.9 KB  |  397 lines

  1. Path: uunet!fernwood!apple!sun-barr!newstop!exodus!appserv!ukc.ac.uk
  2. From: rlh2@ukc.ac.uk (Richard Hesketh)
  3. Newsgroups: comp.sources.x
  4. Subject: v10i072: xsel, Patch2, Part01/01
  5. Message-ID: <310@appserv.Eng.Sun.COM>
  6. Date: 16 Nov 90 05:34:14 GMT
  7. References: <csx-10i072:xsel@uunet.UU.NET>
  8. Sender: news@exodus.Eng.Sun.COM
  9. Lines: 380
  10. Approved: argv@sun.com
  11.  
  12. Submitted-by: Richard Hesketh <rlh2@ukc.ac.uk>
  13. Posting-number: Volume 10, Issue 72
  14. Archive-name: xsel/patch2
  15. Patch-To: xsel: Volume 6, Issue 79
  16. Patch-To: xsel: Volume 7, Issue 35 (patch 1)
  17.  
  18. This is the second patch to "xselection" to allow it to set and retrieve
  19. cutbuffer values.  It also now allows prepending and appending of values to
  20. both selection properties and cutbuffers.
  21.  
  22. These additions were contributed by trost@reed.earn
  23.  
  24. Richard Hesketh   :   rlh2@ukc.ac.uk
  25. Computing Officer, Computing Lab., University of Kent at Canterbury,
  26. Canterbury, Kent, CT2 7NF, United Kingdom.
  27.         Tel: +44 227 764000 ext 7620/7590      Fax: +44 227 762811
  28.  
  29. *** /tmp/,RCSt1a24678    Fri Oct 12 11:27:39 1990
  30. --- patchlevel.h    Fri Oct 12 11:26:27 1990
  31. ***************
  32. *** 1 ****
  33. ! #define PATCHLEVEL 1
  34. --- 1 ----
  35. ! #define PATCHLEVEL 2
  36.  
  37. *** /tmp/,RCSt1a24681    Fri Oct 12 11:27:41 1990
  38. --- xselection.c    Fri Oct 12 11:26:27 1990
  39. ***************
  40. *** 1,6 ****
  41.   #ifndef lint
  42. ! static char rcsid[] = "$Header: xselection.c 1.2 90/07/31 09:06:57 rlh2 Rel $";
  43. ! #endif !lint
  44.   
  45.   /* 
  46.    * Copyright 1990 Richard Hesketh / rlh2@ukc.ac.uk
  47. --- 1,7 ----
  48.   #ifndef lint
  49. ! static char rcsid[] =
  50. !    "$Xukc: xselection.c,v 1.3 90/10/12 11:12:53 rlh2 Rel $";
  51. ! #endif /* !lint */
  52.   
  53.   /* 
  54.    * Copyright 1990 Richard Hesketh / rlh2@ukc.ac.uk
  55. ***************
  56. *** 41,46 ****
  57. --- 42,48 ----
  58.   #include <X11/Xresource.h>
  59.   
  60.   #define PROG_NAME "xselection"
  61. + #define PROG_CLASS "XSelection"
  62.   
  63.   #define CHUNK 1000
  64.   
  65. ***************
  66. *** 49,57 ****
  67. --- 51,66 ----
  68.   
  69.   static Atom selection_atom = NULL;
  70.   static unsigned char *current_selection = NULL;
  71. + static unsigned char *old_selection = NULL;
  72.   
  73. + static int selection_mode;
  74.   static XrmOptionDescRec options[] = {
  75.       { "-display", ".display", XrmoptionSepArg, (caddr_t)NULL },
  76. +     { "-append", ".mode", XrmoptionNoArg, (caddr_t)"2" },
  77. +     { "-prepend", ".mode", XrmoptionNoArg, (caddr_t)"1" },
  78. +     { "-replace", ".mode", XrmoptionNoArg, (caddr_t)"0" },
  79. +     { "-cutbuffer", ".cutbuffer", XrmoptionNoArg, (caddr_t)"true" },
  80.   };
  81.   
  82.   static unsigned char *get_selection();
  83. ***************
  84. *** 59,66 ****
  85.   static void
  86.   usage()
  87.   {
  88. !     fprintf(stderr, "usage: %s property [- | [--] new_value]\n",
  89. !                                 PROG_NAME);
  90.       exit (-1);
  91.   }
  92.   
  93. --- 68,79 ----
  94.   static void
  95.   usage()
  96.   {
  97. !     fprintf(stderr,
  98. !         "usage: %s [ -display display_name ] [ -append | -prepend",
  99. !         PROG_NAME);
  100. !     fprintf(stderr, " | -replace ]\n");
  101. !     fprintf(stderr, "\t\t  [ PROPERTY_NAME | -cutbuffer [0-7] ]");
  102. !     fprintf(stderr, " [ - | [--] new_value ]\n");
  103.       exit (-1);
  104.   }
  105.   
  106. ***************
  107. *** 136,151 ****
  108.       Display *dpy;
  109.       XEvent ev;
  110.       char resource[100];
  111.       char *blank, *display;
  112. !     XrmDatabase db = NULL;
  113.       XrmValue value;
  114.       Window window;
  115.       int want_to_own = 0;
  116.   
  117. !     XrmParseCommand(&db, options, 1, PROG_NAME, &argc, argv);
  118.   
  119.       (void)sprintf(resource, "%s.display", PROG_NAME);
  120. !     if (XrmGetResource(db, resource, "", &blank, &value))
  121.           display = (char *)value.addr;
  122.       else
  123.           display = NULL;
  124. --- 149,167 ----
  125.       Display *dpy;
  126.       XEvent ev;
  127.       char resource[100];
  128. +     char Resource[100];    /* for class information */
  129.       char *blank, *display;
  130. !     XrmDatabase odb = NULL, db = NULL;
  131.       XrmValue value;
  132.       Window window;
  133.       int want_to_own = 0;
  134.   
  135. !     XrmParseCommand(&odb, options, sizeof(options)/sizeof(options[0]),
  136. !             PROG_NAME, &argc, argv);
  137.   
  138.       (void)sprintf(resource, "%s.display", PROG_NAME);
  139. !     (void)sprintf(Resource, "%s.Display", PROG_CLASS);
  140. !     if (XrmGetResource(odb, resource, Resource, &blank, &value))
  141.           display = (char *)value.addr;
  142.       else
  143.           display = NULL;
  144. ***************
  145. *** 158,170 ****
  146.   
  147.       if (argc < 2 || argc > 4)
  148.           usage();
  149. -         
  150. -     selection_atom = XInternAtom(dpy, argv[1], argc == 2);
  151.   
  152. !     if (selection_atom == NULL) {
  153. !         fprintf(stderr, "%s: %s not a name of a known selection property\n",
  154. !                     PROG_NAME, argv[1]);
  155. !         exit (-2);
  156.       }
  157.   
  158.       if (argc == 3) {
  159. --- 174,207 ----
  160.   
  161.       if (argc < 2 || argc > 4)
  162.           usage();
  163.   
  164. !     XrmMergeDatabases(dpy->db, &db);
  165. !     XrmMergeDatabases(odb, &db);
  166. !     (void)sprintf(resource, "%s.mode", PROG_NAME);
  167. !     (void)sprintf(Resource, "%s.Mode", PROG_CLASS);
  168. !     if (XrmGetResource(db, resource, Resource, &blank, &value) ||
  169. !         XrmGetResource(dpy->db, resource, Resource, &blank, &value))
  170. !         switch (value.addr[0]) {
  171. !         case '0':
  172. !                 selection_mode = PropModeReplace;
  173. !                 break;
  174. !         case '1':
  175. !                 selection_mode = PropModePrepend;
  176. !                 break;
  177. !         case '2':
  178. !                 selection_mode = PropModeAppend;
  179. !                 break;
  180. !         }
  181. !     else
  182. !         selection_mode = PropModeReplace;
  183. !     if (selection_mode != PropModeReplace &&
  184. !         selection_mode != PropModeAppend &&
  185. !         selection_mode != PropModePrepend) {
  186. !             fprintf(stderr, "%s: Bad selection mode %d.\n",
  187. !                     PROG_NAME, selection_mode);
  188. !             exit(-2);
  189.       }
  190.   
  191.       if (argc == 3) {
  192. ***************
  193. *** 184,197 ****
  194.           want_to_own = 1;
  195.       }
  196.   
  197.       /* this window never gets mapped - its simply used as an ID */
  198.       window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
  199.                               0, 0, 1, 1, 0, 0, 0);
  200.   
  201. !     if (want_to_own)
  202.           /* set the selection and wait for someone to take it */
  203.           own_selection(dpy, window, selection_atom);
  204. !     else {
  205.           /* get the selection */
  206.           current_selection = get_selection(dpy, window, selection_atom);
  207.           if (current_selection != NULL)
  208. --- 221,279 ----
  209.           want_to_own = 1;
  210.       }
  211.   
  212. +     (void)sprintf(resource, "%s.cutbuffer", PROG_NAME);
  213. +     (void)sprintf(Resource, "%s.Cutbuffer", PROG_CLASS);
  214. +     if (XrmGetResource(db, resource, Resource, &blank, &value) ||
  215. +         XrmGetResource(dpy->db, resource, Resource, &blank, &value)) {
  216. +         int cutbuffer = atoi(argv[1]);
  217. +         if (cutbuffer < 0 || cutbuffer > 7) {
  218. +             fprintf(stderr,
  219. +                 "%s: %d is not a valid cut buffer, must be 0-7.\n",
  220. +                 PROG_NAME, cutbuffer);
  221. +             exit(-8);
  222. +         }
  223. +         if (want_to_own)
  224. +             XChangeProperty(dpy, DefaultRootWindow(dpy),
  225. +                     XA_CUT_BUFFER0 + cutbuffer, XA_STRING,
  226. +                     8, selection_mode, current_selection,
  227. +                     strlen(current_selection));
  228. +         else {
  229. +             Atom actual_type;
  230. +             int actual_format;
  231. +             unsigned long nitems, bytes_after;
  232. +             unsigned char* data;
  233. +             XGetWindowProperty(dpy, DefaultRootWindow(dpy),
  234. +                        XA_CUT_BUFFER0 + cutbuffer,
  235. +                        0L, 10240L, False, AnyPropertyType,
  236. +                        &actual_type, &actual_format,
  237. +                        &nitems, &bytes_after, &data);
  238. +             printf("%s", data);
  239. +         }
  240. +         XCloseDisplay(dpy);
  241. +         exit(0);
  242. +     }
  243. +     selection_atom = XInternAtom(dpy, argv[1], argc == 2);
  244. +     if (selection_atom == NULL) {
  245. +         fprintf(stderr,
  246. +             "%s: %s not a name of a known selection property.\n",
  247. +             PROG_NAME, argv[1]);
  248. +         exit (-2);
  249. +     }
  250.       /* this window never gets mapped - its simply used as an ID */
  251.       window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
  252.                               0, 0, 1, 1, 0, 0, 0);
  253.   
  254. !     if (want_to_own) {
  255.           /* set the selection and wait for someone to take it */
  256. +         if (selection_mode != PropModeReplace)
  257. +             old_selection = get_selection(dpy, window,
  258. +                               selection_atom);
  259.           own_selection(dpy, window, selection_atom);
  260. !     } else {
  261.           /* get the selection */
  262.           current_selection = get_selection(dpy, window, selection_atom);
  263.           if (current_selection != NULL)
  264. ***************
  265. *** 242,247 ****
  266. --- 324,340 ----
  267.           XSelectionRequestEvent  *req_event;
  268.           unsigned char *data;
  269.   
  270. +     if (selection_mode != PropModeReplace) {
  271. +         data = old_selection;
  272. +         XChangeProperty(ptr_event->xselectionrequest.display,
  273. +                 ptr_event->xselectionrequest.requestor,
  274. +                 ptr_event->xselectionrequest.property,
  275. +                 ptr_event->xselectionrequest.target,
  276. +                 8, PropModeReplace, data,
  277. +                 (old_selection == NULL) ? 0 :
  278. +                 strlen(old_selection));
  279. +     }
  280.           data = current_selection;
  281.   
  282.           XChangeProperty(ptr_event->xselectionrequest.display,
  283. ***************
  284. *** 248,254 ****
  285.                           ptr_event->xselectionrequest.requestor,
  286.                           ptr_event->xselectionrequest.property,
  287.                           ptr_event->xselectionrequest.target,
  288. !                         8, PropModeReplace, data,
  289.                           (current_selection == NULL) ?
  290.               0 : strlen(current_selection));
  291.   
  292. --- 341,347 ----
  293.                           ptr_event->xselectionrequest.requestor,
  294.                           ptr_event->xselectionrequest.property,
  295.                           ptr_event->xselectionrequest.target,
  296. !                         8, selection_mode, data,
  297.                           (current_selection == NULL) ?
  298.               0 : strlen(current_selection));
  299.   
  300. ***************
  301. *** 267,273 ****
  302.                   notify_event.property = req_event->property;
  303.   
  304.       /* tell the requestor he has a copy of the selection */
  305. !         (void) XSendEvent(req_event->display, req_event->requestor,
  306.                       False, 0, (XEvent *)¬ify_event);
  307.       XFlush(dpy);
  308.   }
  309. --- 360,366 ----
  310.                   notify_event.property = req_event->property;
  311.   
  312.       /* tell the requestor he has a copy of the selection */
  313. !         (void)XSendEvent(req_event->display, req_event->requestor,
  314.                       False, 0, (XEvent *)¬ify_event);
  315.       XFlush(dpy);
  316.   }
  317.  
  318. *** /tmp/,RCSt1a24684    Fri Oct 12 11:27:42 1990
  319. --- xselection.man    Fri Oct 12 11:26:28 1990
  320. ***************
  321. *** 1,8 ****
  322.   .TH XSELECTION l "31 March 1990"
  323.   .SH NAME
  324. ! xselection \- get or set an X selection property value
  325.   .SH SYNOPSIS
  326. ! \fBxselection\fP [ -display display_name ] PROPERTY_NAME [- | [--] new_value ]
  327.   .SH DESCRIPTION
  328.   If the \fInew_value\fP argument is not given \fIxselection\fP retrieves the
  329.   current value of the named selection property
  330. --- 1,12 ----
  331.   .TH XSELECTION l "31 March 1990"
  332.   .SH NAME
  333. ! xselection \- get or set an X selection or cutbuffer property value
  334.   .SH SYNOPSIS
  335. ! \fBxselection\fP
  336. ! [ -display display_name ]
  337. ! [ -append | -prepend | -replace ]
  338. ! [ PROPERTY_NAME | -cutbuffer [0-7] ]
  339. ! [- | [--] new_value ]
  340.   .SH DESCRIPTION
  341.   If the \fInew_value\fP argument is not given \fIxselection\fP retrieves the
  342.   current value of the named selection property
  343. ***************
  344. *** 16,21 ****
  345. --- 20,32 ----
  346.   flag definition off using the '--' argument, e.g.
  347.   \fIxselection PRIMARY -- -\fP
  348.   
  349. + By using the '-cutbuffer' flag, the program will modify the contents
  350. + of the specified cut buffer instead of a selection.
  351. + The '-append', '-prepend', and '-replace' flags are used to modify how
  352. + the program changes the selection.  By default, it simple overwrites
  353. + the selection's value (equivalent to '-replace').  By using the
  354. + '-append' flag, the new value is append to the old selection's
  355. + contents.  The '-prepend' flag behaves similarly.
  356.   This program can be used
  357.   from within shell scripts to set and retrieve the contents of the cut buffer.
  358.   The standard cut buffer selection property is accessed with the property
  359. ***************
  360. *** 24,31 ****
  361.   .SH ENVIRONMENT VARIABLES
  362.   DISPLAY \- default X Server to enquire.
  363.   .SH BUGS
  364. ! As yet unknown.
  365.   .SH AUTHOR
  366. ! Richard Hesketh, University of Kent at Canterbury, March 1990
  367.   .br
  368. ! rlh2@ukc.ac.uk
  369. --- 35,43 ----
  370.   .SH ENVIRONMENT VARIABLES
  371.   DISPLAY \- default X Server to enquire.
  372.   .SH BUGS
  373. ! The program pays no attention to the resources specified on the server
  374. ! (commonly defined in $HOME/.Xdefaults).
  375.   .SH AUTHOR
  376. ! Richard Hesketh (rlh2@ukc.ac.uk), University of Kent at Canterbury, March 1990
  377.   .br
  378. ! prepend/append and cutbuffer support by trost@reed.earn
  379.  
  380. dan
  381. ----------------------------------------------------
  382. O'Reilly && Associates   argv@sun.com / argv@ora.com
  383. Opinions expressed reflect those of the author only.
  384. --
  385. dan
  386. ----------------------------------------------------
  387. O'Reilly && Associates   argv@sun.com / argv@ora.com
  388. Opinions expressed reflect those of the author only.
  389.