1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-21 18:23:59 +00:00

(truncate-string-to-width):

Rename arg WIDTH to END-COLUMN.  Fix the case when START-COLUMN
is after END-COLUMN.  Doc fixes.
This commit is contained in:
Richard M. Stallman 1997-09-13 08:44:55 +00:00
parent a3788d5332
commit be9778f8de

View File

@ -73,12 +73,20 @@ TYPE should be `list' or `vector'."
string)) string))
;;;###autoload ;;;###autoload
(defun truncate-string-to-width (str width &optional start-column padding) (defun truncate-string-to-width (str end-column &optional start-column padding)
"Truncate string STR to fit in WIDTH columns. "Truncate string STR to end at column END-COLUMN.
Optional 1st arg START-COLUMN if non-nil specifies the starting column. The optional 2nd arg START-COLUMN, if non-nil, specifies
Optional 2nd arg PADDING if non-nil is a padding character to be padded at the starting column; that means to return the characters occupying
the head and tail of the resulting string to fit in WIDTH if necessary. columns START-COLUMN ... END-COLUMN of STR.
If PADDING is nil, the resulting string may be narrower than WIDTH."
The optional 3rd 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 fo 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."
(or start-column (or start-column
(setq start-column 0)) (setq start-column 0))
(let ((len (length str)) (let ((len (length str))
@ -93,22 +101,24 @@ If PADDING is nil, the resulting string may be narrower than WIDTH."
idx (+ idx (char-bytes ch)))) idx (+ idx (char-bytes ch))))
(args-out-of-range (setq idx len))) (args-out-of-range (setq idx len)))
(if (< column start-column) (if (< column start-column)
(if padding (make-string width padding) "") (if padding (make-string end-column padding) "")
(if (and padding (> column start-column)) (if (and padding (> column start-column))
(setq head-padding (make-string (- column start-column) ?\ ))) (setq head-padding (make-string (- column start-column) ?\ )))
(setq from-idx idx) (setq from-idx idx)
(condition-case nil (if (< end-column column)
(while (< column width) (setq idx from-idx)
(setq last-column column (condition-case nil
last-idx idx (while (< column end-column)
ch (sref str idx) (setq last-column column
column (+ column (char-width ch)) last-idx idx
idx (+ idx (char-bytes ch)))) ch (sref str idx)
(args-out-of-range (setq idx len))) column (+ column (char-width ch))
(if (> column width) idx (+ idx (char-bytes ch))))
(setq column last-column idx last-idx)) (args-out-of-range (setq idx len)))
(if (and padding (< column width)) (if (> column end-column)
(setq tail-padding (make-string (- width column) padding))) (setq column last-column idx last-idx))
(if (and padding (< column end-column))
(setq tail-padding (make-string (- end-column column) padding))))
(setq str (substring str from-idx idx)) (setq str (substring str from-idx idx))
(if padding (if padding
(concat head-padding str tail-padding) (concat head-padding str tail-padding)