Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple essgd buffers #6

Open
rdiaz02 opened this issue Oct 5, 2024 · 0 comments
Open

Multiple essgd buffers #6

rdiaz02 opened this issue Oct 5, 2024 · 0 comments

Comments

@rdiaz02
Copy link

rdiaz02 commented Oct 5, 2024

I'd like to be able to use multiple essgd buffers, so that if we have displayed, say, three different plots, we can see more than one at the same time. This should work, as httpgd allows multiple clienst to have different plots of the history (nx10/httpgd#7 (comment)). And it works, but the code below can be improved a lot, I think (elisp ignorant here).

This is the code:

;; A one-line modification of essgd-start
(defun essgd-start-another ()
  "Start an *essgd* buffer to plot R output.
Must be called from a buffer that is either an *R* process, or attached to one.
The initial size of the plot is half the current window."
  (interactive)
  (let ((buf (get-buffer-create essgd-buffer))
	(r-proc ess-local-process-name)
	start-output)
    (set-buffer buf)
    (essgd-mode)

    (if r-proc
	(setq ess-local-process-name r-proc)
      (user-error "No r process to communicate with"))

    ;; RDU: do not start the hgd() device here but use existing one
    (setq start-output (ess-string-command "message(httpgd::hgd_url())"))

    ;; if minibuffer changes size, then we might get a dynamic resize!
    (setq-local resize-mini-windows nil)

    (string-match "\\(http://[0-9.:]+\\)/live\\?token=\\(.+\\)" start-output)
    ;; TODO - check case when token is missing.
    ;; TODO - error check if URL ccannot be found.
    (setq essgd-url (match-string 1 start-output))
    (setq essgd-token (match-string 2 start-output))
    (if (> (length essgd-token) 0)
	(setq essgd-token (format "token=%s" essgd-token)))
    (setq essgd-plot-nums (essgd-get-plot-nums))
    (setq essgd-cur-plot
	  (length essgd-plot-nums))
    (setq essgd-latest (make-temp-file "essgd" nil ".svg"))

    (display-buffer buf)
    (setq-local window-size-change-functions '(essgd-window-size-change))
    (when (> essgd-cur-plot 0)
      (essgd-show-plot-n essgd-cur-plot))

    (essgd-start-websocket)
    (setq cursor-type nil)
    (read-only-mode 1)))

(defun new-essgd (num)
  "Create a new *essgdnum* buffer for plotting."
  (interactive "P")
  (setq essgd-buffer (format "*essgd%d*" num))
  (essgd-start-another))

Now:

  • Start R and essgd
  • Plot a bunch of things
  • C-u 2 M-x new-essgd

Right after creation, you can move back and forth independently in each essgd buffer. But if you keep creating plots, the old essgd buffer no longer shows the newer plots (this is in contrast to the behavior with EAF ---below---, where adding plots allows you to move back and forth over all plots in the EAF and essgd buffers). They all get updated, though, if you resize or manually trigger refresh (r).

For the sake of completeness (and because that was my first try) this also works if we combine essgd with EAF, Emacs Application Framework (https://github.com/emacs-eaf/emacs-application-framework).

This is a reproducible example

  • Launch R

  • Start essgd

  • Create two different plots

  • Get the URL via httpgd::hgd_url()

  • Place point on the URL and do M-: (eaf-open-browser (thing-at-point 'url))

  • One now has the essgd and the EAF buffers. And one can move back and forth in each independently, as one keeps adding plots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant