在emacs中是否存在针对每个区域的应用命令?

Mit*_*ops 5 emacs elisp

我有一堆保存在orgmode文件中的链接,比如...

http://www.stackoverflow.com
http://www.google.com
http://www.github.com
Run Code Online (Sandbox Code Playgroud)

我可以通过将光标放在链接上并打开它来打开每一个C-c C-o,它可以方便地弹出我的默认浏览器并在选项卡中打开该链接.

现在假设我有20个这样的链接.是否有方便的方法将这样的函数应用于选定区域内的每一行,而不记录显式宏?

我想它看起来像......

Select region
M-x foreach-in-region
Keystrokes to apply to each line: C-c C-o
Run Code Online (Sandbox Code Playgroud)

这仅适用于已定义的功能.我想象没有的方式会像......

with cursor on first line of link
F3 # to start record macro
C-c C-o 
down arrow
F4
Select region (omitting the first line, since that's now already opened in my browser)
C-x C-k r
Run Code Online (Sandbox Code Playgroud)

这存在吗?如果没有,我会怎么做?

eve*_*_jr 8

您应该记录一行的宏,然后使用apply-macro-to-region-lines它来执行区域中的所有行. C-x C-k r

或者,您可以使用多个游标在每一行上创建一个游标并C-c C-o打开所有游标. multiple-cursors如果你给它机会,将会改变你的使用模式.


Dre*_*rew 5

(defun do-lines (fun &optional start end)
  "Invoke function FUN on the text of each line from START to END."
  (interactive
   (let ((fn   (intern (completing-read "Function: " obarray 'functionp t))))
     (if (use-region-p)
         (list fn (region-beginning) (region-end))
       (list fn (point-min) (point-max)))))
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (funcall fun (buffer-substring (line-beginning-position) (line-end-position)))
      (forward-line 1))))
Run Code Online (Sandbox Code Playgroud)

评论后更新 -

现在听起来你想要不输入一个函数名但是点击一个键,并将绑定到该键的命令应用于区域(或缓冲区)中的每一行.

像下面这样的东西会做到这一点.但是,请注意命令通常具有特定行为.例如,如果你要点击key C-k(kill-lines),它就会在它杀死的每一行之后向前移动.因为do-lines不知道你将调用什么类型的函数(命令),所以它会在每次调用后前进到下一行.因为这样的命令kill-lines会做错误的事情:它最终会推进两条线而不是一条线,从而跳过线.IOW,请注意,代码do-lines无法补偿它调用的特定函数可能与您期望的不一致.相反,它做了它所说的.

(defun do-lines (command &optional start end)
  "Invoke COMMAND on the text of each line from START to END."
  (interactive
   (let* ((key  (read-key-sequence-vector "Hit key sequence: "))
          (cmd  (lookup-key global-map key t)))
     (when (numberp cmd) (error "Not a valid key sequence"))
     (unless (commandp cmd) (error "Key `%s' is not defined" (key-description key)))
     (if (use-region-p)
         (list cmd (region-beginning) (region-end))
       (list cmd (point-min) (point-max)))))
  (setq start  (copy-marker start)
        end    (copy-marker end))
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (funcall command (buffer-substring (line-beginning-position) (line-end-position)))
      (forward-line 1))))
Run Code Online (Sandbox Code Playgroud)