2012-02-10

special characters

Special characters

Living in a post-ASCII world offers great opportunities, but brings some problems, too. After all, it's nice to be able to write Ångström or Καλημέρα or ☺☎☯, but it's not necessarily easy to enter those characters.

input methods

So - what to do? First, you can set the input-method, as explained in the emacs manual. This is the best solution if you're writing a non-Latin language – Russian, Thai, Japanese, …

If you only occasionally need some accented character, input methods like latin-postfix (e" -> ë), latin-prefix ("e -> ë) or TeX (\"e -> ë) are useful. They also tend to annoy me a bit, as they often assume I need an accented character, when all I want is to put a word in quotation marks…

compose key

Another method is to use a special compose key; for example, under Gnome 3 it's in the the Region and Language applet, under Options..., in Gnome 2 it's in the Keyboard applet in Layouts/Options…. This works for all programs, not just emacs (see this Ubuntu help page for some details). I've set my Compose-key to Right-Alt, so Right-Alt "e -> ë.

Using the compose key works pretty well for me; setting the input method may be more convenient when you need to write a lot of accented characters.

Now, his may be good and well for the accented characters and other variants of Latin characters, such as the German Ess-Zet ligature "ß" (note, you can get that character with latin-prefix "s -> ß, latin-postfix s" -> ß or <compose-key> ss -> ß). But what about Greek characters? Mathematical symbols? Smileys?

ucs-insert

One way to add characters like α, ∞ or ☺ is to use ucs-insert, by default bound to C-x 8 RET. If you know the official Unicode name for a character, you can find it there; note that there's auto-completion and you can use * wild-cards. For the mentioned characters, that would be GREEK SMALL LETTER ALPHA, INFINITY and WHITE SMILING FACE.

You can also use the Unicode code points; so C-x 8 RET 03b1 RET will insert α as well, since its code point is U+03B1. In case you don't know the code points of Unicode characters, a tool like the Character Map (gucharmap) in Gnome may be useful.

abbrev

Since ucs-insert may not be convenient in all cases, you may want to add shortcuts for oft-used special characters to you abbrev table. See the entry on Abbrevs in the emacs manual. I usually edit the entries by hand with M-x edit-abbrevs, and I have entries like:

(text-mode-abbrev-table)
"Delta0"       0    "Δ"
"^2"           0    "²"
"^3"           0    "³"
"almost0"      0    "≈"
"alpha0"       0    "α"
"any0"         0    "∀"
"beta0"        0    "β"
"c0"           0    "©"
"deg0"         0    "℃"
"delta0"       0    "δ"
"elm0"         0    "∈"
"epsilon0"     0    "ε"
"eta0"         0    "η"
"heart0"       0    "♥"
"inf0"         0    "∞"
"int0"         0    "∫"
"notis0"       0    "≠"

Now, alpha0 will be auto-replaced with α. I'm using the 0 suffix for most entries so I can easily remember them, without making it hard to use alpha as a normal word. Note, abbrevs are a bit picky when it comes to the characters in the shortcut – for example, setting != -> ≠ won't work.

inheriting abbrevs from other modes

If you have set up a nice set of abbreviations for text-mode, you may want to use them in other modes as well; you can accomplish this by including the text-mode abbreviations into the table for the current one, for example in your ERC setup:

;; inherit abbrevs from text-mode
(abbrev-table-put erc-mode-abbrev-table :parents (list text-mode-abbrev-table))

2012-02-01

update

Regular emacs-fu programming will resume shortly, but for now I'll provide a brief update on some of my projects.

  • Recently, I discussed sauron, and event-tracking tool for emacs. I added some new features, fixed some bugs, and got some contributions (yay!)
    • tweak/improve priority handling
    • add a backend for the emacs-24 notification system
    • add settings to make the sauron frame 'sticky', and to hide the mode-line
    • enable dbus-message source outside your session; useful for cron/procmail etc.
    • display events in a tabular fashion
    • add support for John Wiegley's event.el
    • much improved documentation
    • other things…

    Sauron version 0.2 is available through Marmalade (see Package Management Revisited)

  • I released version 0.9.8 of mu (e-mail searcher/indexer, previously discussed in Searching e-mails with Wanderlust and mu). Now, for emacs-users I've added mu4e (manual), a new & experimental e-mail client. It won't be big and professional like gnus, but it's fun to hack on, and I've been using it for a few months.

2011-12-22

sauron: keeping an eye on what's going on

I'm a fairly busy person, and need to keep track of a lot of things. That includes following a bunch of internal IRC channels, attending meetings, meeting deadlines and so on. But I don't want to stare at my org-mode calendar, or flip through ERC buffers all the time.

Instead, I'd like to have one little emacs frame (window) that gathers these ('events'), and transfers me to wherever the event came from when I click it - some IRC-channel in ERC, my org-calendar etc. and other inputs. Note, using Bitlbee, you can include Facebook-contacts, GoogleTalk-buddies and Twitter-tweets, … in ERC - so, you can track just about anything.

In addition, with so many inputs, I'd also like the possibility to filter out unwanted events, and generate various light/sound effects and fireworks, proportional to the priority of the event.

For all this, I wrote a little emacs-tool called Sauron that does just that. M-x sauron-start pops up a frame that receives events, and M-x sauron-stop hides it and stops listening. It works with ERC, org, and listens for D-Bus messages; so it's pretty easy to get events from all over the place.

It's a bit of a balancing act to get all the important information while not being swamped in noise, but Sauron allows you to fine-tune it to whatever works the best for you. I've tried to have sane defaults though, so things should mostly work without too much configuration - but if you need the power, it's there. I also added some convenience functions to make it easy to get sounds and other special effects.

So - it's brand new, it is of seems-to-work-for-me-quality, and I'd like to invite others to try it out, hack it, give feedback, add new back-ends and so on – what better Christmas present to ask for!

There's documentation, examples etc. to be found in Sauron's github repository.

2011-12-10

system administration with emacs

When performing system administration tasks, one often needs to edit files owned by root.

For both security and safety reasons, it's a good idea to do as little as possible as root (or with root privileges). For that reason, you probably don't want to run Emacs as 'root', because it's simply too powerful. I often see people use vi (usually, vim) instead – but since it allows you to do just about anything as well (like running shell commands), that's not much of an improvement.

Now, a while back we discussed editing files owned by root with tramp – you can use emacs with your normal user-account, and use sudo to write the root-owned file. That makes it much harder to screw things up.

Another reason people use vi for little editing jobs is because its startup time is significantly shorter than the startup time for a new emacs instance. For that, however, we have emacs daemon.

Combining tramp and emacs daemon and a shell function:

# edit file with root privs
function E() {
         emacsclient -c -a emacs "/sudo:root@localhost:$1"
}               

Now, we can very quickly edit any file owned by root using 'E' --

$ E /etc/hosts

So, if you prefer emacs, there's little reason to use vi, even for editing system files – although I have to admit that it takes time to evict 'sudo vi <system file>' from my muscle memory.

Update reader Yi Wang mentions that, in fact, we can make this a bit more general using sudoedit; so, instead of using Tramp, we can use:

# edit file with root privs
alias E="SUDO_EDITOR=\"emacsclient -c -a emacs\" sudoedit"   

This works also without absolute paths.

2011-11-21

package management revisited

Ages ago we discussed ELPA, the emacs packaging system. It allows you to
browse through an online repository of emacs packages and install, uninstall
and upgrade them, all from within the emacs comfort zone (menu:
Options/Manage Emacs Packages).

Emacs 24.1 (a pretest is available) will include a version of ELPA --
package.el; the version included with emacs 24 supports multiple
archives. The official archive is the one provided by GNU, then there is
the current one for ELPA, but there's now a third one available: Marmalade.

Marmelade ('Spreadable Elisp') offers a very easy way for developers to
make their packages available to emacs users, and an even easier way for
users to test out interesting packages.

To use all available package repositories (GNU, ELPA and Marmalade), add
the following snippet to your .emacs:

(setq package-archives '(("ELPA" . "http://tromey.com/elpa/") 
                          ("gnu" . "http://elpa.gnu.org/packages/")
                          ("marmalade" . "http://marmalade-repo.org/packages/")))

This works out-of-the-box for Emacs 24; for Emacs 23, you'll need the newer
package.el, which you can get from the emacs 24 repo (if necessary, remove
the old package.el first).

Currently, there are 870 packages listed in the three repositories – a lot of
toys to play with! In fact, it is already getting quite hard to find what
you're looking for; a bit more structured way (maybe using categories) would
be useful. Also, a bit more information than the one-line description would
be very useful.

Nevertheless, it's a great addition to emacs, which will make it much easier
to play with packages – without even having to read the installation
instructions.

2011-09-15

quick note-taking with deft and org-mode

Emacs must be gathering a lot of enthusiasts lately; there's hardly a week where I don't discover some new gem. Recently, I discovered deft. And apparently, I wasn't the only one.

So what is it deft good for? Well, often I want to jot down some quick thing during a meeting or a telephone-call. Of course, I don't want to think about file names or anything else distracting me from my task, just get me that note already! In addition, at some later time, I want to be able to quickly search through the notes I made.

For MacOS, there's a program called Notational Velocity which does this. But really - it sounds like a typical task for emacs - wouldn't it be nice to have an emacs package that does roughly the same?

And that is what deft does - enable you to quickly write notes, and retrieving them later. The author has an excellent introduction on his website, so my job is very easy :) deft is not part of org-mode, but they can work together seamlessly. Here's my set-up:

;; http://jblevins.org/projects/deft/
(when (require 'deft nil 'noerror) 
   (setq
      deft-extension "org"
      deft-directory "~/Org/deft/"
      deft-text-mode 'org-mode)
   (global-set-key (kbd "<f9>") 'deft))

This blob goes in my .emacs. Note, the first line ensures that emacs starts without errors, even when I run on a system without deft. Apart from that, I make deft use org files for note taking, which makes it all very familiar.

All notes are saved ~/Org/deft - you can set it to something else of course. A Dropbox-folder seems to be a popular choice for synchronizing between machines.

Finally, the last line binds F9 to deft-mode. So, when I need a quick note, I can type F9 C-c C-n and start writing.

2011-09-08

finding just about anything

Buffer-switching is one of those things I do all the time in Emacs. And it should be quick and efficient, or it will break my 'flow'. There are many ways to customize the buffer switching experience, and we already discussed quite a few of those here: using Ctrl-Tab, iswitchb/ido, ibuffer, the lusty explorer and others.

Some of those packages - ido and lusty-explorer - do not solely focus on buffer-switching - they also let you open files using the same user interface. But why stop at files and buffers - wouldn't it be nice if we could quickly find just about anything quickly?

Indeed that would be nice - and there's a way to do just that - using the aptly named anything package. I was always a bit put off by this package due to the screenshots (see link), but once I got over that, I've become a very happy user.

Anyhow, what can anything find? I mentioned buffers and files, but it can also find bookmarks, recently used files, any file on your system (using locate), man-pages, info-pages, emacs-function/variables, FIXME-comments, org-headlines, bbdb-contacts, google-suggests… and a million other things. It can probably find your car keys, too.

Installation is pretty straightforward, using the git-repository:

git clone git://repo.or.cz/anything-config.git

Then, go into the just-cloned directory and execute make. After that, add to your .emacs (or ~/.emacs.d/init.el):

;; path-to-anything is the path which has the 'anything' we just cloned
(add-to-list 'load-path "path-to-anything")
(require 'anything-config)

This will add a menu with various anything-commands, and a bunch of key-bindings, starting with F5. Play around a bit with it, it's nice. The results are shown in a little temporary buffer, and pressing Tab will let you do (search-type dependent) actions on the matches - for example ediff two buffers.

Of course, the real fun starts when we super-charge some of the normal emacs functions with anything-based ones. Let's look at buffer switching. Let's create an anything-based version, and assign it to C-x b, the good-old switch-to-buffer.

The thing to consider is that while anything can find just about anything, you (well, I) usually only want to search for a certain set of things; when I want to switch to another buffer, I don't want to match man-pages. Luckily, anything allows for making nifty function which use certain subsets of the search sources. So for buffer switching, I have:

(global-set-key (kbd "C-x b")
  (lambda() (interactive)
    (anything
     :prompt "Switch to: "
     :candidate-number-limit 10                 ;; up to 10 of each 
     :sources
     '( anything-c-source-buffers               ;; buffers 
        anything-c-source-recentf               ;; recent files 
        anything-c-source-bookmarks             ;; bookmarks
        anything-c-source-files-in-current-dir+ ;; current dir
        anything-c-source-locate))))            ;; use 'locate'

This will search in buffers, then in my recent-files, then in my bookmarks, the files in the current directory, and finally check with the locate tool. That last one is pretty neat, and finally gives me something back for the countless times I've wondered why the hard disk is grinding – indeed, it was locate, updating its database.

Then, I've defined another binding for searching general documentation on my system; I've put it under C-c I. This looks look something like the following:

(global-set-key (kbd "C-c I")  ;; i -> info
  (lambda () (interactive)
    (anything
      :prompt "Info about: "
      :candidate-number-limit 3
      :sources
      '( anything-c-source-info-libc             ;; glibc docs
         anything-c-source-man-pages             ;; man pages
         anything-c-source-info-emacs))))        ;; emacs 

These are sources I query quite regularly; there are many more to be found - for most packages with info-pages there's a corresponding anything-c-source-info-...; there's a list in anything-config.el.

Now, those are my general documentation sources; in specific modes, I have specialized information sources; for example, for elisp-mode:

(add-hook 'emacs-lisp-mode-hook
  (lambda()
  ;; other stuff...
  ;; ...
  ;; put useful info under C-c i
    (local-set-key (kbd "C-c i")
      (lambda() (interactive)
        (anything
          :prompt "Info about: "
          :candidate-number-limit 5
          :sources
          '( anything-c-source-emacs-functions
             anything-c-source-emacs-variables
             anything-c-source-info-elisp
             anything-c-source-emacs-commands
             anything-c-source-emacs-source-defun
             anything-c-source-emacs-lisp-expectations
             anything-c-source-emacs-lisp-toplevels
             anything-c-source-emacs-functions-with-abbrevs
             anything-c-source-info-emacs))))

This post just scratches the surface of what is possible – so go and experiment :) One interesting thing is to add your own sources; I played a bit with that already,

There are many things in anything I did not cover yet. First, there are many more sources to search - and it's pretty easy to write your own – see the EmacsWiki-page.