Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Use Magit sections in refs buffers #21

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions elisp-refs.el
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ KIND should be 'function, 'macro, 'variable, 'special or 'symbol."
(elisp-refs--pluralize searched-file-count "file")))))
(s-word-wrap 70 (format "%s %s" found-str searched-str))))

(require 'magit)

;; TODO: if we have multiple matches on one line, we repeatedly show
;; that line. That's slightly confusing.
(defun elisp-refs--show-results (symbol description results
Expand All @@ -543,33 +545,34 @@ KIND should be 'function, 'macro, 'variable, 'special or 'symbol."
render a friendly results buffer."
(let ((buf (get-buffer-create (format "*refs: %s*" symbol))))
(switch-to-buffer buf)
(setq buffer-read-only nil)
(erase-buffer)
;; Insert the header.
(insert
(elisp-refs--format-count
description
(-sum (--map (length (car it)) results))
(length results)
searched-file-count
prefix)
"\n\n")
;; Insert the results.
(--each results
(-let* (((forms . buf) it)
(path (with-current-buffer buf elisp-refs--path)))
(insert
(propertize "File: " 'face 'bold)
(elisp-refs--path-button path) "\n")
(--each forms
(-let [(_ start-pos end-pos) it]
(insert (elisp-refs--containing-lines buf start-pos end-pos)
"\n")))
(insert "\n")))
;; Prepare the buffer for the user.
(goto-char (point-min))
(elisp-refs-mode)
(setq buffer-read-only t)
(let ((inhibit-read-only t))
(save-excursion
(magit-insert-section (elisp-refs-buf symbol)
(magit-insert-heading
(elisp-refs--format-count
description
(-sum (--map (length (car it)) results))
(length results)
searched-file-count
prefix)
"\n\n")
(--each results
(-let* (((forms . buf) it)
(path (with-current-buffer buf elisp-refs--path)))
(magit-insert-section (elisp-refs-file path)
(magit-insert-heading
(propertize "File: " 'face 'bold)
(elisp-refs--path-button path) "\n")
(--each forms
(-let [(_ start-pos end-pos) it]
(magit-insert-section (elisp-refs-ref
(list start-pos end-pos))
(magit-insert-heading
(elisp-refs--containing-lines buf start-pos end-pos)
"\n"))))
(insert "\n")))))))
;; Cleanup buffers created when highlighting results.
(kill-buffer elisp-refs--highlighting-buffer)))

Expand Down Expand Up @@ -749,7 +752,16 @@ search."
(elisp-refs--read-and-find-symbol buf symbol))
path-prefix))

(define-derived-mode elisp-refs-mode special-mode "Refs"
(defvar elisp-refs-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map magit-mode-map)
(define-key map (kbd "q") #'kill-this-buffer)
(define-key map (kbd "RET") #'elisp-refs-visit-match)
(define-key map (kbd "<return>") #'elisp-refs-visit-match)
map)
"Keymap for `elisp-refs-mode'.")

(define-derived-mode elisp-refs-mode magit-mode "Refs"
"Major mode for refs results buffers.")

(defun elisp-refs-visit-match ()
Expand Down Expand Up @@ -827,14 +839,5 @@ If DIRECTION is -1, moves backwards instead."
(interactive)
(elisp-refs--move-to-match 1))

;; TODO: it would be nice for TAB to navigate to file buttons too,
;; like *Help* does.
(define-key elisp-refs-mode-map (kbd "<tab>") #'elisp-refs-next-match)
(define-key elisp-refs-mode-map (kbd "<backtab>") #'elisp-refs-prev-match)
(define-key elisp-refs-mode-map (kbd "n") #'elisp-refs-next-match)
(define-key elisp-refs-mode-map (kbd "p") #'elisp-refs-prev-match)
(define-key elisp-refs-mode-map (kbd "q") #'kill-this-buffer)
(define-key elisp-refs-mode-map (kbd "RET") #'elisp-refs-visit-match)

(provide 'elisp-refs)
;;; elisp-refs.el ends here