3

I have a elisp program which check server in a endless loop, like

(while t
  (let ((status (check-server)))
     (case status
        ...)
     (sleep-for 100))
  )

and I want to debug or update code in ielm, like

(print status)

or

(defun check-server ...)

However when I run it, I can't enter any code in ielm. Is there a method to finish my purpose?

Scott Weldon
  • 2,695
  • 1
  • 18
  • 31
netawater
  • 245
  • 1
  • 6

2 Answers2

4

In the menus, enable:

Options -> Enter debugger on Quit/C-g

and then press C-g when your code is running and you want to debug.

Robin Green
  • 949
  • 7
  • 17
1

Run your server with run-with-timer. In this way you can do anything you like with it. For an instance you can instrument check-server for edebug or redefine check-server while it is still running.

An example how to run the check-server with run-with-timer. Each started server has its own buffer. The variable my-server-status is buffer local to this buffer. If you want to see its value first switch to this buffer.

(defun check-server ()
  (let ((time (current-time)))
    (message "Running server at %s" time)
    time))

(defun my-server-start (time fun)
  "Run `fun' with timer for every time seconds."
  (interactive "nTime in seconds:\naServer to run:")
  (let ((buf (generate-new-buffer (concat "*my-server(" (symbol-name fun) ")"))))
    (with-current-buffer buf
      (setq-local my-server-fun fun)
      (setq-local my-server-timer (run-with-timer 0 time `(lambda () (with-current-buffer ,buf (setq-local my-server-status (funcall my-server-fun))))))
      (add-hook 'kill-buffer-hook (lambda () (cancel-timer my-server-timer)) t t))))

EDIT: You get rid of the server timer when you kill the server buffer.

Tobias
  • 33,167
  • 1
  • 37
  • 77