diff options
Diffstat (limited to 'modules/system-lib.el')
| -rw-r--r-- | modules/system-lib.el | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/modules/system-lib.el b/modules/system-lib.el index 49bb6cd1a..f1049c021 100644 --- a/modules/system-lib.el +++ b/modules/system-lib.el @@ -164,6 +164,50 @@ contributes its own modes regardless of load order." (setq font-lock-global-modes (cj/--font-lock-global-modes-excluding font-lock-global-modes mode)))) +(defun cj/completion-table (category collection) + "Return a completion table over COLLECTION tagged with completion CATEGORY. +COLLECTION is anything `completing-read' accepts (list, alist, obarray, hash +table, or another table). The table reports CATEGORY in its metadata so +marginalia (and embark, consult, sorting) can recognize and annotate the +candidates. Use a standard category (file, buffer, function, theme, ...) when +the candidates match one; marginalia then annotates them with no further work." + (lambda (string predicate action) + (if (eq action 'metadata) + `(metadata (category . ,category)) + (complete-with-action action collection string predicate)))) + +(defun cj/completion-table-annotated (category annotate collection) + "Like `cj/completion-table' but also attach ANNOTATE as the annotation function. +ANNOTATE is called with a candidate string and returns its annotation suffix, or +nil. Use this for a custom CATEGORY that marginalia has no built-in annotator +for: marginalia falls back to the table's own annotation function." + (lambda (string predicate action) + (if (eq action 'metadata) + `(metadata (category . ,category) + (annotation-function . ,annotate)) + (complete-with-action action collection string predicate)))) + +(defun cj/completion-file-annotator (candidate->path) + "Return an annotation function for completion candidates backed by files. +CANDIDATE->PATH maps a candidate string to its absolute file path, or nil when +the candidate has no backing file. The returned function, suitable as a +completion table's annotation function (see `cj/completion-table-annotated'), +yields a suffix with the file size and modification date for a regular file, +the marker \"dir\" plus the date for a directory, or nil when the path is nil +or the file is missing -- so marginalia then shows no suffix for that +candidate." + (lambda (cand) + (let ((path (funcall candidate->path cand))) + (when (and path (file-exists-p path)) + (let* ((attrs (file-attributes path)) + (dirp (eq t (file-attribute-type attrs))) + (size (if dirp "dir" + (file-size-human-readable (file-attribute-size attrs)))) + (date (format-time-string + "%Y-%m-%d" + (file-attribute-modification-time attrs)))) + (format " %8s %s" size date)))))) + (defun cj/format-region-with-program (program &rest args) "Replace the current buffer with PROGRAM ARGS run over its contents, via argv. Runs PROGRAM (with ARGS) on the whole buffer through `call-process-region' |
