0

I want to write a function that does one thing if erc is running and starts erc (using command erc) if not.

I tried to write it myself, but I couldn't find out how to check if erc is running. How can I do this in elisp?

Kaligule
  • 319
  • 3
  • 10

1 Answers1

1

There are two aspects: is there an erc process running in the buffer? and if there is such a process, is there an established network connection to the server?

If you look in erc.el, you will see how erc itself determines the process status to add to the mode line (line 6438 in the file):

    ...
    (let (       ...
          (process-status (cond ((and (erc-server-process-alive)
                                      (not erc-server-connected))
                                 ":connecting")
                                ((erc-server-process-alive)
                                 "")
                                (t
                                 ": CLOSED")))

IOW, it uses the function erc-server-process-alive to check whether the process is alive and the value of the variable erc-server-connected to see if it is connected to the server: if it is alive but not connected to the server, it reports "connecting"; else if it is alive, it reports nothing (i.e. everything is good); else it reports "CLOSED".

You should check the doc strings of the function above (C-h f erc-server-process-alive) and the variable (C-h v erc-server-connected) and read them carefully, but this should allow you to determine the state of erc.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • Very helpfull, thank you. (erc-server-connected) gives me what I want, My function now looks like this: (defun erc-start-or-cycle () "if erc is already running, go to the next channel. Else, start erc." (interactive) (if (erc-server-connected) (erc-next-channel-buffer) (erc))) – Kaligule Apr 09 '21 at 17:39
  • 1
    `erc-server-connected` is a variable, not a function, so that should read: `(if erc-server-connected ...)` with no parens (unless you have defined your own `erc-server-connected` function). It might be possible for the value of the variable to be `t`, but the process might have died (I don't know if that's possilble, but it is at least a theoretical possibility): it would behoove you to check the state of the process as well IMO. – NickD Apr 09 '21 at 18:45