Read-only editor

Is there any way to make an editor read-only?

3 Likes

Not that I know of, but that sounds like a good idea …

It is absolutely necessary when opening a read-only file although I need it for a different reason.

Cool idea. It’d be cool if you could get to this from all or any:

  • Tree View
  • Context Menu
  • A button on the tab

Well, no, it’s useful to be able to edit read-only files and save them somewhere else (or elevate your permissions and save over the read-only file anyway).

I actually can’t think of a reason why editing should ever be absolutely forbidden… not that you don’t have one. :slight_smile:

1 Like

You are right. And actually my need has gone away since my package design
changed.

So ignore this thread.

Are there any updates on this thread or maybe a third party package built to support this?
Would love to have a feature to mark a whole project as read only or making individual editor tabs as readonly by having an option to rightclick on the tab and change its mode.

+1
Read-only tabs are really useful.
Especially when you work with lots of windows - sometimes focus is not where you’re expecting it to be.

Example usage:

  • log viewing
  • peaking on some important configuration file
  • viewing a diff file or diff output (from a plugin?)

…Generally viewing any file that you don’t want to mess in (big files, important files), like sudoers - one mistake and your locked out of sudo.

It is important though to allow turning this on/off for the tab.
While working on an important configuration file I frequently block edition, search, unblock and change a thing, then block again not to mess anything.

You can add the following to your init.coffee or init.js. It adds a command, custom:make-active-editor-readonly, that will turn the active editor into a readonly editor. No option to turn it back off, you’ll have to close the editor. Or hack some more on this :smile:

atom.commands.add('atom-workspace', 'custom:make-active-editor-readonly', () =>
  editor = atom.workspace.getActiveTextEditor()
  editor.onDidChange(() =>
    atom.commands.dispatch(atom.views.getView(editor), 'core:undo');
  )
)
1 Like

Here’s my case for read-only mode: When working with transpiled languages (CoffeeScript, Moonscript) I often keep the output file side by side with source so I can see the code that’s generated. In the heat of development, It’s all too easy to make changes in the output file resulting in momentary confusion and wasted time.

That’s a great example.

+1
Such a great syntax highlighting editor would be a perfect file viewer for me (to be used with Total Commander)

I needed a way to make editors read-only for a package I’m working on.
I moved the relevant code to a package, so if anyone is still looking for a way to make editors read-only: toggle-read-only.

Another use case is commands like “bundle open”. It’d be really nice to be able to “bundle open faraday” and read the source code without an accidental keystroke changing one of the files in the gem. There are myriad use cases in OSS (both contribution and learning) that could be a little better with a flag on the the command line that would make a window or buffer read-only.

Another use case:
when reading code from a huge project/repo and just need to study (this happens a lot in hardware design for example). Read only mode would be absolutely useful.

Readonly editors exist now

so is there any native way to put atom into readonly?

If you mean “is there a button” by “native”, I don’t know. But all you need to do is call the set read only method on the TextEditor, which could easily be implemented in a command (e.g., “toggle read only”) if it doesn’t already exist.

Thanks @Aerijo.
How about the following implementation as an example:
(additions to existing files)

KEYMAP … keymap.cson

# MAIN TEXT EDITOR
'atom-text-editor:not(.mini)':
  'ctrl-k ctrl-j': 'custom:toggle-read-only'

(Change the keybinding to anything that makes sense for you.)

INIT SCRIPT … init.coffee

# ****************************************************
# TOGGLE READ-ONLY FOR TEXT EDITOR
# ****************************************************
atom.commands.add 'atom-text-editor', 'custom:toggle-read-only', (e) ->
  console.log 'TOGGLE EDITOR READ-ONLY MODE'
  return false unless e = atom.workspace.getActiveTextEditor()
  
  # visible & in-focus editor
  viewEditor = atom.views.getView(e)
  
  # tab belonging to visible & in-focus editor
  view = atom.views.getView(atom.workspace.getActivePane())
  selector = "li.texteditor.tab.sortable.active"
  viewTab = view.querySelector(selector)
  
  if (e.isReadOnly() == true)
    e.setReadOnly(false)
    viewEditor.style.backgroundColor = ""
    viewTab.style.borderTop = ""
    console.log 'Read-Only = false'
  else
    e.setReadOnly(true)
    viewEditor.style.backgroundColor = "hsl(216, 13%, 12%)"
    viewTab.style.borderTop = "3px solid red"
    console.log 'Read-Only = true'
    

EFFECT

The unaffected tabs in my current theme looks like:
(All the editors in the picture is read-write capable.)
image

Triggering Crtl K, Ctrl J will set the active editor to read-only:
image
Still marked even if the editor is not active:
image
Writing to the marked editor is not possible.



EDIT1: Change method to read / write the `read-only` state as per @Aerijo example.

EDIT2:
Fix a bug - At times wrong tab was marked in red.
Addition - Change background colour of text-editor; usable when using zen.

EDIT3:
Fix a bug - Incorrect selection if tabs had same title.
Add some small notes in code.

Yeah, pretty much that. It seems the method is not part of the public API, but it’s been supported for years so I wouldn’t worry too much about breaking changes (i.e., it being removed).

Here’s an alternative outline in JS, that uses the this scoping of function to get the editor associated with the element. It should also be more robust against future changes, as it uses the setReadOnly methods rather than changing the field directly (there could potentially be other things the editor needs to do when changing readonly mode).

atom.commands.add("atom-text-editor", {
  "toggle-readonly": function() {
    const editor = this.getModel();
    const isReadOnly = editor.isReadOnly();
    if (editor.isReadOnly()) {
      editor.setReadOnly(false);
      // do things
    } else {
      editor.setReadOnly(true);
      // do things
    }
  }
});
1 Like