如何在lisp函数中使用键盘宏,直到它失败

时间:2011-05-18 16:37:38

标签: emacs lisp

现在我已经定义并命名了一个键盘宏,我想创建一个lisp函数,它位于缓冲区的顶部,并执行:

i = 1
do{
    run macro
    if macro hit the end of the buffer, break out of the loop
    insert i
    i++
}while(true)

这是我的.emacs中的内容

(fset 'next-id
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([19 73 68 61 34 13 67108896 19 34 13 2 23] 0 "%d")) arg)))
(global-set-key (kbd "C-x n") 'next-id)

我该怎么做?

2 个答案:

答案 0 :(得分:3)

Srsly。以下是如何操作:C-u 0 F4

答案 1 :(得分:2)

这应该可以解决问题:

(defun apply-macro-to-buffer (&optional macro)
  "Apply last keyboard macro to the buffer"
  (interactive "CEnter the name of the macro to apply: ")
  (or macro
      (progn
        (if (null last-kbd-macro)
            (error "No keyboard macro has been defined"))
        (setq macro last-kbd-macro)))
  (let ((end-marker (copy-marker (point-max)))
        (i 1))
    (save-excursion
      (goto-char (point-min))
      (while (and  (< (point) end-marker))
        (let ((mark-active nil))
          (execute-kbd-macro macro))
        (insert (format "%d\n" i))
        (setq i (1+ i))))))

要对常规命令执行相同操作,请尝试以下操作:

(defun apply-command-to-buffer (command)
  "Apply a command to the buffer"
  (interactive "CEnter the name of the command to apply: ")
  (let ((end-marker (copy-marker (point-max)))
        (i 1))
    (save-excursion
      (goto-char (point-min))
      (while (and  (< (point) end-marker))
        (call-interactively command)
        (insert (format "%d\n" i))
        (setq i (1+ i))))))