 ;; rectangles have lines of varying lengths.
 ;; http://emacs.stackexchange.com/a/3661/115
 
+(defvar modi/extend-rectangle--added-whitespace-boundaries '()
+  "Alist to hold the (start . end) positions of the white space added
+to the last line of the rectangle by `modi/extend-rectangle--core'.")
+
 ;; Below is inspired from the `rectangle-utils' package.
 ;; https://github.com/thierryvolpiatto/rectangle-utils
 (defun modi/extend-rectangle--core (beg end)


Similar rectangle extension behavior is applied when using the
prefix \\[universal-argument] with similarly advised functions too."
  (if (eq 4 (prefix-numeric-value current-prefix-arg)) ; when using C-u
      (let* ((beg (nth 0 args))
             (end (nth 1 args)))
        ;; Override the original END argument
        (setcar (cdr args) (modi/extend-rectangle--core beg end))
        (apply orig-fun args))
    (apply orig-fun args)))

(dolist (fn modi/extend-rectangle-fns)
  (advice-add fn :around #'modi/advice-select-rectangle-to-end)

          (when (> len max-len)
            (setq max-len len))
          (forward-line 1)))
      ;; Pad the last line with spaces if it's not the longest
      (let* ((last-line-num (1- num-lines))
             (line-end-col (nth 1 (assoc last-line-num line-info)))
             (len (- line-end-col rect-left-col))
             (diff (- max-len len)))
        (when (< len max-len)
          (forward-line last-line-num) ; go to the last line of the rectangle
          (goto-char (line-end-position))
          (let ((inhibit-read-only t)) ; ignore read-only status of buffer
            (insert (make-string diff ? )))
          (setq end (line-end-position))
          (setcdr modi/extend-rectangle--added-whitespace-boundaries (list end))))
      ;; (message "After: end=%0d" end)
      end)))

