Function: kmacro-menu-do-copy

kmacro-menu-do-copy is an interactive and byte-compiled function defined in kmacro.el.gz.

Signature

(kmacro-menu-do-copy)

Documentation

Duplicate macros in the region, those with markers, or the one at point.

Macros are duplicated at their current position in the macro ring.

If there's an active region, duplicate macros in the region; otherwise duplicate the marked macros or, if there are no marks, the macro on the current line.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/kmacro.el.gz
(defun kmacro-menu-do-copy ()
  "Duplicate macros in the region, those with markers, or the one at point.

Macros are duplicated at their current position in the macro ring.

If there's an active region, duplicate macros in the region; otherwise
duplicate the marked macros or, if there are no marks, the macro on the
current line."
  (declare (modes kmacro-menu-mode))
  (interactive nil kmacro-menu-mode)
  (kmacro-menu--query-revert)
  (let* ((region-exists (use-region-p))
         (mark-exists (kmacro-menu--marks-exist-p))
         (id-alist (if (or region-exists
                           (not mark-exists))
                       (let ((region-alist))
                         (kmacro-menu--do-region
                          (lambda (id)
                            (push (cons (kmacro-menu--id-position id)
                                        t)
                                  region-alist)))
                         region-alist)
                     kmacro-menu--marks))
         (num-duplicates 0))
    (let ((res))
      (kmacro-menu--map-ids (lambda (id)
                              (let ((pos (kmacro-menu--id-position id))
                                    (km (kmacro-menu--id-kmacro id)))
                                (push km res)
                                (when (alist-get pos id-alist)
                                  (push km res)
                                  (setq num-duplicates (1+ num-duplicates))))))
      ;; Confirm the action if we operated on marks or the region, but
      ;; don't confirm if operating on a single line without a region.
      (when (if (or mark-exists region-exists)
                (yes-or-no-p (if (= 1 num-duplicates)
                                 "Copy (duplicate) 1 keyboard macro?"
                               (format "Copy (duplicate) %d keyboard macros?"
                                       num-duplicates)))
              t)
        (kmacro-menu--replace-all (nreverse res))
        (tabulated-list-revert)))))