summaryrefslogtreecommitdiff
path: root/extensions/corfu-info.el
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2022-03-31 16:23:27 +0200
committerDaniel Mendler <mail@daniel-mendler.de>2022-03-31 16:29:59 +0200
commit967a84ee7b33ce4c9cb3d7b4e33fcd07ba374404 (patch)
treeae351da925ba0f1a2a46287e1662996914d15892 /extensions/corfu-info.el
parentc8e6607c90a89ff19062cd37afc17e8bbb86aba3 (diff)
Add corfu-history and corfu-info extensionsextensions
Diffstat (limited to 'extensions/corfu-info.el')
-rw-r--r--extensions/corfu-info.el95
1 files changed, 95 insertions, 0 deletions
diff --git a/extensions/corfu-info.el b/extensions/corfu-info.el
new file mode 100644
index 0000000..152aeea
--- /dev/null
+++ b/extensions/corfu-info.el
@@ -0,0 +1,95 @@
+;;; corfu-info.el --- Show candidate information in separate buffer -*- lexical-binding: t -*-
+
+;; Copyright (C) 2022 Free Software Foundation, Inc.
+
+;; Author: Daniel Mendler <mail@daniel-mendler.de>
+;; Maintainer: Daniel Mendler <mail@daniel-mendler.de>
+;; Created: 2021
+;; Version: 0.1
+;; Package-Requires: ((emacs "27.1") (corfu "0.20"))
+;; Homepage: https://github.com/minad/corfu
+
+;; This file is part of GNU Emacs.
+
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This Corfu extension provides commands to show additional information
+;; to the candidates in a separate buffer.
+
+;;; Code:
+
+(require 'corfu)
+(eval-when-compile
+ (require 'subr-x))
+
+(defun corfu-info--restore-on-next-command ()
+ "Restore window configuration before next command."
+ (let ((config (current-window-configuration))
+ (other other-window-scroll-buffer)
+ (restore (make-symbol "corfu--restore")))
+ (fset restore
+ (lambda ()
+ (setq other-window-scroll-buffer other)
+ (unless (memq this-command '(scroll-other-window scroll-other-window-down))
+ (when (memq this-command '(corfu-quit corfu-reset))
+ (setq this-command #'ignore))
+ (remove-hook 'pre-command-hook restore)
+ (set-window-configuration config))))
+ (add-hook 'pre-command-hook restore)))
+
+;;;###autoload
+(defun corfu-info-documentation ()
+ "Show documentation of current candidate."
+ (interactive)
+ ;; Company support, taken from `company.el', see `company-show-doc-buffer'.
+ (when (< corfu--index 0)
+ (user-error "No candidate selected"))
+ (if-let* ((fun (plist-get corfu--extra :company-doc-buffer))
+ (res (funcall fun (nth corfu--index corfu--candidates))))
+ (let ((buf (or (car-safe res) res)))
+ (corfu-info--restore-on-next-command)
+ (setq other-window-scroll-buffer (get-buffer buf))
+ (set-window-start (display-buffer buf t) (or (cdr-safe res) (point-min))))
+ (user-error "No documentation available")))
+
+;;;###autoload
+(defun corfu-info-location ()
+ "Show location of current candidate."
+ (interactive)
+ ;; Company support, taken from `company.el', see `company-show-location'.
+ (when (< corfu--index 0)
+ (user-error "No candidate selected"))
+ (if-let* ((fun (plist-get corfu--extra :company-location))
+ (loc (funcall fun (nth corfu--index corfu--candidates))))
+ (let ((buf (or (and (bufferp (car loc)) (car loc)) (find-file-noselect (car loc) t))))
+ (corfu-info--restore-on-next-command)
+ (setq other-window-scroll-buffer buf)
+ (with-selected-window (display-buffer buf t)
+ (save-restriction
+ (widen)
+ (if (bufferp (car loc))
+ (goto-char (cdr loc))
+ (goto-char (point-min))
+ (forward-line (1- (cdr loc))))
+ (set-window-start nil (point)))))
+ (user-error "No candidate location available")))
+
+;; Emacs 28: Do not show Corfu commands with M-X
+(put #'corfu-info-location 'completion-predicate #'ignore)
+(put #'corfu-info-documentation 'completion-predicate #'ignore)
+
+(provide 'corfu-info)
+;;; corfu-info.el ends here