From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Ben Wing" Newsgroups: gmane.emacs.devel Subject: Re: API changes Date: Fri, 17 May 2002 05:30:50 -0700 Sender: emacs-devel-admin@gnu.org Message-ID: <013e01c1fd9e$af600450$772efea9@neeeeeee> References: <3CCAA9FF.4000905@666.com> <200204290505.g3T556106038@aztec.santafe.edu> <1020493576.5286.5.camel@space-ghost> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: main.gmane.org 1021638625 6082 127.0.0.1 (17 May 2002 12:30:25 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 17 May 2002 12:30:25 +0000 (UTC) Cc: , Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.33 #1 (Debian)) id 178gsL-0001Zz-00 for ; Fri, 17 May 2002 14:30:25 +0200 Original-Received: from fencepost.gnu.org ([199.232.76.164]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 178h4p-0000mU-00 for ; Fri, 17 May 2002 14:43:19 +0200 Original-Received: from localhost ([127.0.0.1] helo=fencepost.gnu.org) by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian)) id 178gsR-00073a-00; Fri, 17 May 2002 08:30:31 -0400 Original-Received: from cpimssmtpu12.email.msn.com ([207.46.181.87]) by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian)) id 178gqx-0006rQ-00 for ; Fri, 17 May 2002 08:29:00 -0400 Original-Received: from neeeeeee ([63.11.141.186]) by cpimssmtpu12.email.msn.com with Microsoft SMTPSVC(5.0.2195.4905); Fri, 17 May 2002 05:28:15 -0700 Original-To: "Colin Walters" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-OriginalArrivalTime: 17 May 2002 12:28:16.0626 (UTC) FILETIME=[520BF520:01C1FD9E] Errors-To: emacs-devel-admin@gnu.org X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.0.9 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.devel:4058 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:4058 ----- Original Message ----- From: "Colin Walters" To: Cc: ; 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 > >