aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2020-12-21 20:01:28 +0100
committerLars Ingebrigtsen2020-12-21 20:01:28 +0100
commitb3dec3176673fa99e57e3916b36ea4367d47c0fa (patch)
treee9a6a6b6926a9abd4c93138ed6e7acb84438a318
parent87e422f1044068a4d27e5e4bfdbc664d9e4bbc43 (diff)
downloademacs-b3dec3176673fa99e57e3916b36ea4367d47c0fa.tar.gz
emacs-b3dec3176673fa99e57e3916b36ea4367d47c0fa.zip
Add `string-pad'
* doc/lispref/strings.texi (Creating Strings): Document it. * lisp/emacs-lisp/shortdoc.el (string): Add example. * lisp/emacs-lisp/subr-x.el (string-pad): New function.
-rw-r--r--doc/lispref/strings.texi9
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/emacs-lisp/shortdoc.el4
-rw-r--r--lisp/emacs-lisp/subr-x.el20
-rw-r--r--test/lisp/emacs-lisp/subr-x-tests.el6
5 files changed, 40 insertions, 1 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index e4ca2617512..958ae4c0a15 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -419,6 +419,15 @@ Split @var{string} into a list of strings on newline boundaries. If
419@var{omit-nulls}, remove empty lines from the results. 419@var{omit-nulls}, remove empty lines from the results.
420@end defun 420@end defun
421 421
422@defun string-pad string length &optional padding
423Pad @var{string} to the be of @var{length} using @var{padding} as the
424padding character (defaulting to the space character). If
425@var{string} is shorter than @var{length}, no padding is done. If
426@var{length} is positive, the padding is done to the end of the
427string, and if it's negative, to the start of the string (using the
428absolute value).
429@end defun
430
422@node Modifying Strings 431@node Modifying Strings
423@section Modifying Strings 432@section Modifying Strings
424@cindex modifying strings 433@cindex modifying strings
diff --git a/etc/NEWS b/etc/NEWS
index 17c6ce61f94..9b4fcd92fc1 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1443,7 +1443,7 @@ that makes it a valid button.
1443+++ 1443+++
1444*** A number of new string manipulation functions have been added. 1444*** A number of new string manipulation functions have been added.
1445'string-clean-whitespace', 'string-fill', 'string-limit', 1445'string-clean-whitespace', 'string-fill', 'string-limit',
1446'string-limit' and 'slice-string'. 1446'string-limit', 'string-pad' and 'slice-string'.
1447 1447
1448+++ 1448+++
1449*** New variable 'current-minibuffer-command'. 1449*** New variable 'current-minibuffer-command'.
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index 8b11b57ff7f..3e1476adfc1 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -131,6 +131,10 @@ There can be any number of :example/:result elements."
131 (mapconcat 131 (mapconcat
132 :eval (mapconcat (lambda (a) (concat "[" a "]")) 132 :eval (mapconcat (lambda (a) (concat "[" a "]"))
133 '("foo" "bar" "zot") " ")) 133 '("foo" "bar" "zot") " "))
134 (string-pad
135 :eval (string-pad "foo" 5)
136 :eval (string-pad "foobar" 5)
137 :eval (string-pad "foo" -5 ?-))
134 (mapcar 138 (mapcar
135 :eval (mapcar #'identity "123")) 139 :eval (mapcar #'identity "123"))
136 (format 140 (format
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 41a20795378..250ba6e6fa2 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -317,6 +317,26 @@ The boundaries that match REGEXP are not omitted from the results."
317 (push (substring string start-substring) result) 317 (push (substring string start-substring) result)
318 (nreverse result)))) 318 (nreverse result))))
319 319
320(defun string-pad (string length &optional padding)
321 "Pad STRING to LENGTH using PADDING.
322If PADDING is nil, the space character is used. If not nil, it
323should be a character.
324
325If STRING is longer than the absolute value of LENGTH, no padding
326is done.
327
328If LENGTH is positive, the padding is done to the end of the
329string, and if it's negative, padding is done to the start of the
330string."
331 (if (> (length string) (abs length))
332 string
333 (let ((pad-length (- (abs length) (length string))))
334 (concat (and (< length 0)
335 (make-string pad-length (or padding ?\s)))
336 string
337 (and (> length 0)
338 (make-string pad-length (or padding ?\s)))))))
339
320(defun replace-region-contents (beg end replace-fn 340(defun replace-region-contents (beg end replace-fn
321 &optional max-secs max-costs) 341 &optional max-secs max-costs)
322 "Replace the region between BEG and END using REPLACE-FN. 342 "Replace the region between BEG and END using REPLACE-FN.
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el b/test/lisp/emacs-lisp/subr-x-tests.el
index 949bbb163eb..94ff459869c 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -608,5 +608,11 @@
608 (should (equal (slice-string "-foo-bar-" "-") '("-foo" "-bar" "-"))) 608 (should (equal (slice-string "-foo-bar-" "-") '("-foo" "-bar" "-")))
609 (should (equal (slice-string "ooo" "lala") '("ooo")))) 609 (should (equal (slice-string "ooo" "lala") '("ooo"))))
610 610
611(ert-deftest subr-string-pad ()
612 (should (equal (string-pad "foo" 5) "foo "))
613 (should (equal (string-pad "foo" 5 ?-) "foo--"))
614 (should (equal (string-pad "foo" -5 ?-) "--foo"))
615 (should (equal (string-pad "foo" 2 ?-) "foo")))
616
611(provide 'subr-x-tests) 617(provide 'subr-x-tests)
612;;; subr-x-tests.el ends here 618;;; subr-x-tests.el ends here