+ ((eq def 'edit)
+ (let (executing-kbd-macro defining-kbd-macro)
+ (recursive-edit)))
+ ((eq def 'quit)
+ (setq quit-flag t))
+ (t
+ (or (eq def 'help)
+ (ding))
+ (with-output-to-temp-buffer "*Help*"
+ (princ
+ (substitute-command-keys
+ "Specify how to proceed with keyboard macro execution.
+Possibilities: \\<query-replace-map>
+\\[act] Finish this iteration normally and continue with the next.
+\\[skip] Skip the rest of this iteration, and start the next.
+\\[exit] Stop the macro entirely right now.
+\\[recenter] Redisplay the screen, then ask again.
+\\[edit] Enter recursive edit; ask again when you exit from that."))
+ (save-excursion
+ (set-buffer standard-output)
+ (help-mode)))))))))))
+
+;;;###autoload
+(defun apply-macro-to-region-lines (top bottom &optional macro)
+ "Apply last keyboard macro to all lines in the region.
+For each line that begins in the region, move to the beginning of
+the line, and run the last keyboard macro.
+
+When called from lisp, this function takes two arguments TOP and
+BOTTOM, describing the current region. TOP must be before BOTTOM.
+The optional third argument MACRO specifies a keyboard macro to
+execute.
+
+This is useful for quoting or unquoting included text, adding and
+removing comments, or producing tables where the entries are regular.
+
+For example, in Usenet articles, sections of text quoted from another
+author are indented, or have each line start with `>'. To quote a
+section of text, define a keyboard macro which inserts `>', put point
+and mark at opposite ends of the quoted section, and use
+`\\[apply-macro-to-region-lines]' to mark the entire section.
+
+Suppose you wanted to build a keyword table in C where each entry
+looked like this:
+
+ { \"foo\", foo_data, foo_function },
+ { \"bar\", bar_data, bar_function },
+ { \"baz\", baz_data, baz_function },
+
+You could enter the names in this format:
+
+ foo
+ bar
+ baz
+
+and write a macro to massage a word into a table entry:
+
+ \\C-x (
+ \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
+ \\C-x )
+
+and then select the region of un-tablified names and use
+`\\[apply-macro-to-region-lines]' to build the table from the names."
+ (interactive "r")
+ (or macro
+ (progn
+ (if (null last-kbd-macro)
+ (error "No keyboard macro has been defined"))
+ (setq macro last-kbd-macro)))
+ (save-excursion
+ (let ((end-marker (copy-marker bottom))
+ next-line-marker)
+ (goto-char top)
+ (if (not (bolp))
+ (forward-line 1))
+ (setq next-line-marker (point-marker))
+ (while (< next-line-marker end-marker)
+ (goto-char next-line-marker)
+ (save-excursion
+ (forward-line 1)
+ (set-marker next-line-marker (point)))
+ (save-excursion
+ (let ((mark-active nil))
+ (execute-kbd-macro (or macro last-kbd-macro)))))
+ (set-marker end-marker nil)
+ (set-marker next-line-marker nil))))