Custom mode per domain

I would like to be able to have custom modes and keybindings for certain domain.
For example, I’d like to have a mode for zulip, slack, gmail and so on.

Are there any examples that demonstrate how to do that?

1 Like

Sure, so the first thing you’ll need to do is define a mode.

To do so, you can use the define-mode macro. An example of that could be like this:

(define-mode website-specific-mode ()
  "Mode for website X."
  ((keymap-scheme
    (define-scheme "website-specific-mode"
      scheme:cua
      (list
       "down" 'some-command)))))

then you can use auto mode rules to apply your mode. To do that refer to the documentation from your auto-mode-rules.lisp file. Located in ~/.local/share/nyxt/auto-mode-rules.lisp:

;; List of auto-mode rules.
;; It is made to be easily readable and editable, but you still need to remember some things:
;;
;; Every rule starts on a new line and consists of one or more of the following elements:
;; - Condition for rule activation. It is either (match-domain ...),
;;   (match-host ...), (match-regex ...) or a string.
;; - :included (optional) -- List of the modes to enable on condition.
;; - :excluded (optional) -- List of the modes to disable on condition.
;; - :exact-p  (optional) -- Whether to enable only :included modes and disable
;;    everything else (if :exact-p is t), or just add :included and exclude :excluded
;;    modes from the current modes list (if :exact-p is nil or not present).
;;
;; Included modes is a list of mode symbols, or a list of lists in the form
;; of (MODE-SYMBOL INIT-ARGS), where init-args are passed to the mode when instantiated.
;;
;; Conditions work this way:
;; - match-domain matches the URL domains only.
;;   Example: (match-domain "reddit.com") will work for all of Reddit.
;; - match-host is more specific -- it activates only on certain subdomains of the website.
;;   Example: (match-host "old.reddit.com") will work on old Reddit only.
;; - match-regex works for any address that matches a given regex. You can add these manually,
;;   but remember: with great regex comes great responsibility!
;;   Example: (match-regex "https://github\.com/.*/.*") will activate only in repos on GitHub.
;; - String format matches the exact URL and nothing else
;;   Example: "https://lispcookbook.github.io/cl-cookbook/pattern_matching.html"
;;            will work on the Pattern Matching article of CL Cookbook, and nowhere else.
;;
;; You can write additional URLs in the bracketed conditions, to reuse the rule for other URL
;; Example: (match-host "reddit.com" "old.reddit.com" "www6.reddit.com")

Good luck! I hope that helps!

1 Like

Thanks for the fast response.

I don’t have an auto-mode-rules.lisp file in that directory (even though the directory does exist).
So I tried creating a file that contains:

(match-domain “zulipchat.com”) :included '(zulip-mode)

but it didn’t seem to work, without giving me an error or something.

Any pointers?

Hi,

I do not know how to solve your problem using mode customizations for certain domains. Modes are really powerful because they blend keybindings, hooks, functions, and more to modify buffer behavior. I am pretty sure @jmercouris knows much more than I and will help you with that.

However, maybe you could try a smaller approach and get something you are craving for to work immediately.

Have you checked out the manual on URL-dispatch?

URL-dispatchers

You can configure which actions to take depending on the URL to be loaded. For instance, you can configure which Torrent program to start to load magnet links. See theurl-dispatching-handler function documentation.

I have never used them. But they seem interesting. Maybe you could automate something in your nyxt and slack (desktop app) interaction?

I am also new to Nyxt. Sorry if this does not help you, I am just trying to help.

You also have to enable auto-mode in your config. Auto-mode is responsible for reading the auto-mode-rules.lisp file. If it doesn’t exist, it will generate it. You can read more about it here: Nyxt.

To test without adding it to your config, you can just manually invoke auto-mode and then navigate to the domain in question. If you want some more inspiration, you can take a look at my config: My lightweight configuration - #5 by egorenar

In my case the file neither exists nor it gets generated.
I tried manually enabling auto-mode via M-x and also tried to enable it using default-modes.

Right, does it appear in your modeline? I believe if you have no auto-mode rules, the file will not be created. @aartaka do you have any suggestions?

auto-mode does appear in the mode-line, but not in the inital page / dashboard (if that makes any difference). In all pages I open after that (usually via bookmarks) I do see the auto in the listed modes.

Can you please share a copy of your config? Maybe there are some things we can try :slight_smile:

Here’s my config: GitHub - iocanel/nyxt.d: Nyxt browser configuration
Please note, that I also tried with blank config and enabling auto-mode by hand and I still don’t get an auto-mode-rules.lisp.

I used nyxt 2.0.0 from archlinux (aur) and also a custom build with the latest main branch from github, with no noticable change in behavior

I managed to get the file generated by calling the save-exact-modes-for-future-visits command.

However, that does not solve my issue. Even with blank configuration + generated auto-mode-rules.lisp, things doesn’t seem to work.

I start my browser, manually add the auto-mode and then open the url but no luck.

(
((match-host "quarkusio.zulipchat.com")  :included (certificate-exception-mode
                                                    vi-normal-mode) :exact-p t)
)

That rule appears to set the modes to include just vi-normal-mode and certificate-exception-mode. Is there something I am missing?

That rule was added just to test if auto-mode works for me without any configuration at all.
So, I completely wiped .config/nyxt, I just visited the “quarkusio.zulipchat.com” added vi-normal-mode called save-exact-modes-for-futture-visits and that generated the configuration I pasted.

I restarted the browser, called auto-mode, visited “quarkusio.zulipchat.com” and I expected to see vi-normal-mode get automatically activated, but it didn’t.

@aartaka any ideas? is auto mode working for you on master?

It appears to be working for me with the following rule:

(
("https://en.wikipedia.org/wiki/Tomato"  :included (nyxt/style-mode:dark-mode))
("https://en.wikipedia.org/wiki/Cucumber"  :included (nyxt/style-mode:dark-mode))
("https://en.wikipedia.org/wiki/Salmon"  :included (nyxt/style-mode:dark-mode))
)

when I go to the wikipedia page about cucumber, it jumps into dark mode. Does that not work for you @iocanel ?

I am afraid that doesn’t work for me either. I wipe clean everything (.config/nyxt and .local/share/nyxt) and then just create a .local/share/nyxt/auto-mode-rules.lisp with what you just provided. I start nyxt, enable auto-mode go to all 3 pages and no dark mode gets enabled.

Just to make sure: The exact version of nyxt I built is: Nyxt version 2.0.0-101-g02a98235

Had some problems with it (package mismatch for match-regex, in particular) on master.

So, is master fixed now?