+ (save-match-data
+ (let ((beg (progn (goto-char beg) (line-beginning-position)))
+ indent)
+ (goto-char beg)
+ (while (re-search-forward "^\\s-*[[:print:]]" end t)
+ (setq indent (min (or indent (current-indentation))
+ (current-indentation))))
+ indent))))
+
+(defvar indent-rigidly-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map [left] 'indent-rigidly-left)
+ (define-key map [right] 'indent-rigidly-right)
+ (define-key map [S-left] 'indent-rigidly-left-to-tab-stop)
+ (define-key map [S-right] 'indent-rigidly-right-to-tab-stop)
+ map)
+ "Transient keymap for adjusting indentation interactively.
+It is activated by calling `indent-rigidly' interactively.")
+
+(defun indent-rigidly (start end arg &optional interactive)
+ "Indent all lines starting in the region.
+If called interactively with no prefix argument, activate a
+transient mode in which the indentation can be adjusted interactively
+by typing \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop].
+Typing any other key deactivates the transient mode.
+
+If called from a program, or interactively with prefix ARG,
+indent all lines starting in the region forward by ARG columns.
+If called from a program, START and END specify the beginning and
+end of the text to act on, in place of the region.
+
+Negative values of ARG indent backward, so you can remove all
+indentation by specifying a large negative ARG."
+ (interactive "r\nP\np")
+ (if (and (not arg) interactive)
+ (progn
+ (message
+ (substitute-command-keys
+ "Indent region with \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop]."))
+ (set-transient-map indent-rigidly-map t))
+ (save-excursion
+ (goto-char end)
+ (setq end (point-marker))
+ (goto-char start)
+ (or (bolp) (forward-line 1))
+ (while (< (point) end)
+ (let ((indent (current-indentation))
+ eol-flag)
+ (save-excursion
+ (skip-chars-forward " \t")
+ (setq eol-flag (eolp)))
+ (or eol-flag
+ (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
+ (delete-region (point) (progn (skip-chars-forward " \t") (point))))
+ (forward-line 1))
+ (move-marker end nil)
+ ;; Keep the active region in transient mode.
+ (when (eq (cadr overriding-terminal-local-map) indent-rigidly-map)
+ (setq deactivate-mark nil)))))
+
+(defun indent-rigidly--pop-undo ()
+ (and (memq last-command '(indent-rigidly-left indent-rigidly-right
+ indent-rigidly-left-to-tab-stop
+ indent-rigidly-right-to-tab-stop))
+ (consp buffer-undo-list)
+ (eq (car buffer-undo-list) nil)
+ (pop buffer-undo-list)))
+
+(defun indent-rigidly-left (beg end)
+ "Indent all lines between BEG and END leftward by one space."
+ (interactive "r")
+ (indent-rigidly--pop-undo)
+ (indent-rigidly
+ beg end
+ (if (eq (current-bidi-paragraph-direction) 'right-to-left) 1 -1)))
+
+(defun indent-rigidly-right (beg end)
+ "Indent all lines between BEG and END rightward by one space."
+ (interactive "r")
+ (indent-rigidly--pop-undo)
+ (indent-rigidly
+ beg end
+ (if (eq (current-bidi-paragraph-direction) 'right-to-left) -1 1)))
+
+(defun indent-rigidly-left-to-tab-stop (beg end)
+ "Indent all lines between BEG and END leftward to a tab stop."
+ (interactive "r")
+ (indent-rigidly--pop-undo)
+ (let* ((current (indent-rigidly--current-indentation beg end))
+ (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
+ (next (indent-next-tab-stop current (if rtl nil 'prev))))
+ (indent-rigidly beg end (- next current))))
+
+(defun indent-rigidly-right-to-tab-stop (beg end)
+ "Indent all lines between BEG and END rightward to a tab stop."
+ (interactive "r")
+ (indent-rigidly--pop-undo)
+ (let* ((current (indent-rigidly--current-indentation beg end))
+ (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
+ (next (indent-next-tab-stop current (if rtl 'prev))))
+ (indent-rigidly beg end (- next current))))