Reqesting new add-on hook for editor to wrap selection

I would so much like to have a hook that, on a keyboard shortcut, wrapped the text selected in the editor with strings supplied as arguments to the hook function. Two strings: one prefix, one postfix. My use would be an add-on that let users (me!) specify shortcuts and associated prefix/postfix strings. I’d be wrapping with HTML start/end tags, but I suppose there could be other uses.

I know at least one add-on is doing this with custom code for each type of pre-/postfix (“Custom Styles (font color, background colour, classes)”) but despite spending a lot of time with a debugger I haven’t been able to derive a reliable way of accomplishing what I need with existing hooks.

So exactly would you need? I’m the maintainer of the add-on you mentioned and I wonder what you mean.

Thanks for asking, @hengiesel. I’m editing around 675 cards that I didn’t create, and there are some frequent editing tasks that I’d like to automate down to a shortcut key. One of the tasks is changing text (foreground) color to one of a set of about six colors and optionally simultaneously toggling the style of the text with respect to one of five Anki built-in styles. I wrote an add in to do that (Set Color and Maybe Format), but those functions don’t require wrapping because I can call an editor function (_wrapWithColour, toggleBold, …, toggleSubscript) that does the wrapping for me.

There are some other things I’d like to do that aren’t so simple. Well, conceptually simple but not so simple for me to implement in an add-on; they require only wrapping the selection with p, div, or span tags to get desired display effects. However, I might be able to do them with the “Wrapper meta-addon” add-on which I was just looking at when I got notice of your reply.

1 Like

Hey, I’ve got pretty much the same request, and you already know about the two addons that allow some functionality of wrapping with custom html. There is a possible issue with the Wrapper meta-addon. So there’s a third addon that works where Wrapper meta-addon fails, though it only can be configured for one wrapping option: editor: wrap selected text with custom html - AnkiWeb.