2010-06-30

console apps in emacs with multi-term

multi-term

updated Whenever it makes sense, I try to use emacs for my computer-based activities; surely, programs like The Gimp or a graphical web browser cannot yet be replace by emacs, but I'm making progress. I like the ways emacs gives me to automate and speed-up my tasks; I get some return-on-time-investment.

2010 or not, I still spend quite a bit of time on the console. So why not do that from within emacs? There different ways to run shells within emacs.

The simplest one is shell (i.e,, M-x shell), which starts a simple shell, which does not support which does not support 'graphical' console applications, such as mutt, mc, htop.

Then there are term and ansi-term (M-x ansi-term) that do support such applications, which ansi-term supporting colors as well (it seems to have become the default for term in recent emacs versions).

Another one is the nifty EShell (included with emacs), which is not just a (simple) terminal, but also a full shell environment, and has integration with other things in emacs. It's nice, but has some of the limitations that shell has - you cannot run 'graphical' applications; also, I don't really need a shell, as I am quite happy with zsh (zed shell) already, which is more powerful, and I prefer a shell that works both inside and outside emacs.

For all these reasons, I am using MultiTerm, which has 'graphical' support that ansi-term has, but adds a nice extra, namely support for multiple terminals within emacs. I'm not fully up to date with the exact difference in the terminal support between the two, but I haven't had any problems so far.

You can install multi-term (put it in your load-path), and add the following to your .emacs:

(autoload 'multi-term "multi-term" nil t)
(autoload 'multi-term-next "multi-term" nil t)

(setq multi-term-program "/bin/bash")   ;; use bash
;; (setq multi-term-program "/bin/zsh") ;; or use zsh...

;; only needed if you use autopair
(add-hook 'term-mode-hook
  #'(lambda () (setq autopair-dont-activate t)))


(global-set-key (kbd "C-c t") 'multi-term-next)
(global-set-key (kbd "C-c T") 'multi-term) ;; create a new one

With this, C-c t will jump through your multi-term buffers (create a new one if it doesn not exist yet), while C-c T unconditionally creates a new terminal.

2010-06-17

automatic pairing of brackets and quotes

Some text-editors, notably TextMate for MacOS, have a nice feature where inserting a opening ( will automatically insert the closing ), and put the cursor in between them (and does same for [], {}, and various quote-marks).

Not surprisingly, there are some implementations for emacs as well; the best one I have found so far is called autopair, which was written by João Távora. It usually does things just right. Do things 'just right' is essential for such a tool; even small annoyances can disturb your flow. Autopair tries to do whatever makes the most sense for a given mode (programming language etc.), but it can be tuned as well.

After installation, you can automatically activate it for all modes with (in your .emacs):

(require 'autopair)
(autopair-global-mode 1)

Now, evaluate this or restart emacs, and enjoy the autopairing-magic!

Except for autopairing, autopair also takes care of autocleaning; that is, if I press ( it turns that into () (the pairing part), and if I press Backspace then, it removes the whole () (the cleaning part). This makes things much less annoying if you type a pair by accident. Autopairing is the kind of thing that can get annoying quickly if it does not things exactly right – and autopair succeeds!

Another nice trick it offers is autowrapping – that is, I select a word, press ", and automatically it's turned into "word". To enable that, you need to add the following:

(setq autopair-autowrap t)

Note: you might want to see the notes below about delete-selection-mode and cua-mode.

Anyway, autopair with autowrap makes for a really smooth editing experience, I love it! There are two small issues for me though. First, when the cursor in front of some non-whitespace, I'd like autopairing not to happen, and second, somehow I can't seem to get "-autopairing to work in org-mode; of course, that could be my own fault. These things might be tunable; I haven't tried very hard yet.

delete-selection-mode

Important to mention here is that autopair is (by default) not fully compatible with delete-selection-mode. As you may know, that is the mode that causes emacs to replace the current selection with a character typed, similar to what most other programs do. I think many people have it enabled in their .emacs with something like:

(delete-selection-mode 1)

If you want to keep on using that together with autopair, add the following to your .emacs:

(put 'autopair-insert-opening 'delete-selection t)
(put 'autopair-skip-close-maybe 'delete-selection t)
(put 'autopair-insert-or-skip-quote 'delete-selection t)
(put 'autopair-extra-insert-opening 'delete-selection t)
(put 'autopair-extra-skip-close-maybe 'delete-selection t)
(put 'autopair-backspace 'delete-selection 'supersede)
(put 'autopair-newline 'delete-selection t)

But, not that that still won't give you the autowrap behavior mentioned above. For that, we can use cua-mode.

cua-mode

We discussed CUA-mode before, focusing on its nice rectangle-editing features. But CUA-mode can also be an alternative for delete-selection-mode, and it goes together more nicely with autopair; so, instead of delete-selection-mode and the put's, add the following to your .emacs:

(setq cua-enable-cua-keys nil)           ;; don't add C-x,C-c,C-v
(cua-mode t)                             ;; for rectangles, CUA is nice

See the linked CUA-mode article for the 'why' of that first line. With this change, autopair should be working smoothly, including autowrap.

further customization

As I have hinted at, autopair can be tuned for different modes, and can differentiate between it's behaviour in literal strings, code, comments etc. The default are usually sane, but if you're interested, have a look at the documentation, in particular autopair-extra-pairs and the More tricks-section in the documentation.

2010-06-10

worldcup games in your org-mode agenda

A significant part of the world population will be watching the Football World Cup in South-Africa this month. For people who use org-mode to organize their lives, find the schedule of all the games in this message I sent to the org-mode mailing list.

In order to have the games show up in your agenda, make sure the file is in your org-agenda-files. If needed, you could add it with something like in your org-mode settings:

(add-to-list 'org-agenda-files "~/org/fifa-worldcup-2010.org")

One small issue with the schedule is that it use the South-African times, and there is no automatic way to adjust times for the local time zone. As a work-around, Juan Pechiar provided the following function which makes it easy to update all org-timestamps in a file:

(defun uphours (n)
  "update all timestamps n hours"
  (interactive "nAdd hours: ")
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "[[<]" nil t)
      (when (org-at-timestamp-p t)
        (org-timestamp-change n 'hour)
        ))))

Evaluate this function (in emacs, put your cursor after the last ")"), then press C-x C-e. After that, you can go to the file with the world cup schedule, and give an M-x uphours, provide the offset for your timezone, compare to South-African time (positive or negative).

2010-05-20

100th post

100

With that last post, emacs-fu reached the 100 posts milestone! Hurray! Thank you for all the support, it's been a great ride so far, and there's so much more to write about - if only there were 36 hours in a day.

Anyway, to celebrate, I'll be off for the coming weeks (Korea), and I'm not sure if I have much time to blog from there. So, let's take this opportunity for a small reader poll: what would you be interested to read about? More programming-related stuff, more org-mode, more about integration with other programs, more interviews, more …?

Please leave your ideas in the comments. I'd be interested to hear!

Update: I am back now; thanks for all the replies. It seems that many people are interested in CEDET. In fact, I am interested in it myself as well, but am not using it right now, so it will take a while. For the time being, Alex Ott's Gentle Introduction might be the best way to get started.

zenburn for org-mode-generated html

If you read this blog directly, instead of through some aggregator or feed-reader, you can now see the code blocks rendered in the nice zenburn color theme that I discussed before. I'm really enjoying it, so I added some style sheet definitions, so org-mode #+BEGIN_SRC / #+END_SRC blocks look as such in the web page (and just like they look on my screen), for instance:

(defun fibo (n) 
     "calculate the Nth (N>=0) fibonacci number in a simple yet
  inefficient way"
    (cond
      ((= n 0) 0)
      ((= n 1) 1)
      (t (+ (fibo (- n 1)) (fibo (- n 2))))))

;; now, gimme a list of fibo numbers 0..20
(mapcar 'fibo (number-sequence 0 20))

Note, I discussed the use of such code blocks earlier; it's one of many nice features of org-mode. Only quite recently I found that I can press C-c ' in such a code block to edit them in a mode-specific little buffer… something new and obvious to learn every day.

Anyhow, to get the nice zenburn-output in the generated HTML, you can use the following CSS (note, so far I have only done the code blocks):

/* zenburnesque code blocks in for html-exported org mode */

pre.src {
   background: #3f3f3f; color: #dcdccc; 
}

.org-preprocessor {
   color: #8cd0d3;
}

.org-preprocessor {
   color: #8cd0d3;
}

.org-variable-name {
   color: #f0dfaf;
}

.org-string {
   color: #cc9393;
}

.org-type {
   color: #dfdfbf;
   font-weight: bold;
}

.org-function-name {
   color: #8cd0d3;
   font-weight: bold;
}

.org-keyword {
   color: #f0dfaf;
   font-weight: bold;
}

.org-comment {
   color: #7f9f7f;
}

.org-doc {
   color: #afd8af;
}

.org-comment-delimiter {
   color: #708070;
}

.org-constant {
   color: #dca3ac;
   font-weight:bold;
}

.org-builtin {
   color: #7f9f7f;
}

You can save the above CSS-blob in a file (say, zenburn-code.css), and set the style sheet for the org-html export by putting a #+STYLE:-line in your org files:

#+STYLE: <link rel="stylesheet" type="text/css" href="zenburn-code.css" />

2010-05-14

emacs 23.2

Recently, emacs version 23.2 was released. It's a quick update after 23.1 came out (July 29 of 2009); it seems Chong Yidong / Stefan Monnier (interview) are doing releases more often than before they took over emacs maintainership. A welcome change, I would say.

The amount of changes is obviously also a bit smaller than in 23.1, but there are still some interesting updates. Let's go through a couple of those here; I am not striving for completeness, and I won't really go into the biggest change (inclusion of the CEDET IDE-framework), as I haven't been using that enough to say anything about it. Instead, let's look at some of the other highlights; for the full list of changes, please refer to the NEWS-file. If you have some other cool new feature that deserves mentioning, please add a comment.

Some highlights

  • Maximum file size increased to 512Mb (this used to be 256 on 32-bit machines). This may be useful for big log files etc. It does take a while to load such big files, but after that it's not too slow, at least if you have enough RAM. For 'normal' files, you're unlikely to ever hit the limit; e.g. Tolstoy's War and Peace is only 3 MB…

    Note, you can set large-file-warning-threshold to set the maximum file size after which emacs will starting asking you for confirmation when trying to open (eh, visit) files.

  • By default, the mouse cursor turns invisible when typing, so there is no more need for mouse-avoidance-mode and similar tricks. However, if you insist on seeing the mouse cursor, you can add to your .emacs:
(setq make-pointer-invisible nil)
  • On X-based systems, emacs now listens for font changes (Xft), and can automatically use the GNOME mono-spaced font (as set in the GNOME Appearance preferences dialog). Note that this may not work for all fonts/settings (at least in my tests, setting the font to italic does not seem to reflect in emacs). Anyway, to enable this, put the following in your .emacs (or the moral equivalent):
(setq font-use-system-font t)
  • On Unix, Emacs 23.2 now follows the freedesktop trash specification for file deletion; thus, the hacks we hacks we mentioned before are no longer needed.
  • Some cool additions for Tramp, allowing emacs to access files in rsync and even imap://-locations. On systems supporting GVFS, emacs can now directly use e.g. obex://-uris (Bluetooth). I need to play a bit with these things! Tramp support has also been built into eshell.
  • There are already quite some ways to do auto-completion in emacs using the TAB-key, and emacs 23.2 makes this a bit easier to set up. You can add basic auto-completion with:
(setq tab-always-indent 'complete)
  • After setting that, the TAB-key will (after trying if there's anything to indent first) provide possible completions. It works pretty well for Emacs-Lisp (I did not test other languages), although the way it shows the completions (separate *Completions*-buffer) is a bit clumsier that what for instance yasnippet or company-mode do.
  • You can also do partial completions now, by appending initials to the completion style, i.e.:
;; there must be a more elegant way...
(setq completion-styles (append completion-style '(initials)))
  • With this, you can complete on the initials of functions and variables in the minibuffer, say, typing C-h v tai and then pressing TAB will give you tab-always-indent.
  • As mentioned, the biggest change is the addition on the CEDET-framework, which contains things like smart completion, code browsing, UML diagram creation, project management – features somewhat similar to those in e.g. Eclipse. I don't know how well it works in practice, but I will give it a try. At least, inclusion in Emacs should make setting it up with all dependencies a bit easier, as there is now a guaranteed-to-work setup for Emacs 23.2 at least.

Summarizing, 23.2 provides us with some nice updates all around and brings CEDET to the masses. Chong Yidong / Stefan Monnier have done a very good job in making faster releases, while still keeping an eye on the quality. On the other hand, the previous version (23.1) is a very solid release, and if you don't need CEDET, there is no real need to hurry to 23.2.

Future releases

A lot is happening in the world of GNU/Emacs, with changes being proposed and implemented in many different places. There's Eli Zaretskii's way work on making emacs support bidirectional languages (for right-to-left writing systems such as Hebrew and Arabic; the /bi/directional part is that one should be able to mix left-to-right and right-to-left). There is Jan Djärv's work on adding UI-tabs to emacs (like e.g. Firefox has them). There is Ken Raeburn and Andy Wingo's work on adding Guile Scheme support to emacs - possibly replacing the current Emacs Lisp implementation in the future. These are just a few of the more prominent projects.

Nobody knows in which release these items will be available (if at all), but it's exciting to see all the directions people are taking emacs.

2010-05-12

cleaning up the mode-line

Emacs' version on a status-bar is called the mode-line, and contains all kind of information – the current buffer name, the cursor position and a lot of other things, depending on what major and minor modes are active.

Customizing the mode-line is, unfortunately, rather hard. One day, I'll write something about that… but for now at least we may be able to improve things a little bit, by reducing mode line pollution. Mode line pollution? Well, many parts of emacs like to announce their presence and state in the mode line. With the limited space available there, this can become a bit of an issue, the (Lisp Interaction company Yas abbrev) takes quite some space:


But there are some ways to limit the space taken by modes and minor-modes. Note, these snippets should go in your .emacs, and you need to restart emacs to make them active.

First, the minor modes (note, you can see the currently activated ones with C-h m); install the handy diminish.el (or get it using the emacs-goodies-el package when using Debian/Ubuntu) and add something like the following:
(when (require 'diminish nil 'noerror)
  (eval-after-load "company"
      '(diminish 'company-mode "Cmp"))
  (eval-after-load "abbrev"
    '(diminish 'abbrev-mode "Ab"))
  (eval-after-load "yasnippet"
    '(diminish 'yas/minor-mode "Y")))

And the major-modes, for example for Emacs Lisp mode:
(add-hook 'emacs-lisp-mode-hook 
  (lambda()
    (setq mode-name "el"))) 
This looks a bit shorter:

You can of course set these names to whatever is obvious to you.