dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

corfu-info.el (4155B)


      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") (corfu "0.36"))
     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 ;;;###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   (let ((cand (nth corfu--index corfu--candidates)))
     63     (if-let ((fun (plist-get corfu--extra :company-doc-buffer))
     64              (res (funcall fun cand)))
     65         (let ((buf (or (car-safe res) res)))
     66           (corfu-info--restore-on-next-command)
     67           (setq other-window-scroll-buffer (get-buffer buf))
     68           (set-window-start (display-buffer buf t) (or (cdr-safe res) (point-min))))
     69       (user-error "No documentation available for `%s'" cand))))
     70 
     71 ;;;###autoload
     72 (defun corfu-info-location ()
     73   "Show location of current candidate."
     74   (interactive)
     75   ;; Company support, taken from `company.el', see `company-show-location'.
     76   (when (< corfu--index 0)
     77     (user-error "No candidate selected"))
     78   (let ((cand (nth corfu--index corfu--candidates)))
     79     ;; BUG: company-location may throw errors if location is not found
     80     (if-let ((fun (ignore-errors (plist-get corfu--extra :company-location)))
     81              (loc (funcall fun cand)))
     82         (let ((buf (or (and (bufferp (car loc)) (car loc))
     83                        (find-file-noselect (car loc) t))))
     84           (corfu-info--restore-on-next-command)
     85           (setq other-window-scroll-buffer buf)
     86           (with-selected-window (display-buffer buf t)
     87             (save-restriction
     88               (widen)
     89               (goto-char (point-min))
     90               (when-let (pos (cdr loc))
     91                 (if (bufferp (car loc))
     92                     (goto-char pos)
     93                   (forward-line (1- pos))))
     94               (set-window-start nil (point)))))
     95       (user-error "No location available for `%s'" cand))))
     96 
     97 ;; Emacs 28: Do not show Corfu commands with M-X
     98 (put #'corfu-info-location 'completion-predicate #'ignore)
     99 (put #'corfu-info-documentation 'completion-predicate #'ignore)
    100 
    101 (provide 'corfu-info)
    102 ;;; corfu-info.el ends here