Home Download Buy Blog Forum Support

Saving a read-only file

Saving a read-only file

Postby handycam on Wed Jan 25, 2012 2:40 am

If I open a read-only file in TextMate or BBEdit, make changes and save I get a prompt that the file was opened read-only and do I want to overwrite the file.

However, in ST it just generates an error that the file couldn't be saved.

Could we get the behavior found in the other editors?
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: Saving a read-only file

Postby KPr on Mon Feb 20, 2012 7:52 pm

Just started testing Sublime yesterday. So far I do really like it but this behavior for read-only files is really annoying.

Using it with perforce is pretty much impossible as I have to manually remove read only flag by saving files as then looking for the file and editing its permissions in windows. Notepad++ makes this much easier by not only blocking edits while its read only but you can right click on the tab and toggle the read only status of an open file very easily.

Tried to find something similar in sublime but failed...

KPr
KPr
 
Posts: 2
Joined: Mon Feb 20, 2012 7:28 pm

Re: Saving a read-only file

Postby KPr on Mon Mar 12, 2012 11:16 am

Any ideas on how to improve on the current limitations with read only files?
KPr
 
Posts: 2
Joined: Mon Feb 20, 2012 7:28 pm

Re: Saving a read-only file

Postby xapoh on Sat Jun 02, 2012 2:35 am

I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime's Packages\User folder and it should just work.

Code: Select all
import sublime, sublime_plugin, os, stat

class PreSaveCommand(sublime_plugin.EventListener):
   def on_pre_save(self, view):
      myFile = view.file_name()
      fileAtt = os.stat(view.file_name())[0]
      if view.is_dirty():
         if (not fileAtt & stat.S_IWRITE):
            if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
               os.chmod(myFile, stat.S_IWRITE)

xapoh
 
Posts: 8
Joined: Fri Jun 01, 2012 11:50 pm

Re: Saving a read-only file

Postby prmaple on Sat Jun 02, 2012 10:12 am

xapoh wrote:I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime's Packages\User folder and it should just work.

Code: Select all
import sublime, sublime_plugin, os, stat

class PreSaveCommand(sublime_plugin.EventListener):
   def on_pre_save(self, view):
      myFile = view.file_name()
      fileAtt = os.stat(view.file_name())[0]
      if view.is_dirty():
         if (not fileAtt & stat.S_IWRITE):
            if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
               os.chmod(myFile, stat.S_IWRITE)



although I can't imagine that under any situation I would edit a read-only file, that's good code.
Find me
HTML5 Games: SNAKE! Tetris!
prmaple
 
Posts: 77
Joined: Sun Dec 25, 2011 11:06 am
Location: Melbourne

Re: Saving a read-only file

Postby tito on Sat Jun 02, 2012 11:00 am

It would be nice to hook postsave and let the file in the original sttus.
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 846
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: Saving a read-only file

Postby xapoh on Sat Jun 02, 2012 8:05 pm

Actually, I needed files to stay not read-only. That way later I can easily find files that were modified by me (not received from VCS) to check them in.

Code to re-set the read-only attribute is about the same as clearing it, so I'll leave it as an exercise for the reader. ;)
xapoh
 
Posts: 8
Joined: Fri Jun 01, 2012 11:50 pm

Re: Saving a read-only file

Postby xapoh on Fri Jun 22, 2012 8:30 pm

I added one more even listener to the plugin.
I am using the TFS and if the read-only attribute was cleared for the file, later I'd have to find the file, properly check it out and commit. Of course, I have the Sublime TFS plugin installed. But it is hard to remember which files were already checked out.
In order to make it little easier, a new listener was added to the plugin. Now, if a read-only file is being edited, the plugin will prompt if user wants to check the file out first. After that check out will continue in the background (and in the Sublime status bar) and user can continue editing the file.
To avoid re-prompting on every keystroke, buffer_id will be saved and user will not be asked about the view again.

Code: Select all
import sublime, sublime_plugin, os, stat, sublime_tfs

known_views = {}
class CustomFileCommands(sublime_plugin.EventListener):
    def on_modified(self, view):
        global known_views
        id = view.buffer_id()
        myFile = view.file_name()
        fileAtt = os.stat(view.file_name())[0]
        if (not (id in known_views) and (not fileAtt & stat.S_IWRITE)):
            known_views[id] = True
            if(sublime.ok_cancel_dialog('The file is Read-Only. Check out?', 'Go ahead!')):
                tfs = sublime_tfs.TfsCheckoutCommand(view)
                tfs.run(view)

   def on_pre_save(self, view):
      myFile = view.file_name()
      fileAtt = os.stat(view.file_name())[0]
      if view.is_dirty():
         if (not fileAtt & stat.S_IWRITE):
            if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
               os.chmod(myFile, stat.S_IWRITE)

xapoh
 
Posts: 8
Joined: Fri Jun 01, 2012 11:50 pm

Re: Saving a read-only file

Postby HellesAngel on Fri Jun 28, 2013 2:05 pm

xapoh wrote:I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime's Packages\User folder and it should just work.


Unfortunately this doesn't seem to work with ST3 - any idea why not, and any chance of an update? Thanks in advance.
HellesAngel
 
Posts: 11
Joined: Thu Jun 27, 2013 8:44 pm

Re: Saving a read-only file

Postby HellesAngel on Sat Jun 29, 2013 2:21 pm

After a bit of research into plugin basics I found the problem - mysterious indentation problems from the copy-n-paste of the plugin, there was an error loading the plugin in the console ctrl ~. It now works as advertised.
HellesAngel
 
Posts: 11
Joined: Thu Jun 27, 2013 8:44 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: Google [Bot], natebeaty, Yahoo [Bot] and 32 guests