aboutsummaryrefslogtreecommitdiff
path: root/justify.sed
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2014-09-18 16:36:35 -0300
committerSilvio Rhatto <user@example.org>2014-09-18 16:36:35 -0300
commit0d9ff9ecbe486fa0efe89d079266f0d416d5067c (patch)
treeb5389e2506ea3dfda3d26b44e362656e13c89d8a /justify.sed
downloadsedscripts-0d9ff9ecbe486fa0efe89d079266f0d416d5067c.tar.gz
sedscripts-0d9ff9ecbe486fa0efe89d079266f0d416d5067c.tar.bz2
Initial import
Diffstat (limited to 'justify.sed')
-rwxr-xr-xjustify.sed68
1 files changed, 68 insertions, 0 deletions
diff --git a/justify.sed b/justify.sed
new file mode 100755
index 0000000..0a2e5af
--- /dev/null
+++ b/justify.sed
@@ -0,0 +1,68 @@
+#!/bin/sed -f
+# justify.sed - aurélio marinho jargas <verde (a) aurelio net>
+#
+# 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 1st release
+# 20000722 code cleaned
+
+# cleaning extra spaces of the line
+s/ \+/ /g
+s/^ //
+s/ $//
+
+# we'll only justify lines with less than 65 chars
+/^.\{65\}/b
+
+# backup of the 'stripped' 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