[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: API changes

From: Ben Wing
Subject: Re: API changes
Date: Fri, 17 May 2002 05:30:50 -0700

----- Original Message -----
From: "Colin Walters" <address@hidden>
To: <address@hidden>
Cc: <address@hidden>; <address@hidden>
Sent: Friday, May 03, 2002 11:26 PM
Subject: Re: API changes

> On Mon, 2002-04-29 at 01:05, Richard Stallman wrote:
> > I think it would be a cleaner interface to add an optional way to
> > specify an ellipsis in truncate-string-to-width.
> Have we come to any conclusion on this issue?  In this case I think
> Richard is right in that adding to `truncate-string-to-width' would be
> better.
> Regardless, I would definitely like to have this functionality available
> in both Emacs and XEmacs.

OK, i agree with the change and I made it.  I'm attaching the new code.  I'm
willing to sign papers for this if someone tells me how I go about doing
that -- getting the forms, etc.


(defun truncate-string-to-width (str end-column &optional start-column
  "Truncate string STR to end at column END-COLUMN.
The optional 3rd arg START-COLUMN, if non-nil, specifies
the starting column; that means to return the characters occupying

The optional 4th arg PADDING, if non-nil, specifies a padding character
to add at the end of the result if STR doesn't reach column END-COLUMN,
or if END-COLUMN comes in the middle of a character in STR.
PADDING is also added at the beginning of the result
if column START-COLUMN appears in the middle of a character in STR.

If PADDING is nil, no padding is added in these cases, so
the resulting string may be narrower than END-COLUMN.

BUG: Currently assumes that the padding character is of width one.  You
will get weird results if not.

If ELLIPSES is non-nil, add ellipses (specified by ELLIPSES if a string,
else `...') if STR extends past END-COLUMN.  The ellipses will be added in
such a way that the total string occupies no more than END-COLUMN columns
-- i.e. if the string goes past END-COLUMN, it will be truncated somewhere
short of END-COLUMN so that, with the ellipses added (and padding, if the
proper place to truncate the string would be in the middle of a character),
the string occupies exactly END-COLUMN columns."
  (or start-column
      (setq start-column 0))
  (let ((len (length str))
 (idx 0)
 (column 0)
 (head-padding "") (tail-padding "")
 ch last-column last-idx from-idx)

    ;; find the index of START-COLUMN; bail out if end of string reached.
    (condition-case nil
 (while (< column start-column)
   (setq ch (aref str idx)
  column (+ column (char-width ch))
  idx (1+ idx)))
      (args-out-of-range (setq idx len)))
    (if (< column start-column)
 ;; if string ends before START-COLUMN, return either a blank string
 ;; or a string entirely padded.
 (if padding (make-string (- end-column start-column) padding) "")
      (if (and padding (> column start-column))
   (setq head-padding (make-string (- column start-column) padding)))
      (setq from-idx idx)
      ;; If END-COLUMN is before START-COLUMN, then bail out.
      (if (< end-column column)
   (setq idx from-idx ellipses "")

 ;; handle ELLIPSES
 (cond ((null ellipses) (setq ellipses ""))
       ((if (<= (string-width str) end-column)
     ;; string fits, no ellipses
     (setq ellipses "")))
        ;; else, insert default value and ...
        (or (stringp ellipses) (setq ellipses "..."))
        ;; ... take away the width of the ellipses from the
        ;; destination.  do all computations with new, shorter
        ;; width.  the padding computed will get us exactly up to
        ;; the shorted width, which is right -- it just gets added
        ;; to the right of the ellipses.
      (setq end-column (- end-column (string-width ellipses)))))

 ;; find the index of END-COLUMN; bail out if end of string reached.
 (condition-case nil
     (while (< column end-column)
       (setq last-column column
      last-idx idx
      ch (aref str idx)
      column (+ column (char-width ch))
      idx (1+ idx)))
   (args-out-of-range (setq idx len)))
 ;; if we went too far (stopped in middle of character), back up.
 (if (> column end-column)
     (setq column last-column idx last-idx))
 ;; compute remaining padding
 (if (and padding (< column end-column))
     (setq tail-padding (make-string (- end-column column) padding))))
      ;; get substring ...
      (setq str (substring str from-idx idx))
      ;; and construct result
      (if padding
   (concat head-padding str tail-padding ellipses)
 (concat str ellipses)))))

> _______________________________________________
> Emacs-devel mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/emacs-devel

reply via email to

[Prev in Thread] Current Thread [Next in Thread]