Archive for August, 2009

Persistent Emacs

Wednesday, August 5th, 2009

Having persistent emacs sessions - where your data remains when you load up emacs - is supposed to be handled by desktop.el, a library that comes with emacs. However, I’ve never actually gotten it to work - I’m not sure why, the installation seems fairly easy. It’s just never loaded my state properly. But since I like some of my settings persisting across sessions, I use several other, more specific modes that come with Emacs, and a mode I wrote to save and load currently-opened files.

Saveplace is a built-in emacs package for saving your location in files. If you have a file open in emacs and then close it, with saveplace enabled the file will be opened with point at it’s last location. This is often what I want - just to pick up where I left off - so of course I enabled it.

 save-place-file "~/.emacs.d/.saveplace")
(setq-default save-place t)
(require 'saveplace)

Another similar mode is savehist, which will save your minibuffer history across sessions. I don’t actually use the minibuffer history very much, but it does occasionally help so I just save it across sessions.

(setq savehist-file "~/.emacs.d/.savehist")
(savehist-mode 1)

The other mode I wrote, save-visited-files.el, is available here. It is a lightweight replacement to the part of desktop that persists files across sessions. At the moment, it is very simple, though if you want any features added or have any bug reports feel free to contact me. It have three functions: save-visited-files, open-visited-files, and save-visited-files-mode. save-visited-files will just save a list of all the files you have opened. open-visited-files will open every file in this list. save-visited-files-mode will set up a timer that runs every 60 seconds by default and saves what files you have opened(unless you are currently in the minibuffer).

There are currently only two configuration options: save-visited-files-location, which will change what file to use to save and load file lists, and save-visited-files-interval, which controls how often the list of visited files are saved. To enable this mode and load your previous opened files, add the following to your initialization file:

(require 'save-visited-files)
(open-visited-files)
(save-visited-files-mode 1)

This is actually the second revision of this mode; In the first, instead of using a timer, I advised kill-buffer-hook and find-file-hook to maintain completely up-to-date file lists, instead of just saving every 60 seconds. This had some problems, however. It had to ensure that the file it was opening/closing wasn’t from another instance of save-visited-files. There were performance issues when the entire list was written whenever a buffer was killed, since it turns out temporary background buffers are always popping up, and attempting to just delete the specific line started getting complicated, so I decided saving every 60 seconds was good enough.

New Features in Emacs23

Monday, August 3rd, 2009

Emacs 23.1 was released earlier this week, and if you haven’t upgraded yet you should. It comes with all sorts of new goodies. I’ve been working off of the emacs-snapshot package for Ubuntu, which has many of these new features, but the final version is much more polished. While I won’t cover all of the new features in this blog, I will cover the ones that seem most interesting to me. For the full list of enhancements, use ‘C-h n’ to view the NEWS file.

The big improvement many people are talking about is the new font antialiasing. If you compile with the Xft library, your emacs fonts will be antialiased, making it much prettier and more readable. Support for internationalization was also increased, with the Emacs character set now having 4 times the space that Unicode does.

Emacs can now also be started as a daemon, with emacs –daemon. This will start emacs in the background and allow you to connect to it with emacsclient for very fast load times without having to load your initializations. While you could run a server in emacs before this, you had to have an open instance of emacs - you coulnd’t have one running in the background like you can now. If you have a fairly stable configuration his would be great - I change mine all the time, sometimes to states that require me to restart, so I’m not sure how much I’ll use it.

Visual-line-mode is a new addition that is set on by default. It is something of a replacement for longlines-mode. It doesn’t insert soft line breaks, so it works better on larger files, and it wraps to whatever your screen is, so if you are using longlines-mode you should switch to using visual-line-mode. C-n and C-p will move by visual lines, which may screw up some macros, but mostly it seems to work quite well.

Transient-mark-mode is now enabled by default. I used to not like this mode, but it’s grown on
me. Transient-mark-mode is a mode that highlights the region between point and mark(most of the time) and has many commands operate on the highlighted region instead of the entire buffer. I think transient-mark-mode being on is a good default, since people new to emacs will be used to their old editors highlighting their selection.

The VC system, which integrates with many different version control systems, now has support for changelists. Previously, each file was checked in individually, which isn’t what you want for systems such as SVN or GIT. I’m going to need to play with this a bit more before I understand quite how it works; I’m working on a few posts about a few different version control systems, so I’ll probably discuss this more then. A new command was also added, vc-dir, which shows you the VC status for the directory and subdirectory. This will tell you which files need updates, which were added, etc.

C-l, which previously just recentered the screen to have the point in the center, has been slightly improved. While C-l once will act the old way, pressing it again will then scroll the buffer so that the point is at the top of the screen, and then C-l a third time will scroll the buffer so that the point is at the bottom of the screen.

Lots of new modes were added to Emacs-23. Doc-view is one useful one, which allows displaying PDF, PS, and DVI files in emacs buffers. You can even search through the pages, although this only gets you to the correct page. While useful, I find this mode suffers from some inherent limitations caused by Emacs’ display system, which requires the PDFs to be converted to PNG files first. This means you can’t select text in it, highlight the actual match for your search, etc. Another cool addition is EasyPG support. A library I used to use for numbering lines, linum.el, is now in Emacs proper. nXML, a very mode for editing and validating XML documents, and ruby-mode are also included. The last mode I’m going to mention is proced, which is like dired for processes. I find it very useful, allowing you to do things like mark arbitrary processes and send a signal to all of them.

Another new feature is directory-local-variables. You could previously have file-local-variables by adding text to your file, but if you wanted many variables applied to everything in a directory you had to implement this on your own. You can now just have a .dir-locals.el, with a specially-formatted list such as:

     ((nil . ((indent-tabs-mode . t)
              (fill-column . 80)))
      (c-mode . ((c-file-style . "BSD")))
      (java-mode . ((c-file-style . "BSD")))
      ("src/imported"
       . ((nil . ((change-log-default-name . "ChangeLog.local"))))))

Which is a list specifying that in indent-tabs-mode should be t and fill-column should be 80 in every mode.  In c- and java-mode, c-file-style should be “BSD”, and in and that any file in the “src/imported” subdirectory should have the change-log-default-name of “ChangeLog.local”.

Then of course we come to emacs-uptime. It does pretty much what you’d expect: run this function and it tells you how long your Emacs session has been running. Mine’s only at about 14 hours right now, due to having to restart due to some issues when I tried to install ropemacs.

That’s all I’m going to cover in this post, but there is much more that was changed and added to. I highly recommend upgrading to emacs23 and reading the NEWS file to find out all the other changes that you may not know about. You can access this file by using ‘C-h n’.