Posts Tagged ‘multiple commands’

Gaz asks:

Is there an easy way to select (via buffer-name regex or similar) a subset of terminals in elisp, and then squirt the same command into each one?

Sure Gaz, that sounds fairly straightforward. The shell wrappers already stores the terminal buffers in sw-buffers. We just need to think of how we want to select the buffers. And it probably doesn’t come as any surprise that I’m going to use ido.

Ido provides regex matching by pressing C-t or defining ido-enable-regexp. It also has flex matching with ido-enable-flex-matching and you can continuously refine selections using C-SPC. It really is amazingly cool.

Surprisingly, I couldn’t find a straightforward way of accessing the interim matches. Nor is there a hook that executes at selection time. Instead, we will override RETURN. Dynamic scoping means we can define an effectively global variable, sw-buffer-matches to store the interim matches for later use. Then, we iterate over these matches and send the command using term-simple-send.

We only want RETURN to be overridden for this particular usage of ido, so we set the keymap on the ido-setup-hook.

(defun sw-ido-save-matches ()
  (setq sw-buffer-matches ido-matches)

(defun sw-ido-my-keys ()
  (define-key ido-completion-map (kbd "RET") 'sw-ido-save-matches))

(defun sw-multi-cmd (&optional cmd)
  (interactive "sCommand: ")
  (let ((ido-setup-hook 'sw-ido-my-keys)
        (sw-buffer-matches ""))
    (ido-completing-read "choose buffers: " sw-buffers)
    (dolist (buffer sw-buffer-matches)
      (term-simple-send (sw-shell-get-process buffer) cmd))))

Read Full Post »


Get every new post delivered to your Inbox.