Skip to content

Commit 4c961d9

Browse files
committed
[Fix #1014] Jump-to other window with prefix
1 parent 213c293 commit 4c961d9

File tree

2 files changed

+95
-23
lines changed

2 files changed

+95
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
* [#1032](https://github.com/clojure-emacs/cider/issues/1032) New functions, `cider-find-dwim` and
4141
`cider-find-dwim-other-window`. These functions combine the functionality of `cider-jump-to-var` and
4242
`cider-jump-to-resource`. Which are now renamed to `cider-find-var` and `cider-find-resource` respectively.
43+
* [#1014](https://github.com/clojure-emacs/cider/issues/1014) A prefix of <kbd>-</kbd> causes `cider-find-var` and
44+
`cider-find-resource` to show results in other window. Additionally, a double prefix argument <kbd>C-u C-u</kbd>
45+
inverts the meaning of `cider-prompt-for-symbol` and shows the results in other window.
4346

4447
### Changes
4548

cider-interaction.el

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ which nREPL uses for temporary evaluation file names."
694694
(defun cider-find-file (url)
695695
"Return a buffer visiting the file URL if it exists, or nil otherwise.
696696
If URL has a scheme prefix, it must represent a fully-qualified file path
697-
or an entry within a zip/jar archive. If URL doesn't contain a scheme
698-
prefix and is an absolute path, it is treated as such. Finally, if URL is
697+
or an entry within a zip/jar archive. If URL doesn't contain a scheme
698+
prefix and is an absolute path, it is treated as such. Finally, if URL is
699699
relative, it is expanded within each of the open Clojure buffers till an
700700
existing file ending with URL has been found."
701701
(cond ((string-match "^file:\\(.+\\)" url)
@@ -781,46 +781,100 @@ window."
781781
(cider--find-dwim symbol-file 'cider-find-dwim-other-window t))
782782

783783
(defun cider-find-dwim (symbol-file)
784-
"Try to jump to the SYMBOL-FILE at point. If thing at point is empty dired
785-
on project. If var is not found, try to jump to resource of the same name.
786-
When called interactively, A prompt is given according to the variable
787-
cider-prompt-for-symbol. A prefix inverts the meaning. A default value of
788-
thing at point is given when prompted."
784+
"Find and display the SYMBOL-FILE at point.
785+
786+
SYMBOL-FILE could be a var or a resource. If thing at point is empty
787+
then show dired on project. If var is not found, try to jump to resource
788+
of the same name. When called interactively, a prompt is given according
789+
to the variable `cider-prompt-for-symbol'. A single or double prefix argument
790+
inverts the meaning. A prefix of `-` or a double prefix argument causes the
791+
results to be displayed in a different window.
792+
A default value of thing at point is given when prompted."
789793
(interactive (cider--find-dwim-interactive "Jump to: "))
790-
(cider--find-dwim symbol-file 'cider-find-dwim))
794+
(cider--find-dwim symbol-file `cider-find-dwim
795+
(cider--open-other-window-p current-prefix-arg)))
791796

792797
(defun cider--find-dwim (symbol-file callback &optional other-window)
793-
"Try to jump to the SYMBOL-FILE at point, show results in OTHER-WINDOW as indicated.
798+
"Find the SYMBOL-FILE at point.
799+
794800
CALLBACK upon failure to invoke prompt if not prompted previously.
795-
If thing at point is empty dired on project."
801+
Show results in a different window if OTHER-WINDOW is true."
796802
(-if-let (info (cider-var-info symbol-file))
797803
(cider--jump-to-loc-from-info info other-window)
798804
(progn
799805
(cider-ensure-op-supported "resource")
800806
(-if-let* ((resource (cider-sync-request:resource symbol-file))
801807
(buffer (cider-find-file resource)))
802808
(cider-jump-to buffer 0 other-window)
803-
(if (cider--should-prompt-for-symbol current-prefix-arg)
809+
(if (cider--prompt-for-symbol-p current-prefix-arg)
804810
(error "Resource or var %s not resolved" symbol-file)
805811
(let ((current-prefix-arg (if current-prefix-arg nil '(4))))
806812
(call-interactively callback)))))))
807813

808814
(defun cider--find-dwim-interactive (prompt)
809815
"Get interactive arguments for jump-to functions using PROMPT as needed."
810-
(if (cider--should-prompt-for-symbol current-prefix-arg)
816+
(if (cider--prompt-for-symbol-p current-prefix-arg)
811817
(list
812818
(cider-read-from-minibuffer prompt (thing-at-point 'filename)))
813819
(list (or (thing-at-point 'filename) "")))) ; No prompt.
814820

815821
(defun cider-find-resource (path)
816-
"Jump to the resource at the resource-relative PATH.
817-
When called interactively, this operates on point."
818-
(interactive (list (thing-at-point 'filename)))
822+
"Find the resource at PATH.
823+
824+
Prompt for input as indicated by the variable `cider-prompt-for-symbol`.
825+
A single or double prefix argument inverts the meaning of
826+
`cider-prompt-for-symbol`. A prefix argument of `-` or a double prefix
827+
argument causes the results to be displayed in other window. The default
828+
value is thing at point."
829+
(interactive
830+
(list
831+
(if (cider--prompt-for-symbol-p current-prefix-arg)
832+
(cider-read-from-minibuffer "Resource: " (thing-at-point 'filename))
833+
(or (thing-at-point 'filename) ""))))
834+
819835
(cider-ensure-op-supported "resource")
820836
(-if-let* ((resource (cider-sync-request:resource path))
821837
(buffer (cider-find-file resource)))
822-
(cider-jump-to buffer)
823-
(error "Cannot find resource %s" path)))
838+
(cider-jump-to buffer nil (cider--open-other-window-p current-prefix-arg))
839+
(if (cider--prompt-for-symbol-p current-prefix-arg)
840+
(error "Cannot find resource %s" path)
841+
(let ((current-prefix-arg (cider--invert-prefix-arg current-prefix-arg)))
842+
(call-interactively `cider-find-resource)))))
843+
844+
(defun cider--open-other-window-p (arg)
845+
"Test prefix value ARG to see if it indicates displaying results in other window."
846+
(let ((narg (prefix-numeric-value arg)))
847+
(pcase narg
848+
(-1 t) ; -
849+
(16 t) ; empty empty
850+
(narg nil))))
851+
852+
(defun cider--invert-prefix-arg (arg)
853+
"Invert the effect of prefix value ARG on `cider-prompt-for-symbol'.
854+
855+
This function preserves the `other-window' meaning of ARG."
856+
(let ((narg (prefix-numeric-value arg)))
857+
(pcase narg
858+
(16 -1) ; empty empty -> -
859+
(-1 16) ; - -> empty empty
860+
(4 nil) ; empty -> no-prefix
861+
(nil 4)))) ; no-prefix -> empty
862+
863+
(defun cider--prefix-invert-prompt-p (arg)
864+
"Test prefix value ARG for its effect on `cider-prompt-for-symbol`."
865+
(let ((narg (prefix-numeric-value arg)))
866+
(pcase narg
867+
(16 t) ; empty empty
868+
(4 t) ; empty
869+
(narg nil))))
870+
871+
(defun cider--prompt-for-symbol-p (&optional prefix)
872+
"Check if cider should prompt for symbol.
873+
874+
Tests againsts PREFIX and the value of `cider-prompt-for-symbol'.
875+
Invert meaning of `cider-prompt-for-symbol' if PREFIX indicates it should be."
876+
(if (cider--prefix-invert-prompt-p prefix)
877+
(not cider-prompt-for-symbol) cider-prompt-for-symbol))
824878

825879
(defun cider--jump-to-loc-from-info (info &optional other-window)
826880
"Jump to location give by INFO.
@@ -835,26 +889,41 @@ OTHER-WINDOW is passed to `cider-jamp-to'."
835889
(cider-jump-to buffer (cons line nil) other-window)
836890
(error "No source location"))))
837891

892+
(defun cider--find-var-other-window (var &optional line)
893+
"Find the definition of VAR, optionally at a specific LINE.
894+
895+
Display the results in a different window."
896+
(-if-let (info (cider-var-info var))
897+
(progn
898+
(if line (setq info (nrepl-dict-put info "line" line)))
899+
(cider--jump-to-loc-from-info info t))
900+
(error "Symbol %s not resolved" var)))
901+
838902
(defun cider--find-var (var &optional line)
839-
"Jump to the definition of VAR, optionally at a specific LINE."
903+
"Find the definition of VAR, optionally at a specific LINE."
840904
(-if-let (info (cider-var-info var))
841905
(progn
842906
(if line (setq info (nrepl-dict-put info "line" line)))
843907
(cider--jump-to-loc-from-info info))
844908
(error "Symbol %s not resolved" var)))
845909

846910
(defun cider-find-var (&optional arg var line)
847-
"Jump to the definition of VAR, optionally at a specific LINE.
848-
Prompts for the symbol to use, or uses the symbol at point, depending on
849-
the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
850-
opposite of what that option dictates."
911+
"Find definition for VAR at LINE. Prompt according to prefix ARG
912+
and `cider-prompt-for-symbol'.
913+
914+
A single or double prefix argument inverts the meaning of
915+
`cider-prompt-for-symbol. A prefix of `-` or a double prefix argument causes
916+
the results to be displayed in a different window. The default value is
917+
thing at point."
851918
(interactive "P")
852919
(cider-ensure-op-supported "info")
853920
(if var
854921
(cider--find-var var line)
855922
(funcall (cider-prompt-for-symbol-function arg)
856923
"Symbol: "
857-
#'cider--find-var)))
924+
(if (cider--open-other-window-p arg)
925+
#'cider--find-var-other-window
926+
#'cider--find-var))))
858927

859928
(define-obsolete-function-alias 'cider-jump-to-resource 'cider-find-resource "0.9.0")
860929
(define-obsolete-function-alias 'cider-jump-to-var 'cider-find-var "0.9.0")

0 commit comments

Comments
 (0)