corfu-info.el (3906B)
1 ;;; corfu-info.el --- Show candidate information in separate buffer -*- lexical-binding: t -*- 2 3 ;; Copyright (C) 2022 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") (corfu "0.27")) 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 <http://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 ;;;###autoload 56 (defun corfu-info-documentation () 57 "Show documentation of current candidate." 58 (interactive) 59 ;; Company support, taken from `company.el', see `company-show-doc-buffer'. 60 (when (< corfu--index 0) 61 (user-error "No candidate selected")) 62 (if-let* ((fun (plist-get corfu--extra :company-doc-buffer)) 63 (res (funcall fun (nth corfu--index corfu--candidates)))) 64 (let ((buf (or (car-safe res) res))) 65 (corfu-info--restore-on-next-command) 66 (setq other-window-scroll-buffer (get-buffer buf)) 67 (set-window-start (display-buffer buf t) (or (cdr-safe res) (point-min)))) 68 (user-error "No documentation available"))) 69 70 ;;;###autoload 71 (defun corfu-info-location () 72 "Show location of current candidate." 73 (interactive) 74 ;; Company support, taken from `company.el', see `company-show-location'. 75 (when (< corfu--index 0) 76 (user-error "No candidate selected")) 77 (if-let* ((fun (plist-get corfu--extra :company-location)) 78 (loc (funcall fun (nth corfu--index corfu--candidates)))) 79 (let ((buf (or (and (bufferp (car loc)) (car loc)) (find-file-noselect (car loc) t)))) 80 (corfu-info--restore-on-next-command) 81 (setq other-window-scroll-buffer buf) 82 (with-selected-window (display-buffer buf t) 83 (save-restriction 84 (widen) 85 (if (bufferp (car loc)) 86 (goto-char (cdr loc)) 87 (goto-char (point-min)) 88 (forward-line (1- (cdr loc)))) 89 (set-window-start nil (point))))) 90 (user-error "No candidate location available"))) 91 92 ;; Emacs 28: Do not show Corfu commands with M-X 93 (put #'corfu-info-location 'completion-predicate #'ignore) 94 (put #'corfu-info-documentation 'completion-predicate #'ignore) 95 96 (provide 'corfu-info) 97 ;;; corfu-info.el ends here