home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmsmp.zip / JUSTIFY.E < prev    next >
Text File  |  1996-02-06  |  7KB  |  131 lines

  1.  
  2. /****************************************************************************/
  3. /* PJUSTIFY_MARK: REFLOW THE TEXT AND LEFT JUSTIFY , THEN RIGHT JUSTIFY     */
  4. /*                                                                          */
  5. /* Ctrl-J to initiate                                                       */
  6. /*     Operates on LINE marked text with current margin setting (not>=254)  */
  7. /*     Operates on the text marked with BLOCK mark within the mark          */
  8. /*     If NO MARK, operates from current line to next blank line            */
  9. /*         with current margin setting  (Thanks to Cristian Peter)          */
  10. /*     Honors paragraph indent specification of Margin setting              */
  11. /*     Does NOT right justify last line                                     */
  12. /*                                                                          */
  13. /*     Uses a simple insert blanks starting from right end to right justify */
  14. /*     Uses some procedures from EXTPROCS.E                                 */
  15. /*     Include 'justify.e' in main.e                                        */
  16. /*          between  include 'stdkeys.e' and  include 'stdcnf.e'            */
  17. /*                                                                          */
  18. /*  Patrick Leabo  (LEABO at TUCVM2)         7-23-86   Initial Ver 1.00     */
  19. /*  7-24-86 Corrected First Line bug                               1.01     */
  20. /*  7-26-86 Changed NO MARK to work to next blank line,            1.10     */
  21. /*          and BLOCK to work just in the block                             */
  22. /*  8-03-86 Changed justify algorithm to space out blanks a bit    1.11     */
  23. /*          and got rid of flashing screen in block justify                 */
  24. /* 12-17-86 Checked it out on E3 -- OK as is on vanilla E3                  */
  25. /* 11-28-89 Massage with brass hammer until functional under EPM; J e r r y */
  26. /****************************************************************************/
  27.  
  28. compile if not defined(REFLOW_LIKE_PE)
  29. const
  30.    REFLOW_LIKE_PE = 0
  31. compile endif
  32.  
  33. def c_j=pjustify_mark()     /* Use the Ctrl-J key to initiate */
  34.  
  35. defproc pjustify_mark       /* First reflow the text per the mark type */
  36.   mkt = marktype()               /* save some initial conditions */
  37.   plsvcol =.col;plsvline = .line;plsvcx = .cursorx;plsvcy = .cursory
  38.   if  mkt = 'BLOCK' then
  39.     getmark  bfirstline,blastline,firstcol,lastcol,plfileid
  40.     if lastcol - firstcol < 8 then
  41.       sayerror 'Quitting Justify - Block Mark < 8 chars wide'
  42.       stop
  43.       endif
  44.     'xcom e /q /n /h .tempo'        /* move the source to a temporary file */
  45.     getfileid pltempfid ; sayerror 0 ; activatefile pltempfid
  46.     .col = firstcol     ; .line = 1
  47.     copy_mark           ; unmark
  48.     activatefile plfileid
  49.     call pset_mark(bfirstline,blastline,firstcol,lastcol,mkt,plfileid)
  50.     fill_mark ' ' ; unmark          /* blank the source */
  51.     activatefile pltempfid
  52.     savemargins = pmargins()
  53.     'ma 'firstcol lastcol
  54.     .line = 1     ; mark_line
  55.     .line = .last ; mark_line
  56.     reflow                          /* reflow first */
  57.     getmark  firstline,lastline,bfirstcol,blastcol,pltempfid
  58.     unmark
  59.     parcol = firstcol
  60.     'ma 'savemargins
  61.   elseif mkt = 'LINE' or mkt = '' then          /* line type mark or no mark */
  62.     parse value pmargins() with  firstcol lastcol parcol
  63.     if lastcol >= 254 then
  64.       sayerror 'Quitting Justify - Right Margin is 254'
  65.       stop
  66.     endif
  67.     if mkt = '' then  /* no mark, look for a blank line */         /* M */
  68.       if (.line = 0)  then                                         /* o */
  69.         sayerror "Can't justify here" ; stop                       /* d */
  70.       else                                                         /*   */
  71.         getline line
  72.         if line = '' then sayerror "Can't justify here" ; stop endif
  73.         mark_line ; call pfind_blank_line()                        /* b */
  74.         getline line; if line='' then up else .line=.last endif    /* y */
  75.         mark_line                                                  /*   */
  76.       endif                                                        /* C */
  77.     endif                                                          /* P */
  78.     reflow                          /* reflow first */
  79.     getmark  firstline,lastline,mfirstcol,mlastcol,plfileid
  80.   else sayerror 'Character mark invalid'; stop endif
  81. /****** this is the justify code ********/
  82.   savemargins = pmargins() ; 'ma 1 254'
  83.   if not insert_state() then insrtflg=1 ; insert_toggle else insrtflg=0 endif
  84.   oddlinetgl = 0
  85.   for i=firstline to (lastline-1)
  86. /*  if ((i = lastline) and (firstline <> lastline)) then leave endif   */
  87.     if i = firstline then startcol = parcol else startcol = firstcol endif
  88.     .line = i ; begin_line
  89.     keyin '>' ; rubout           /* just to get rid of trailing blanks */
  90.     end_line  ; nspaces = lastcol + 1 - .col
  91.     backtab_word ; pltgl = 0
  92.     if (.col > startcol) and (nspaces > 0)  then  /* this does the deed */
  93.       backtab_word ;
  94.       if oddlinetgl = 0 then
  95.          backtab_word ; oddlinetgl = 1   /* start in 3rd space */
  96.       else oddlinetgl = 0;
  97.       endif
  98.       for j = nspaces to 1 by -1
  99.         if .col <= startcol then
  100.           end_line ; backtab_word
  101.           if pltgl = 0 then backtab_word ; pltgl = 1 else pltgl = 0 endif
  102.           if .col <= startcol then end_line ; backtab_word endif
  103.         endif
  104.         keyin ' ' ; backtab_word ; backtab_word
  105.       endfor
  106.     endif
  107.   endfor
  108.   'ma 'savemargins                /* cleanup */
  109.   if mkt = 'BLOCK' then
  110.     call pset_mark(firstline,lastline,firstcol,lastcol,mkt,pltempfid)
  111.     activatefile plfileid
  112.     .line = bfirstline ; .col  = firstcol
  113.     overlay_block      ; activatefile pltempfid
  114.     .modify=0          ; 'xcom q'
  115.     activatefile plfileid
  116.   endif
  117.   if insrtflg = 1  then insert_toggle  endif /* restore initial conditions */
  118.   if mkt      = '' then unmark         endif
  119.   .cursorx = plsvcx; .cursory = plsvcy; .col = plsvcol;
  120.   if plsvline>.last then .line=.last else .line = plsvline endif
  121.   compile if REFLOW_LIKE_PE   /* position on next paragraph (like PE) */
  122.     call pfind_blank_line()
  123.     if plsvline = .line then .line=.last else .line = .line + 1 endif
  124.     call pfind_nonblank_line()
  125.   compile else
  126.     getmark firstline,lastline
  127.     .line=firstline
  128.     .cursory=plsvcy;.cursorx=plsvcx; .line=plsvline;.col=plsvcol;
  129.   compile endif
  130.  
  131.