corfu-info.el (4774B)
1 ;;; corfu-info.el --- Show candidate information in separate buffer -*- lexical-binding: t -*- 2 3 ;; Copyright (C) 2022-2023 Free Software Foundation, Inc. 4 5 ;; Author: Daniel Mendler <mail@daniel-mendler.de> 6 ;; Maintainer: Daniel Mendler <mail@daniel-mendler.de> 7 ;; Created: 2022 8 ;; Version: 0.1 9 ;; Package-Requires: ((emacs "27.1") (compat "29.1.4.4") (corfu "1.0")) 10 ;; Homepage: https://github.com/minad/corfu 11 12 ;; This file is part of GNU Emacs. 13 14 ;; This program is free software: you can redistribute it and/or modify 15 ;; it under the terms of the GNU General Public License as published by 16 ;; the Free Software Foundation, either version 3 of the License, or 17 ;; (at your option) any later version. 18 19 ;; This program is distributed in the hope that it will be useful, 20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 ;; GNU General Public License for more details. 23 24 ;; You should have received a copy of the GNU General Public License 25 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 26 27 ;;; Commentary: 28 29 ;; This Corfu extension provides commands to show additional information to the 30 ;; candidates in a separate buffer. The commands `corfu-info-location' and 31 ;; `corfu-info-documentation' are bound by default in the `corfu-map' to M-g and 32 ;; M-h respectively. 33 34 ;;; Code: 35 36 (require 'corfu) 37 (eval-when-compile 38 (require 'subr-x)) 39 40 (defun corfu-info--restore-on-next-command () 41 "Restore window configuration before next command." 42 (let ((config (current-window-configuration)) 43 (other other-window-scroll-buffer) 44 (restore (make-symbol "corfu--restore"))) 45 (fset restore 46 (lambda () 47 (setq other-window-scroll-buffer other) 48 (unless (memq this-command '(scroll-other-window scroll-other-window-down)) 49 (when (memq this-command '(corfu-quit corfu-reset)) 50 (setq this-command #'ignore)) 51 (remove-hook 'pre-command-hook restore) 52 (set-window-configuration config)))) 53 (add-hook 'pre-command-hook restore))) 54 55 (defun corfu-info--display-buffer (buffer name) 56 "Display BUFFER and return window displaying the buffer. 57 Make the buffer persistent with NAME if non-nil." 58 (if name 59 (unless (buffer-local-value 'buffer-file-name buffer) 60 (if-let ((old (get-buffer name))) 61 (setq buffer (prog1 old (kill-buffer buffer))) 62 (with-current-buffer buffer 63 (rename-buffer name)))) 64 (corfu-info--restore-on-next-command)) 65 (setq other-window-scroll-buffer buffer) 66 (display-buffer buffer t)) 67 68 ;;;###autoload 69 (defun corfu-info-documentation (&optional arg) 70 "Show documentation of current candidate. 71 If called with a prefix ARG, the buffer is persistent." 72 (interactive "P") 73 ;; Company support, taken from `company.el', see `company-show-doc-buffer'. 74 (when (< corfu--index 0) 75 (user-error "No candidate selected")) 76 (let ((cand (nth corfu--index corfu--candidates))) 77 (if-let ((fun (plist-get corfu--extra :company-doc-buffer)) 78 (res (funcall fun cand))) 79 (set-window-start (corfu-info--display-buffer 80 (get-buffer (or (car-safe res) res)) 81 (and arg (format "*corfu doc: %s*" cand))) 82 (or (cdr-safe res) (point-min))) 83 (user-error "No documentation available for `%s'" cand)))) 84 85 ;;;###autoload 86 (defun corfu-info-location (&optional arg) 87 "Show location of current candidate. 88 If called with a prefix ARG, the buffer is persistent." 89 (interactive "P") 90 ;; Company support, taken from `company.el', see `company-show-location'. 91 (when (< corfu--index 0) 92 (user-error "No candidate selected")) 93 (let ((cand (nth corfu--index corfu--candidates))) 94 ;; BUG: company-location may throw errors if location is not found 95 (if-let ((fun (ignore-errors (plist-get corfu--extra :company-location))) 96 (loc (funcall fun cand))) 97 (with-selected-window 98 (corfu-info--display-buffer 99 (or (and (bufferp (car loc)) (car loc)) 100 (find-file-noselect (car loc) t)) 101 (and arg (format "*corfu loc: %s*" cand))) 102 (without-restriction 103 (goto-char (point-min)) 104 (when-let ((pos (cdr loc))) 105 (if (bufferp (car loc)) 106 (goto-char pos) 107 (forward-line (1- pos)))) 108 (set-window-start nil (point)))) 109 (user-error "No location available for `%s'" cand)))) 110 111 ;; Emacs 28: Do not show Corfu commands with M-X 112 (put #'corfu-info-location 'completion-predicate #'ignore) 113 (put #'corfu-info-documentation 'completion-predicate #'ignore) 114 115 (provide 'corfu-info) 116 ;;; corfu-info.el ends here