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

Emacs 如何在lisp函数中使用键盘宏,直到它失败,emacs,lisp,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." (

现在我已经定义并命名了一个键盘宏,我想创建一个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)

我该怎么做呢?

这应该可以做到:

(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))))))
(取消将宏应用到缓冲区(&可选宏)
“将最后一个键盘宏应用于缓冲区”
(交互式“将要应用的宏的名称居中:”)
(或宏)
(项目
(如果(最后一个kbd宏为null)
(错误“未定义键盘宏”))
(setq宏最后一个kbd宏)))
(let((结束标记(复制标记(最大点)))
(一)
(省去远足
(转到字符(最小点))
(while(和(<(点)结束标记))
(let((标记为活动零))
(执行kbd宏)
(插入(格式“%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))))))
(defun将命令应用到缓冲区(命令)
“将命令应用于缓冲区”
(交互式“将要应用的命令的名称居中:”)
(let((结束标记(复制标记(最大点)))
(一)
(省去远足
(转到字符(最小点))
(while(和(<(点)结束标记))
(以交互方式调用命令)
(插入(格式“%d\n”i))
(setq i(1+i()()))

这应该可以做到:

(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))))))
(取消将宏应用到缓冲区(&可选宏)
“将最后一个键盘宏应用于缓冲区”
(交互式“将要应用的宏的名称居中:”)
(或宏)
(项目
(如果(最后一个kbd宏为null)
(错误“未定义键盘宏”))
(setq宏最后一个kbd宏)))
(let((结束标记(复制标记(最大点)))
(一)
(省去远足
(转到字符(最小点))
(while(和(<(点)结束标记))
(let((标记为活动零))
(执行kbd宏)
(插入(格式“%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))))))
(defun将命令应用到缓冲区(命令)
“将命令应用于缓冲区”
(交互式“将要应用的命令的名称居中:”)
(let((结束标记(复制标记(最大点)))
(一)
(省去远足
(转到字符(最小点))
(while(和(<(点)结束标记))
(以交互方式调用命令)
(插入(格式“%d\n”i))
(setq i(1+i()()))

Srsly。下面是操作方法:
C-u0f4

Srsly。下面是操作方法:
C-u 0 F4

当您到达缓冲区的末尾时会发生什么?它只是不断地添加数字?我希望它在宏到达缓冲区的末尾时停止添加数字,这样伪代码实际上就不完全正确了当您到达缓冲区的末尾时会发生什么?它只是不断地添加数字?我希望它在宏到达缓冲区末尾时停止添加数字,这样伪代码实际上就不完全正确了。这不太正确。宏应该删除一些文本,但在这里使用时,它似乎只是在它通常删除的文本之前插入文本。而且它从2开始,而不是从2开始1@Bwmat我不认为你的宏做了你认为它做的。代码在递增之前会清楚地插入
i
。可能宏正在步进插入的文本。注意:
insert
函数将点移动到插入文本的末尾。哦,我知道发生了什么,最后一个kbd宏不是我想要使用的宏。我有一个要使用的命名宏“next id”,如何使用该宏调用此函数?(编辑缓冲区时)@Bwmat我添加了一个宏名称提示。注意:它会提示输入任何命令名,我不认为有办法将其限制为仅使用键盘宏。这不太合适。宏应该删除一些文本,但在这里使用时,它似乎只是在它通常删除的文本之前插入文本。而且它从2开始,而不是从2开始1@Bwmat我不认为你的宏做了你认为它做的。代码在递增之前会清楚地插入
i
。可能宏正在步进插入的文本。注意:
insert
函数将点移动到插入文本的末尾。哦,我知道发生了什么,最后一个kbd宏不是我想要使用的宏。我有一个要使用的命名宏“next id”,如何使用该宏调用此函数?(编辑缓冲区时)@Bwmat我添加了一个宏名称提示。注意:它会提示输入任何命令名,我认为没有办法将其限制为键盘宏。Emacs中“无限参数”的一些背景:Emacs中“无限参数”的一些背景: