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

* Re: easy-to-use bookmarks functionality
  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
  0 siblings, 1 reply; 4+ messages in thread
From: José A. Romero L. @ 2010-03-16 20:58 UTC (permalink / raw)
  To: help-gnu-emacs

On 16 Mar, 16:51, alex_sv <avshaba...@gmail.com> wrote:
> 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.
(...)

Cool idea. Here is a somewhat shorter implementation:

(defun jarl/bind-navigation-command-to-numkeys ()
  (dotimes (i 9)
    (local-set-key
     (read-kbd-macro (concat "C-" (number-to-string (1+ i))))
     `(lambda () (interactive) (point-to-register ,(1+ i))))
    (local-set-key
     (read-kbd-macro (concat "M-" (number-to-string (1+ i))))
     `(lambda () (interactive) (register-to-point ,(1+ i))))))

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)


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

* Re: easy-to-use bookmarks functionality
  2010-03-16 20:58 ` José A. Romero L.
@ 2010-03-17  9:48   ` alex_sv
  2010-03-19 16:47     ` Christian Dietrich
  0 siblings, 1 reply; 4+ messages in thread
From: alex_sv @ 2010-03-17  9:48 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 16, 11:58 pm, José A. Romero L. <escherdra...@gmail.com> wrote:
>...

Great!
I didn't noticed possibility to use read-kbd-macro and back-quote
form.
Thank you!

I slightly modified your function - now it prints message that
bookmark is set, just a copestone:

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


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

* Re: easy-to-use bookmarks functionality
  2010-03-17  9:48   ` alex_sv
@ 2010-03-19 16:47     ` Christian Dietrich
  0 siblings, 0 replies; 4+ messages in thread
From: Christian Dietrich @ 2010-03-19 16:47 UTC (permalink / raw)
  To: help-gnu-emacs

alex_sv <avshabanov@gmail.com> schrieb:
> On Mar 16, 11:58 pm, José A. Romero L. <escherdra...@gmail.com> wrote:
>>...
>
> Great!
> I didn't noticed possibility to use read-kbd-macro and back-quote
> form.
> Thank you!

I just used the given function to implement a switch position, which
uses register 9 to save the last position in in. With H-x, i jump to
register 9 and save the last position in it. So i can jump fore and
backwards.

(loop for key-index from 1 to 9 do
      (let ((key-str (int-to-string key-index)))
        ;; save point
        (global-set-key
         ;; retrieve C-{index} keyboard sequence
         (read-kbd-macro (concat "H-s " (int-to-string key-index)))
         ;; handler form
         `(lambda ()
            (interactive)
            (point-to-register 9)
            (point-to-register ,key-index)
            (message (concat "setting bookmark #" ,key-str))))
        ;; goto saved point
        (global-set-key
         ;; retrieve M-{index} keyboard sequence
         (read-kbd-macro (concat "H-l " (int-to-string key-index)))
         ;; handler form
         `(lambda () (interactive) 
            (point-to-register 9)
            (register-to-point ,key-index)))))

(global-set-key 
 (kbd "H-x")
 '(lambda () (interactive)
    "Switch point with position in register 9"
    (let ((marker (point-marker)))
      (jump-to-register 9)
      (setcdr (assoc 9 register-alist) marker))))

greetz didi


^ 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.