Debian Codegraph @

ipl/packs/ibpag2/rewrap.icn

raw
1 ############################################################################
2 #
3 # Name: rewrap.icn
4 #
5 # Title: advanced line rewrap utility
6 #
7 # Author: Richard L. Goerwitz
8 #
9 # Version: 1.4
10 #
11 ############################################################################
12 #
13 # The procedure rewrap(s,i), included in this file, reformats text
14 # fed to it into strings < i in length. Rewrap utilizes a static
15 # buffer, so it can be called repeatedly with different s arguments,
16 # and still produce homogenous output. This buffer is flushed by
17 # calling rewrap with a null first argument. The default for
18 # argument 2 (i) is 70.
19 #
20 # Here's a simple example of how rewrap could be used. The following
21 # program reads the standard input, producing fully rewrapped output.
22 #
23 # procedure main()
24 # every write(rewrap(!&input))
25 # write(rewrap())
26 # end
27 #
28 # Naturally, in practice you would want to do things like check for in-
29 # dentation or blank lines in order to wrap only on a paragraph-by para-
30 # graph basis, as in
31 #
32 # procedure main()
33 # while line := read(&input) do {
34 # if line == "" then {
35 # write("" ~== rewrap())
36 # write(line)
37 # } else {
38 # if match("\t", line) then {
39 # write(rewrap())
40 # write(rewrap(line))
41 # } else {
42 # write(rewrap(line))
43 # }
44 # }
45 # }
46 # end
47 #
48 # Fill-prefixes can be implemented simply by prepending them to the
49 # output of rewrap:
50 #
51 # i := 70; fill_prefix := " > "
52 # while line := read(input_file) do {
53 # line ?:= (f_bit := tab(many('> ')) | "", tab(0))
54 # write(fill_prefix || f_bit || rewrap(line, i - *fill_prefix))
55 # etc.
56 #
57 # Obviously, these examples are fairly simplistic. Putting them to
58 # actual use would certainly require a few environment-specific
59 # modifications and/or extensions. Still, I hope they offer some
60 # indication of the kinds of applications rewrap might be used in.
61 #
62 # Note: If you want leading and trailing tabs removed, map them to
63 # spaces first. Rewrap only fools with spaces, leaving tabs intact.
64 # This can be changed easily enough, by running its input through the
65 # Icon detab() function.
66 #
67 ############################################################################
68 #
69 # See also: wrap.icn
70 #
71 ############################################################################
72
73
74 procedure rewrap(s,i)
75
76 local extra_bit, line
77 static old_line
78 initial old_line := ""
79
80 # Default column to wrap on is 70.
81 /i := 70
82 # Flush buffer on null first argument.
83 if /s then {
84 extra_bit := old_line
85 old_line := ""
86 return "" ~== extra_bit
87 }
88
89 # Prepend to s anything that is in the buffer (leftovers from the last s).
90 s ?:= { tab(many(' ')); old_line || trim(tab(0)) }
91
92 # If the line isn't long enough, just add everything to old_line.
93 if *s < i then old_line := s || " " & fail
94
95 s ? {
96
97 # While it is possible to find places to break s, do so.
98 while any(' -',line := EndToFront(i),-1) do {
99 # Clean up and suspend the last piece of s tabbed over.
100 line ?:= (tab(many(' ')), trim(tab(0)))
101 if *&subject - &pos + *line > i
102 then suspend line
103 else {
104 old_line := ""
105 return line || tab(0)
106 }
107 }
108
109 # Keep the extra section of s in a buffer.
110 old_line := tab(0)
111
112 # If the reason the remaining section of s was unrewrapable was
113 # that it was too long, and couldn't be broken up, then just return
114 # the thing as-is.
115 if *old_line > i then {
116 old_line ? {
117 if extra_bit := tab(upto(' -')+1) || (tab(many(' ')) | "")
118 then old_line := tab(0)
119 else extra_bit := old_line & old_line := ""
120 return trim(extra_bit)
121 }
122 }
123 # Otherwise, clean up the buffer for prepending to the next s.
124 else {
125 # If old_line is blank, then don't mess with it. Otherwise,
126 # add whatever is needed in order to link it with the next s.
127 if old_line ~== "" then {
128 # If old_line ends in a dash, then there's no need to add a
129 # space to it.
130 if old_line[-1] ~== "-"
131 then old_line ||:= " "
132 }
133 }
134 }
135
136 end
137
138
139
140 procedure EndToFront(i)
141 # Goes with rewrap(s,i)
142 *&subject+1 - &pos >= i | fail
143 suspend &subject[.&pos:&pos <- &pos+i to &pos by -1]
144 end