2011-02-10

keeping your secrets secret

If you want to keep your secrets secret, it is a good idea to encrypt your data; I usually do that for files with passwords for various services, banking data, and so on. Since version 23, Emacs includes a package called EasyPG (an interface to GnuPG) which makes this seamless – just make sure that you have GnuPG installed.

It's easy to use EasyPG – the only thing you need to do is adding the .gpg -extension to your files, and EasyPG will automatically encrypt/decrypt them when writing/reading. So, for example, to create an encrypted org-mode-file, simply visit (C-x C-f) a file with a name like myfile.org.gpg; emacs opens this in Org-Mode (just like any .org-file). When you want to save the file, emacs will ask you for a password, and with this same password, you can open it again. Don't forget that password!

account data

You can store any kind of secret data in your encrypted files. One type I find particularly useful is to store account data (user names, passwords) for various services there. For example, I use the emacs identi.ca-mode client, which gets its account data through variables identica-username and identica-password.

I do not want to put this information in my main .emacs file for safety reasons, but instead, put it an encrypted file, together with the account data for other services (mail, twitter etc.). Emacs' require does not understand encrypted files, but load-library does. To deal with that, I have two files, secrets.el and secrets.el.gpg (in my load-path):

;; secrets.el
(load-library "secrets.el.gpg")
(provide 'secrets)

and

;; secrets.el.gpg
(setq identica-username "djcb"
      identica-password "$ekr3t")
;; ... other passwords ...

Now, in my .emacs I have a function for various services, like:

(defun start-identica ()
  (interactive)
  (require 'secrets)
  (identica-friends-timeline))

This will prompt me for the password, but only if I use anything that requires the secret data, and only once per session.

Update: as Richard notes in the comments, you can also use require by explicitly specifying the filename (parameter two). That might actually be easier -- thanks Richard!

using public keys

By default, EasyPG performs symmetric encryption; if you want to use public key encryption instead (useful when you want to share the encrypted files with others), you can use:

;; 'silent to use symmetric encryption
;; nil to ask for users unless specified
;; t to always ask for a user
(setq epa-file-select-keys t) 

The 'users' in this snippet are the people in your GnuPG-keyring – EasyPG lists your keyring inhabitants, allowing for easy selection. You can also specify the people who can decrypt your file by putting something like the following at the beginning of the file you want to encrypt.

# -*- epa-file-encrypt-to: ("foo@bar.org") -*-

so

EasyPG brings more functionality for encryption, decryption, signing, managing your keyring and so on, but I haven't used that much yet. Anyhow, the automatic support for reading/writing file is really nice.

2011-02-02

extending ERC with your own commands

ERC is the leading Emacs-based IRC-client; I already discussed ERC before. I have been using ERC a lot in recent times, as it's an essential way to communicate at work with team members in remote locations. There are other IRC-clients – most people around me seem to use either irssi or xchat, but these don't integrate so well with my emacs-based workflow, the easy with which it can be extended to do exactly what I want; in this although they have their own strenghts. One of the great strengths of ERC is article I give some examples.

Apart from chatting, you can send commands (long list) to the IRC-server, for example to request information about other users, change your 'nick', leave the channel, and so on. As in most IRC-clients, you can send these commands with ERC by prefixing them with /, so you'd type:

/nick ninjaturtle

to change your nickname.

The nice thing about ERC is how easy it to add your own commands to this. In your .emacs (after loading ERC), you can add something like:

(defun erc-cmd-MYSYSTEM ()
  "show some information about my system"
  (let ((str (shell-command-to-string "uname -a")))
    (when str (erc-send-message str))))

Or, add add a function called erc-cmd-XXXX (with the XXXX being the command name in capitals, will add command XXXX, which you can invoke with /XXXX or /xxxx). So, with the above function, I can now do something like:

ERC> /mysystem
<djcb> Linux cthulhu 2.6.35-25-generic #44 SMP Fri Jan 21 17:40:48 UTC
       2011 i686 GNU/Linux

Let's look at some other (somewhat) useful command: /calc; again, just a small example, I'm sure something can come up with something a bit more elegant - perhaps using emacs' built-in calc.

(defun erc-cmd-CALC (&rest args)
  "calculate value of some expression using bc"
  (let ((expr (mapconcat 'identity args " ")))
    (when (length expr)
      (let ((result (shell-command-to-string (concat "echo '" expr "' | bc "))))
        (when result (erc-send-message (concat expr " = " result)))))))
ERC> /calc 2 * (3 + 4) / 7
<djcb> 2 * (3 + 4) / 7  = 2

Now, a small warning, just because it's easy to dump the output of, say, cowsay in an IRC-channel using your own /cowsay command, does not mean it is a good idea – in fact, using something like that is guaranteed to get you kicked out fairly quickly from many channels.

That being said, I'm sure many people have come up with much more clever things than the examples here; feel free to share your inventions in the comments!

2011-01-06

setting the frame title

The 'frame title' (window title) that emacs uses in graphical environments defaults to something like emacs@hostname.

Of course emacs lets us customize this, by changing the value of frame-title-format. Emacs accepts many different things there, (see the documentation for frame-title-format and mode-line-format for that), but let's look at an example.

Instead of the default emacs@hostname, I find it more useful to include the name of the file I'm working on instead, or, in case of non-file buffers, the buffer name. To do this, I have something like the following in my .emacs:

(setq frame-title-format
  '("" invocation-name ": "(:eval (if (buffer-file-name)
                (abbreviate-file-name (buffer-file-name))
                  "%b"))))

As you see, frame-title-format is a template for the items that are present in the title bar; i.e.. emacs concatenates the items in the list, and it supports various %-constructs, which are replaced with actual values; see below.

In addition to the %-constructs, you can use :eval to make emacs evaluate the expression whenever it wants to update the title bar.

invocation-name is the name of the emacs binary.

abbreviate-file-name replaces the home directory part in file names with ~; for very deep paths it might be nice to do some abbreviation as well as some shells do; this is left as an exercise to the reader :)

You can experiment with some other things to put in frame-title-format; use the :eval construct as above to use emacs-lisp functions, and the various %-specifiers which are replaced by certain values; the emacs documentation lists the following:

%b -- print buffer name.      %f -- print visited file name.
%F -- print frame name.
%* -- print %, * or hyphen.   %+ -- print *, % or hyphen.
      %& is like %*, but ignore read-only-ness.
      % means buffer is read-only and * means it is modified.
      For a modified read-only buffer, %* gives % and %+ gives *.
%s -- print process status.
%i -- print the size of the buffer.
%I -- like %i, but use k, M, G, etc., to abbreviate.
%p -- print percent of buffer above top of window, or Top, Bot or All.
%P -- print percent of buffer above bottom of window, perhaps plus Top,
      or print Bottom or All.
%n -- print Narrow if appropriate.
%t -- visited file is text or binary (if OS supports this distinction).
%z -- print mnemonics of keyboard, terminal, and buffer coding systems.
%Z -- like %z, but including the end-of-line format.
%e -- print error message about full memory.
%@ -- print @ or hyphen.  @ means that default-directory is on a
      remote machine.
%[ -- print one [ for each recursive editing level.  %] similar.
%% -- print %.   %- -- print infinitely many dashes.
Decimal digits after the % specify field width to which to pad.

So, if we'd like to include the host (system) name and some indication of the status of this buffer, we could do something like:

(setq frame-title-format
  '("emacs%@" (:eval (system-name)) ": " (:eval (if (buffer-file-name)
                (abbreviate-file-name (buffer-file-name))
                  "%b")) " [%*]"))

Of course, some of the information is available elsewhere already, but it might be clearer in the frame-title. Or not – there's a lot of room for tweaking and experimentation here.

2010-12-30

color theming with emacs 24

The emacs development team is hard at work on emacs 24. As far as I know, there is no planned date for it yet. I'll write more about all the new cool things when we're a bit closer to the release date, but I'd like to highlight emacs 24 color-theming already, as it's an area people outside the core development theme should be able to contribute to as well.

Here at Emacs-fu, we already discussed color-theming and in particular the zenburn color theme; this was all based on the color-theme-package. While being popular, there were a few problems with that package, which is why emacs-24 comes with a new scheme for doing this.

The current development versions of emacs ship with a few themes (tango, tango-dark and wheatgrass), which you can activate with M-x load-theme. It would be great to include some more color themes, and to encourage this, emacs maintainer Chong Yidong has written the GNU Emacs Theme Generator (currently in bèta), which helps you in the process of making a simple theme. This tool currently only support changing colors, and not e.g. the boldness or slant, but it's a nice way to get started, and to immediately see the results.

For more complex theming, there is M-x customize-create-theme; however, that is built on the (in my opinion) cumbersome customize-interface, so I'll probably start by creating a basic theme with the web tool, and then edit it by hand. If you design some attractive color theme and would like to propose it for inclusion in emacs 24, you can send it to emacs-devel.

2010-12-01

conkeror: web browsing the emacs way

Web browsing is one of the few computer-based activities for which I usually do not use emacs. Of course, there are of emacs-based browsers like w3m, which is pretty good. But for the 21st-century web, a text-mode browser is a bit limited. There are some efforts underway to integrate a graphical web browser experience in emacs (e.g., using Xembed), but it will take a while before that is ready.
The next best thing to web browsing with emacs, is a browser that is inspired by emacs. Such a browser exists: it's called conkeror (not to be confused with KDE's Konqueror). conkeror is a Mozilla-based browser that brings an emacs-like experience to web browsing. I've been using it for more than a year now as my primary browser, and I am quite happy with it.
Conkeror is a user-interface for the normal Mozilla/Firefox engine (xulrunner); this means that all those websites that work with Firefox, work just as well in conkeror, including things like ad-blockers, Java, Flash etc. and many plug-ins. This is a very important point for me.
If you like emacs, you'll probably also like conkeror. For starters, it uses many of the same key bindings – C-x C-f will open a url in a new buffer (tab), C-x 5 2 will open a new window (frame), I can inspect a key-binding with C-h k… and so on. Some of the single-key shortcuts are the same as those in w3m, like g to go to a webpage, or B to go back. Conkeror is fully keyboard-driven; it allows you do almost anything on the web without using the mouse – it can overlay numbers on items in a webpage so you can interact with them by their number. Of course, you can still use a mouse if you want, too.
The conkeror wiki gives many more examples. It also discusses installation in quite some detail, which makes life a bit easier for me :) Instead, I will just list a couple of the customizations I have made and encourage you to try for yourself.

configuration

Like emacs, conkeror is very configurable. The big difference is that conkeror uses Javascript instead of Elisp for the configuration; however, the concepts and naming should be familiar for emacs-users. Conkeror's equivalent of ~/.emacs is ~/.conkerorrc. Below are some snippets from my setup, that can hopefully help you to create your Perfect Browser ™; all examples below are based on the latest development version of Conkeror, as per end of November 2010.
First, some basics:
//allow for 'contrib' stuff
load_paths.unshift("chrome://conkeror-contrib/content/");

// teach me something whenever I start my browser
homepage = "http://en.wikipedia.org/wiki/Special:Random";

// give me new tabs; open buffers (tabs) in the background
require("new-tabs.js");
require("clicks-in-new-buffer.js");
clicks_in_new_buffer_target = OPEN_NEW_BUFFER_BACKGROUND; 
clicks_in_new_buffer_button = 1; //  midclick links in new buffers with

// auto completion in the minibuffer
minibuffer_auto_complete_default = true;
url_completion_use_history = true; // should work since bf05c87405
url_completion_use_bookmarks = true;

modeline customization

As in emacs, you can customize the modeline. Also in good emacs tradition, this is far from intuitive…
require("mode-line.js");
// funky icons in the modeline
require("mode-line-buttons.js");
mode_line_add_buttons(standard_mode_line_buttons, true);

// we'd like to see the # of buffers being loaded 
add_hook("mode_line_hook", mode_line_adder(loading_count_widget), true);
// we don't need a clock
remove_hook("mode_line_hook", mode_line_adder(clock_widget));

bookmarks

Plain-old bookmarks are easy to add with define_webjump – and you can just bind them to a short string. You can then 'jump' to the corresponding website by pressing g and then this short string. For example, to go to Emacs-Fu, you could type g efu RET. No mousing around needed.
// some bookmarks
define_webjump("conk",  "http://conkeror.org");
define_webjump("efu",   "http://emacs-fu.blogspot.com");
define_webjump("lkml",  "http://lkml.org");
define_webjump("ew",    "http://emacswiki.org");
// ...

smart links

Smartlinks are just like normal bookmarks, which the one difference that they contain an %s-parameter which will be replaced by whatever you type after the name of the bookmark. Thus, g imdb grande bouffe RET would take you to the IMDB-page about the movie La Grande Bouffe.
Some examples:
define_webjump("so",    "http://stackoverflow.com/search?q=%s");
define_webjump("yts",   "http://www.youtube.com/results?search_query=%s&aq=f");
define_webjump("imbd",  "http://www.imdb.com/find?s=all&q=%s");
// ...

integration with emacs

Another useful feature in conkeror is the ability to interact with Emacs (or any other text editor), so you can use emacs for writing text in web pages; for example, when you're using emacs-daemon (you should), you could can add the below, and C-i will then take you to emacs whenever you are in a text field in a web page.
editor_shell_command = "emacsclient -c";

adding your own functions

It's fairly easy to add your own functions; admittedly, I'm still at the beginning stages with that, but it's not too hard to combine the internal function, and assign them to key bindings. Some examples:
// copy url with C-c u
interactive("copy-url",
        "Copy the current buffer's URL to the clipboard",
        function(I) {
            var text = I.window.buffers.current.document.location.href;
            writeToClipboard(text);
            I.window.minibuffer.message("copied: " + text);
        }
);
define_key(default_global_keymap, "C-c u", "copy-url");


// reload conkerorrc with C-c r
interactive("reload-config", "reload conkerorrc",
       function(I) {
          load_rc();
          I.window.minibuffer.message("config reloaded");
       }
);
define_key(default_global_keymap, "C-c r", "reload-config");
It's not necessarily so easy to find the exact functions / objects you need to accomplish what you want. There's a certain learning curve – just like when using emacs.

integration with org-mode

The integration with emacs can go further than merely editing text fields though; it's possible to integrate conkeror with org-mode in emacs; I have the following in my .conkerorrc (this requires emacs-daemon), based on the code in WOrg:
// org-protocol stuff
function org_capture (url, title, selection, window) {
    var cmd_str =
        'emacsclient \"org-protocol:/capture:/w/'+url+'/'+title+'/'+selection+'\"';
    if (window != null) {
      window.minibuffer.message('Issuing ' + cmd_str);
    }
    shell_command_blind(cmd_str);
}

interactive("org-capture", "Clip url, title, and selection to capture via org-protocol",
          function (I) {
              org_capture(encodeURIComponent(I.buffer.display_uri_string),
                        encodeURIComponent(I.buffer.document.title),
                                encodeURIComponent(I.buffer.top_frame.getSelection()),
                        I.window);
          });
// capture with C-c c
define_key(content_buffer_normal_keymap, "C-c c", "org-capture");
Then, on the emacs-side (in your .emacs), you should have a capture template for this; for example:
;; ;; the 'w' corresponds with the 'w' used before as in:
;;   emacsclient \"org-protocol:/capture:/w/  [...]
(setq org-capture-templates
  '(
     ("w" "" entry ;; 'w' for 'org-protocol'
       (file+headline "www.org" "Notes")
       "* %^{Title}\n\n  Source: %u, %c\n\n  %i")
     ;; other templates
))
Now, when in conkeror, you can select some text, push C-c c ('capture'); emacs will offer you to save this text, including a link to the source, the date and so on, and saves it for later retrieval. I really love this feature!

so…

These were just a couple of the many things you can do with conkeror; there's so much potential here. I haven't discussed many of the other powerful features, such as the page-specific modes, that give special features to specific websites, be it GMail or Wikipedia.
I've been using this fine browser for a year or so now, and I really like it. There are a few rough edges here and there, but it's quite amazing what the small development team has accomplished. I heartily recommend it – for web-browsing the emacs-way, there's simple no better alternative.
Finally, if you're using conkeror already, and have some clever tricks you'd like to share, feel free to do so in the comments.

2010-11-15

undo

One of the advantages that text-editors have over daily life is the ease of undoing things. Emacs in particular has a powerful undo-system to return to the previous state of things.

Undo can be activated by pressing C-x u or C-_; if you are more comfortable with using C-z for undo (as is the standard in many non-Emacs environments), you can of course add that key binding as well, by putting in your .emacs:

(global-set-key (kbd "C-z") 'undo)

This overrides the default of C-z triggering suspend-frame, which is not a great loss for most people.

Emacs-undo has some nice features - for example, if you have selected some text ('marked a region'), the undo operation only applies to that area. At the same time, however, the undo-model can be confusing: in emacs, undo is treated like any other command, which means that undo can be applied even to… undo, which is different from what most other editors do. Let me give an example – if it's confusing or tedious then, well, that's what it is…

Suppose I type

hello
world

now I press C-_, and I get

hello

Now I type foo

hello
foo

And press C-_ C-_; and I get back:

hello
world

This is because we're 'undoing the undo' of adding the word 'world', and thus, it reappears! When you try this in most other editors, it would result in

hello

because those editors completely forget about 'world' after it is undone.

So, emacs' model is strictly more powerful - but some (many? )people find it a bit confusing, esp. when a series of 'undos' is interrupted by a 'do'.

If you prefer the model that many other editors use, you might be interested in redo-mode, in particular in RedoPlus. Using that package, undo (or rather, redo) follows a different model. In that model, redo is 'special': it's not registered as a buffer change, and as such it's conceptually different from the redo=undo-undo model that emacs uses by default. As seen above, you actually lose some information in the process.

Yet another way to tackle the undo-problem is implemented by UndoTree: the states of your buffer are seen as nodes in a tree, and you can freely move to specific nodes. UndoTree is as powerful as the emacs system, yet easier to understand. It can even visualize the tree of changes - and you can then by clicking on a node go back to the corresponding buffer state.

Now, when using UndoTree, let's look at our example again:

We started with:

hello
world

then did C-_ (which removed 'world') and typed 'foo' to get:

hello
foo

Now press C-u again, and 'foo' disappear. Now we press C-x u (undo-tree-visualize) and we get a buffer with:

  |
  o
  |
  |
  x
  | 
 / \
o   o

and we can now visually move to any of the nodes, and our buffer is instantly brought back. Cool! The two branches correspond to the states 'world' and 'foo'. I have been undo-tree-mode for the last few weeks, and it works very well: usually I don't even notice it, but when I need the extra power, it's there.

I am told that UndoTree is inspired by the way the vim-text editor does this. Anyway, there is another very powerful feature in the vim undo-system that would be nice to have in emacs to: time-based undo. In vim you can e.g. say something like:

:earlier 5m

to go to the state of your buffer 5 minutes ago. That would be a nice addition for undo-tree-mode!

Update Tavis Rudd notes that pressing t in undo-tree-visualizer-mode (i.e.. what you see when your press C-x u) will give you timestamps instead of o and x:

                  |
                  | 
             18:54:20
                  |
                  |
             18:54:20
          ________|___ 
         /            \
     18:54:15       18:53:37
        |              |
        |              |
     18:54:15       18:53:37
     ___|___           |
    /       \          |
18:54:10  18:53:43  18:53:36

2010-10-23

auto-complete-mode

Recently, we discussed CEDET in the interview with Eric Ludlam. CEDET has a deep understanding of the source code, and assists the development process by exploiting that knowledge. For example by drawing class diagrams (COGRE) or providing auto-completion for function names and parameters.

Currently, however, CEDET is also be a bit hard to set up effectively, and may also not support your programming language yet. Therefore, it's still useful to look at some other packages that can substitute (part of) the functionality.

For me, auto-complete-mode has been very useful for that. It's not as fancy as CEDET in the sense that it does not really understand the code – but in practice I found it to work quite well for both C/C++, Elisp and shell scripts. It can even display the docstrings of functions. And when editing shell-scripts, it can complete path names as you are editing the scripts; very nice.

To install, follow the instructions. Then, in your .emacs, have something like:

(when (require 'auto-complete-config nil 'noerror) ;; don't break if not installed 
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
  (setq ac-comphist-file  "~/.emacs.d/ac-comphist.dat")
  (ac-config-default))

After that, M-x auto-complete-mode to start using it. Alternatively, there's a YouTube-video explaining how to install and use auto-complete-mode; recommended. In any case, it is fully documented.

auto-complete-mode uses a configurable set of sources from which it takes it knowledge about what can be completed. For example, when writing in org-mode, it takes its input from

(ac-source-filename ac-source-abbrev ac-source-dictionary
 ac-source-words-in-same-mode-buffers)

while in a c-mode buffer it is (in my case):

(ac-source-filename ac-source-yasnippet ac-source-gtags ac-source-abbrev
ac-source-dictionary ac-source-words-in-same-mode-buffers)

You can customize this, and (for the more ambitious), it's possible to add your own sources as well.