Search code examples
emacselispess

How to stay in the same buffer window after evaluation?


I'm trying to write a function R> that evaluates the code given as a string in the active ESS R process and returns basic conversions to elisp data structures or just nil. I'm not an experience elisp coder, so right now I'm just trying to get a really basic function working.

(defun R> (s)
  "Evaluate simple R command."
  (save-excursion
    (with-temp-buffer
      (setq ess-dialect "R")
      (let (out list)
    (setq tmpbuf (current-buffer))
    (ess-command (s-concat s "\n") tmpbuf)
    (setq list  (s-split "\w" (R>--remove-numbered-brackets)))
    (apply 'vector
           (--map
        (let ((it* (s-trim it)))
          ( if (s-numeric? it*)
              (string-to-number it*)
            (strip-inner-quotes it*)))
        list))))))

(defun R>--remove-numbered-brackets ()
  (replace-regexp-in-string "[\\[0-9\\]+]" "" (buffer-string)))

(defun strip-inner-quotes (s)
  "If the string has inner quotes, remove them."
  (replace-regexp-in-string "\"" "" s))

Unfortunately, running this causes the active windows to change. Not the desired behavior! I thought save-excursion was designed to prevent this, but I guess I'm not using properly. Is there some alternative way to use save-excursion that doesn't have this issue?


Solution

  • Use save-selected-window instead of/in addition to save-excursion.