home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / microema.sit / src / clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-09  |  3.2 KB  |  163 lines  |  [TEXT/Earl]

  1. /*
  2.  * clipboard.c
  3.  * Routines to handle cut-copy-paste from microemacs, mainly for
  4.  * interaction with desk accessories and such.    Compiles into
  5.  * nothing if not a Macintosh.
  6.  */
  7. #include <stdio.h>
  8. #include "estruct.h"
  9. #include "edef.h"
  10. #if FINDER
  11. #if LSC
  12. #include <ScrapMgr.h>
  13. #include <TextEdit.h>
  14. #include <IntlPkg.h>
  15. #endif
  16.     
  17. #if MPW
  18. #include <Scrap.h>
  19. #include <TextEdit.h>
  20. #include <Memory.h>
  21. #include <Packages.h>
  22. /*  low-memory globals    */
  23. #define TEScrpLength *((short *)0xAB0)
  24. #define TEScrpHandle *((Handle *)0xAB4)
  25.  
  26. #ifdef MPU68000 /* Aztec */
  27. pascal void Pack6() = 0xA9ED;
  28. #endif
  29.  
  30. #ifdef macintosh /* MPW C */
  31. pascal void Pack6(long time,short form,Ptr buf,short sel)
  32.     extern 0xA9ED;
  33. #endif
  34.  
  35. Handle NewHandle();
  36. #undef IUDateString
  37. #define IUDateString(a,b,c) Pack6((long)(a),((b)<<8),(c),(short)0)
  38. #endif
  39.     
  40. /*
  41.  * Look for 'TEXT' resource in the desk scrap and in the TextEdit
  42.  * scrap.  If found, then insert into the current buffer at dot.
  43.  */
  44. emacs_paste()
  45. {
  46. Handle    tHndl;
  47. char    *tPtr;
  48. long    tscraplen,toffset;
  49. long    k;
  50. char    ch;
  51.     tHndl = NewHandle(0L);
  52.     tscraplen = GetScrap(tHndl,(ResType)'TEXT',&toffset);
  53.     if(tscraplen < 0){
  54.         if(TEScrpLength > 0){
  55.             ZeroScrap();
  56.                 TEToScrap();
  57.             }
  58.         }
  59.     if(tscraplen > 0){
  60.         HLock(tHndl);
  61.         tPtr = (char *)(*tHndl);
  62.         for (k=0;tscraplen-k;k++){
  63.             ch = *tPtr++;
  64.             switch(ch){
  65.                 case CR:
  66.                     if(lnewline()!=TRUE)
  67.                         return(FALSE);
  68.                     break;
  69.                 default:
  70.                     if(linsert(1,ch)!=TRUE)
  71.                         return(FALSE);
  72.                     break;
  73.             }
  74.         }
  75.     }
  76.     DisposHandle(tHndl);
  77.     update(FALSE);
  78. }
  79. long        bytes_to_copy;
  80. REGION        region;
  81. /*
  82.  * Copy the text in the region to the desk scrap and to the TextEdit
  83.  * scrap.  Do nothing with the kill buffer.
  84.  */
  85. emacs_copy()
  86. {
  87. #ifdef macintosh
  88. #define lmalloc malloc
  89. #endif
  90. char *lmalloc();
  91.     register LINE    *linep;
  92.     register int    loffs;
  93.     register int    s;
  94.     register char *ch;
  95.     char    *source;
  96.     
  97.     if ((s=getregion(®ion)) != TRUE)
  98.         return (s);
  99.     if((source = lmalloc(region.r_size))==NULL) {
  100.         TTbeep();
  101.         mlwrite("Can't get memory to copy to.");
  102.         return (FALSE);
  103.     }
  104.     ch = source;
  105.  
  106.     ZeroScrap();
  107.     linep = region.r_linep;         /* Current line.    */
  108.     loffs = region.r_offset;        /* Current offset.    */
  109.     bytes_to_copy = region.r_size;
  110.     while (region.r_size--) {
  111.         if (loffs == llength(linep)) {    /* End of line.     */
  112.             *ch++ = CR;
  113.             linep = lforw(linep);
  114.             loffs = 0;
  115.         } else {            /* Middle of line.    */
  116.             *ch++ = lgetc(linep, loffs);
  117.             ++loffs;
  118.         }
  119.     }
  120.     PutScrap(bytes_to_copy,(ResType)'TEXT',source);
  121.     TEFromScrap();
  122.     free(source);
  123.     return (TRUE);
  124. }    
  125. /*
  126.  * emacs_cut().  Copy, then delete.  Note that this operation, like
  127.  * emacs_copy(), does not use the kill buffer.    The cut text can be
  128.  * found in the clipboard, rather than by yanking it.
  129.  *    cut, then paste
  130.  *    kill, then yank
  131.  * separate classes of operation.
  132.  *
  133.  * You can, of course, switch between the two by yanking something,
  134.  * then cutting it, and so forth.
  135.  */
  136. emacs_cut()
  137. {
  138.     if (emacs_copy()==TRUE) {
  139.         curwp->w_dotp = region.r_linep;
  140.         curwp->w_doto = region.r_offset;
  141.         ldelete(bytes_to_copy, FALSE);
  142.     }
  143.     update(FALSE);
  144. }
  145. ins_date(f,n)
  146. {
  147.     {
  148.     long curdate;
  149.     char strbuf[256];
  150.     int i;
  151.     GetDateTime(&curdate);
  152.     IUDateString(curdate,n,strbuf);
  153.     for(i=0;i++ < strbuf[0];){
  154.         linsert(1,strbuf[i]);
  155.     }
  156.     }
  157.     return(TRUE);
  158. }
  159.  
  160.  
  161. #endif
  162.  
  163.