# undo removing textmarking

**URL:** <https://discuss.codemirror.net/t/undo-removing-textmarking/541>\
**Category:** Uncategorized\
**Created:** [November 16, 2015, 8:02pm UTC](https://discuss.codemirror.net/t/undo-removing-textmarking/541 "2015-11-16T20:02:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![janoonk](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/j/57b2e6/32.png) [@janoonk](https://discuss.codemirror.net/u/janoonk)\
**Post date:** [November 16, 2015, 8:02pm UTC](https://discuss.codemirror.net/t/undo-removing-textmarking/541/1 "2015-11-16T20:02:52Z")

</div>

I have this codemirror textarea where a user types in a SQL statement. When the user presses the “Run query” button the query gets run. When the query fails (for example because of a typo) I textmark the query red, when query is correct I textmark the query green. When the user continues typing/correcting/adding to the query I remove the green/red textmark. But when the user undos (CTRL-Z) I also want the textmarking to reappear. How can I accomplish this?

I use the following code to add a green/red marker:

> editor.markText(CodeMirror.Pos(0,0),CodeMirror.Pos(editor.lastLine(),Infinity),{className: colorClass, addToHistory: true});

when the marked text becomes dirty (changes) I use this code to remove the marker:

> var ranges=editor.getAllMarks();  
> for (var i=0;i`<`ranges.length;i++) ranges[i].clear();

gr Jan

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [November 21, 2015, 12:29pm UTC](https://discuss.codemirror.net/t/undo-removing-textmarking/541/2 "2015-11-21T12:29:29Z")

</div>

This isn’t something that CodeMirror will take care of, but you could, when clearing the marker, store its position, associated with `editor.changeGeneration`, and when you later get a `"change"` event that moves the editor back to that state (`editor.isClean(generation)`), restore the marker.

---

<div class="post-metadata">

**Author:** ![barelyfitz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/barelyfitz/32/215_2.png) [@barelyfitz](https://discuss.codemirror.net/u/barelyfitz)\
**Post date:** [February 12, 2016, 6:38pm UTC](https://discuss.codemirror.net/t/undo-removing-textmarking/541/3 "2016-02-12T18:38:29Z")

</div>

I’m having trouble figuring out how that might work.

Let’s say every time I want to clear a marker, I call editor.changeGeneration to get an ID.  
Then I save the marker info along with the generation ID. So I end up with a bunch of IDs and marks something like this: {8:mark1Data, 12:mark2Data, 17:mark3Data}

Then I add a listener on the “change” event. Is there anything in the change event that would tell me which generation id I should check?

Or would I have to go through all the generations I have saved (8, 12, 17) and check each one with isClean? If there’s no way to determine which one to check, then perhaps I should save the marks in a stack, and only check the last one that was cleared (and pop it off the stack if it is added back).

---

<div class="post-metadata">

**Author:** ![barelyfitz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/barelyfitz/32/215_2.png) [@barelyfitz](https://discuss.codemirror.net/u/barelyfitz)\
**Post date:** [February 15, 2016, 4:03pm UTC](https://discuss.codemirror.net/t/undo-removing-textmarking/541/4 "2016-02-15T16:03:49Z")

</div>

Actually after playing with this some more, I don’t think this will work for me; I might need to provide my own undo/redo functionality instead of relying on that provided by CodeMirror.
