0

Ho can I run an eshell command in another window keeping focus in the working one?

Let's say I'm scanning a buffer searching for a regexp (to be precise an URL) and every time I find it I want to run a command and see the output in another buffer but keeping the focus on the working one.

I'm working on this piece of code:

(while (search-forward-regexp "\\\\\\(?:href\\|url\\){\\([^}]+\\)}" nil t)
  (save-excursion
    (let ((URL (match-string 1))
      (kill-buffer-query-functions nil))

      (eshell-command
       (concat
    "lynx -cookies=1 -accept_all_cookies=1 -cookie_file=cookie.file "
    URL))
      ;; I need to kill the buffer opened by the previous command
      (kill-buffer "*lynx*")
      )))
Drew
  • 75,699
  • 9
  • 109
  • 225
Gabriele Nicolardi
  • 1,199
  • 8
  • 17
  • In the alternative answer to the linked question, I wrote-up a modification of Eshell that permits a user to submit commands under-the-hood. I submitted a feature request to the Emacs team, but never heard back. It may need some modification for circumstances I am unaware of as of yet, but I used that solution previously for certain things. https://emacs.stackexchange.com/questions/7617/how-to-programmatically-execute-a-command-in-eshell/7625#7625 You may also wish to consider using the built-in `compile` command if you don't need an Eshell buffer per se -- e.g., `(compile "date")`. – lawlist Dec 10 '18 at 00:35
  • Just in case you are not already familiar with the macro `with-current-buffer`, you can use that to do stuff with another buffer .... – lawlist Dec 10 '18 at 00:42
  • @lawlist I found a way to get my purpose using `with-temp-buffer`. If you will post an answer showing the use of `with-current-buffer` I have pleasure to accept it. Otherwise I will post my own solution for the sake of completeness. – Gabriele Nicolardi Dec 10 '18 at 13:47
  • I would recommend posting your own answer so that the forum participants can see your use-case. – lawlist Dec 10 '18 at 14:50

1 Answers1

1

Here I post a solution to my issue using with-temp-buffer macro:

(save-excursion
  (delete-other-windows)
  (split-window-below)
  (goto-char (point-min))

  (while (search-forward-regexp "\\\\\\(?:href\\|url\\){\\([^}]+\\)}" nil t)
    (save-excursion
      (let ((URL (match-string 1))
        (kill-buffer-query-functions nil))

    (with-temp-buffer
      (switch-to-buffer-other-window (current-buffer))
      (eshell-command
       (concat
        "lynx -cookies=1 -accept_all_cookies=1 -cookie_file=cookie.file "
        URL))

      (other-window 1)
      (read-string (concat URL ": Do something? ")))
    (kill-buffer "*lynx*")

    ))))

The purpose of this piece of code is explained in my previous question: How to call lynx on URL inside a program.

Gabriele Nicolardi
  • 1,199
  • 8
  • 17