From: ·····@mama.indstate.edu
Subject: padding function
Date: 
Message-ID: <77du3p$e2r$1@nnrp1.dejanews.com>
Hello:

I am in emacs and want to insert lines number line vi and then print the
buffer. I want to right shift the code by max digits in max line number and a
space. Then would want to be able to insert the numbers in the space right
padded or leftpadded with spaces.

so basically,

1  jjj
2  jjj
.. ...
27 jjj

should be output for file like
jjj
jjj
...
jjj

with 27 lines.

I am able to insert line numbers but donot know how to do padding.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

From: Barry Margolin
Subject: Re: padding function
Date: 
Message-ID: <utwm2.57$oD6.5416@burlma1-snr1.gtei.net>
In article <············@nnrp1.dejanews.com>,  <·····@mama.indstate.edu> wrote:
>I am able to insert line numbers but donot know how to do padding.

Padding is done automatically by format when you specify a field width.

Unfortunately, Emacs's format function doesn't allow you to provide a
variable substitution for the field width, like you can with CL's FORMAT by
using V in the format string, in order to make it depend on the highest
value.  The best way I can think of doing it is to use format to create the
format string:

(format (format "%%%dd" field-width) line-number)

BTW, Emacs Lisp questions are usually best posted in one of the Emacs
newsgroups (e.g. comp.emacs or gnu.emacs.help) rather than here in
comp.lang.lisp.  I almost didn't notice that you said Emacs, and was going
to give you the Common Lisp answer:

(format t "~VD" field-width line-number)

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
From: Hrvoje Niksic
Subject: Re: padding function
Date: 
Message-ID: <kigsodgit4p.fsf@jagor.srce.hr>
Barry Margolin <······@bbnplanet.com> writes:

> Unfortunately, Emacs's format function doesn't allow you to provide
> a variable substitution for the field width, like you can with CL's
> FORMAT by using V in the format string, in order to make it depend
> on the highest value.

Beginning with 20.4, XEmacs does allow it.  You would use:

    (format "%*d" field-width line-number)

This is similar to how C's printf behaves.
From: Kent M Pitman
Subject: Re: padding function
Date: 
Message-ID: <sfwbtk53zub.fsf@world.std.com>
·····@mama.indstate.edu writes:

> I am in emacs and want to insert lines number line vi and then print the
> buffer. I want to right shift the code by max digits in max line number and a
> space. Then would want to be able to insert the numbers in the space right
> padded or leftpadded with spaces.

Since this actually sounds useful, I assume it's not for a class.
(Sigh.)

This is how you do it in CL:

(defun printem (min max)
  (check-type min (integer 1))
  (check-type max (integer 1))
  (loop for i from min to max do
     (fresh-line)
     (print-with-max i max)
     (terpri)))

(defun print-with-max (num max)
  (format t "~V,' D"
          (ceiling (log (1+ max) 10))
          num)))

(I did test this basic idea, then I changed it some for 
presentation--I hope I didn't break it in the process.)

I don't know the specifics of GNU Emacs Lisp I/O but presumably
if you just start with the ceiling computation there, you'll
be on the right track.  In English, the computation says that
the log base 10 of a number, rounded up (because there are no
fractional digits or columns), is a good approximation to the
width, except that you have to remember that (log 10 10) => 1,
(log 100 10) => 2, etc. and so it's off by one at the fenceposts,
which is what the 1+ accomodates.

I hope this is what you were asking.  If you were just asking 
the simple question of how to insert text in an emacs buffer or
how to concatenate two strings, I don't know the answer. :-)
From: ·····@mama.indstate.edu
Subject: Re: padding function
Date: 
Message-ID: <77fro7$211$1@nnrp1.dejanews.com>
1/12/99

Thank you all for reply.  The (format (format ..) ..) was easier for me though
other replies were educationaly helpful.


In article <···············@world.std.com>,
  Kent M Pitman <······@world.std.com> wrote:
> ·····@mama.indstate.edu writes:
>
> > I am in emacs and want to insert lines number line vi and then print the
> > buffer. I want to right shift the code by max digits in max line number and
a
> > space. Then would want to be able to insert the numbers in the space right
> > padded or leftpadded with spaces.
>
> Since this actually sounds useful, I assume it's not for a class.
> (Sigh.)
>
> This is how you do it in CL:
>
> (defun printem (min max)
>   (check-type min (integer 1))
>   (check-type max (integer 1))
>   (loop for i from min to max do
>      (fresh-line)
>      (print-with-max i max)
>      (terpri)))
>
> (defun print-with-max (num max)
>   (format t "~V,' D"
>           (ceiling (log (1+ max) 10))
>           num)))
>
> (I did test this basic idea, then I changed it some for
> presentation--I hope I didn't break it in the process.)
>
> I don't know the specifics of GNU Emacs Lisp I/O but presumably
> if you just start with the ceiling computation there, you'll
> be on the right track.  In English, the computation says that
> the log base 10 of a number, rounded up (because there are no
> fractional digits or columns), is a good approximation to the
> width, except that you have to remember that (log 10 10) => 1,
> (log 100 10) => 2, etc. and so it's off by one at the fenceposts,
> which is what the 1+ accomodates.
>
> I hope this is what you were asking.  If you were just asking
> the simple question of how to insert text in an emacs buffer or
> how to concatenate two strings, I don't know the answer. :-)
>

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own