[comp.editors] Centering lines in vi

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (09/11/89)

Does anyone know a combination of commands that will center a given line 
(or better yet, a range of lines) in vi?  

A (perhaps) related question:
Is there a command that returns the current column position of the cursor 
in vi?

-- 
Rouben Rostamian                               Phone: 301 455-2458
Department of Mathematics                      e-mail:
University of Maryland Baltimore Counnty       Rostamian@umbc.bitnet
Baltimore, MD 21228                            rostamia@umbc3.umbc.edu

maart@cs.vu.nl (Maarten Litmaath) (09/12/89)

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:
\Does anyone know a combination of commands that will center a given line 
\(or better yet, a range of lines) in vi?  

I think you're better off filtering the range through some nroff shell script.
Example (to set indentation and linelength):
----------8<----------8<----------8<----------8<----------8<----------
#!/bin/sh

# usage: fill [indent] [linelength]
# author: Hans-Juergen Ehling (eh@astbe.uucp)

indent=${1-0}
llength=${2-72}

tmp=/tmp/format.$$
umask 077

/bin/rm -f $tmp
exec 3>&1 > $tmp 4< $tmp
/bin/rm -f $tmp

echo ".hy 0
.ll $llength
.in $indent"

tr -s ' ' ' ' # To squeeze blanks

nroff <&4 >&3
----------8<----------8<----------8<----------8<----------8<----------
Usage:
	!}fill 3 78

to reformat up to the end of the paragraph.

\A (perhaps) related question:
\Is there a command that returns the current column position of the cursor 
\in vi?

I guess not. :-(
Maybe some disgusting macro...
-- 
C, the programming language that's the same |Maarten Litmaath @ VU Amsterdam:
         in all reference frames.           |maart@cs.vu.nl, mcvax!botter!maart

art@felix.UUCP (Art Dederick) (09/12/89)

In article <2283@umbc3.UMBC.EDU> rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:
>
>Does anyone know a combination of commands that will center a given line 
>(or better yet, a range of lines) in vi?  

I do this by mapping a key sequence to pass the range of lines through
nroff using the .ce request.  I'm sure there are other formatters that
could do the same.  The use of filters to operate on vi's buffer has
long been a means to add functionality to vi without the baggage of
extra use-once-in-a-while code in vi.

My $.02 on the subject.

Art D.			art@felix.uucp

jdpeek@rodan.acs.syr.edu (Jerry Peek) (09/13/89)

In article <2283@umbc3.UMBC.EDU> rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:
> Is there a command that returns the current column position of the cursor 
> in vi?

Here's something that gets close.  I just dreamed this up, so I haven't tested
it real thoroughly.  It's a kludge, but it seems to work.

First part:  Define this vi macro named "v":
	:map v i^A^[:.w !colpos^[0f^Ax
where the ^A is a real control-A character (I listed it here as a ^ followed
by an A) and ^[ is a real ESC character (type it as control-V ESC).  The ^A
isn't important; you can use any character that you won't appear in your
file.  The macro inserts a ^A character in the line, then writes the line to
the standard input of a shell script called "colpos" (COLumn POSition).
After that, the macro finds the ^A again and removes it from the line.

Second part: the 'colpos' script (written for 4.3 BSD):
	#! /bin/sh
	sed 's/.*//' | wc -c
	# time to read answer:
	# sleep 1
(sorry, no "shar file").  Again, replace the ^A with a real control-A
character.  The script reads the line from vi (from the script's standard
input), strips off the ^A and the rest of the line, and uses "wc -c" to count
how many characters are left on the line.  It prints that number to its
standard output, which 'vi' displays.  I commented out the last line; see
below.

This kludge has some problems:
	- sometimes, 'vi' tells me 'press return to continue' and pauses
	  after running the macro.  Other times, it doesn't pause.
	- sometimes, the ^A is removed from the line.  Other times, it isn't.
I don't have time to work on this more.  If I wait to clean it up, I'll never
get to it!  I posted this in case someone wants to clean it up.  Hope it
helps.

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek@rodan.acs.syr.edu///JDPEEK@SUVM.BITNET///GEnie: J.PEEK1
  +1 315 443-3995

hamlin@blackbird.afit.af.mil (Joe Hamlin) (09/14/89)

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:

>Does anyone know a combination of commands that will center a given line 
>(or better yet, a range of lines) in vi?  

                                      The 
                                   following 
                                 macro seems to 
                             work just fine.  These 
                           painful-to-read lines were 
                                 centered with 
                                      it. 

map v $ma81a ^[81^V|D`alld0:s/  / /g^M$p^M

Note: "^[" is entered as CTRL-V ESC
	  "^V" as CTRL-V CTRL-V,
	  "^M" as CTRL-V CTRL-M.
-- 
Joe Hamlin    <hamlin@blackbird.afit.af.mil>

jdpeek@rodan.acs.syr.edu (Jerry Peek) (09/14/89)

In article <745@rodan.acs.syr.edu> I wrote:
< Second part: the 'colpos' script (written for 4.3 BSD):
< 	#! /bin/sh
< 	sed 's/.*//' | wc -c
<...
< (sorry, no "shar file").  Again, replace the ^A with a real control-A
< character.  The script reads the line from vi (from the script's standard
< input), strips off the ^A and the rest of the line...

I forgot the control-A (^A) in that sed expression.  It should have read:
	sed 's/^A.*//' | wc -c

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek@rodan.acs.syr.edu///JDPEEK@SUVM.BITNET///GEnie: J.PEEK1
  +1 315 443-3995

maart@cs.vu.nl (Maarten Litmaath) (09/15/89)

hamlin@blackbird.afit.af.mil (Joe Hamlin) writes:
\...                                   The 
\                                   following 
\                                 macro seems to 
\                             work just fine.  These 
\                           painful-to-read lines were 
\                                 centered with 
\                                      it. 
\
\map v $ma81a ^[81^V|D`alld0:s/  / /g^M$p^M

A nice map, but it has a small bug: try to center a line that's been centered
already, or any line with indentation.  Furthermore, after applying the macro
every line ends in a space!
Let's try again:

	map v :s/^[ ^I]*//^V^M$ma81a ^V^[81^V^V|D`alxd0:s/  / /g^V^M$p^V^M
		 ^^		       ^^
		 NOT an escape	       this IS an escape
-- 
   creat(2) shouldn't have been create(2): |Maarten Litmaath @ VU Amsterdam:
      it shouldn't have existed at all.    |maart@cs.vu.nl, mcvax!botter!maart