;;; completion-conf.el --- My configurations for completions inside Emacs -*- lexical-binding: t; -*- ;; Author: JSDurand ;; Created: 2021-01-22 21:41:30 ;; Version: 0.0.1 ;; Keywords: matching ;; This file is NOT 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 . ;;; Commentary: ;; Thsi file is my configurations of the built in completion framework ;; in Emacs. I intend to configure it to use my yet-to-come ;; completion style using suffix trees. ;;; Code: (require 'minibuffer) (require 'simple) ;;; Basic settings (setq completion-styles '(initials substring partial-completion regex flex)) (setq completion-category-defaults nil) (setq completion-flex-nospace nil) (setq completion-pcm-complete-word-inserts-delimiters t) (setq completion-show-help nil) (setq completion-ignore-case t) (setq read-file-name-completion-ignore-case t) (setq read-buffer-completion-ignore-case t) (setq resize-mini-windows t) (set 'completions-format 'one-column) (define-key minibuffer-local-completion-map (vector 'down) #'durand-embark-scroll-down-or-go-to-completions) (define-key minibuffer-local-completion-map (vector 'up) #'durand-embark-scroll-up-or-go-to-completions) (define-key minibuffer-local-completion-map (vector ?\C-n) #'durand-embark-scroll-down-or-go-to-completions) (define-key minibuffer-local-completion-map (vector ?\C-p) #'durand-embark-scroll-up-or-go-to-completions) (define-key minibuffer-local-completion-map (vector ?\s-v) #'durand-completion-toggle-display) (define-key minibuffer-local-completion-map (vector 32) nil) (define-key minibuffer-local-must-match-map (vector 32) nil) (define-key completion-list-mode-map (vector 'down) #'durand-completion-scroll-down-or-go-to-minibuffer) (define-key completion-list-mode-map (vector 'up) #'durand-completion-scroll-up-or-go-to-minibuffer) (define-key completion-list-mode-map (vector ?\C-n) #'durand-completion-scroll-down-or-go-to-minibuffer) (define-key completion-list-mode-map (vector ?\C-p) #'durand-completion-scroll-up-or-go-to-minibuffer) ;;; Headlong stuff ;;;###autoload (defvar durand-headlong-entered-minibuffer-p nil "Whether or not we have entered minibuffer. This is used for determining if we shall exit the minibuffer when there is only one candidate left.") ;;;###autoload (defun durand-headlong-minibuffer-setup-hook () "The entry for the completion to be headlong. Simply add this function to `minibuffer-setup-hook'." ;; NOTE: When we run this function we first enter minibuffer, so we ;; set a variable to its appropriate value. (set 'durand-headlong-entered-minibuffer-p nil) (add-hook 'post-command-hook 'durand-headlong-post-command-hook t) (add-hook 'minibuffer-exit-hook 'durand-headlong-minibuffer-exit-hook)) ;;;###autoload (defun durand-headlong-post-command-hook () "Exit the minibuffer if there is only one candidate left. In practice, when we first enter minibuffer, there is some abnormal behaviour, so we only check when we have entered the minibuffer as usual." (let ((comps (completion-all-completions (minibuffer-contents) minibuffer-completion-table minibuffer-completion-predicate (- (point) (minibuffer-prompt-end))))) (cond ((and durand-headlong-entered-minibuffer-p comps (not (consp (cdr comps)))) (minibuffer-force-complete-and-exit)) (t (set 'durand-headlong-entered-minibuffer-p t))))) ;;;###autoload (defun durand-headlong-minibuffer-exit-hook () "Remove the hooks we added." (remove-hook 'post-command-hook 'durand-headlong-post-command-hook) (remove-hook 'minibuffer-setup-hook 'durand-headlong-minibuffer-setup-hook) (remove-hook 'minibuffer-exit-hook 'durand-headlong-minibuffer-exit-hook)) ;; Make sure `minibuffer-message' respects `inhibit-message'. (defun durand-minibuffer-respect-inhibit-advice (fn &rest args) (cond (inhibit-message) (t (apply fn args)))) (advice-add 'minibuffer-message :around 'durand-minibuffer-respect-inhibit-advice) ;;; Display completion candidates ;;;###autoload (defun durand-completion-display-after-change (&rest args) "Call `minibuffer-completion-help' with no input. \(fn &rest ARGS)" (cond ((minibufferp) (let ((while-no-input-ignore-events '(selection-request))) (while-no-input (condition-case nil (save-excursion (goto-char (point-max)) (save-match-data (let ((inhibit-message t)) (minibuffer-completion-help)))) (quit (abort-recursive-edit)))))))) ;; (fset ;; 'durand-completion-display-after-change ;; (make-byte-code ;; #x080 ;; (unibyte-string 192 32 135) ;; (vector #'minibuffer-completion-help) ;; 2 ;; "Call `minibuffer-completion-help' with no input. ;; \(fn &rest ARGS)" ;; nil)) ;;;###autoload (defalias 'durand-completion-toggle-display (make-byte-code 0 (unibyte-string 192 9 62 ; determine if it is a member 131 14 0 ;; memq 194 193 192 196 35 130 20 0 ;; not memq 195 193 192 197 196 36 135) (vector #'durand-completion-display-after-change 'after-change-functions 'remove-hook 'add-hook t nil) 6 "Toggle to display the list of completions after each input. \(fn)" nil)) ;; (defun durand-completion-toggle-display () ;; "Toggle to display the list of completions after each input." ;; (interactive) ;; (cond ;; ((memq #'durand-completion-display-after-change 'post-command-hook) ;; (remove-hook 'post-command-hook #'durand-completion-display-after-change t)) ;; ((add-hook 'post-command-hook #'durand-completion-display-after-change nil t)))) ;;; Live previews ;; TODO: Add the functionality to "preview" the completions in some ;; sense. (defun durand-preview-in-completion-list () "Preview the current candidate in the completion list buffer." (cond ((derived-mode-p 'completion-list-mode) (let ((mes "Nothing to preview here") prop) (cond ((setq prop (get-text-property (point) 'durand-preview-property)) (cond ((eq (car prop) 'buffer) (display-buffer-use-least-recent-window (cadr prop) '((".*" (display-buffer-use-some-window display-buffer-use-least-recent-window)))) (cond ((integerp (caddr prop)) (goto-line (caddr prop))))))) ((active-minibuffer-window) (minibuffer-message mes)) ((message mes))))))) (defun durand-propertize-buffer-for-completion-list (ls) "Propertize the buffer candidates so as to preview buffers. LS has two argument: STR and GROUP-FUN. STR is the string to insert, and GROUP-FUN is used to group candidates." (let ((str (car ls)) (group-fun (cadr ls))) (cond ((not (consp str)) ;; if it is a cons, it will be processed and called again. (list (propertize str 'durand-preview-property (list 'buffer (get-buffer str))) group-fun)) ((list str grou-fun))))) (defun durand-open-completion-list-with-buffer-preview () "Open the completion list buffer and propertize the candidates." (interactive) (advice-add #'completion--insert :filter-args #'durand-propertize-buffer-for-completion-list) (let ((minibuffer-allow-text-properties t)) (minibuffer-completion-help)) (advice-remove #'completion--insert #'durand-propertize-buffer-for-completion-list) (goto-char (point-max)) (durand-embark-scroll-down-or-go-to-completions 1) (cond ((derived-mode-p 'completion-list-mode) (add-hook 'post-command-hook #'durand-preview-in-completion-list)) ((user-error "Something is wrong!")))) ;; (defun durand-quit-completion-list-with-buffer-preview () ;; "Restore the buffer and quit the completion list." ;; (interactive) ;; (quit-window) ;; (set-window-configuration durand-completion-list-window-conf)) (defvar durand-preview-keymap (make-sparse-keymap "Buffer:") "A keymap used to read buffers with previews.") (define-key durand-preview-keymap (vector 'tab) #'durand-open-completion-list-with-buffer-preview) (defun durand-read-buffer-with-preview-function (prompt &optional def require-match predicate) "Use a custom keymap when reading buffers. The arguments PROMPT, DEF, REQUIRE-MATCH and PREDICATE have the same meaning as in `read-buffer'." (let* ((base-keymap (cond (require-match minibuffer-local-must-match-map) (minibuffer-local-completion-map))) (keymap (make-composed-keymap (list base-keymap durand-preview-keymap))) (buffer (current-buffer)) (result (minibuffer-with-setup-hook (lambda () (setq-local minibuffer-completion-table #'internal-complete-buffer) (setq-local minibuffer-completion-predicate predicate) ;; FIXME: Remove/rename this var, see the next one. (setq-local minibuffer-completion-confirm (unless (eq require-match t) require-match)) (setq-local minibuffer--require-match require-match) (setq-local minibuffer--original-buffer buffer)) (read-from-minibuffer prompt nil keymap nil 'buffer-name-history def)))) (when (and (equal result "") def) (setq result (if (consp def) (car def) def))) result)) (defun durand-switch-to-buffer-preview () "Preview switching buffers." (interactive) (let ((read-buffer-function #'durand-read-buffer-with-preview-function)) (switch-to-buffer (let ((default (other-buffer))) (read-buffer-to-switch (format "Switch to buffer%s" (cond ((and (bufferp default) (buffer-live-p default)) (format " (default %s): " (buffer-name default))) (": ")))))))) ;;; Regular expression completion style ;;;###autoload (defun regex-try-completion (string table pred point &optional metadata) "The function that tries to complete STRING using completion table \ TABLE for the `regex' style. Only the elements that satisfy the predicate PRED are considered. POINT is the position of point within STRING. The return value is nil if there is no completion, and is t if STRING is the only possible completion. It is a pair \(NEWSTRING . NEWPOINT) otherwise, where NEWSTRING is the completed result and NEWPOINT is the new position for point." (let* ((completion-regexp-list (cons string completion-regexp-list)) (result (try-completion "" table pred))) (cond ;; For nil values we don't have to return nil explicitly. ((null result) nil) ((eq result t)) ((stringp result) (cons result (length result)))))) ;;;###autoload (defun regex-all-completion (string table pred point &optional metadata) "List the possible completions of STRING in completion table \ TABLE for the style `regex'. Only the elements of TABLE that satisfy the predicate PRED are considered. POINT is the position of point within STRING. The return value is a list of completions and may contain the base-size in the last `cdr'." (let ((completion-regexp-list (cons string completion-regexp-list))) (all-completions "" table pred))) (let ((style-elements (list #'regex-try-completion #'regex-all-completion "Simple regular expression completion. This considers the input as the regular expression itself." )) (assoc-result (assoc 'regex completion-styles-alist))) (cond (assoc-result (setcdr assoc-result style-elements)) ((setq completion-styles-alist (cons (cons 'regex style-elements) completion-styles-alist))))) (provide 'completion-conf) ;;; completion-conf.el ends here