[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/engine-mode 7b0872e297 23/71: Provide a hook to transform
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/engine-mode 7b0872e297 23/71: Provide a hook to transform the search term |
Date: |
Wed, 21 Dec 2022 09:59:08 -0500 (EST) |
branch: elpa/engine-mode
commit 7b0872e29703532733136425b28221591074c369
Author: Harry Schwartz <hello@harryrschwartz.com>
Commit: Harry Schwartz <hello@harryrschwartz.com>
Provide a hook to transform the search term
Sometimes, for a given engine, we want to apply a function to a search
term before it's interpolated into the URL. This commit provides a
`term-transformation-hook` keyword argument in `defengine` that takes a
function and ensures that it'll be invoked, with the raw term as its
argument, every time the engine is evaluated.
For example, suppose we want to ensure that search terms are encoded as
latin-1. We could pass in an anonymous function:
(defengine duckduckgo
"https://duckduckgo.com/?q=%s"
:term-transformation-hook (lambda (term) (encode-coding-string term
latin-1)))
Thanks to @patxoca for the suggestion and preliminary implementation!
---
README.md | 27 +++++++++++++++++++++++++++
engine-mode.el | 21 +++++++++++++++++----
2 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 5bc61fa3b4..f93b68c69c 100644
--- a/README.md
+++ b/README.md
@@ -86,6 +86,32 @@ keyword argument:
:docstring "Search the Comprehensive TeX Archive Network (ctan.org)")
```
+## Modifying the search term before sending it
+
+An engine might want to transform a search term in some way before it
+interpolates the term into the URL. Maybe the term should have a
+different encoding, or be capitalized differently, or, uh, be passed
+through [ROT13]. Whatever the reason, you can apply a custom
+transformation to a search term by passing a function to `defengine`
+through the `:term-transformation-hook` keyword argument.
+
+For example, to UPCASE all of your DuckDuckGo searches:
+
+```emacs
+(defengine duckduckgo
+ "https://duckduckgo.com/?q=%s"
+ :term-transformation-hook 'upcase)
+```
+
+Or, to ensure that all your queries are encoded as latin-1:
+
+```emacs
+(defengine diec2
+ "dlc.iec.cat/results.asp?txtEntrada=%s"
+ :term-transformation-hook (lambda (term) (encode-coding-string term latin-1))
+ :keybinding "c")
+```
+
## Importing keyword searches from other browsers
Since many browsers save keyword searches using the same format as
@@ -149,4 +175,5 @@ the `:keybinding` keyword to the generated engine
definitions).
```
[the talk @hrs gave at EmacsNYC]: https://www.youtube.com/watch?v=MBhJBMYfWUo
+[ROT13]: https://en.wikipedia.org/wiki/ROT13
[import from Chrome on OS X]:
https://gist.github.com/sshaw/9b635eabde582ebec442
diff --git a/engine-mode.el b/engine-mode.el
index 662eb2b866..e5830bfdc0 100644
--- a/engine-mode.el
+++ b/engine-mode.el
@@ -97,18 +97,31 @@
`(define-key engine-mode-map (kbd ,(engine/scope-keybinding keybinding))
(quote ,(engine/function-name engine-name)))))
-(cl-defmacro defengine (engine-name search-engine-url &key keybinding
docstring)
+(cl-defmacro defengine (engine-name search-engine-url &key keybinding
docstring (term-transformation-hook 'identity))
"Define a custom search engine.
`engine-name' is a symbol naming the engine.
`search-engine-url' is the url to be queried, with a \"%s\"
standing in for the search term.
-The optional keyword argument `keybinding' is a string describing
-the key to bind the new function.
The optional keyword argument `docstring' assigns a docstring to
the generated function. A reasonably sensible docstring will be
generated if a custom one isn't provided.
+The optional keyword argument `term-transformation-hook' is a
+function that will be applied to the search term before it's
+substituted into `search-engine-url'. For example, if we wanted
+to always upcase our search terms, we might use:
+
+(defengine duckduckgo
+ \"https://duckduckgo.com/?q=%s\"
+ :term-transformation-hook 'upcase)
+
+In this case, searching for \"foobar\" will hit the url
+\"https://duckduckgo.com/?q=FOOBAR\".
+
+The optional keyword argument `keybinding' is a string describing
+the key to bind the new function.
+
Keybindings are prefixed by the `engine/keymap-prefix', which
defaults to `C-c /'.
@@ -128,7 +141,7 @@ Hitting \"C-c / w\" will be bound to the newly-defined
,(or docstring (engine/docstring engine-name))
(interactive
(list (engine/get-query ,(symbol-name engine-name))))
- (engine/execute-search ,search-engine-url search-term))
+ (engine/execute-search ,search-engine-url (,term-transformation-hook
search-term)))
,(engine/bind-key engine-name keybinding)))
(provide 'engine-mode)
- [nongnu] elpa/engine-mode c3f2de1a8a 01/71: initial commit, (continued)
- [nongnu] elpa/engine-mode c3f2de1a8a 01/71: initial commit, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode f8540c9982 05/71: get search term through interactive, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode e20ec930b2 08/71: Merge pull request #2 from lgatto/master, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode da00833130 09/71: we're on MELPA! :D, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode baa5151552 32/71: Reformat README to use 80-character lines, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 9e8b10b029 27/71: Release version 1.0.0, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 9a1271b005 38/71: Update documentation to change to `C-x /` prefix, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 2ddc5ec627 20/71: Switch engine keybinding to be a keyword argument, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 615e55a806 31/71: Add GPLv3 badge to README, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 2935da0bc8 15/71: Link to @sshaw's script to import from Chrome/OSX, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 7b0872e297 23/71: Provide a hook to transform the search term,
ELPA Syncer <=
- [nongnu] elpa/engine-mode f7b5aadafa 47/71: Fix cl/-lib usage, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 5d99c1d49d 41/71: Add autoload cookie for lazy loading, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 2c92e76819 56/71: Bump version to 2.1.2, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 3d4870c337 50/71: Fall back to the browse-url-browser-function, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode f12a4e5ed1 03/71: rename engine -> engine-mode for consistency, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 8851758d4d 21/71: Allow defengine to assign custom docstrings, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 4e1d462a30 18/71: Add link to the EmacsNYC presentation, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode feb41a46e3 48/71: Escape docstring bol open-parentheses, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode 08d1a52ce6 43/71: Autoload engine-mode, ELPA Syncer, 2022/12/21
- [nongnu] elpa/engine-mode def7195c87 55/71: Include dependency on format-spec, ELPA Syncer, 2022/12/21