diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2025-05-09 17:43:32 +0200 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2025-05-09 17:43:32 +0200 |
| commit | 1d579099f2d59842ddbf2a32f7bfc09268015016 (patch) | |
| tree | 0b1b9349403389015df24f6f71416ba8043d53d7 | |
| parent | 5546a2db8e3928d5a95e1174e69a5742ddf97c0f (diff) | |
Simplify cape-dabbrev behavior
* Introduce new configuration cape-dabbrev-buffer-function which controls
precisely which buffers to scan.
* Remove cape-dabbrev-min-length
| -rw-r--r-- | CHANGELOG.org | 16 | ||||
| -rw-r--r-- | cape.el | 103 |
2 files changed, 66 insertions, 53 deletions
diff --git a/CHANGELOG.org b/CHANGELOG.org index 4d73995..292dd8c 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -2,10 +2,24 @@ #+author: Daniel Mendler #+language: en +* Development + +- Simplify ~cape-dabbrev~ configuration. +- ~cape-dabbrev-buffer-function~: New variable to configure the buffers searched + by ~cape-dabbrev~. +- ~cape-dabbrev-check-other-buffers~: Removed in favor of ~cape-dabbrev-buffer-function~. +- ~cape-dabbrev-min-length~: Remove variable, since it leads to confusion due to + missing completion candidates. +- ~cape-same-mode-buffers~: New public function, renamed from + ~cape--buffers-major-mode~. +- ~cape-text-buffers~: New function. +- ~cape-dabbrev-buffer-function~, ~cape-line-buffer-function~: Use + ~cape-same-mode-buffers~. + * Version 2.0 (2025-03-11) - ~cape-file~: Improve handling of environment variables as part of the path, - e.g., $HOME. + e.g., ~$HOME~. - ~cape-company-to-capf~: Handle updated return value convention of the ~prefix~ action of Company backends. @@ -76,36 +76,24 @@ a function returning a single or more paths." (defcustom cape-dict-case-replace 'case-replace "Preserve case of input. See `dabbrev-case-replace' for details." - :type '(choice (const :tag "off" nil) - (const :tag "use `case-replace'" case-replace) - (other :tag "on" t))) + :type '(choice (const :tag "Disable" nil) + (const :tag "Use `case-replace'" case-replace) + (other :tag "Enable" t))) (defcustom cape-dict-case-fold 'case-fold-search "Case fold search during search. See `dabbrev-case-fold-search' for details." - :type '(choice (const :tag "off" nil) - (const :tag "use `case-fold-search'" case-fold-search) - (other :tag "on" t))) - -(defcustom cape-dabbrev-min-length 4 - "Minimum length of Dabbrev expansions. -This setting ensures that words which are too short -are not offered as completion candidates, such that -auto completion does not pop up too aggressively." - :type 'natnum) - -(defcustom cape-dabbrev-check-other-buffers t - "Buffers to check for Dabbrev. - -If t, check all other buffers, subject to Dabbrev ignore rules. -If a function, only search the buffers returned by this function. -Any other non-nil value only checks some other buffers, as per -`dabbrev-select-buffers-function'." - :type `(choice (const :tag "off" nil) - (const :tag "same-mode buffers" ,#'cape--buffers-major-mode) - (function :tag "function") - (const :tag "some" some) - (other :tag "all" t))) + :type '(choice (const :tag "Disable" nil) + (const :tag "Use `case-fold-search'" case-fold-search) + (other :tag "Enable" t))) + +(defcustom cape-dabbrev-buffer-function #'cape-same-mode-buffers + "Function which returns list of buffers. +The buffers are scanned for completion candidates by `cape-dabbrev'." + :type `(choice (const :tag "Current buffer" current-buffer) + (const :tag "Text buffers" ,#'cape-text-buffers) + (const :tag "Buffers with same mode" ,#'cape-same-mode-buffers) + (function :tag "Custom function"))) (defcustom cape-file-directory nil "Base directory used by `cape-file." @@ -122,12 +110,12 @@ buffers via `org-open-at-point-global'." "The parent directory must exist for file completion." :type 'boolean) -(defcustom cape-line-buffer-function #'cape--buffers-major-mode +(defcustom cape-line-buffer-function #'cape-same-mode-buffers "Function which returns list of buffers. The buffers are scanned for completion candidates by `cape-line'." - :type '(choice (const :tag "Current buffer" current-buffer) - (const :tag "All buffers" buffer-list) - (const :tag "Buffers with same major mode" cape--buffers-major-mode) + :type `(choice (const :tag "Current buffer" current-buffer) + (const :tag "Text buffers" ,#'cape-text-buffers) + (const :tag "Buffers with same mode" ,#'cape-same-mode-buffers) (function :tag "Custom function"))) (defcustom cape-elisp-symbol-wrapper @@ -145,6 +133,26 @@ The buffers are scanned for completion candidates by `cape-line'." ;;;; Helpers +(defun cape-same-mode-buffers () + "Return buffers with same major mode as current buffer." + (let ((cur (current-buffer))) + (cons cur + (cl-loop for buf in (buffer-list) + if (and (not (eq buf cur)) + (eq major-mode (buffer-local-value 'major-mode buf))) + collect buf)))) + +(defun cape-text-buffers () + "Return `text-mode' and `prog-mode' buffers." + (let ((cur (current-buffer))) + (cons cur + (cl-loop for buf in (buffer-list) + for mode = (buffer-local-value 'major-mode buf) + if (and (not (eq buf cur)) + (or (provided-mode-derived-p mode #'text-mode) + (provided-mode-derived-p mode #'prog-mode))) + collect buf)))) + (defun cape--case-fold-p (fold) "Return non-nil if case folding is enabled for FOLD." (if (eq fold 'case-fold-search) case-fold-search fold)) @@ -551,19 +559,17 @@ If INTERACTIVE is nil the function acts like a Capf." (defun cape--dabbrev-list (input) "Find all Dabbrev expansions for INPUT." (cape--silent - (let* ((chk cape-dabbrev-check-other-buffers) - (funp (and (not (memq chk '(nil t some))) (functionp chk)))) - (dlet ((dabbrev-check-other-buffers (and chk (not funp))) - (dabbrev-check-all-buffers (eq chk t)) - (dabbrev-search-these-buffers-only (and funp (funcall chk)))) - (dabbrev--reset-global-variables) - (cons - (apply-partially #'string-prefix-p input) - (cl-loop with min-len = (+ cape-dabbrev-min-length (length input)) - with ic = (cape--case-fold-p dabbrev-case-fold-search) - for w in (dabbrev--find-all-expansions input ic) - if (>= (length w) min-len) collect - (cape--case-replace (and ic dabbrev-case-replace) input w))))))) + (dlet ((dabbrev-check-other-buffers nil) + (dabbrev-check-all-buffers nil) + (dabbrev-search-these-buffers-only + (ensure-list (funcall cape-dabbrev-buffer-function)))) + (dabbrev--reset-global-variables) + (cons + (apply-partially #'string-prefix-p input) + (cl-loop + with ic = (cape--case-fold-p dabbrev-case-fold-search) + for w in (dabbrev--find-all-expansions input ic) + collect (cape--case-replace (and ic dabbrev-case-replace) input w)))))) (defun cape--dabbrev-bounds () "Return bounds of abbreviation." @@ -594,11 +600,10 @@ If INTERACTIVE is nil the function acts like a Capf." If INTERACTIVE is nil the function acts like a Capf. In case you observe a performance issue with auto-completion and `cape-dabbrev' it is strongly recommended to disable scanning in other buffers. -See the user options `cape-dabbrev-min-length' and -`cape-dabbrev-check-other-buffers'." +See the user option `cape-dabbrev-buffer-function'." (interactive (list t)) (if interactive - (cape-interactive '((cape-dabbrev-min-length 0)) #'cape-dabbrev) + (cape-interactive #'cape-dabbrev) (when-let ((bounds (cape--dabbrev-bounds))) `(,(car bounds) ,(cdr bounds) ,(completion-table-case-fold @@ -714,12 +719,6 @@ If INTERACTIVE is nil the function acts like a Capf." :category 'cape-line) "Completion extra properties for `cape-line'.") -(defun cape--buffers-major-mode () - "Return buffers with same major mode as current buffer." - (cl-loop for buf in (buffer-list) - if (eq major-mode (buffer-local-value 'major-mode buf)) - collect buf)) - (defun cape--line-list () "Return all lines from buffer." (let ((ht (make-hash-table :test #'equal)) |
