unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ben Wing" <ben@666.com>
Cc: <emacs-devel@gnu.org>, <xemacs-design@xemacs.org>
Subject: Re: API changes
Date: Fri, 17 May 2002 05:30:50 -0700	[thread overview]
Message-ID: <013e01c1fd9e$af600450$772efea9@neeeeeee> (raw)
In-Reply-To: 1020493576.5286.5.camel@space-ghost


----- Original Message -----
From: "Colin Walters" <walters@verbum.org>
To: <ben@666.com>
Cc: <emacs-devel@gnu.org>; <xemacs-design@xemacs.org>
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.

ben

(defun truncate-string-to-width (str end-column &optional start-column
padding
         ellipses)
  "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
columns START-COLUMN ... END-COLUMN of STR.

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 "")))
       (t
        ;; 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
> Emacs-devel@gnu.org
> http://mail.gnu.org/mailman/listinfo/emacs-devel
>
>

  parent reply	other threads:[~2002-05-17 12:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3CCAA9FF.4000905@666.com>
2002-04-28  0:58 ` API changes Daniel Pittman
2002-04-28  5:48 ` Colin Walters
2002-04-28 23:45 ` Stefan Monnier
2002-04-29  5:05 ` Richard Stallman
2002-05-04  6:26   ` Colin Walters
     [not found]   ` <1020493576.5286.5.camel@space-ghost>
2002-05-17 12:30     ` Ben Wing [this message]
2002-05-18 18:49       ` Richard Stallman
2002-05-18 19:54       ` Colin Walters
     [not found]         ` <200205191940.g4JJeij24049@aztec.santafe.edu>
2002-05-19 20:09           ` Colin Walters
2002-05-20  3:07             ` Stephen J. Turnbull
     [not found] ` <1019972901.27106.63.camel@space-ghost>
2002-04-29  5:05   ` Richard Stallman
2002-04-29  6:25     ` Colin Walters
2002-04-27 13:39 Ben Wing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='013e01c1fd9e$af600450$772efea9@neeeeeee' \
    --to=ben@666.com \
    --cc=emacs-devel@gnu.org \
    --cc=xemacs-design@xemacs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).