corfu-info.el (4881B)
1 ;;; corfu-info.el --- Show candidate information in separate buffer -*- lexical-binding: t -*- 2 3 ;; Copyright (C) 2022-2024 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: 1.3 9 ;; Package-Requires: ((emacs "27.1") (compat "29.1.4.4") (corfu "1.3")) 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 ((extra (nth 4 completion-in-region--data)) 78 (fun (plist-get extra :company-doc-buffer)) 79 (res (funcall fun cand))) 80 (set-window-start (corfu-info--display-buffer 81 (get-buffer (or (car-safe res) res)) 82 (and arg (format "*corfu doc: %s*" cand))) 83 (or (cdr-safe res) (point-min))) 84 (user-error "No documentation available for `%s'" cand)))) 85 86 ;;;###autoload 87 (defun corfu-info-location (&optional arg) 88 "Show location of current candidate. 89 If called with a prefix ARG, the buffer is persistent." 90 (interactive "P") 91 ;; Company support, taken from `company.el', see `company-show-location'. 92 (when (< corfu--index 0) 93 (user-error "No candidate selected")) 94 (let ((cand (nth corfu--index corfu--candidates))) 95 (if-let ((extra (nth 4 completion-in-region--data)) 96 (fun (plist-get extra :company-location)) 97 ;; BUG: company-location may throw errors if location is not found 98 (loc (ignore-errors (funcall fun cand)))) 99 (with-selected-window 100 (corfu-info--display-buffer 101 (or (and (bufferp (car loc)) (car loc)) 102 (find-file-noselect (car loc) t)) 103 (and arg (format "*corfu loc: %s*" cand))) 104 (without-restriction 105 (goto-char (point-min)) 106 (when-let ((pos (cdr loc))) 107 (if (bufferp (car loc)) 108 (goto-char pos) 109 (forward-line (1- pos)))) 110 (set-window-start nil (point)))) 111 (user-error "No location available for `%s'" cand)))) 112 113 ;; Emacs 28: Do not show Corfu commands with M-X 114 (put #'corfu-info-location 'completion-predicate #'ignore) 115 (put #'corfu-info-documentation 'completion-predicate #'ignore) 116 117 (provide 'corfu-info) 118 ;;; corfu-info.el ends here