2009-12-30

automatically checking your spelling

When I'm typing fast, it's easy to make spelling mistakes (as you may have noticed reading Emacs-Fu). It's not so much that I don't know how to write things, but sometimes there seems to be a bit of a disconnect between brain and fingers.

One tool that helps me to make fewer mistakes is automatic spelling checking, and in particular on-the-fly spelling checking. Spell-checking in emacs is taken care of by the ispell-package. Traditionally, this was a way to interface emacs with the ispell-program on Unix, but nowadays it's often used with other programs, such as aspell.

aspell is GNU's intended replacement for ispell, and has been for a long time. In the meantime, other spelling checkers have come up; in the Free Software world, the most prominent one is probably hunspell, which is used by e.g. Mozilla and OpenOffice. As I said, Emacs supports all of those; I'm using aspell, which works well for me. To use aspell for spelling-checking, I have the following in my .emacs:

(setq ispell-program-name "aspell"
  ispell-extra-args '("--sug-mode=ultra"))

Please consult the aspell documentation for the details.

You can spell-check your text with ispell-buffer and friends, which are also available through the menu (Tools/Spell Checking/...). This works fine, but it makes spelling checking a separate step you have to go through and you may forget. So, I like to do the spelling-checking on-the-fly, that is, while I am typing. This can be done using flyspell-mode (you can also use flyspell-prog-mode, to do spell checking inside comments in computer programs, but I find that a bit over the top).

When flyspell-mode is enabled, it will mark misspelt eh misspelled words by painting them in a different color while you are typing -- the common way many word processing programs do it. A common way to enable flyspell-mode is to put it the the mode-hook for the modes where you want to use it. For example, to enable flyspell-mode for all your org-mode buffers, you can add something like the following to your .emacs:

(add-hook 'org-mode-hook
  (lambda()
    (flyspell-mode 1)))

Note, you can use the middle mouse button to get alternatives for a misspelled word.

By default, it uses the English dictionary, but it's easy to switch to another using M-x ispell-change-dictionary. To make it easier, I have defined the C-c N key binding to activate Dutch-language ("nederlands") spelling checking, and update the buffer.

(global-set-key (kbd "C-c N") 
  (lambda()(interactive)
    (ispell-change-dictionary "nederlands")
    (flyspell-buffer))) 

Now, there's another category of mistakes – their-they're-there, its-it's or to-too-two that require a spelling checker that's a bit smarter. There are some free implementations in OpenOffice and Abiword; it'd be interesting to see if those could be integrated with emacs as well.

Now, laugh about the sweet irony of the spelling errors that I failed to notice :)

2009-12-13

scrolling

Scrolling It's an integral part of just about any graphical user interface, including emacs. However, I always found that the default way scrolling works in emacs left something to be desired. It puts the scroll bar on the left (!), and when scrolling around, it does not scroll smoothly, but instead it seem to do so in bursts. But, this being emacs, we can change it!

First, the position of the scroll bar. Presumably for historical reasons, emacs puts the scroll bar on the left of the window, unlike most other programs. We can easily change that, by putting the following in .emacs (or ~/.emacs.d/init.el):

(set-scroll-bar-mode 'right)

Instead of right, you can also use left, or nil to hide the scroll bar completely. You can also do this through the menu (Options / Show/Hide / Scroll bar). Note that on X, when the cursor (point) reaches the end of the document, the slider on the scroll bar may not be at the bottom; I understand this is because of some disagreement between Emacs and the toolkit (GTK+ in this case).

Now, what about the other issue, the non-smoothness when scrolling with the cursor-keys or with C-n, C-p? Below are my settings for making scrolling a bit smoother, and the explanation. Of course, these are just my personal preferences.

(setq
  scroll-margin 0                  
  scroll-conservatively 100000
  scroll-preserve-screen-position 1)
  • The scroll-margin. This determines when scrolling should start; by setting it to 0, emacs will start to scroll whenever you are entering the top or bottom line of the window. You can also this to, say, 5 to let scrolling start whenever you're getting closer than 5 lines from top or bottom
  • Then, scroll-conservatively determines how far the cursor is allowed to be distanced from the center of the screen when scrolling start. The default sets this to 0, which means that whenever you start scrolling, the cursor jumps to the center of the screen. I find that quite annoying, so I set it to some big number (the 'effective maximum' for that is lines-in-window / 2, but you can put any bigger number there to avoid the jumpiness)
  • scroll-preserve-screen-position tries to maintain the current screen position when you scroll using Page-Up/Page-Down. I like that.

There are also the variables scroll-up-aggressively and scroll-down-aggressively. Normally, they determine how far emacs will scroll (up and down, respectively) when it does so. However, they don't make any difference with a big scroll-conservatively like I am using. Still, if you want to play with it, their values are fractions between 0.0 and 1.0 (inclusive); a value of 1 means that it will move a full screen when scrolling starts, a value of 0.0 causes a move of only one single line.

2009-12-06

changing the cursor color and shape dynamically

When typing some text, it's often useful to know what mode we're in – are we in overwrite-mode, in read-only-mode, or in normal insert-mode. The information is available in the mode-line – but wouldn't it be nicer to get some small visual cue, for example by changing the cursor color or style?

That indeed is possible. There are some existing ways to do this, described in EmacsWiki. However, I want to be able to control both the cursor color and cursor shape, and also distinguish between overwrite, read-only and 'normal' mode. Below is my attempt.

By putting the following snippet in your .emacs, the cursor becomes a yellow vertical bar during normal mode, it becomes a red block when you're in overwrite-mode and it becomes a gray vertical bar when you're in read-only mode.

;; Change cursor color according to mode; inspired by
;; http://www.emacswiki.org/emacs/ChangingCursorDynamically
(setq djcb-read-only-color       "gray")
;; valid values are t, nil, box, hollow, bar, (bar . WIDTH), hbar,
;; (hbar. HEIGHT); see the docs for set-cursor-type

(setq djcb-read-only-cursor-type 'hbar)
(setq djcb-overwrite-color       "red")
(setq djcb-overwrite-cursor-type 'box)
(setq djcb-normal-color          "yellow")
(setq djcb-normal-cursor-type    'bar)

(defun djcb-set-cursor-according-to-mode ()
  "change cursor color and type according to some minor modes."

  (cond
    (buffer-read-only
      (set-cursor-color djcb-read-only-color)
      (setq cursor-type djcb-read-only-cursor-type))
    (overwrite-mode
      (set-cursor-color djcb-overwrite-color)
      (setq cursor-type djcb-overwrite-cursor-type))
    (t 
      (set-cursor-color djcb-normal-color)
      (setq cursor-type djcb-normal-cursor-type))))

(add-hook 'post-command-hook 'djcb-set-cursor-according-to-mode)

You can change the colors and cursor types by modifying the various variables.

I should probably turn this into a proper minor mode, but for now this seems to work well.