-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustifica.sed
More file actions
85 lines (70 loc) · 1.98 KB
/
justifica.sed
File metadata and controls
85 lines (70 loc) · 1.98 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
prompt$ chmod +x justifica.sed
prompt$ ./justifica.sed arquivo.txt > arquivo-justificado.txt
se quiser alterar, o nzmero de colunas maximo, troque todos os 65
do script pelo nzmero desejado.
se quiser usar no vim, selecione o texto com o modo visual e
:'<,'>!justifica.sed
ah! esta mensagem foi justificada por ele &:)
justifica.sed
#!/bin/sed -f
# justify.sed
#
# it gets a text already wrapped on the desired number of columns
# and add extra white spaces, from left to right, word by word,
# to justify all the lines. there is a maximum of 5 spaces to be
# inserted between the words. if this limit is reached, the line
# is not justified (come on, more than 5 is horrible). empty
# lines are ignored. btw, this comments were justified with this
# script &:)
#
# 20000715 <[email protected]>
# we'll only justify lines with less than 65 chars
/^.\{65\}/!{
# cleaning extra spaces of the line
s/^ \+//
s/ \+/ /g
s/ \+$//
# don't try to justify blank lines
/^$/b
# backup of the line
h
# spaces -> pattern
# convert series of spaces to a internal pattern `n
:s2p
s/ /`5/g
s/ /`4/g
s/ /`3/g
s/ /`2/g
s/ /`1/g
t 1space
b
# pattern -> spaces
# restore the spaces converted to the internal pattern `n
:p2s
s/`5/ /g
s/`4/ /g
s/`3/ /g
s/`2/ /g
s/`1/ /g
t check
b
# check if we've reached our right limit
# if not, continue adding spaces
:check
/^.\{65\}/!b s2p
b
# here's the "magic":
# add 1 space to the first and minor internal pattern found.
# this way, the extra spaces are always added from left to right,
# always balanced, one by one.
# right after the substitution, we'll restore the spaces and
# test if our limit was reached.
:1space
s/`1/`2/ ; t p2s
s/`2/`3/ ; t p2s
s/`3/`4/ ; t p2s
s/`4/`5/ ; t p2s
# we don't want to justify with more than 5 added spaces between
# words, so let's restore the original line
/`5/x
}