Removing Emacs server file on startup

Since I became a happy user of Org Capture Chrome Extension, I needed to have the ability to properly run emacsclient.

Few words of introduction: emacsclient is a separate binary, which can connect to running Emacs instance, sharing all its resources. It starts almost instantly, which helps, if we want to use it for quick editing in a separate terminal (instead of vi). To connect to server, client requires a configuration file, which contains socket address and some additional data (for me, it is located ~/.emacs.d/server/). There could be multiple servers running, and every one of them would have its config file. Usually, the first one uses the file named server, and subsequent ones add a number to the name.

As I wanted only one Emacs server in the system, I have configured my init.el file as follows:

(if (not (server-running-p))

I was in constant trouble. Sometimes the server was working fine and clients could connect to it. When Emacs crashed (which is happening often under Windows), the server file was left in the folder, and when another instance came up, it was using a different server name.

Finally, I could put some time into investigation. It turns out that predicate (server-running-p) is not limited to returning t and nil. When another server file exists, it returns :other. I assume it only checks the server file existence, as after crash it still returns :other. Apparently, we need to check not only if file exists, but also if the Emacs instance it points to is active. After quick Google search, I learned about function (server-eval-at), which allows to execute code on another Emacs instance. Given these conditions, I came up with a little switch-like construct:

(cond ((eq (server-running-p) nil)
	   (progn (message "Starting new server.") (server-start)))
	  ((eq (server-running-p) :other)
	   (let ((my-status 0)) (ignore-errors (setq my-status (server-eval-at server-name (+ 1 1))))
			(if (/= my-status 2) (progn
								   (message "Killing stale socket and starting server.")
			  (message "Other server works."))))
	  (t (message "Server already started.")))

It checks the server-running-p and depending on its value starts new server instance, removes stale server file and spawns new server, or does nothing. Being a beginner in Elisp, I suspect this code could be written in more efficient and prettier form, but it works for now.

Removing Emacs server file on startup

C programming with Windows

It’s been almost a year since I switched jobs. In the previous one we had Linux-based development environment for not-so-embedded devices, where I could use C, C++, Python and Bash (with little of node.js on top) in a single project. After I transferred, I had to dive into actually embedded, C-only project with MS Windows used as main OS on every machine.

After the initial shock (“Why can’t these windows manage themselves?!”) I started slowly setting up my configuration, trying to recreate well-working setup I had before. Some things worked, some did not. I discovered few useful tools and wasted some time chasing the unobtainable ones. Below you can find a short list of programs I use in my coding setup.


The Editor. I started tinkering with it under Linux mid-2013. Coming from vim (which still remains irreplaceable when connecting remotely!), I went with Evil from the start, which helped immensely with the transition. Muscle memory for vi shorcuts serves me very well. I use Emacs mainly for coding, maintaining documentation and notes (org-mode!) and for pretty much anything that comes to mind (like displaying notifications, connecting to remote servers, etc). It’s crazy extensible and has very active community. To be fair, it also has its drawbacks, like handling the large files (doable, not the smoothest) and very long lines (there are many solutions online, but no perfect one). Emacs is extremely customizable and has vast number of plugins – my current instalation has almost fifty of them. From code snippets engine, through git, spellchecking, syntax checking, external apps integration to various color schemes – if you need something, there probably is a mode (plugin) for it.


Cscope is a tool, which allows the programmer to dig around in the project’s source code. I use it solely for C, and it is perfect for quick jumping to definitions and tracking down execution chains. In terms of speed it beats the good old grep and even ag (which I highly recommend). The main disadvantage of cscope is the necessity to rebuild its database every time the codebase changes, which could become tedious in large projects. It also requires a workaround under Windows, which I plan to describe shortly. It is possible to run it from commandline, but for convenience I use helm-cscope, which provides nice, unified interface inside Emacs.


Figure 1: Searching for symbol in Emacs with helm-cscope.


I feel guilty not starting with this one. Cygwin offers you the familiar tools you had to ditch moving to Windows. From sed, grep, and find to sshd and gcc – everything works, with very little trouble on the way. There are some unavoidable quirks, but overall I cannot imagine being productive on Windows without Cygwin.


There are more applications I use every day, but I’m planning a separate post for all Windows programs that I run frequently. When it comes down to user’s satisfaction, I still feel like I’m struggling with the environment and I would be more productive running Linux. I make small improvements every day, but there are some things that just cannot be fixed (I am looking at you, window management!). Hopefully, one day I would be able to return to the environment, which will be driven only by my choice.

C programming with Windows