all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [elisp] easy-to-use bookmarks functionality
@ 2010-03-16 15:51 alex_sv
  2010-03-16 20:58 ` José A. Romero L.
  0 siblings, 1 reply; 4+ messages in thread
From: alex_sv @ 2010-03-16 15:51 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,

For my own needs I wrote a small function that provides easy-to-use
bookmarking: C-[1..9] - sets "bookmark" (in fact remembers point in
the corresponding register), M-[1..9] - jumps to the corresponding
position.

Now I would like to see whether it could be implemented in a less
verbose and more intelligent way.

The flaws of my function are:
1) I don't know how to construct "key sequence" object from the
strings part, e.g. get "\C-1" sequence from the source strings "C-"
and "1", so I wrote local helper function - get-key-code that accepts
key sequence strings and creates the corresponding key sequence using
eval form with a call to kbd.
2) local-set-key function can't use locally defined closures so I
constructed corresponding lambda using append/list/quote facility that
looks quite ugly.

Here is the function:

(defun bind-navigation-command-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"
  (let (
	;; helper function that returns key sequence object that
	;; corresponds to the concatenated string sequence given
	(get-key-code (lambda (&rest key-sequence-str-list)
			(eval (let ((key-sequence
				     (mapconcat
				      (function
				       (lambda (c) c))
				      key-sequence-str-list "")))
				(append (list 'kbd)
					(list key-sequence)))))))
    ;; 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 (funcall get-key-code "C-" key-str)
			   ;; handler form
			   (list 'lambda '()
				 '(interactive)
				 (list 'point-to-register key-index)))
	    ;; goto saved point
	    (local-set-key (funcall get-key-code "M-" key-str)
			   ;; handler form
			   (list 'lambda '()
				 '(interactive)
				 (list 'register-to-point key-index)))))))

comments appreciated :)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-03-19 16:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-16 15:51 [elisp] easy-to-use bookmarks functionality alex_sv
2010-03-16 20:58 ` José A. Romero L.
2010-03-17  9:48   ` alex_sv
2010-03-19 16:47     ` Christian Dietrich

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.