all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: alex_sv <avshabanov@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: [elisp] easy-to-use bookmarks functionality
Date: Tue, 16 Mar 2010 08:51:48 -0700 (PDT)	[thread overview]
Message-ID: <d8825ea0-e095-42b9-8c22-c756df47dc93@k17g2000yqb.googlegroups.com> (raw)

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 :)


             reply	other threads:[~2010-03-16 15:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-16 15:51 alex_sv [this message]
2010-03-16 20:58 ` easy-to-use bookmarks functionality José A. Romero L.
2010-03-17  9:48   ` alex_sv
2010-03-19 16:47     ` Christian Dietrich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d8825ea0-e095-42b9-8c22-c756df47dc93@k17g2000yqb.googlegroups.com \
    --to=avshabanov@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.