Toggling readOnly and extraKeys options

short version:
can you set options, particularly extraKeys option at runtime (rather than in constructor config)? More importantly, if I set an extra key options, e.g. extraKeys: { Tab: false }, how do I restore it to default behavior later?

long version:
i need to be able to toggle editability of a CodeMirror instance after it’s been constructed. Setting readOnly to false or ‘nocursor’ does not completely make an editor ReadOnly, as you can still Tab into the document and then start inserting Tabs and deleting. (You can’t type anything else, but Tab and backspace still work in readOnly mode.) Hence, I need to set the {extraKeys: { Tab: false }} option. But then I need to later unset it to whatever the default behavior is.

thanks,
John

A read-only editor is supposed to be read-only. even when it comes to tab and backspace. I haven’t been able to reproduce the issue you describe.

As for changing options at runtime, if you go to the manual and read the ‘configuration’ section, it’s hard to miss the setOption method.

Yeah, unfortunately you can still Tab into CodeMirror and then start inserting tabs when in ‘nocursor’ mode.

Apparently setOption is hard to miss in the docs, as I found it faster by scanning through the source code than going through the docs. :slight_smile: Also, apparently there is a removeKeyMap function, so that myself my other problem. I will give it a shot.

Could you describe how you’re doing that? Because I can not reproduce it.

If I launch a page with a CodeMirror editor in it in with readOnly: ‘nocursor’ or readOnly: false set, then start pressing tab, eventually CodeMirror gains focus and subsequent tab presses start inserting tabs into the document. when readOnly is set to false, you see the text insertion cursor when this happens, whereas when it’s set to ‘nocursor’ you do not, but in either case, the user can insert tabs. Interestingly, they can also delete them, but no other characters can be entered.

For what it’s worth, I run CodeMirror within Firepad, so maybe firepad is somehow to blame. Regardless, I can disable it by setting ‘Tab’ to false in extraKeys. Assuming addKeyMap and removeKeyMap work like how they sound they ought to, I think I can solve my toggling problem. (Along with using setOption to update the value of readOnly.)

Which version of CodeMIrror are you using? Can you reproduce this by going to, for example, http://codemirror.net/mode/javascript/ , and typing editor.setOption("readOnly", true) into the console? (I can’t.)

No, I can’t reproduce it there, the editor doesn’t get focus in ‘nocursor’ mode. Maybe there is something to it being embedded in Firepad that breaks it for me. I’m using version 5.10.0 of CM.

Strange. Thanks for verifying!