2

I'm interested in tracking how I spend my time at work. I have been using ManicTime, and it does much of what I want.

However, I use Emacs as my primary editor, and ManicTime doesn't know how to get info from Emacs. In its generic querying way, it attempts to get the window title (frequently in apps, the open document is in the title). For some apps, there are even plugins for ManicTime that are smarter about the target app.

I attempted to add a hook when switching buffers that wrote the Emacs PID and buffer name to a file that a plugin could read, but this did not work out very well for me. (My elisp could very well be suspect, but from a design perspective, this seems like a less than elegant and efficient solution.)

Is there a way to query Emacs from another process? This would be the simplest way to add Emacs support to ManicTime, but I don't know how to do it. Is there a good way?

5
  • 2
    what exactly do you want to query Emacs about? If you're running Emacs in server-mode (i.e., started via emacs --daemon, or calling (server-start) in your config file), you can use emacsclient to execute any elisp you want in the emacs process. If you have an an emacs function that returns the information you want, you could then do something like emacsclient -e '(get-manic-time-data)' (called from the command line), and then do whatever you need with the output.
    – Tyler
    Dec 4, 2019 at 20:08
  • E.g. I'd like to get the name of the buffer/file that's active (suggesting what I'm working on). Does running in server-mode diminish any of the app's user experience, or is it only enabling a feature that is off by default?
    – mojo
    Dec 4, 2019 at 20:09
  • many Emacs users run in server mode by default, as it allows for quickly opening and closing new frames as needed, and simplifies interaction with other programs. So there's no real downside in most cases.
    – Tyler
    Dec 4, 2019 at 20:10
  • See: C-h i g (emacs)Emacs Server
    – phils
    Dec 4, 2019 at 20:13
  • 2
    Put some meaningful information into Emacs's frame title via frame-title-format, such as buffer filename, directory name, and project name. I guess there is no other efficient way that a user can achieve. For example, your tracking app knows what URL and title Chrome is visiting because the Chrome app exposes the information in some way, however, the Emacs application probably doesn't do this. I'm using Timing, I use the buffer file name as Emacs's frame title, and Timing tracks it.
    – xuchunyang
    Dec 4, 2019 at 20:29

1 Answer 1

1

Running Emacs in server-mode, you can use emacsclient to execute lisp code in that Emacs process. This makes it relatively simple to find out the file name of the currently selected window:

emacsclient -e '(buffer-file-name (window-buffer (selected-window)))'

You may need to add some more logic to deal with the case where your active window isn't visiting a file (like the scratch buffer, or a help window). But elisp is a very capable language, it wouldn't take much work to get the information your after.

4
  • If I wanted to have several emacs frames open, and distinguish between them (as if each one represented work on a different billable job), is there a way I can distinguish between them based on PID when I call emacsclient?
    – mojo
    Dec 5, 2019 at 19:17
  • It's all the same (server) PID. I haven't tested, but you possibly want to use set-frame-name in each frame, and use (make-frame-names-alist) when you call emacsclient to obtain a frame object, and from there you can get to the window(s) displayed in that frame, and then to the buffer(s) themselves. with-selected-frame may also help.
    – phils
    Dec 5, 2019 at 22:41
  • Also see M-x pp-eval-expression RET (frame-parameters) RET to see other things you might find useful.
    – phils
    Dec 5, 2019 at 22:44
  • 1
    you can run multiple servers at the same time. see gnu.org/software/emacs/manual/html_node/emacs/… . That would allow you to pass code to different servers by name, so you could run each project in its own server. see also gnu.org/software/emacs/manual/html_node/emacs/…
    – Tyler
    Dec 5, 2019 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.