Function: comment-padright
comment-padright is a byte-compiled function defined in
newcomment.el.gz.
Signature
(comment-padright STR &optional N)
Documentation
Construct a string composed of STR plus comment-padding.
It also adds N copies of the last non-whitespace chars of STR.
If STR already contains padding, the corresponding amount is
ignored from comment-padding.
N defaults to 0.
If N is re, a regexp is returned instead, that would match
the string for any N.
Ensure that comment-normalize-vars has been called before you use this.
Source Code
;; Defined in /usr/src/emacs/lisp/newcomment.el.gz
(defun comment-padright (str &optional n)
"Construct a string composed of STR plus `comment-padding'.
It also adds N copies of the last non-whitespace chars of STR.
If STR already contains padding, the corresponding amount is
ignored from `comment-padding'.
N defaults to 0.
If N is `re', a regexp is returned instead, that would match
the string for any N.
Ensure that `comment-normalize-vars' has been called before you use this."
(setq n (or n 0))
(when (and (stringp str) (string-match "\\S-" str))
;; Separate the actual string from any leading/trailing padding
(string-match "\\`\\s-*\\(.*?\\)\\s-*\\'" str)
(let ((s (match-string 1 str)) ;actual string
(lpad (substring str 0 (match-beginning 1))) ;left padding
(rpad (concat
(substring str (match-end 1)) ;original right padding
(if (numberp comment-padding)
(make-string (min comment-padding
(- (match-end 0) (match-end 1)))
?\s)
(if (not (string-match-p "\\`\\s-" comment-padding))
;; If the padding isn't spaces, then don't
;; shorten the padding.
comment-padding
(substring comment-padding ;additional right padding
(min (- (match-end 0) (match-end 1))
(length comment-padding)))))))
;; We can only duplicate C if the comment-end has multiple chars
;; or if comments can be nested, else the comment-end `}' would
;; be turned into `}}}' where only the first ends the comment
;; and the rest becomes bogus junk.
(multi (not (and comment-quote-nested
;; comment-end is a single char
(string-match "\\`\\s-*\\S-\\s-*\\'" comment-end)))))
(if (not (symbolp n))
(concat lpad s (when multi (make-string n (aref str (1- (match-end 1))))) rpad)
;; construct a regexp that would match anything from just S
;; to any possible output of this function for any N.
(concat (mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
lpad "") ;padding is not required
(regexp-quote s)
(when multi "+") ;the last char of S might be repeated
(mapconcat (lambda (c) (concat (regexp-quote (string c)) "?"))
rpad "")))))) ;padding is not required