Skip to content

armindarvish/consult-web

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

consult-web - a powerful versatile omni search inside emacs

Armin Darvish GNU Emacs

Announcement

This package is now deprecated in favor of its successor consult-omni!

About consult-web

consult-web is a package for getting search results from one or several custom sources (web search engines, AI assistants, elfeed database, org notes, …) directly in Emacs minibuffer. It provides wrappers and macros around consult, to make it easier for users to get results from search engines, websites, AI assistants, etc. inside emacs minibuffer completion. In other words, consult-web enables getting consult-style multi-source or dynamically completed results in minibuffer but for search engines and APIs (e.g. simmilar to consult-web but for runing a google search from within emacs minibuffer). It provides a range of sources as examples, but the main idea here is to remain agnostic of the source and provide the toolset to the users to define their own sources similar to what consult does for local sources.

Here is the mandatory screenshot:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/dynamic-omni-screenshot.gif

For a detailed review of the package and comparison to other tools, see my youtube video here: https://www.youtube.com/watch?v=7pDfyqBZwvo

You can also read my blog post about the motivation and broader context of why I made consult-web and how I use it in everyday examples.

Getting Started

Before you start, make sure you understand three points:

  1. Important Note 1: This is work in progress in its early stage and bugs and issues are very much expected.
  2. Important Note 2: You should consider the general risks of using emacs to browse the web. By default, all codes are trusted inside emacs and browsers are naturally the target of many attacks. Therefore, it is important to be aware of the risks and be intentional about what links you open (or do not open) inside emacs. By default, consult-web would only be opening web pages (or calling APIs of) the sources (e.g. search engines, …) and not any other websites. It’s up to the user then to decide how she or he wants to open the links and chose their own risk tolerance. consult-web provides customization variables for different actions (e.g. opening links in external browsr v.s. in emacs), so make sure you know how to set everything up.
  3. Important Note 3: The functions provided in consult-web-sources, provide a basic demonstration for integrating different services (such as search providers), however since each service comes with its own terms and conditions (that may change over time and vary from location to location), it is difficult to provide all-encompassing solutions and maintain them over time. consult-web is agnostic of how you connect and integrate other services in your setup (because neither consult-web nor emacs collect any information of the users or their usage), and therefore ultimately only you the user are responsible for setting up everything correctly and understand consequences of the usage (e.g. costs of using paid APIs) and ensure to stay within the bounds of relevant laws and regulations for your usage (i.e. follow software user agreements, etc.). Therefore, it is important for you to read and understand how to use each service, and also understand what happens under the hood when you integrate the service with consult-web. I try my best to provide documentation here as well as on the wiki pages, and will try to help when possible but before you proceed understand that you do everything at your own risk.

Installation

If you want an example config see Drop-in *Example Config*. Here is some detailed explanation;

Requirements

In order to use consult-web, you need emacs >28.0 (I have not tested earlier versions) and consult. While this is the only requirements, I suggest you review consult’s README since it recommends some other packages and useful configurations for different settings. Some of those extra packages and settings can improve your experience of consult-web as well. In particular, the section about asynchronous search is important for learning how to use inputs to search for result and narrow down in minibuffer. In addition combining consult with other packages such as vertico, orderless, and embark can improve the functionality as well as user-experience.

Installing consult-web Package

consult-web is not currently on ELPA or MELPA. Therefore, you need to install it using an alternative non-standard package manager such as straight.el or use manual installation.

straight.el

To install consult-web with straight.el you can use the following command. Make sure you load consult-web after loading consult (e.g. require 'consult).

(straight-use-package
 '(consult-web :type git :host github :repo "armindarvish/consult-web" :files (:defaults "sources/*.el"))

or if you use use-package macro with straight, you can do:

(use-package consult-web
	:straight (consult-web :type git :host github :repo "armindarvish/consult-web" :files (:defaults "sources/*.el"))
        :after consult
)

You can also fork this repository and use your own repo.

manual installation

Clone this repo and make sure the files are on your load path, as described on EmacsWiki.

Make sure you load consult (e.g. require 'consult) before you load consult-web.

Adding Search Sources

Loading Sources

load all default sources at once

You can add search ALL the default sources by loading the provided consult-web-sources module:

(require 'consult-web-sources)

This provides sources for some popular services, and adds a long list of interactive commands (dynamic search, static search or both depending on the source). Over time I hope to add more services, hopefully by contribution from the community as well. Note that these are also good examples for you to learn how to add your own sources or tweak the current ones for your specific use-cases.

load a single source

Alternatively you can load a single source by just requiring the corresponding file. For example for google, you can do:

(require 'consult-web-google)
(require 'consult-web-sources)

This would add interactive commands only for searching google (e.g. consult-web-google and consult-web-dynamic-google).

load multiple sources but not all

You can also load multiple sources (but not all) by setting the list consult-web-sources-modules-to-load and then loading consult-web-sources;

(setq consult-web-sources-modules-to-load '(consult-web-google consult-web-wikipedia))
(require 'consult-web-sources)

This limits the sources that consult-web-sources loads to ONLY those defined in consult-web-sources-modules-to-load.

List of Sources Provided by Default

SourceCategory
chatGPTSimple AI prompts
BingSearch Engine
BraveSearch Engine
Brave AutoSuggestAutoSuggest
consult-line-multiLocal Text in Buffers
consult-notesLocal Notes
consult-bufferLocal Buffers
DuckDuckGo (Limited API)Search Suggestions
ElfeedFeeds (RSS, videos,…)
GoogleSearch Engine
Google AutosuggestAutoSuggest
gptelAI Assistant
Doi.orgAcademic Reference
PubMedAcademic Reference
ScopusAcademic Reference
StackOverflowCommunity Forum
WikipediaEncyclopedia
YouTubeVideos

Web Search Sources

Bing

Microsoft Azure cloud services include Bings Search API. You need to make an account and get an API Key. Follow the official documents here: Bing Search APIs | Microsoft Bing to get started. Once you have an API key, you can set it up in consult-web using the following variables;

(require 'consult-web-bing)
(setq consult-web-bing-api-key "YOUR-BING-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "Bing")  ;; or (add-to-list 'consult-web-multi-sources...)
Brave

Brave provides a very good and easy-to-use API with reasonable limits and is one of my favorites when it comes to programmatic search. For API documentation refer to the official website: Brave Search API. Once you sign up and create an account you can access the documentation and create an API key. This API key can then be set in consult-web in the following custom variable. For a more secure approach you can pass a function that retrieves the key to this variable:

(require 'consult-web-brave)
(setq consult-web-brave-api-key "YOUR-BRAVE-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "Brave")  ;; or (add-to-list 'consult-web-multi-sources...)
Brave Autosuggest

Brave AutoSuggest API, provides completion for search terms. You need to subscribe to autosuggest plan and create an API key specifically for the autosuggest service. Follow the official documentation here: Brave Search API and once you have an API key for autosuggest, you can set it in consult-web in the following custom variable. For a more secure approach you can pass a function that retrieves the key to this variable:

(require 'consult-web-brave-autosuggest)
(setq consult-web-brave-autosuggest-api-key "YOUR-BRAVE-AUTOSUGGEST-API-KEY-OR-FUNCTION")
(setq consult-web-default-autosuggest-command #'consult-web-dynamic-brave-autosuggest)
DOI.org

DOI.org allows you to get the target url for DOI strings (for example for academic literature). You can use the this source if you want to enter the DOI and get the target link. consult-web uses this in the backend to get target links for paper found on scopus. If you want interactive commands for the DOI.org service, you can use also use this source by:

(require 'consult-web-doi)

and then run consult-web-dynamic-doiorg or use consult-web--doi-to-url function.

DuckDuckGo Limited API

Unfortunately DuckDuckGo does not support an official API access (anymore!). In fact on their website they clearly discourage the users against doing programmatic searches. They do provide a very Limited API that only suggests relevant topics with search links on DuckDuckGo. It’s not a very useful tool as is, but I am including it in the resources as an interesting example fr users that may like the idea. You can use it by;

(require 'consult-web-duckduckgo)
(add-to-list 'consult-web-dynamic-sources "DuckDuckGo API") ;; or (add-to-list 'consult-web-multi-sources...)
Google

The official way to use google as a search engine and fetch results is through an API, Google Custom Search. consult-web provides example functions for using this service in sources/consult-web-google.el. This source can also be added to the multi-source interactive commands (e.g. consult-web-dynamic, consult-web-multi,…) by adding "Google" to the relevant source list variable.

Note that to use this source you need to sign up for google API services and get an API key and a cx number (see REST API). Once you have an API KEY and a cx number, you can set them in the following custom variables. For a more secure approach you can also pass functions that return the values instead of directly passing strings:

(require 'consult-web-google)
(consult-web-google-customsearch-key "YOUR-GOOGLE-API-KEY-OR-FUNCTION")
(consult-web-google-customsearch-cx "YOUR-GOOGLE-CX-NUMBER-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "Google") ;; or (add-to-list 'consult-web-multi-sources...)
Google Autosuggest

Google Autosuggest is not well-documented and likely not officially upported, but using suggestqueries.google.com seems to work, if you pass a client parameter (e.g. http://suggestqueries.google.com/complete/search?client=firefox&q=YOURQUERY). I have not been able to find any official document that clearly states that this is not an allowed use-case, yet there are not official documentation saying that this is allowed either. Therefore this is currently not a reliable tool but since it works, I have included it as an example in the sources. You can use it by:

(require 'consult-web-google-autosuggest)
(setq consult-web-default-autosuggest-command #'consult-web-dynamic-google-autosuggest)
Wikipedia

Wikipedia’s API, provides a way to get programmatic search results for free. Search only operations do not need an account or API key. Therefore, you can directly use consult-web-wikipedia or consult-web-dynamic-wikipedia and you can also add Wikipedia to multi-source interactive commands for example by:

(require 'consult-web-wikipedia)
(add-to-list 'consult-web-dynamic-sources "Wikipedia") ;; or (add-to-list 'consult-web-multi-sources...)
StackOverFlow

While stack exchange allows using its API without an account or API key (a.k.a. anonymously), it is recommended to register for an API key through stackapps.com, to get larger quota, and avoid getting blocked by human verification, etc. Once you have a key, you can set it in consult-web by the following custom variable. alternatively use a function that returns the key for a more secure approach:

(require 'consult-web-stackoverflow)
(setq consult-web-stackexchange-api-key "YOUR-STACKEXCHANGE-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "StackOverflow")  ;; or (add-to-list 'consult-web-multi-sources...)

Follow the official API documentation here: StackExchange API Docs. You can use their interactive tools, such as Advanced Search API to get a sense of query parameters and build your custom tool. consult-web provides examples in consult-web-stackoverflow and consult-web-dynamic-stackoverflow for fetching results from stackoverflow, but you can make your own custom tool from other stack exchange sources as well.

PubMed Entrez API

If you would like to use PubMed to search academic literature, then consult-web-pubmed and consult-web-dynamic-pubmed provide interactive commands to search PubMed. This uses PubMed’s API (as opposed to parsing the html webpage), through PubMed’s Entez e-utilities services which requires an API Key. For official documentation, see PubMed Entrez e-utilities. Once you create an API key, you can set it in consult-web using the following custom variable or a function that returns the key instead of directly using the string.

(require 'consult-web-pubmed)
(setq consult-web-pubmed-api-key "YOUR-PUBMED-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-scholar-sources "PubMed")  ;; or (add-to-list 'consult-web-multi-sources...)

I do provide an example of using html-parsing for pubmed webpage (i.e. search pubmed anonymously) in the wiki pages. This is mainly to demonstrate how one can define a source that uses html-parsing, otherwise APIs are generally preferred.

Scopus

You can also use Scopus for academic literature. To use scopus, you need to sign up for an API key. Follow the official documents here: Elsevier Developer Portal. Once you create an API key, you can set it in consult-web using the following custom variable or a function that returns the key instead of directly using the string.

(require 'consult-web-scopus)
(setq consult-web-scopus-api-key "YOUR-SCOPUS-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-scholar-sources "Scopus")  ;; or (add-to-list 'consult-web-multi-sources...)
Youtube

You can also search YouTube videos in consult-web. You need a Google API key. Refer to the official documentation: YouTube Data API | Google for Developers for how to set things up. Then you can use your API key by setting the corresponding variable in consult-web:

(require 'consult-web-youtube)
(setq consult-web-scopus-api-key "YOUR-GOOGLE-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "Youtube")  ;; or (add-to-list 'consult-web-multi-sources...)

AI Assistants

Open AI (a.k.a. chatGPT)

If you want to use Open AI’s API (e.g. for chatGPT results), you need to get an API key. Follow the official documentation here: Open AI API docs, and once you have an API key, you can set it in consult-web with the following custom variable. Using a function that returns the key is more secure than using the string directly.

(require 'consult-web-chatgpt)
(setq consult-web-openai-api-key "YOUR-OPENAI-API-KEY-OR-FUNCTION")
(add-to-list 'consult-web-dynamic-sources "chatGPT")  ;; or (add-to-list 'consult-web-multi-sources...)

consult-web-sources provides consult-web-chatgpt and consult-web-dynamic-chatgpt commands for directly sending a prompt to Open AI API and retrieving responses. Note that depending on the prompt this may be a bit slower and also be aware that when prompts are sent to OpenAI, this can incur charges on your account, so you may want to be cautious of how you use these functions.

Note that these commands are simple one-time http requests. For a more extended conversation and a full-feature experience, you can use consult-web-gptel below.

gptel

Another easy way to integrate AI assistants with consult-web is to use the amazing package, gptel. This is by far my favorite generative AI package in emacs because of how easy it is to integrate it with other things in emacs. To use this with consult-web, install gptel following the packages’s documentation here: gptel. Once you have gptel setup, you can call consult-web-gptel, or consult-web-dynamic-gptel to get answers to your prompts. Note that, these functions do not fetch the answers to your prompt right away and wait for either a preview or for selecting the candidate to send the prompt to the backend AI. This is because sending the prompt to the backend can be expensive (in terms of paid services) and can also be slow depending on the backend and how long the answer is. Therefore, by design the user needs to actively decide to send the prompt to the backend.

You can add any of the gptel sources to any of the multi-source interactive commands. For example:

(require 'consult-web-gptel)
(add-to-list consult-web-dynamic-sources "gptel")  ;; or add-to-list consult-web-multi,...

Local Sources

In addition consult-web provides utilities to combine web search sources with local sources to create “omni” multi-source searches. The followings are the sources that are provided by default.

buffers

Generally for consult-sources, you can use consult-web--make-source-from-consult-source you make a consult-web version of the same source. This is for example done in consult-web-buffers to covert sources in consult-buffer-sources to consult-web compatible versions.

(require 'consult-web-buffers)
(add-to-list 'consult-web-dynamic-omni "Buffer")
consult-line-multi

Another useful consult source for omni searches is consult-line-multi. this is provided in consult-web-line-multi.

(require 'consult-web-line-multi)
(add-to-list 'consult-web-dynamic-omni "Line Multi")
consult-notes

If you use consult-notes, you can also combine the consult-web compatible version with other sources to create powerful omni search results.

(require 'consult-web-notes)
(add-to-list 'consult-web-dynamic-omni "Reference Roam Nodes" "Zettel Roam Nodes")
elfeed

You can also add your elfeed database as a source to consult-web by;

(require 'consult-web-elfeeed)
(add-to-list 'consult-web-dynamic-omni "elfeed")

What about other sources?

I am hoping to add examples for more sources over time. I think the currently provided examples in sources, should be sufficient for users to learn how to make new sources on their own. I will add more in-depth instructions and explanations in the wiki pages for you to understand how to define new custom sources. If you use the package and come up with new sources or use-cases, I encourage you to share it with others in the wiki pages as well so everyone can benefit from it. If such source or use-case is novel enough that adds some new value beyond the current examples, I can also consider adding that to the repo, otherwise we can just keep these in the wiki pages, to avoid adding unnecessary bloat.

Configuration

consult-web is built with the idea that the user should be able to customize everything based on their use-case, therefore the user is very much expected to learn how to configure this package. Therefore, I recommend you read through this section and understand how to configure the package according to your needs and for your specific use-case.

Customization Variables

The following customizable variables are provided:

main

consult-web-default-browse-function

Default browse function for opening urls. This can be set to external browser function by;

(setq consult-web-default-browse-function 'browse-url)
consult-web-alternate-browse-function

Secondary browse function for opening urls. This can for example be set to eww or some other browsers for quick access to an alternative browser with embark actions.

(setq consult-web-alternate-browse-function 'eww-browse-url)
consult-web-default-preview-function

Default function to use for previewing links. This can for example be set to eww:

(setq consult-web-default-preview-function #'eww-browse-url)

or xwidegt-webkit:

(setq consult-web-default-preview-function #'xwidget-webkit-browse-url)
consult-web-show-preview

This turns previews on/off for all consult-web sources. It is recommended to set this to t and use preview-key to control previews per source.

consult-web-preview-key

This is the default preview key. A good choice might be “C-o”.

(setq consult-web-preview-key "C-o")
consult-web-default-count

By default consult-web retrieves only up to this many results per source. It is recommended to keep this to a low number (e.g. 5 to 10) to keep the performance fast. The default is set to 5 because nowadays for most everyday use-cases, you probably won’t need more than the top 5 results.

(setq consult-web-default-count "5")

Keep in mind that with dynamic commands of consult-web, you can always increase the number by passing arguments to -n, --count, or :count (for example by typing search term -- -n 30, you can retrieve up to 30 results.

consult-web-default-page

This is similar to going to page 2, 3,…,N on a classic search result page. If this is set to N, first (N * count/page) results are skipped and the next page of results are shown. It is recommended to keep this as default, 0, to see the top results.

Keep in mind that with dynamic commands of consult-web, you can always change the page by passing values to arguments -p, --page, or :page (for example by typing search term -- -p 2, you can get page 2.

consult-web-group-by

This is the field that is used to group the results. By default, results are grouped by the domain of the url (e.g. “bbc.com” v.s. “nytimes.com”), but you can change this to group by the name of the source (e.g. “Google” v.s. “Brave”,…) or by the full url ,etc.

(setq consult-web-group-by :source)
consult-web-multi-sources

This is a list of source name strings (e.g. '("Google", "Wikipedia", "chatGPT")) that will be used in the command consult-web-multi (see above for description).

consult-web-dynamic-sources

This is a list of source name strings (e.g. '("Google", "Wikipedia", "chatGPT")) that will be used in the command consult-web-dynamic (see above for description).

consult-web-scholar-sources

This is a list of source name strings (e.g. '("PubMed")) that will be used in the command consult-web-scholar (see above for description).

consult-web-omni-sources

This is a list of source name strings (e.g. '("Buffer" "Brave" "gptel")) that will be used in the command consult-web-omni (see above for description).

consult-web-dynamic-omni-sources

This is a list of source name strings (e.g. '("Reference Roam Nodes" "Zettel Roam Nodes" "Line Multi" "elfeed" "Brave" "gptel" "Youtube")) that will be used in the command consult-web-dynamic-omni (see above for description).

consult-web-highlight-matches

Whether consult-web highlights matches of the search term in the minibuffer candidates. This is useful to highlight the relevance of the search results.

consult-web-default-interactive-command

This is a convenient feature to bind your favorite consult-web interactive command to the command called consult-web, so it is easier to remember and find when you call M-x. You can bind this to any of the multi-source interactive commands (such as consult-web-dyamic or consult-web-multi, …) or you can bind it to a single-source command (e.g. consult-web-dynamic-google or consult-web-google,…) or alternatively define your own custom command.

consult-web-default-autosuggest-command

Default autosuggest command. consult-web provides two examples with consult-web-dynamic-brave-autosuggest and consult-web-dynamic-google-autosuggest, but you can also define other custom autosuggest commands from other sources (e.g. google, wikipedia, …)

(setq consult-web-default-autosuggest-command #'consult-web-dynamic-brave-autosuggest)
consult-web-dynamic-input-debounce

In dynamic commands, the dynamic collection process is started only when there has not been new input for consult-web-dynamic-input-debounce seconds. If you type slow or think you need time to think for what you want to search, you may want to increase this number, so you don’t run searches prematurely, especially if you want to avoid running premature search terms on paid services. By default this inherits from consult’s built-in input-debounce value, which is 0.5. Personally I find that a bit too fast for consult-web because I do not want consult-web to send a query to paid openai API while I am still typing my query so I slow it down to 0.8 - 1s.

(setq consult-web-dynamic-input-debounce 0.8)
consult-web-dynamic-input-throttle

In dynamic commands, the dynamic collection process is started only every consult-web-dynamic-input-throttle seconds. If you use API services that have limited number of queries per second, you may want to increase this number to avoid getting errors. I set this to 2x my input-debounce value:

(setq consult-web-dynamic-input-throttle 1.6)
consult-web-dynamic-refresh-delay

In dynamic commands, the completion UI is only updated every consult-web-dynamic-refresh-delay seconds. You probably want to run this as fast as consult-web-dynamic-input-debounce.

(setq consult-web-dynamic-input-throttle 0.8)

per source

As mentioned above, once you load sources (e.g. (require 'consult-web-sources)), then you will get more customization variables per source. These include variables for API keys. Here are some examples:

consult-web-google-customsearch-key and consult-web-google-customsearch-cx

API Key and cx-number for Google custom Search.

(consult-web-google-customsearch-key "YOUR-GOOGLE-API-KEY-OR-FUNCTION")
(consult-web-google-customsearch-cx "YOUR-GOOGLE-CX-NUMBER-OR-FUNCTION")
consult-web-brave-api-key

Brave Search API key.

(setq consult-web-brave-api-key "YOUR-BRAVE-API-KEY-OR-FUNCTION")
consult-web-openai-api-key

Open AI’s API key

(setq consult-web-openai-api-key "YOUR-OPENAI-API-KEY-OR-FUNCTION")
consult-web-stackexchange-api-key

StackExchange API key from stackapps.com.

(setq consult-web-stackexchange-api-key "YOUR-STACKEXCHANGE-API-KEY-OR-FUNCTION")

Be aware that as I add more sources, there may be more customization variables added. Refer to release notes or .... pages for more info if needed.

Drop in “example config”

Here is a drop-in config snippet that puts everything mentioned above together. Read the sections above for more details.

(use-package consult-web
  :straight (consult-web :type git :host github :repo "armindarvish/consult-web" :branch "main" :files (:defaults "sources/*.el"))
  :after consult
  :custom
  ;; General settings that apply to all sources
  (consult-web-show-preview t) ;;; show previews
  (consult-web-preview-key "C-o") ;;; set the preview key to C-o
  (consult-web-highlight-matches t) ;;; highlight matches in minibuffer
  (consult-web-default-count 5) ;;; set default count
  (consult-web-default-page 0) ;;; set the default page (default is 0 for the first page)

  ;;; optionally change the consult-web debounce, throttle and delay.
  ;;; Adjust these (e.g. increase to avoid hiting a source (e.g. an API) too frequently)
  (consult-web-dynamic-input-debounce 0.8)
  (consult-web-dynamic-input-throttle 1.6)
  (consult-web-dynamic-refresh-delay 0.8)

  :config
  ;; Add sources and configure them
  ;;; load the example sources provided by default
  (require 'consult-web-sources)

  ;;; set multiple sources for consult-web-multi command. Change these lists as needed for different interactive commands. Keep in mind that each source has to be a key in `consult-web-sources-alist'.
  (setq consult-web-multi-sources '("Brave" "Wikipedia" "chatGPT" "Google")) ;; consult-web-multi
  (setq consult-web-dynamic-sources '("gptel" "Brave" "StackOverFlow" )) ;; consult-web-dynamic
  (setq consult-web-scholar-sources '("PubMed")) ;; consult-web-scholar
  (setq consult-web-omni-sources (list "elfeed" "Brave" "Wikipedia" "gptel" "YouTube" 'consult-buffer-sources 'consult-notes-all-sources)) ;;consult-web-omni
  (setq consult-web-dynamic-omni-sources (list "Known Project" "File" "Bookmark" "Buffer" "Reference Roam Nodes" "Zettel Roam Nodes" "Line Multi" "elfeed" "Brave" "Wikipedia" "gptel" "Youtube")) ;;consult-web-dynamic-omni

  ;; Per source customization
  ;;; Pick you favorite autosuggest command.
  (setq consult-web-default-autosuggest-command #'consult-web-dynamic-brave-autosuggest) ;;or any other autosuggest source you define

  ;;; Set API KEYs. It is recommended to use a function that returns the string for better security.
  (setq consult-web-google-customsearch-key "YOUR-GOOGLE-API-KEY-OR-FUNCTION")
  (setq consult-web-google-customsearch-cx "YOUR-GOOGLE-CX-NUMBER-OR-FUNCTION")
  (setq consult-web-brave-api-key "YOUR-BRAVE-API-KEY-OR-FUNCTION")
  (setq consult-web-stackexchange-api-key "YOUR-STACKEXCHANGE-API-KEY-OR-FUNCTION")
  (setq consult-web-pubmed-api-key "YOUR-PUBMED-API-KEY-OR-FUNCTION")
  (setq consult-web-openai-api-key "YOUR-OPENAI-API-KEY-OR-FUNCTION")
  ;;; add more keys as needed here.
  )

Usage and Features

For explanation of features and comparison to some other packages, you can watch my youtube video: https://www.youtube.com/watch?v=7pDfyqBZwvo

Static v.s. Dynamic Interactive Commands

For each source, you may have static or dynamic commands. Static commands query the use for an input and then fetch the results. Dynamic commands, do dynamic completion as the user types (fetch results as the user is typifying). Dynamic commands feel a bit more intuitive and modern in 2024, but on the other hand have the disadvantage of sending the query to the servers multiple times especially if you type slowly! Depending on the service provider and the API model, you may want to avoid hitting the server too frequently (for example for services that you pay per query), therefore for certain services a static command might be a better choice than the dynamic command. Using the macro consult-web-define-source, you can chose to create static, dynamic or both by passing nil, t, or 'both to the keyword :dynamic. Here is an example from the source code, for creating both static and dynamic commands for Brave search:

(consult-web-define-source "Brave"
                           :narrow-char ?b
                           :face 'consult-web-engine-source-face
                           :request #'consult-web--brave-fetch-results
                           :preview-key consult-web-preview-key
                           :search-history 'consult-web--search-history
                           :selection-history 'consult-web--selection-history
                           :dynamic 'both
                           )

Multi-Source Interactive Commands

consult-web does provide a few interactive commands. These are provided as good examples for users to follow when making their own custom commands for their work flow.

  1. consult-web-multi: This is an interactive command that uses multiple sources, as defined by consult-web-multi-sources, and shows the results in minibuffer completion. Here is an example screenshot:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/multi-screenshot.gif

Note that consult-web-multi does not provide dynamic completion but some might find using this more intuitive for narrowing down the results. The user provides one search term, and once the results are retrieved, typing in the minibuffer will narrow down the candidates. In addition, as can be seen in the screenshot above, depending on your minibuffer config for sorting, it is possible to remember the candidates you selected before by sorting them on top (by using packages like orderless or prescient.el).

  1. consult-web-dynamic: This is a “multi-source” interactive command that uses “dynamic” collection. This allows dynamic completion of the search results (results are fetched as the user types). Here is a screenshot:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/dynamic-screenshot.gif

  1. consult-web-scholar: This is similar to consult-web-dynamic, and is provided as an extra example to show how to make combination of sources for a specific purpose, in this case searching academic research literature. Here is a screenshot:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/scholar-screenshot.gif

  1. consult-web-omni: This is another static multi-source command provided for combining web sources and local sources to do omni searches. Here is a screenshot:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/omni-screenshot.gif

  1. consult-web-dynamic-omni: This is the dynamic version of the omni search for combination of web and local sources. For example, in the screenshot below you see results form org-roam notes, brave search engine, wikipedia, as well as gptel AI assistant:

https://github.com/armindarvish/consult-web/blob/screenshots/screenshots/dynamic-omni-screenshot.gif

Note that the difference between consult-web-multi and consult-web-omni (and similarly between consult-web-dynamic, consult-web-scholar, consult-web-dynamic-omni) is the list of sources they use and therefore you can use them as you wish for any combination of sources. You can also define more interactive commands with various variation of sources following these examples.

Dynamic Completion: Passing Arguments and Narrow Down

Arguments can be passed to the dynamic interactive commands and further narrowing down the results can be done using a syntax similar to the “perl splitting” style in consult asynchronous search.

For narrowing down the results you need adding # after the search query. For example typing the following in the minibuffer:

#emacs web search#github

First searches for “emacs web search”, and then uses “github” for narrow down.

Furthermore, arguments can be passed to dynamic commands using similar syntax as consult-grep, too. For example typing the following in the minibuffer:

#how to browse a url in emacs -- --model gpt-3.5-turbo

passes gpt-3.5-turbo as the value for the keyword argument :model to the backend functions of all the sources that fetch results. If any of those sources accept the keyword argument :model, the value gpt-3.5-turbo gets passed to them. For this reason it is recommended to always use functions that accept any keyword arguments (a.k.a. add &allow-other-keys) to avoid errors when non-existing keywords are passed to them.

instead of using -- , you can also use a keyword with colon :. The following would be similar to the example above:

#how to browse a url in emacs -- :model gpt-3.5-turbo

Embark Actions

You can load the default embark actions by;

(require 'consult-web-embark)

The default actions allow you to open the links in the default or alternate browser and also to copy or insert, title and/or url of the links. Other embark actions can be defined per your own specific work flow. See the YouTube video for an example, here: https://youtu.be/7pDfyqBZwvo?t=4962.

Other Important Features

Minimal Code Base

Without docstrings and whitespaces the code is less than 1000 lines and it only depends on consult and built-in url-retrieve.

Modular

You can only load the parts you need. For example if all you need is an autosuggestion utility similar to helm-google-autosuggest, then you can do:

(require 'consult-web-brave-autosuggest)

This adds an extra 100-200 lines of code per source. This also means to add a new source, you only need to write a short piece of code following those examples!

Customizable and Extendable

Lots of customization options both for sources and also for running actions on the results. New sources can be added as you wish with different format, different actions,…

Power User Capabilities

Dynamic collection allows for complex workflows on the fly. Change query parameters on the fly by passing arguments. Select a random set of results ad-hoc using embark and run embark actions on them. This allows batch processing as well. For example to add a long list of results to an org-mode note for later review (as shown in this youtube video: https://youtu.be/7pDfyqBZwvo?t=4774).

Comparison to Other Packages

To the best of my knowledge no other package provides the functionality and versatility of consult-web. Browsers like EWW (GNU Emacs Manual) and Embedded WebKit Widgets (GNU Emacs Manual) only provide a browser and not the ability to get search results directly in minibuffer. Other built-in commands and packages such as WebJump or engine-mode enable passing queries to search engines, but do not provide results in the minibuffer. helm-google-autosuggest in helm provides autosuggestion utility only. counsel-web is the only package I know that provides google results directly in the minibuffer but the functionality is limited and the way it parses google website may get your IP flagged. consult-web took inspiration from all those packages and provides a much more powerful solution than any of those available solutions.

Bug Reports

To report bug, first check if it is already reported in the *issue tracker* and see if there is an existing solution or add relevant comments and discussion under the same issue. If not file a new issue following these steps:

  1. Make sure the dependencies are installed, and both consult and url-retrieve (or other relevant commands) work as expected.
  2. Remove the package and install the latest version (along with dependencies) and see if the issue persists.
  3. In a bare bone vanilla Emacs (>=28) (e.g. emacs -Q), install the latest version of consult-web (and its dependencies) without any configuration or other packages and see if the issue still persists.
  4. File an issue and provide important information and context in as much detail as possible in your bug report. Important information can include:
  • Your operating system, version of Emacs (or the version of emacsen you are using), version of consult (see pkg-info).
  • The installation method and the configuration you are using with your consult-web.
  • If there is an error message, turn debug-on-error on (by M-x toggle-debug-on-error) and include the backtrace content in your report.
  • If you are using consult-web’s built-in url-retrieve (e.g. consult-web-url-retrieve-sync) , you can change consult-web-log-level to 'debug, and inspect the log buffer (hidden buffer called ” **consult-web-log**” or other name set in consult-web-log-buffer-name). If you chose to include this information in your issue, please make sure personal information and secrets (like API keys) are not exposed.
  • If the error only exists when you have some other packages installed, list those packages (e.g. problem happens when evil is installed)

Contributions

This is an open source package, and I appreciate feedback, suggestions, ideas, etc. There are lots of functionalities or sources that can be added to this package to improve different user’s workflows, so if you have some ideas, feel free to file an issue for a feature request.

If you want to contribute to the code, please note that the main branch is currently stable (as stable as a work in progress like this can be) and the develop branch is the current work in progress. So, start from the develop branch to get the latest work-in-progress updates and create a new branch with names such as feature/name-of-the-feature or fix/issue, … Do the edits and then create a new pull request to merge back with the develop branch when you are done with your edits.

Importantly, keep in mind that I am using a literate programming approach (given that this is a small project with very limited number of files) where everything goes into consult-web.org and then gets tangled to appropriate files (e.g. consult-web.el). If you open a pull-request where you directly edited the .el files, I will likely not approve it because that will then get overwritten later when I tangle from the .org file. In other words, Do Not Edit The .el Files! only edit the .org file and tangle to .el files.

Acknowledgments

Obviously this package would not have been possible without the fabulous consult and embark packages. It also took inspiration from other packages including but not limited to engine-mode, emacs-google-this, helm, and counsel-web.