(blog-of ‘Alex’)

A personal blog about software development and other things. All opinions expressed here are my own, unless explicitly stated.

Easy to Use Bookmarks

Several days ago I wrote a small function that provides easy-to-use bookmarking for emacs.

It just binds C-{index} keys to save bookmark (in fact save point to register) and M-{index} - to jump to the saved position, index is a key from 1 to 9.

Here is the code:

(defun bind-navigation-commands-to-numkeys ()
  "provides easy-to-use bookmarking functionality - binds navigation commands to
C-{index}, M-{index} keys, where index is a numeric key from 1 to 9,
C-{index} - saves bookmark, M-{index} - jumps to the saved point"
  ;; assign handlers for C/M-[1..9] keys
  (loop for key-index from 1 to 9 do
       (let ((key-str (int-to-string key-index)))
	 ;; save point
	 (local-set-key
	  ;; retrieve C-{index} keyboard sequence
	  (read-kbd-macro (concat "C-" (int-to-string key-index)))
	  ;; handler form
	  `(lambda ()
	     (interactive)
	     (point-to-register ,key-index)
	     (message (concat "setting bookmark #" ,key-str))))
	 
	 ;; goto saved point
	 (local-set-key
	  ;; retrieve M-{index} keyboard sequence
	  (read-kbd-macro (concat "M-" (int-to-string key-index)))
	  ;; handler form
	  `(lambda () (interactive) (register-to-point ,key-index))))))