Function: comment-padleft

comment-padleft is a byte-compiled function defined in newcomment.el.gz.

Signature

(comment-padleft STR &optional N)

Documentation

Construct a string composed of comment-padding plus STR.

It also adds N copies of the first 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
			   rpad "")))))) ;padding is not required

(defun comment-padleft (str &optional n)
  "Construct a string composed of `comment-padding' plus STR.
It also adds N copies of the first 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) (not (string= "" str)))
    ;; Only separate the left pad because we assume there is no right pad.
    (string-match "\\`\\s-*" str)
    (let ((s (substring str (match-end 0)))
	  (pad (concat (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
				    (min (- (match-end 0) (match-beginning 0))
                                         (length comment-padding))))
		       (match-string 0 str)))
	  (c (aref str (match-end 0)))	;the first non-space char of STR
	  ;; 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 pad (when multi (make-string n c)) s)
	;; Construct a regexp that would match anything from just S
	;; to any possible output of this function for any N.
	;; We match any number of leading spaces because this regexp will
	;; be used for uncommenting where we might want to remove
	;; uncomment markers with arbitrary leading space (because
	;; they were aligned).
	(concat "\\s-*"
		(if multi (concat (regexp-quote (string c)) "*"))
		(regexp-quote s))))))