Emacs Extension: Binary-search-inspired movement

Moving forward and backward word-by-word is inefficient, so I thought it would be useful to reduce the distance between your cursor and your desired position on the line using something like binary search.

If you load this emacs extension (in your init file, etc.), then C M-f will move your cursor forward to the midpoint between the current position and the end of the line. C M-b will move your cursor backward to the midpoint between the current position and the beginning of the line.

(global-set-key "\C-\M-b" 'backward-midpoint)
(global-set-key "\C-\M-f" 'forward-midpoint)

(defun backward-midpoint ( )
"Move backward to midpoint between current position and beginning of line."
(interactive)
(backward-char (/ (- (point) (line-beginning-position)) 2)))

(defun forward-midpoint ( )
"Move forward to midpoint between current position and end of line."
(interactive)
(forward-char (/ (- (line-end-position) (point)) 2)))

This is not the same as binary search, because C M-f, C M-f, C M-b will move the cursor to a position that is before the midpoint of the line. However, it has some properties of binary search and it is practically more useful than word-by-word linear search.

Advertisements
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s