Mark Harrison
Mark Harrison

Reputation: 304444

How can I run elisp code automatically in emacs?

I have a debugging/monitoring system for a particular application. I would like to allow monitoring/debugging from an emacs session, and have it run automatically.

Is there a way to have an emacs execute the following code without passing anything on the command line or forcing the user to run a command? I don't want to put it into the emacs init file because I don't want to be running useless debug monitors when users are not running the application.

(defun filter-output (process output)
  (eval (read output)))

(defun doit ()
 (let ((myproc (start-process "my-process" "*My DebugMonitor*" "/tmp/myProcess.py")))
      (set-process-query-on-exit-flag myproc nil)
      (set-process-filter myproc 'filter-output)))

Upvotes: 1

Views: 174

Answers (2)

Robin Green
Robin Green

Reputation: 33063

Unclear what you're asking, but it sounds like you want it to be executed by an external event. Try

emacsclient -e doit

But of course you will still have to put that code in the init file, so it knows what doit is.

Upvotes: 0

Drew
Drew

Reputation: 30701

If by automatically you mean that it will be run once when an Emacs session is created, and you don't want it to be on the command line (e.g. part of a startup script that invokes an OS command), and you don't want it to be in the user's init file, then consider placing it in default.el or site-start.el. See the Emacs manual, node Init File.

If by automatically you mean that it should be run periodically or run when some event triggers it, then invoke it from a timer. See the Elisp manual, node Timers.

Upvotes: 2

Related Questions