forked from syl20bnr/spacemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-jump.el
133 lines (123 loc) · 5.61 KB
/
core-jump.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
;;; core-jump.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <[email protected]>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defvar spacemacs-default-jump-handlers '()
"List of jump handlers available in every mode.")
(defvar-local spacemacs-jump-handlers '()
"List of jump handlers local to this buffer.")
(defmacro spacemacs|define-jump-handlers (mode &rest handlers)
"Defines jump handlers for the given MODE.
This defines a variable `spacemacs-jump-handlers-MODE' to which
handlers can be added, and a function added to MODE-hook which
sets `spacemacs-jump-handlers' in buffers of that mode."
(let ((mode-hook (intern (format "%S-hook" mode)))
(func (intern (format "spacemacs//init-jump-handlers-%S" mode)))
(handlers-list (intern (format "spacemacs-jump-handlers-%S" mode))))
`(progn
(defvar ,handlers-list ',handlers
,(format (concat "List of mode-specific jump handlers for %S. "
"These take priority over those in "
"`spacemacs-default-jump-handlers'.")
mode))
(defun ,func ()
(setq spacemacs-jump-handlers
(append ,handlers-list
spacemacs-default-jump-handlers)))
(add-hook ',mode-hook ',func)
(with-eval-after-load 'bind-map
(spacemacs/set-leader-keys-for-major-mode ',mode
"gg" 'spacemacs/jump-to-definition
"gG" 'spacemacs/jump-to-definition-other-window)))))
(defmacro spacemacs|define-reference-handlers (mode &rest handlers)
"Defines reference handlers for the given MODE.
This defines a variable `spacemacs-reference-handlers-MODE' to which
handlers can be added, and a function added to MODE-hook which
sets `spacemacs-reference-handlers' in buffers of that mode."
(let ((mode-hook (intern (format "%S-hook" mode)))
(func (intern (format "spacemacs//init-reference-handlers-%S" mode)))
(handlers-list (intern (format "spacemacs-reference-handlers-%S" mode))))
`(progn
(defvar ,handlers-list ',handlers
,(format (concat "List of mode-specific reference handlers for %S. "
"These take priority over those in "
"`spacemacs-default-reference-handlers'.")
mode))
(defun ,func ()
(setq spacemacs-reference-handlers
(append ,handlers-list
spacemacs-default-reference-handlers)))
(add-hook ',mode-hook ',func)
(with-eval-after-load 'bind-map
(spacemacs/set-leader-keys-for-major-mode ',mode
"gr" 'spacemacs/jump-to-reference)))))
(defun spacemacs/jump-to-definition ()
"Jump to definition around point using the best tool for this action."
(interactive)
(catch 'done
(let ((old-buffer (current-buffer))
(old-point (point)))
(dolist (-handler spacemacs-jump-handlers)
(let ((handler (if (listp -handler) (car -handler) -handler))
(async (when (listp -handler)
(plist-get (cdr -handler) :async))))
(ignore-errors
(call-interactively handler))
(when (or (eq async t)
(and (fboundp async) (funcall async))
(not (eq old-point (point)))
(not (equal old-buffer (current-buffer))))
(throw 'done t)))))
(message "No jump handler was able to find this symbol.")))
(defun spacemacs/jump-to-definition-other-window ()
"Jump to definition around point in other window."
(interactive)
(let ((pos (point)))
;; since `spacemacs/jump-to-definition' can be asynchronous we cannot use
;; `save-excursion' here, so we have to bear with the jumpy behavior.
(switch-to-buffer-other-window (current-buffer))
(goto-char pos)
(spacemacs/jump-to-definition)))
(defun spacemacs/jump-to-reference ()
"Jump to reference around point using the best tool for this action."
(interactive)
(catch 'done
(let ((old-window (selected-window))
(old-buffer (current-buffer))
(old-point (point)))
(dolist (-handler spacemacs-reference-handlers)
(let ((handler (if (listp -handler) (car -handler) -handler))
(async (when (listp -handler)
(plist-get (cdr -handler) :async))))
(ignore-errors
(call-interactively handler))
(when (or (eq async t)
(and (fboundp async) (funcall async))
(not (eq old-point (point)))
(not (equal old-buffer (window-buffer old-window))))
(throw 'done t)))))
(message "No reference handler was able to find this symbol.")))
(defun spacemacs/jump-to-reference-other-window ()
"Jump to reference around point in other window."
(interactive)
(let ((pos (point)))
;; since `spacemacs/jump-to-reference' can be asynchronous we cannot use
;; `save-excursion' here, so we have to bear with the jumpy behavior.
(switch-to-buffer-other-window (current-buffer))
(goto-char pos)
(spacemacs/jump-to-reference)))
;; Set the `:jump' property manually instead of just using `evil-define-motion'
;; in an `eval-after-load' macro invocation because doing that prevents
;; `describe-function' from correctly finding the source.
;;
;; See discussion on https://github.com/syl20bnr/spacemacs/pull/6771
(with-eval-after-load 'evil
(evil-set-command-property 'spacemacs/jump-to-definition :jump t)
(evil-set-command-property 'spacemacs/jump-to-reference :jump t))
(provide 'core-jump)