>>110225329
I also have an Elisp function that takes functions that accept a string and generate a derivative function that works on regions.
(defun u/funcall-with-dwim-fn (fn)
"Return a function that either operates on the word at point or region if a region is selected.
Region selection takes precedence over `word-at-point'."
(interactive)
(lambda ()
(interactive)
(let* ((query (if (use-region-p)
(buffer-substring (use-region-beginning) (use-region-end))
(word-at-point))))
(funcall fn query))))
This is how I apply it to ichiran-romanize to create ichiran-romanize-dwim.
(defalias #'ichiran-romanize-dwim (u/funcall-with-dwim-fn #'ichiran-romanize)
"Romanize the region or word at point (if no region is selected) with ichiran.")
The ichiran-romanize-dwim is the function that I bind to a key to do phrase lookups.