1 | /****************************** Module Header *******************************
|
---|
2 | *
|
---|
3 | * Module Name: tabsspaces.e
|
---|
4 | *
|
---|
5 | * Copyright (c) Netlabs EPM Distribution Project 2004
|
---|
6 | *
|
---|
7 | * $Id: tabsspaces.e 2417 2011-05-15 23:32:51Z aschn $
|
---|
8 | *
|
---|
9 | * ===========================================================================
|
---|
10 | *
|
---|
11 | * This file is part of the Netlabs EPM Distribution package and is free
|
---|
12 | * software. You can redistribute it and/or modify it under the terms of the
|
---|
13 | * GNU General Public License as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * Netlabs EPM Distribution. This library is distributed in the hope that it
|
---|
16 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
---|
17 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | * General Public License for more details.
|
---|
19 | *
|
---|
20 | ****************************************************************************/
|
---|
21 |
|
---|
22 | ; ---------------------------------------------------------------------------
|
---|
23 | defc Spaces2Tabs, TabsCompress
|
---|
24 | parse arg arg1 .
|
---|
25 | if arg1 = '' then
|
---|
26 | 'commandline Spaces2Tabs 'word( .tabs, 1)
|
---|
27 | return
|
---|
28 | else
|
---|
29 | TabWidth = arg1
|
---|
30 | endif
|
---|
31 | dummy = ''
|
---|
32 | Changed = 0
|
---|
33 | do l = 1 to .last
|
---|
34 | Line = textline(l)
|
---|
35 | Line = ExpandLine( Line, TabWidth, dummy)
|
---|
36 | Line = CompressLine( Line, TabWidth, Changed)
|
---|
37 | if Changed then
|
---|
38 | replaceline Line, l
|
---|
39 | endif
|
---|
40 | enddo
|
---|
41 |
|
---|
42 | ; ---------------------------------------------------------------------------
|
---|
43 | defc Tabs2Spaces, TabsExpand
|
---|
44 | parse arg arg1 .
|
---|
45 | if arg1 = '' then
|
---|
46 | 'commandline Tabs2Spaces 'word( .tabs, 1)
|
---|
47 | return
|
---|
48 | else
|
---|
49 | TabWidth = arg1
|
---|
50 | endif
|
---|
51 | Changed = 0
|
---|
52 | do l = 1 to .last
|
---|
53 | Line = textline(l)
|
---|
54 | Line = ExpandLine( Line, TabWidth, Changed)
|
---|
55 | if Changed then
|
---|
56 | replaceline Line, l
|
---|
57 | endif
|
---|
58 | enddo
|
---|
59 |
|
---|
60 | ; ---------------------------------------------------------------------------
|
---|
61 | ; Col and NextTabCol start at 0. So it's easier to calculate:
|
---|
62 | ; TabWidth 8 gives stops at 0, 8, 16,...
|
---|
63 | ; Avoid tab chars in quoted strings, if quote starts in this line.
|
---|
64 | defproc CompressLine( Line, TabWidth, var Changed)
|
---|
65 | Changed = 0
|
---|
66 | TabChar = \9
|
---|
67 | rest = Line
|
---|
68 | Line = ''
|
---|
69 | Col = 0 -- processed chars, means .col - 1
|
---|
70 | p = pos( ' ', rest)
|
---|
71 | do while p <> 0
|
---|
72 | LeftPart = substr( rest, 1, p - 1)
|
---|
73 | RightPart = substr( rest, p) -- including all spaces
|
---|
74 | Line = Line''LeftPart
|
---|
75 | Col = Col + length(LeftPart)
|
---|
76 | SpaceLen = max( verify( RightPart, ' ', 'N') - 1, 0)
|
---|
77 | if SpaceLen = 0 then
|
---|
78 | SpaceLen = length(RightPart) -- Avoid endless loop if spaces at end of line
|
---|
79 | endif
|
---|
80 | rest = substr( rest, p + SpaceLen) -- stripped leading spaces
|
---|
81 | if IsInQuotes( Line) then -- Line is here only left part before current doublespace
|
---|
82 | Line = Line''copies( ' ', SpaceLen)
|
---|
83 | Col = Col + SpaceLen
|
---|
84 | else
|
---|
85 | do while SpaceLen > 0
|
---|
86 | NextTabCol = Col + TabWidth - (Col//TabWidth)
|
---|
87 | if (NextTabCol > Col) & (Col + SpaceLen >= NextTabCol) then
|
---|
88 | Line = Line''TabChar
|
---|
89 | SpaceLen = SpaceLen - (NextTabCol - Col)
|
---|
90 | Col = NextTabCol
|
---|
91 | if Changed = 0 then
|
---|
92 | Changed = 1
|
---|
93 | endif
|
---|
94 | else
|
---|
95 | Line = Line''copies( ' ', SpaceLen)
|
---|
96 | Col = Col + SpaceLen
|
---|
97 | SpaceLen = 0
|
---|
98 | endif
|
---|
99 | enddo
|
---|
100 | endif
|
---|
101 | p = pos( ' ', rest)
|
---|
102 | enddo
|
---|
103 | Line = Line''rest
|
---|
104 | return Line
|
---|
105 |
|
---|
106 | ; ---------------------------------------------------------------------------
|
---|
107 | ; Col and NextTabCol start at 0. So it's easier to calculate:
|
---|
108 | ; TabWidth 8 gives stops at 0, 8, 16,...
|
---|
109 | defproc ExpandLine( Line, TabWidth, var Changed)
|
---|
110 | Changed = 0
|
---|
111 | TabChar = \9
|
---|
112 | if pos( TabChar, Line) = 0 then
|
---|
113 | return Line
|
---|
114 | endif
|
---|
115 | rest = Line
|
---|
116 | Line = ''
|
---|
117 | p = pos( TabChar, rest)
|
---|
118 | do while p <> 0
|
---|
119 | LeftPart = substr( rest, 1, p - 1)
|
---|
120 | Line = Line''LeftPart
|
---|
121 | rest = substr( rest, p + 1)
|
---|
122 | Col = length(Line) -- processed columns before current tab char
|
---|
123 | NextTabCol = Col + TabWidth - (Col//TabWidth)
|
---|
124 | SpaceLen = NextTabCol - Col
|
---|
125 | Line = Line''copies( ' ', SpaceLen)
|
---|
126 | if Changed = 0 then
|
---|
127 | Changed = 1
|
---|
128 | endif
|
---|
129 | p = pos( TabChar, rest)
|
---|
130 | enddo
|
---|
131 | Line = Line''rest
|
---|
132 | return Line
|
---|
133 |
|
---|
134 | ; ---------------------------------------------------------------------------
|
---|
135 | ; Check if position p is quoted in Line, '...' or "...".
|
---|
136 | ; Line is at this point only the left part of Line.
|
---|
137 | ; If it contains an odd number of quotes or doublequotes, then 1 is returned.
|
---|
138 | defproc IsInQuotes( Line)
|
---|
139 | ret = 0
|
---|
140 | if Line <> '' then
|
---|
141 | -- Count " in Line
|
---|
142 | cdq = 0
|
---|
143 | startp = 1
|
---|
144 | pdq = 1
|
---|
145 | do while pdq <> 0
|
---|
146 | pdq = pos( '"', Line, startp)
|
---|
147 | if pdq > 0 then
|
---|
148 | cdq = cdq + 1
|
---|
149 | endif
|
---|
150 | startp = pdq + 1
|
---|
151 | enddo
|
---|
152 | if cdq//2 = 1 then
|
---|
153 | ret = 1
|
---|
154 | else
|
---|
155 | -- Count ' in Line
|
---|
156 | cq = 0
|
---|
157 | startp = 1
|
---|
158 | pq = 1
|
---|
159 | do while pq <> 0
|
---|
160 | pq = pos( "'", Line, startp)
|
---|
161 | if pq > 0 then
|
---|
162 | cq = cq + 1
|
---|
163 | endif
|
---|
164 | startp = pq + 1
|
---|
165 | enddo
|
---|
166 | if cq//2 = 1 then
|
---|
167 | ret = 1
|
---|
168 | endif
|
---|
169 | endif
|
---|
170 | endif
|
---|
171 | return ret
|
---|
172 |
|
---|
173 |
|
---|