2015-12-22

Evil Mode hack for a colorful mode line

Intro

After trying out Spacemacs for about a week (and failing - too much old configuration to "convert") I am trying out Evil Mode by itself. It goes forward, slowly, step by step.

One thing I have been a bit annoyed with was how the different states are visualized. Being a Evil/Vim newbie I want to see, clearly, in which state/mode I am in.

Today I decided to do something about it and here is how my Emacs looks now:

Normal state


Insert state


Emacs state




How it was done

I could not find any good entry points for customizing the mode line with respect to the Evil Mode states, but I found the function (evil-generate-mode-line-tag) that generates the part of the mode line that comes from Evil Mode. I simply made a copy of it and changed it to add also a face attribute to the mode line, and let the face be different depending on the mode.

The code

Copy the code below and save it in a file called my-evil-hacks.el.

After this you need to make sure to load this file after you have loaded Evil mode for the first time. One way to achieve this is to put the following snippet in your .emacs or init.el file:
(with-eval-after-load "evil"
  (load "my-evil-hacks"))
Enjoy!

2015-08-21

Something I should have done a long time ago...

This is something I should have done a long time ago, to make window creation and manipulation easier in Emacs:

(global-set-key (kbd "C-1") 'delete-other-windows)
(global-set-key (kbd "C-2") 'split-window-below)
(global-set-key (kbd "C-3") 'split-window-right)
(global-set-key (kbd "C-0") 'delete-window)

It gives the window manipulating commands I use the most nicer key bindings; just by keeping the Ctrl key pressed and typing away (crazily) on the keys 2 and 3 you can create master pieces of window configurations :)

I could do this since I realized I seldom or never use these shortcuts to enter a numeric argument. If I need to, I can use a slightly longer key sequence like C-u 1 or M-1 which I have kept (for now...).

Perhaps this post will inspire someone else to do the same.

2015-07-18

Evaluating elisp expressions from a web page with a custom URI scheme

Intro


Very boring title, eh? :) Well, this is kinda neat...

Some of you might already know that you can install a custom URI scheme handler in most operating system. Today I did an experiment with that together with Emacs and it turned out quite nicely. I did
this in Windows but it should work in GNU/Linux and OS X as well.


So, what is an URI scheme?


Briefly explained, the URI scheme is the "http" part in URLs that we all use everyday. Other examples are "ftp" and "file". The most common ones are handled by your web browser but you can also have another program on your computer handle a certain URI scheme. You can register your own URI schemes as well and in this post we will experiment with a new URI scheme that we name "emacs", for evaluating expressions/executing commands in Emacs.


A warning


If you decide to do this, you should be very careful, since it could be a big security risk. Either you let the warning from your web browser always be there to warn you (it can be supressed easily, for a
better user experience) or you register a secret URI scheme (like "myverysecretemacsurischeme") that no one else knows about. Otherwise, bad people can do very nasty things with your computer...

Okay, with that aside, let's go!


Step 1 - Registering a new URI scheme


In Windows you register a new URI scheme in the Windows registry (where else...). Basically you have to create the following key/folder structure under the HKEY_CLASSES_ROOT hive:

<name of URI scheme handler>
  shell
    open
      command

Lastly, the "command" key's Default value (right hand side) should have the path to the program handling the request, and %1 should be there to, to send in the parameters from the URL to the program.

If you dare, you can save the following snippet of text as something.reg on your PC, and double click that file. It will add an URI scheme handler for the "emacs" scheme for you. You have to change the path to the program afterwards:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\emacs]"URL Protocol"=""
[HKEY_CLASSES_ROOT\emacs\shell]
[HKEY_CLASSES_ROOT\emacs\shell\open]
[HKEY_CLASSES_ROOT\emacs\shell\open\command]@="f:\\Dropbox\\home\\dat\\doc\\src\\bat\\emacsschemehandler.cmd \"%1\""


Step 2 - A script to handle the new URI scheme


To handle the new URI scheme I created a small cmd file that processes the arguments a bit and then starts emacsclient. It looks like this:

REM f:\Dropbox\home\dat\doc\src\bat\emacsschemehandler.cmd

@echo off

REM Pick up all arguments to the script (in case there are spaces)
set emacscommand=%*

REM Stript away the part we don't need...
set emacscommand=%emacscommand:emacs:=%

REM Start emacsclient
f:\Dropbox\home\pgm\emacs-24.3\bin\emacsclient -n -e %emacscommand%

Save the script above as a .cmd file on your computer and update the registry to reflect the path to the .cmd file on your system. Also update the path to where your emacsclient is.

The script is, as can be seen, very simple and basically it just trims off part of the argument from the web browser that we do not need and use the rest as an argument to emacsclient. If you decide to use
another name than "emacs" for your own experiments, make sure to update "emacs:" above to whatever name you picked (plus a colon).


Step 3 - Testing it from a web page


Now you only need a web page with some links to try it out. I have provided a couple of links below that you can try right now, if you decided to use the "emacs" URI scheme, that is. The code for the links looks like this (if you do not trust me, view the source code for this blog post):
<a href='emacs:(message """Hello, Emacs!""")'>Hello, Emacs!</a> <a href="emacs:(calendar)">Open Calendar</a><a href="emacs:(info-emacs-manual)">Open the Emacs manual</a>
Here are the links:

Hello, Emacs!
Open Calendar
Open the Emacs manual

The triple double quotes in the first example are needed in order to send strings from the cmd file to emacsclient. That is why I used single quotes around the href value, which I normally don't like.

Now, what do you wait for, try it! Go go go! :)


So, what can this be used for?


Well, not sure. For me it was mostly a nice experiment, but I can see some uses for people who work a lot in Emacs but also a lot in a web browser and where they need some kind of interaction between
those. Perhaps something nice can be done together with Org mode and documentation of code, where you view the documentation in a browser but let the user try out code in Emacs, or just jump to a particular section in the Org mode document there. I guess the possibilities are endless, as with most things Emacs... :)

Hope you enjoyed it!

C'ya!