如何在 Lisp 函数中使用键盘宏直到失败

发布于 2024-11-08 17:36:23 字数 490 浏览 0 评论 0原文

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

我将如何处理这个问题?

right now I have a keyboard macro defined and named, and i want to make a lisp function which goes to the top of the buffer, and does:

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

here's what's in my .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)

how would I go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-11-15 17:36:23

先生。操作方法如下:Cu 0 F4

Srsly. Here's how to do it: C-u 0 F4

记忆消瘦 2024-11-15 17:36:23

这应该可以解决问题:

(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))))))

This should do the trick:

(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))))))

To do the same for a regular command, try this:

(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))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文