Node:Simple Extension, Next:, Previous:Autoload, Up:Emacs Initialization



16.11 A Simple Extension: line-to-top-of-window

Here is a simple extension to Emacs that moves the line point is on to the top of the window. I use this all the time, to make text easier to read.

You can put the following code into a separate file and then load it from your .emacs file, or you can include it within your .emacs file.

Here is the definition:

;;; Line to top of window;
;;; replace three keystroke sequence  C-u 0 C-l
(defun line-to-top-of-window ()
  "Move the line point is on to top of window."
  (interactive)
  (recenter 0))

Now for the keybinding.

Nowadays, function keys as well as mouse button events and non-ASCII characters are written within square brackets, without quotation marks. (In Emacs version 18 and before, you had to write different function key bindings for each different make of terminal.)

I bind line-to-top-of-window to my <F6> function key like this:

(global-set-key [f6] 'line-to-top-of-window)

For more information, see Init Rebinding.

If you run two versions of GNU Emacs, such as versions 20 and 21, and use one .emacs file, you can select which code to evaluate with the following conditional:

(cond
 ((string-equal (number-to-string 20) (substring (emacs-version) 10 12))
  ;; evaluate version 20 code
  ( ... ))
 ((string-equal (number-to-string 21) (substring (emacs-version) 10 12))
  ;; evaluate version 21 code
  ( ... )))

For example, in contrast to version 20, version 21 blinks its cursor by default. I hate such blinking, as well as some other features in version 21, so I placed the following in my .emacs file1:

(if (string-equal "21" (substring (emacs-version) 10 12))
    (progn
      (blink-cursor-mode 0)
      ;; Insert newline when you press `C-n' (next-line)
      ;; at the end of the buffer
      (setq next-line-add-newlines t)
      ;; Turn on image viewing
      (auto-image-file-mode t)
      ;; Turn on menu bar (this bar has text)
      ;; (Use numeric argument to turn on)
      (menu-bar-mode 1)
      ;; Turn off tool bar (this bar has icons)
      ;; (Use numeric argument to turn on)
      (tool-bar-mode nil)
      ;; Turn off tooltip mode for tool bar
      ;; (This mode causes icon explanations to pop up)
      ;; (Use numeric argument to turn on)
      (tooltip-mode nil)
      ;; If tooltips turned on, make tips appear promptly
      (setq tooltip-delay 0.1)  ; default is one second
       ))

(You will note that instead of typing (number-to-string 21), I decided to save typing and wrote `21' as a string, "21", rather than convert it from an integer to a string. In this instance, this expression is better than the longer, but more general (number-to-string 21). However, if you do not know ahead of time what type of information will be returned, then the number-to-string function will be needed.)


Footnotes

  1. When I start instances of Emacs that do not load my .emacs file or any site file, I also turn off blinking:

    emacs -q --no-site-file -eval '(blink-cursor-mode nil)'