Print function

Is there a function to print the content of the editor (with syntax coloring)?

I guess the default method is to create a window with just the editor content, and print the window. However, I don’t know how to disable the scrolling.

EDIT: I will explore the full screen mode. Probably the way to go…

In V5 you have this addon: CodeMirror: Mode Runner Demo

Thanks for the information on Runner mode.

For Print, it does NOT works with the full screen addon and setting the option viewportMargin: Infinity (to get the full text printed). Since you can only print a browser window, I still need to find a way to remove the scrolling control.

I will try with the Runner mode.

I have problem with the mode argument of CodeMirror.runMode.

When I check the source code of the demo, I see

function doHighlight() {
  CodeMirror.runMode(document.getElementById("code").value, "application/xml",
                     document.getElementById("output"));
}

The second argument for the mode is "application/xml".

In my case, I use the ruby mode. The ruby.js script file is in the same folder as the codemirror.js file. I tried to pass "ruby", "application/ruby", {name: 'ruby'}, but the highlighting does not work (the text itself is correctly transferred to the Div node).

Is there something I missed?

The library doesn’t automatically load modes—you have to arrange for that yourself.

I had loaded the ruby.js with a <script> directive and checked it is loaded.

Is there something else I need to do?

No, that should do. Check if it’s available in CodeMirror.modes.

It’s there.

When I inspect CodeMirror.modes['ruby'], I get

start print function (config) {
  var curPunc;

  function chain(newtok, stream, state) {
    state.tokenize.push(newtok);
    return newtok(stream, state);
  }

  function tokenBase(stream, state) {
    if (stream.sol() && stream.match("=begin") && stream.eol()) {

It might be my Sketchup environment (CEF browser) that causes the problem, but it does work well with a true editor instance.

What I do for printing is simply

  • create a page with just one Div
  • execute a small Javascript code
		var hdiv = document.getElementById('DIV_print') ;
		CodeMirror.runMode('some code', 'ruby', hdiv) ;

I found the problem.

This comes from the main CSS codemirror.css, more exactly the rule with descendant selectors like .cm-s-default .cm-keyword {color: #708;}.

Apparently with CodeMirror.runmode-standalone, the <span> elements created are not set with the class .cm-s-default. Adding it makes it work.

I finally managed the print (whole document or selection) with a true editor instance, in order to have the line numbers.

Thanks for your support.