/
testfile2.hc
62 lines (55 loc) · 1.78 KB
/
testfile2.hc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function xIndentScript txt,tabSpaces,selStart,selEnd
put 0 into indentLevel
put 0 into lineStartOffset
put 1 into lineNb
repeat for each line theLine of txt
-- get rid of old spaces:
put countLeadingSpaces(theLine) into oldSpaces
if oldSpaces > 0 then
delete char 1 to oldSpaces of theLine
end if
-- un-indent:
if word 1 of theLine = "end" or word 1 of theLine = "else" then
subtract 1 from indentLevel
end if
-- find out how many spaces we want to indent this line:
put indentLevel * tabSpaces into newSpaces
put spaces(newSpaces) before theLine
-- nudge selection
if lineStartOffset <= selStart-1 then
add newSpaces -oldSpaces to selStart
end if
if lineStartOffset <= selEnd-1 then
add newSpaces -oldSpaces to selEnd
end if
-- decide on indentation for following lines:
if word 1 of theLine = "if" or word 1 of theLine = "repeat" or word 1 of theLine = "on" or word 1 of theLine = "function" then
add 1 to indentLevel
else if word 1 of theLine = "else" then
add 1 to indentLevel -- indent commands following else again.
end if
-- write out new line and increment counter/offset:
put theLine into line lineNb of newTxt
add length(theLine) +1 to lineStartOffset -- +1 for return
add 1 to lineNb
end repeat
return selStart & comma & selEnd & comma & newTxt
end xIndentScript
function countLeadingSpaces str
put 0 into theCount
repeat with x = 1 to length(str)
if char x of str = " " or char x of str = tab then
add 1 to theCount
else
exit repeat
end if
end repeat
return theCount
end countLeadingSpaces
function spaces n
put "" into str
repeat with x = 1 to n
put " " after str
end repeat
return str
end spaces