Improved jump-to-symbol-internal + changed bindings for

`jump-to-previous-like-this` and `jump-to-next-like-this`.
This commit is contained in:
larstvei 2014-10-27 00:00:29 +01:00
parent 54510b3be8
commit 760fb89cde
3 changed files with 88 additions and 53 deletions

View File

@ -714,27 +714,38 @@ number of spaces.
(cycle-spacing -1))) (cycle-spacing -1)))
``` ```
Often I want to find other occurrences of a word I'm at, or more
specifically the symbol (or tag) I'm at. The
`isearch-forward-symbol-at-point` in Emacs 24.4 works well for this, but
I don't want to be bothered with the `isearch` interface. Rather jump
quickly between occurrences of a symbol, or if non is found, don't do
anything.
```lisp ```lisp
(defun jump-to-symbol-internal (symbol forwardp) (defun jump-to-symbol-internal (&optional backwardp)
"Jumps to the next symbol near the point if such a symbol
exists. If BACKWARDP is non-nil it jumps backward."
(let* ((point (point)) (let* ((point (point))
(thing (prin1-to-string symbol)) (bounds (find-tag-default-bounds))
(beg (and (not forwardp) thing (beginning-of-thing 'symbol))) (beg (car bounds)) (end (cdr bounds))
(end (and forwardp thing (end-of-thing 'symbol))) (str (isearch-symbol-regexp (find-tag-default)))
(diff (and thing (if forwardp (- point end) (- point beg))))) (search (if backwardp 'search-backward-regexp
(if (and thing 'search-forward-regexp)))
(funcall (if forwardp (goto-char (if backwardp beg end))
'search-forward 'search-backward) thing nil t) (funcall search str nil t)
(eq (intern thing) symbol)) (cond ((<= beg (point) end) (goto-char point))
(forward-char diff) (backwardp (forward-char (- point beg)))
(goto-char point)))) (t (backward-char (- end point))))))
(defun jump-to-symbol-backward () (defun jump-to-previous-like-this ()
"Jumps to the previous occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) nil)) (jump-to-symbol-internal t))
(defun jump-to-symbol-forward () (defun jump-to-next-like-this ()
"Jumps to the next occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) t)) (jump-to-symbol-internal))
``` ```
I sometimes regret killing the `*scratch*`-buffer, and have realized I I sometimes regret killing the `*scratch*`-buffer, and have realized I
@ -1197,8 +1208,8 @@ Bind some native Emacs functions.
Bind the functions defined above. Bind the functions defined above.
```lisp ```lisp
(define-key custom-bindings-map (kbd "M-p") 'jump-to-symbol-backward) (define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this)
(define-key custom-bindings-map (kbd "M-n") 'jump-to-symbol-forward) (define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this)
(define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch) (define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch)
(define-key custom-bindings-map (kbd "C-x t") 'toggle-shell) (define-key custom-bindings-map (kbd "C-x t") 'toggle-shell)
(define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines) (define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines)
@ -1217,7 +1228,8 @@ Lastly we need to activate the map by creating and activating the
# License<a id="sec-5" name="sec-5"></a> # License<a id="sec-5" name="sec-5"></a>
My Emacs configurations written in Org mode My Emacs configurations written in Org mode.
Copyright (c) 2013 - 2014 Lars Tveito Copyright (c) 2013 - 2014 Lars Tveito
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify

45
init.el
View File

@ -552,26 +552,37 @@ the languages in ISPELL-LANGUAGES when invoked."
(just-one-space -1) (just-one-space -1)
(cycle-spacing -1))) (cycle-spacing -1)))
(defun jump-to-symbol-internal (symbol forwardp) ;; Often I want to find other occurrences of a word I'm at, or more
;; specifically the symbol (or tag) I'm at. The
;; =isearch-forward-symbol-at-point= in Emacs 24.4 works well for this, but
;; I don't want to be bothered with the =isearch= interface. Rather jump
;; quickly between occurrences of a symbol, or if non is found, don't do
;; anything.
(defun jump-to-symbol-internal (&optional backwardp)
"Jumps to the next symbol near the point if such a symbol
exists. If BACKWARDP is non-nil it jumps backward."
(let* ((point (point)) (let* ((point (point))
(thing (prin1-to-string symbol)) (bounds (find-tag-default-bounds))
(beg (and (not forwardp) thing (beginning-of-thing 'symbol))) (beg (car bounds)) (end (cdr bounds))
(end (and forwardp thing (end-of-thing 'symbol))) (str (isearch-symbol-regexp (find-tag-default)))
(diff (and thing (if forwardp (- point end) (- point beg))))) (search (if backwardp 'search-backward-regexp
(if (and thing 'search-forward-regexp)))
(funcall (if forwardp (goto-char (if backwardp beg end))
'search-forward 'search-backward) thing nil t) (funcall search str nil t)
(eq (intern thing) symbol)) (cond ((<= beg (point) end) (goto-char point))
(forward-char diff) (backwardp (forward-char (- point beg)))
(goto-char point)))) (t (backward-char (- end point))))))
(defun jump-to-symbol-backward () (defun jump-to-previous-like-this ()
"Jumps to the previous occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) nil)) (jump-to-symbol-internal t))
(defun jump-to-symbol-forward () (defun jump-to-next-like-this ()
"Jumps to the next occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) t)) (jump-to-symbol-internal))
;; I sometimes regret killing the =*scratch*=-buffer, and have realized I ;; I sometimes regret killing the =*scratch*=-buffer, and have realized I
;; never want to actually kill it. I just want to get it out of the way, and ;; never want to actually kill it. I just want to get it out of the way, and
@ -950,8 +961,8 @@ math-block around the region."
;; Bind the functions defined [[sec:defuns][above]]. ;; Bind the functions defined [[sec:defuns][above]].
(define-key custom-bindings-map (kbd "M-p") 'jump-to-symbol-backward) (define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this)
(define-key custom-bindings-map (kbd "M-n") 'jump-to-symbol-forward) (define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this)
(define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch) (define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch)
(define-key custom-bindings-map (kbd "C-x t") 'toggle-shell) (define-key custom-bindings-map (kbd "C-x t") 'toggle-shell)
(define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines) (define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines)

View File

@ -719,27 +719,38 @@
(cycle-spacing -1))) (cycle-spacing -1)))
#+END_SRC #+END_SRC
Often I want to find other occurrences of a word I'm at, or more
specifically the symbol (or tag) I'm at. The
=isearch-forward-symbol-at-point= in Emacs 24.4 works well for this, but
I don't want to be bothered with the =isearch= interface. Rather jump
quickly between occurrences of a symbol, or if non is found, don't do
anything.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun jump-to-symbol-internal (symbol forwardp) (defun jump-to-symbol-internal (&optional backwardp)
"Jumps to the next symbol near the point if such a symbol
exists. If BACKWARDP is non-nil it jumps backward."
(let* ((point (point)) (let* ((point (point))
(thing (prin1-to-string symbol)) (bounds (find-tag-default-bounds))
(beg (and (not forwardp) thing (beginning-of-thing 'symbol))) (beg (car bounds)) (end (cdr bounds))
(end (and forwardp thing (end-of-thing 'symbol))) (str (isearch-symbol-regexp (find-tag-default)))
(diff (and thing (if forwardp (- point end) (- point beg))))) (search (if backwardp 'search-backward-regexp
(if (and thing 'search-forward-regexp)))
(funcall (if forwardp (goto-char (if backwardp beg end))
'search-forward 'search-backward) thing nil t) (funcall search str nil t)
(eq (intern thing) symbol)) (cond ((<= beg (point) end) (goto-char point))
(forward-char diff) (backwardp (forward-char (- point beg)))
(goto-char point)))) (t (backward-char (- end point))))))
(defun jump-to-symbol-backward () (defun jump-to-previous-like-this ()
"Jumps to the previous occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) nil)) (jump-to-symbol-internal t))
(defun jump-to-symbol-forward () (defun jump-to-next-like-this ()
"Jumps to the next occurrence of the symbol at point."
(interactive) (interactive)
(jump-to-symbol-internal (symbol-at-point) t)) (jump-to-symbol-internal))
#+END_SRC #+END_SRC
I sometimes regret killing the =*scratch*=-buffer, and have realized I I sometimes regret killing the =*scratch*=-buffer, and have realized I
@ -1213,8 +1224,8 @@
Bind the functions defined [[sec:defuns][above]]. Bind the functions defined [[sec:defuns][above]].
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(define-key custom-bindings-map (kbd "M-p") 'jump-to-symbol-backward) (define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this)
(define-key custom-bindings-map (kbd "M-n") 'jump-to-symbol-forward) (define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this)
(define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch) (define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch)
(define-key custom-bindings-map (kbd "C-x t") 'toggle-shell) (define-key custom-bindings-map (kbd "C-x t") 'toggle-shell)
(define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines) (define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines)
@ -1232,7 +1243,8 @@
#+END_SRC #+END_SRC
* License * License
My Emacs configurations written in Org mode My Emacs configurations written in Org mode.
Copyright (c) 2013 - 2014 Lars Tveito Copyright (c) 2013 - 2014 Lars Tveito
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify