Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add affixations to buffer switching functions when exordium-help-extensions #240

Merged
merged 7 commits into from
Feb 25, 2025
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Keybinding | Description
See [Projectile](https://github.com/bbatsov/projectile) documentation for other
keys.

### Helm
## Helm

Helm can be set up as a primary completion and selection narrowing framework
for most commonly used functions. You can achieve that by setting
Expand All @@ -349,8 +349,9 @@ Keybinding | Description
<kbd>M-y</kbd> | Select yank pop.
<kbd>C-x b</kbd> | Switch buffer.
<kbd>C-x C-f</kbd> | Find file.
<kbd>C-x c g</kbd> | Google suggest.

#### Other Helm tools
### Other Helm tools

Helm is a pretty good when you need quickly scan search results. The commands below
will start different search modes. By default, they will use symbol under the point.
Expand Down
35 changes: 35 additions & 0 deletions modules/init-helm-projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,45 @@ project's file using completion and show it in another window."
(helm-add-action-to-source "ripgrep (rg) in project `C-S-r'"
#'exordium-helm-projectile--switch-project-and-do-rg
helm-source-projectile-projects)
;; The following lambda is constructed similarly to what
;; `helm-completing-read-default-1' does. However, it's calculated once on
;; startup (when `helm-projectile' package is configured) and not on each
;; `helm-projectile-switch-to-buffer'/`helm-projectile' call (which is how
;; original behaves). This has been done in an attempt to making this
;; solution slightly simpler as we need to modify a helm-source prototype
;; which would be cumbersome to do dynamically. Caveat is that it doesn't
;; let user to use their desired annotation or affixation function via
;; `completion-extra-properties' when `completions-detailed' is non-nil.
(when exordium-help-extensions
(let* ((metadata (completion-metadata "" 'internal-complete-buffer nil))
(category (completion-metadata-get metadata 'category))
(sort-fn (unless (eq helm-completion-style 'helm-fuzzy)
(or
(completion-metadata-get
metadata 'display-sort-function)
(lambda (candidates)
(sort candidates #'helm-generic-sort-fn)))))
(metadata (assoc-default category helm-completing-read-extra-metadata))
(afun (completion-metadata-get metadata 'annotation-function))
(afix (completion-metadata-get metadata 'affixation-function)))
(when (or afun afix)
(helm-set-attr
'filtered-candidate-transformer
(list
'helm-skip-boring-buffers
(lambda (candidates _source)
(helm-completion--decorate
(if (and sort-fn (> (length helm-pattern) 0))
(funcall sort-fn candidates)
candidates)
afun afix category)))
helm-source-projectile-buffers-list))))

(when exordium-helm-everywhere
(helm-projectile-on)))

(use-package treemacs-projectile
:defer t
:bind
(("C-c e" . #'treemacs)
("C-c E" . #'treemacs-projectile)))
Expand Down
127 changes: 110 additions & 17 deletions modules/init-helm.el
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
;; C-S-a Search with Ag: in current project root.
;; See also`init-helm-porojectile.el'.
;; C-S-s Helm Swoop
;; C-x c g Helm Google suggest.

;;; Code:

Expand All @@ -28,6 +29,9 @@

(use-package helm
:diminish
:bind
(:map helm-command-map
("g" . #'helm-google-suggest))
:custom
(helm-split-window-default-side 'other)
(helm-split-window-other-side-when-one-window 'right)
Expand All @@ -50,52 +54,144 @@
(use-package helm
:diminish
:when exordium-helm-everywhere
:functions (exordium--helm-swith-to-buffer-update-sources
exordium--helm-switch-to-buffer-completing-read)
:init
(use-package helm-lib
:ensure helm
:defer t
:autoload (helm-this-command
helm-symbol-name
helm-get-attr
helm-set-attr
helm-mklist))
(use-package helm-mode
:ensure helm
:defer t
:autoload (helm-completing-read-default-handler))
(use-package helm-source
:ensure helm
:defer t
:autoload (helm-make-source))

(defun exordium--helm-swith-to-buffer-update-sources (&rest args)
"Copy relevant attributes from a `helm-source-buffers' to `:sources' in ARGS."
(if-let* ((args (car args))
((plistp args))
(sources (cl-remove-if (lambda (source)
(equal "Unknown candidate"
(helm-get-attr 'name source)))
(plist-get args :sources)))
(source-buffers (helm-make-source "Buffers" 'helm-source-buffers)))
(progn
(dolist (source sources)
(helm-set-attr 'filtered-candidate-transformer
(append '(helm-skip-boring-buffers)
(helm-get-attr 'filtered-candidate-transformer
source))
source)
(dolist (attr '(keymap
action
persistent-action
persistent-help
help-message
match
mode-line
heder-line
find-file-target))
(helm-set-attr attr
(helm-get-attr attr source-buffers)
source)))
(plist-put args
:sources (append sources
(list helm-source-buffer-not-found))))
args))

(defun exordium--helm-switch-to-buffer-completing-read (&rest args)
; checkdoc-params: (args)
"Ensure `helm-source-buffers' attributes are used."
(let* ((current-command (or (helm-this-command) this-command))
(str-command (if current-command
(helm-symbol-name current-command)
"completing-read"))
(buf-name (format "*%s*" str-command)))
(unwind-protect
(progn
(advice-add
'helm
:filter-args #'exordium--helm-swith-to-buffer-update-sources)

(apply #'helm-completing-read-default-handler
(append args
(list str-command buf-name))))
(advice-remove
'helm #'exordium--helm-swith-to-buffer-update-sources))))

:custom
(history-delete-duplicates t)
(helm-M-x-always-save-history t)
(helm-M-x-show-short-doc t)
(completions-detailed exordium-help-extensions)

:bind
(([remap execute-extended-command] . #'helm-M-x) ; M-x
([remap yank-pop] . #'helm-show-kill-ring) ; M-y
([remap find-file] . #'helm-find-files) ; C-x C-f
([remap find-file-read-only] . #'helm-recentf)) ; C-x C-r

:config
;; Do not show these files in helm buffer
(add-to-list 'helm-boring-file-regexp-list "\\.tsk$")
(add-to-list 'helm-boring-file-regexp-list "\\.log\\.")
(dolist (pat (list (rx ".tsk" string-end)
(rx ".log.")))
(add-to-list 'helm-boring-file-regexp-list pat))

(require 'helm-mode)
(dolist (fun '(switch-to-buffer
switch-to-buffer-other-frame
switch-to-buffer-other-tab
switch-to-buffer-other-window))
(add-to-list 'helm-completing-read-handlers-alist
(cons fun #'exordium--helm-switch-to-buffer-completing-read)))
(helm-mode))

(use-package helm-descbinds
:after (helm)
:defer t
:bind
(("C-h b" . #'helm-descbinds)))
("C-h b" . #'helm-descbinds))

(use-package helm-ag
:after (helm)
:defer t
:custom
(helm-ag-insert-at-point 'symbol)
:bind
(("C-S-d" . #'helm-do-ag)
("C-S-f" . #'helm-do-ag-this-file)))

(use-package helm-ag
:after (helm)
:defer t
:unless exordium-helm-projectile
:bind
(("C-S-a" . #'helm-ag-project-root)))
("C-S-a" . #'helm-ag-project-root))

(use-package helm-rg
:after (helm)
:defer t)

(use-package helm-rg
:after (helm)
:unless exordium-helm-projectile
:defer t
:bind
(("C-S-r" . #'helm-rg)))
("C-S-r" . #'helm-rg))

(use-package helm-swoop
:after (helm)
:defer t
:init
(use-package isearch
:ensure nil
:defer t
:bind
(:map isearch-mode-map
("C-S-s" . #'helm-swoop-from-isearch)))

:commands (helm-swoop--edit-complete
helm-swoop--edit-cancel
helm-swoop--edit-delete-all-lines)
Expand All @@ -109,12 +205,9 @@
("C-c C-k" . #'helm-swoop--edit-cancel)
("C-c C-q C-k" . #'helm-swoop--edit-delete-all-lines)))

(use-package helm-xref
:after helm
:if exordium-helm-everywhere
:commands helm-xref
:config
(setq xref-show-xrefs-function 'helm-xref-show-xrefs))
(when exordium-helm-everywhere
(use-package helm-xref
:defer t))



Expand Down
Loading