home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0925.lha / DonsGenies / DonsGenies.lha / Don'sGenies / StyleTagsCreateSingle.pprx < prev    next >
Text File  |  1993-05-25  |  23KB  |  581 lines

  1. /* This genie creates a new style tag automatically by examining a block of selected text in a document. If the text is in a style which does not match an existing tag, a new tag is created and the text is optionally set to that tag. Tags are named with a simplified description of the typeface; you can alter any of these names to anything you wish (using the "Style Tags Modify" menu item). If you modify the tag, its typeface name might no longer suit it. */
  2.  
  3. /* Method: load in typographic data from selected text. If an existing style is used, see if it is modified (further data after the style name). Ignore any data for text after the first letter. If no existing style is used, assemble the data and compare with all existing styles to see if there is a match. If not, suggest a name for a new style, create it and apply it to the block. */
  4. /* Written by Don Cox 1993. Not Public Domain. All rights reserved. */
  5.  
  6. trace n
  7. if ~show(l, "gdarexxsupport.library") then
  8.     if ~(exists("libs:gdarexxsupport.library") & addlib("gdarexxsupport.library", 0, -30)) then
  9.         exit_msg("Please install the gdarexxsupport.library in your libs: directory before running this Genie")
  10.     
  11.  
  12. address command
  13.  
  14. text = ppm_GetBlockText( 1)
  15.  
  16. if text  = '' then exit_msg("No text selected")
  17. apply = ppm_Inform(2,"Apply style to whole of selected text?","No","Yes")
  18. call ppm_ShowStatus("  Analysing text...")
  19.  
  20. call clearcodes
  21.  
  22.  
  23. position = 1
  24.  
  25. change = 0
  26. position = parsecodes(position) /* parse a block of codes in text */
  27. if position = 0 then exit_msg("No style information found")
  28.  
  29. suggestname=stylename
  30. if change = 1 then do
  31.  
  32. /* put together style definition from text */
  33.     trackstring = "\t<"tracking">"
  34.     if tracking = "" then trackstring = ""
  35.     linespacestring = "\"linespacecode"<"||linespace||">"
  36.     if linespacecode = "" | linespacecode = "" then linespacestring = ""
  37.     fontnamestring = "\ff<"fontname">"
  38.     if fontname = "" then fontnamestring = ""
  39.     fontsizestring = "\fs<"fontsize">"
  40.     if fontsize = "" then fontsizestring = ""
  41.     colourstring = "\c<"colour">"
  42.     if colour = "" then colourstring = ""
  43.     fillpatternstring = "\FP<"fillpattern">"
  44.     if fillpattern = "" then fillpatternstring = ""
  45.     if fillpattern = "\Fp" then fillpatternstring = "\Fp"
  46.  
  47.     styledefinition = "\"paracode||paraname ||typestyle ||bold ||italic ||outline ||underline ||shadow ||superscript ||subscript || fontnamestring|| fontsizestring|| justification|| kerning|| hyphenation|| linespacestring|| trackstring||colourstring ||fillpatternstring
  48.                 
  49.  
  50.  
  51. /* see if new definition matches any of the old ones */
  52.     stylelist = ppm_GetStyleTagList()
  53.     stylelistTest = stylelist||"0a"x
  54.     parse var stylelist NumberOfTags "0a"x stylelist
  55.     tagchange = 1
  56.  
  57.     if NumberOfTags~=0 then do
  58.         do t=1 to NumberOfTags
  59.             parse var stylelist thisname "0a"x stylelist
  60.             thisdata = ppm_GetStyleTagData(thisname)
  61.             thisdata = substr(thisdata, pos("{",thisdata)+1)
  62.             thisdata = left(thisdata, lastpos("}",thisdata)-1)
  63.             if thisdata = styledefinition then do
  64.                 ThisTagName = "\dS<"||thisname||">"
  65.                 tagchange = 0 
  66.                 suggestname = thisname
  67.                 leave t
  68.                 end
  69.             end   /* t=1 to NumberOfTags  */
  70.     end
  71.     
  72. if tagchange ~=0 then do
  73.     
  74.     numbering = 1
  75.     newbold = ""
  76.     if right(bold,1) = "B" then newbold = "B"
  77.     newitalic = ""
  78.     if right(italic,1) = "I" then newitalic = "I"
  79.     suggestname = left(fontname, 9)||newbold||newitalic||"."||(fontsize%1)||"pt."||colour
  80.     testsuggest = "0a"x||suggestname||"0a"x
  81.     
  82.     do i = 1 to 999  /* if name already used, give it a new number */
  83.         if pos(testsuggest,stylelistTest)=0 then break
  84.         numbering = numbering+1
  85.         suggestname = left(fontname, 9)||newbold||newitalic||"."||(fontsize%1)||"pt."||colour"."||right(numbering, 3,"0")
  86.         testsuggest = "0a"x||suggestname||"0a"x
  87.         end
  88.  
  89. do i=1 to 10
  90.     newname = ppm_GetForm(" Name of new style tag",30," Suggested Name :"suggestname)
  91.     if newname = "" then exit_msg("User aborted genie")
  92.     newtest = "0a"x||newname||"0a"x  /* test for complete lines only */
  93.     if pos(newtest,stylelistTest)=0 then do
  94.         styledefinition = "<"newname"{"styledefinition"}>"
  95.         call ppm_DefineStyleTag(styledefinition)
  96.         call ppm_SetStyleTag(newname)
  97.         call exit_msg
  98.         end
  99.  
  100.     choice = ppm_inform(3,"Replace existing tag of same name?","Replace","Try another name","Cancel")
  101.     if choice = 2 then exit_msg("Aborted by User")
  102.     if choice = 0 then do
  103.         styledefinition = "<"newname"{"styledefinition"}>"
  104.         call ppm_DefineStyleTag(styledefinition)
  105.         call ppm_SetStyleTag(newname)
  106.         call exit_msg
  107.         end       
  108.     styledefinition = "<"newname"{"styledefinition"}>"
  109.     call ppm_DefineStyleTag(styledefinition)      
  110. end  /* i=1 to 10  */
  111. end  /* tagchange~=0  */
  112.  
  113.  
  114. if apply = 1 then call ppm_SetStyleTag(suggestname)
  115.  
  116. if tagchange = 0 & apply = 1 then do
  117.     call exit_msg('Applying existing tag "'suggestname'" to text block.')
  118.     end
  119. if tagchange = 0 then do
  120.     call exit_msg('This is existing tag "'suggestname'".')
  121.     end
  122. call exit_msg()
  123. end
  124.  
  125. /*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  126.  
  127. /* Parse a block of style codes in text; stop at first text character  */
  128.  
  129. parsecodes:
  130. parse arg position
  131. position = pos("\",text,position)
  132. if position = 0 then return position
  133. if substr(text,position+1,1) = "." then position = 0 /* end of text */
  134. if position = 0 then return position
  135. nonstylecode = ""
  136. stylecode = "ds"  /* assume new block of codes means new style */
  137.  
  138.         do forever
  139.             if substr(text,position,1)~="\" then break
  140.             if substr(text,position+1,1) = "." then position = -1
  141.             if position = -1 then break
  142.             code = substr(text,position+1,2)
  143.             select
  144.                 when verify(left(code,1),"pNMPTs-!?#","m")~=0 then call parsenonstylecodes
  145.                  
  146.                 when code = "dp" then do
  147.                     paraname = ""
  148.                     paracode = code
  149.                     position = position+3
  150.                     end
  151.                 when code = "dP" then do
  152.                     position1 = pos(">",text,position)
  153.                     position = position+4
  154.                     oldname = paraname
  155.                     paraname = "<"||substr(text, position, position1-position)||">"
  156.                     if paraname~=oldname  then change = 1
  157.                     oldname = paracode
  158.                     paracode = "dP"
  159.                     if paracode~=oldname  then change = 1
  160.                     position = position1+1
  161.                     end
  162.                 when code = "ds" then do
  163.                     stylename = ""
  164.                     stylecode = code
  165.                     position = position+3
  166.                     end
  167.                 when code = "dS" then do
  168.                     position1 = pos(">",text,position)
  169.                     position = position+4
  170.                     stylename = substr(text,position,position1-position)
  171.                     tagdata = ppm_GetStyleTagData(stylename)
  172.                     call parsetag(tagdata) /* new style so reset all variables  */
  173.                     change = 0
  174.                     stylecode = "dS"
  175.                     position = position1+1
  176.                     end
  177.                 when verify(left(code,1),"bB","m")~=0 then do
  178.                     oldname = bold
  179.                     bold = "\"||left(code,1)
  180.                     position = position+2
  181.                     if bold~=oldname  then change = 1
  182.                     end
  183.                 when verify(left(code,1),"iI","m")~=0 then do
  184.                     oldname = italic
  185.                     italic = "\"||left(code,1)
  186.                     position = position+2
  187.                     if italic~=oldname  then change = 1
  188.                     end
  189.                 when verify(left(code,1),"uU","m")~=0 then do
  190.                     oldname = underline
  191.                     underline = "\"||left(code,1)
  192.                     position = position+2
  193.                     if underline~=oldname  then change = 1
  194.                     end
  195.                 when verify(left(code,1),"oO","m")~=0 then do
  196.                     oldname = outline
  197.                     outline = "\"||left(code,1)
  198.                     position = position+2
  199.                     if outline~=oldname  then change = 1
  200.                     end
  201.                 when left(code,1) = "n" then do
  202.                     typestyle = ""
  203.                     if bold~="" then change = 1
  204.                     bold = "\b"
  205.                     if italic~="" then change = 1
  206.                     italic = "\i"
  207.                     if underline~="" then change = 1
  208.                     underline = "\u"
  209.                     if outline~="" then change = 1
  210.                     outline = "\o"
  211.                     position = position+2
  212.                     end
  213.                 when code = "ff" then do
  214.                     position1 = pos(">",text,position)
  215.                     position = position+4
  216.                     oldname = fontname
  217.                     fontname = substr(text,position,position1-position)
  218.                     if fontname~=oldname  then change = 1
  219.                     position = position1+1
  220.                     end
  221.                 when code = "fs" then do
  222.                     position1 = pos(">",text,position)
  223.                     position = position+4
  224.                     oldname = fontsize
  225.                     fontsize = substr(text,position,position1-position)
  226.                     if fontsize~=oldname  then change = 1
  227.                     position = position1+1
  228.                     end
  229.                 when code = "FP" then do
  230.                     position1 = pos(">",text,position)
  231.                     position = position+4
  232.                     oldname = fillpattern
  233.                     fillpattern = substr(text,position,position1-position)
  234.                     if fillpattern ~=oldname  then change = 1
  235.                     position = position1+1
  236.                     notcode = 0
  237.                     end
  238.                 when code = "Fp" then do
  239.                     oldname = fillpattern
  240.                     fillpattern = "\"||code
  241.                     if fillpattern ~= oldname then change = 1
  242.                     position = position+3
  243.                     notcode = 0
  244.                     end
  245.                 when code = "SH"|code = "Sh" then do
  246.                     oldname = shadow
  247.                     shadow = "\"||code
  248.                     if shadow ~= oldname then change = 1
  249.                     position = position+3
  250.                     end
  251.                 when code = "SP"|code = "Sp" then do
  252.                     oldname = superscript
  253.                     superscript = "\"||code
  254.                     if superscript ~= oldname then change = 1
  255.                     position = position+3
  256.                     end
  257.                 when code = "SB"|code = "Sb" then do
  258.                     oldname = subscript
  259.                     subscript = "\"||code
  260.                     if subscript ~= oldname then change = 1
  261.                     position = position+3
  262.                     end
  263.                 when verify(left(code,1),"kK","m")~=0 then do
  264.                     oldname = kerning
  265.                     kerning = "\"||left(code,1)
  266.                     position = position+2
  267.                     if kerning~=oldname  then change = 1
  268.                     end
  269.                 when verify(left(code,1),"hH","m")~=0 then do
  270.                     oldname = hyphenation
  271.                     hyphenation = "\"||left(code,1)
  272.                     if hyphenation~=oldname  then change = 1
  273.                     position = position+2
  274.                     end
  275.                 when code = "lr"|code = "lf"|code = "ll" then do
  276.                     position1 = pos(">",text,position)
  277.                     position = position+4
  278.                     oldname = linespace
  279.                     linespace = substr(text,position,position1-position)
  280.                     if linespace~=oldname  then change = 1
  281.                     oldname = linespacecode
  282.                     linespacecode = code
  283.                     if linespacecode~=oldname  then change = 1
  284.                     position = position1+1
  285.                     end
  286.                 when code = "ls" then do
  287.                     position1 = pos(">",text,position)
  288.                     position = position+4
  289.                     oldname = lineshift
  290.                     lineshift = substr(text,position,position1-position)
  291.                     if lineshift~=oldname  then change = 1
  292.                     position = position1+1
  293.                     end
  294.  
  295.                 when left(code,1) = "t" then do
  296.                     position1 = pos(">",text,position)
  297.                     position = position+3
  298.                     oldname = tracking
  299.                     tracking = substr(text,position,position1-position)
  300.                     if tracking~=oldname  then change = 1
  301.                     position = position1+1
  302.                     end
  303.                 when left(code,1) = "c" then do
  304.                     position1 = pos(">",text,position)
  305.                     position = position+3
  306.                     oldname = colour
  307.                     colour = substr(text,position,position1-position)
  308.                     if colour~=oldname  then change = 1
  309.                     position = position1+1
  310.                     end
  311.                 when code = "jl"|code = "jr"|code = "jc"|code = "jf" then do
  312.                     oldname = justification
  313.                     justification = "\"||code
  314.                     if justification~=oldname  then change = 1
  315.                     position = position+3
  316.                     end
  317.                 when code = "DC" then do
  318.                     position = pos("}>",text,position)+2
  319.                     nonstylecode = "definecolor"
  320.                     change = 0
  321.                     notcode = 1
  322.                     end
  323.                 otherwise position = position+2  
  324.                 end      /* of select */
  325.         end   /* of do forever */
  326. position = position+1
  327. return position
  328.  
  329.  
  330. /*  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  331.  
  332. /* Parse a style tag definition  */
  333. parsetag:
  334.  
  335. parse arg tagdata
  336. tlength = length(tagdata)
  337. tposition = pos("\",tagdata)
  338. if pos = 0 then return
  339.  
  340. /* Clear all codes - the only compulsory info is the tag's name */
  341. call clearcodes
  342.  
  343. do forever
  344.     if substr(tagdata,tposition,1)~="\" then break
  345.     code = substr(tagdata,tposition+1,2)
  346.     select
  347.         when code = "dp" then do
  348.             paraname = ""
  349.             paracode = code
  350.             tposition = tposition+3
  351.             end
  352.         when code = "dP" then do
  353.             tposition1 = pos(">",tagdata,tposition)
  354.             tposition = tposition+4
  355.             paraname = "<"||substr(tagdata,tposition,tposition1-tposition)||">"
  356.             paracode = code
  357.             tposition = tposition1+1
  358.             end
  359.         when verify(left(code,1),"bB","m")~=0 then do
  360.             bold = "\"||left(code,1)
  361.             tposition = tposition+2
  362.             end
  363.         when verify(left(code,1),"iI","m")~=0 then do
  364.             italic = "\"||left(code,1)
  365.             tposition = tposition+2
  366.             end
  367.         when verify(left(code,1),"uU","m")~=0 then do
  368.             underline = "\"||left(code,1)
  369.             tposition = tposition+2
  370.             end
  371.         when verify(left(code,1),"oO","m")~=0 then do
  372.             outline = "\"||left(code,1)
  373.             tposition = tposition+2
  374.             end
  375.         when left(code,1) = "n" then do
  376.             typestyle = "\n"
  377.             bold = ""
  378.             italic = ""
  379.             underline = ""
  380.             outline = ""
  381.             tposition = tposition+2
  382.             end
  383.         when code = "ff" then do
  384.             tposition1 = pos(">",tagdata,tposition)
  385.             tposition = tposition+4
  386.             fontname = substr(tagdata,tposition,tposition1-tposition)
  387.             tposition = tposition1+1
  388.             end
  389.         when code = "fs" then do
  390.             tposition1 = pos(">",tagdata,tposition)
  391.             tposition = tposition+4
  392.             fontsize = substr(tagdata,tposition,tposition1-tposition)
  393.             tposition = tposition1+1
  394.             end
  395.        when code = "FP" then do
  396.             tposition1 = pos(">",text,tposition)
  397.             tposition = tposition+4
  398.             oldname = fillpattern
  399.             fillpattern = substr(text,tposition,tposition1-tposition)
  400.             if fillpattern ~=oldname  then change = 1
  401.             tposition = tposition1+1
  402.             end
  403.         when code = "Fp" then do
  404.             oldname = fillpattern
  405.             fillpattern = "\"||code
  406.             if fillpattern ~= oldname then change = 1
  407.             tposition = tposition+3
  408.             end
  409.         when code = "SH"|code = "Sh" then do
  410.             oldname = shadow
  411.             shadow = "\"||code
  412.             if shadow ~= oldname then change = 1
  413.             tposition = tposition+3
  414.             end
  415.         when code = "SP"|code = "Sp" then do
  416.             oldname = superscript
  417.             superscript = "\"||code
  418.             if superscript ~= oldname then change = 1
  419.             tposition = tposition+3
  420.             end
  421.         when code = "SB"|code = "Sb" then do
  422.             oldname = subscript
  423.             subscript = "\"||code
  424.             if subscript ~= oldname then change = 1
  425.             tposition = tposition+3
  426.             end
  427.         when verify(left(code,1),"kK","m")~=0 then do
  428.             kerning = "\"||left(code,1)
  429.             tposition = tposition+2
  430.             end
  431.         when verify(left(code,1),"hH","m")~=0 then do
  432.             hyphenation = "\"||left(code,1)
  433.             tposition = tposition+2
  434.             end
  435.         when code = "lr"|code = "lf"|code = "ll" then do
  436.             tposition1 = pos(">",tagdata,tposition)
  437.             tposition = tposition+4
  438.             linespace = substr(tagdata,tposition,tposition1-tposition)
  439.             linespacecode = code
  440.             tposition = tposition1+1
  441.             end
  442.         when code = "ls" then do
  443.             tposition1 = pos(">",tagdata,tposition)
  444.             tposition = tposition+4
  445.             lineshift = substr(tagdata,tposition,tposition1-tposition)
  446.             tposition = tposition1+1
  447.             end
  448.         when left(code,1) = "t" then do
  449.             tposition1 = pos(">",tagdata,tposition)
  450.             tposition = tposition+3
  451.             tracking = substr(tagdata,tposition,tposition1-tposition)
  452.             tposition = tposition1+1
  453.             end
  454.         when left(code,1) = "c" then do
  455.             tposition1 = pos(">",tagdata,tposition)
  456.             tposition = tposition+3
  457.             colour = substr(tagdata,tposition,tposition1-tposition)
  458.             tposition = tposition1+1
  459.             end
  460.         when code = "jl"|code = "jr"|code = "jc"|code = "jf" then do
  461.             justification = "\"||code
  462.             tposition = tposition+3
  463.             end
  464.         otherwise tposition = tposition+2
  465.         end
  466. end
  467. return
  468.  
  469. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  470.  
  471. parsenonstylecodes:
  472.                     position = position+2 /* non-style codes */
  473.                     
  474.                     notcode = 1
  475.                     code = substr(text,position-1,5)
  476.                     select
  477.                         when left(code,4) = "#<Pp" then do
  478.                             nonstylecode = "previousboxpage"||substr(code,5,1)
  479.                             position = position+5
  480.                             end
  481.                         when left(code,4) = "#<Pc" then do
  482.                             nonstylecode = "currentpage"||substr(code,5,1)
  483.                             position = position+5
  484.                             end
  485.                         when left(code,4) = "#<Pn" then do
  486.                             nonstylecode = "nextboxpage"||substr(code,5,1)
  487.                             position = position+5
  488.                             end
  489.                         when left(code,4) = "#<Dc" then do
  490.                             nonstylecode = "creationdate"||substr(code,5,1)
  491.                             position = position+5
  492.                             end
  493.                         when left(code,4) = "#<Dp" then do
  494.                             nonstylecode = "printingdate"||substr(code,5,1)
  495.                             position = position+5
  496.                             end
  497.                         when left(code,2) = "pv" then do
  498.                             position1 = pos(">",text,position)
  499.                             position = position+2
  500.                             nonstylecode = "paravalue"||substr(text, position, position1 -position)
  501.                             position = position1+1
  502.                             end
  503.                         when left(code,2) = "ps" then do
  504.                             position1 = pos(">",text,position)
  505.                             position = position+2
  506.                             nonstylecode ="paraspacing"|| substr(text,position, position1 -position)
  507.                             position = position1+1
  508.                             end
  509.                         when left(code,2) = "pi"|left(code,2) = "po" |left(code, 2) = "pn" then do
  510.                             nonstylecode = "indent"||left(code,2)
  511.                             position = position+1
  512.                             end
  513.                         when left(code,1) = "?" then do
  514.                             nonstylecode = "comment"
  515.                             position = pos(">",text,position)
  516.                             end
  517.                         when left(code,1) = "!" then do
  518.                             nonstylecode = "boxbreak"
  519.                             end
  520.                         when left(code,1) = "P" then do
  521.                             nonstylecode = "newparagraph"
  522.                             end
  523.                         when left(code,1) = "M" then do
  524.                             nonstylecode = "mspace"
  525.                             end
  526.                         when left(code,1) = "N" then do
  527.                             nonstylecode = "nspace"
  528.                             end
  529.                         when left(code,1) = "T" then do
  530.                             nonstylecode = "thinspace"
  531.                             end
  532.                         when left(code,1) = "s" then do
  533.                             nonstylecode = "tab"
  534.                             end
  535.                         when left(code,1) = "-" then do
  536.                             nonstylecode = "softhyphen"
  537.                             end
  538.                         otherwise nonstylecode = ""
  539.                         end 
  540. return
  541.  
  542. /*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  543.  
  544. clearcodes:
  545.         paraname = ""
  546.         paracode = ""
  547.         stylename = ""
  548.         stylecode = ""
  549.         fontname = ""
  550.         typestyle = ""
  551.         fontsize = ""
  552.         bold = ""
  553.         italic = ""
  554.         shadow = ""
  555.         superscript = ""
  556.         subscript = ""
  557.         fillpattern = ""
  558.         underline = ""
  559.         outline = ""
  560.         kerning = ""
  561.         hyphenation = ""
  562.         linespace = ""
  563.         linespacecode = ""
  564.         lineshift = ""
  565.         tracking = ""
  566.         colour = ""
  567.         justification = ""
  568. return
  569.  
  570. /*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  571.  
  572. exit_msg:
  573. do
  574.     parse arg message
  575.     if message ~="" then call ppm_inform(1,message,"Resume")
  576.     call ppm_ClearStatus()
  577.  
  578.     exit
  579. end
  580.  
  581.