[<--] Indice [-->]

From jaromil <jaromil@dyne.org>
Date Wed, 2 Mar 2005 17:47:34 +0100
Subject Re: [Hackmeeting] kaostour a milano - 03-10 marzo 2005

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



#!/usr/bin/awk -f
#
# Script to justify plain text paragraphs with same length 
# written on a train ride between Bangalore and Ahmedabad
# use inside vim: press !}giustifica.awk on the paragraph
# copyleft 2005 by jaromil \at\ dyne.org - GNU GPL

BEGIN {
	COLUMNS = 72
	queue["pos"] = 0
	queue["head"] = 0
	queue["tot"] = 0
	queue["wtot"] = 0
}

function fill_line(q, l) {
# q = source array ; l = dest array
	lw = 0
	lf = 0
	qp = q["pos"]
	qh = q["head"]
	qw = q["wtot"]
	qt = q["tot"]

	while(1) {

		wl = length( q[qp] )

		if(lw + wl + lf >= COLUMNS) break

		lw = lw + wl

		l[lf] = q[qp] 
		lf++

		delete q[qp]
		qp++

		if(qp > qh) break # end of queue
		
		qw = qw - wl
		qt--

		# if it's about one last space, go for it
		if(lw + lf  == COLUMNS) break

	}

	q["pos"]  = qp
	q["wtot"] = qw
	q["tot"]  = qt

	l["len"]   = lf
	l["width"] = lw 

	return lf 
}

function print_line(l) {

	lf = l["len"]
	lw = l["width"]

	gap = COLUMNS - (lw + lf)

	# don't justify if the gap is too big
	# if( ! ( gap < (nfs*4) ) ) next


	if( gap == 0 || gap > COLUMNS/2) { # perfect!
	
		for(c=0; c<(lf-1); c++) 
			printf l[c] " "
		print l[lf-1] # print the last without spacing

	} else {

		# line is shorter, we must space it

		gs = 0 # global space to add after every field
		ss = 0 # splitted space to add to begin/end fields
		while(gap>0) {
			gap = gap - (lf-1)
			if(gap<0) ss = (lf-1) + gap
			else gs++
		}


		for(c=0; c<(lf-1); c++) {
			printf l[c] " "
			for(cc=0; cc<gs; cc++) printf " "
			if(ss>0) {
				printf " "
				ss--
			}
		}
		print l[lf-1] # print the last without spacing
	}

}


{

	# save all the new words in our queue
	qf = queue["head"]
	qw = queue["wtot"]
	
	for(c=1; c<=NF; c++) {
		queue[qf] = $c 
		qf++
		qw = qw + length($c)
		queue["tot"]++
	}

	queue["head"] = qf
	queue["wtot"] = qw

	# queue more words (todo intentional newline)
	if(qw < COLUMNS) next


	fill_line(queue, line)

	print_line(line)

	delete line # could use split("", line) for portability

}

END {

	while( queue["tot"]>0 ) {
		fill_line(queue, line)
		print_line(line)
	}

	delete queue

}



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Cryptographically signed mail, see http://gnupg.org

iD8DBQFCJe4lWLCC1ltubZcRAsoIAJ9uRGqXXWVpVbEFJscI//waVivhVgCfXAft
FchD7ClXJ+JS5A3UNyI7zSk=
=yLSP
-----END PGP SIGNATURE-----
_______________________________________________
Hackmeeting mailing list
Hackmeeting@kyuzz.org
https://inventati.org/mailman/listinfo/hackmeeting

[<--] Indice [-->]