Skip to content
This repository has been archived by the owner on May 1, 2022. It is now read-only.

Commit

Permalink
v2 - optional auto readonly removal
Browse files Browse the repository at this point in the history
  • Loading branch information
reflog committed Jun 16, 2013
1 parent ff148a5 commit 39465e8
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 21 deletions.
13 changes: 12 additions & 1 deletion Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
[
{ "command": "toggle_readonly", "caption": "Toggle Readonly", "id": "end" }
{
"caption": "-",
"id": "separator"
},
{
"caption": "Set Readonly",
"command": "set_readonly"
},
{
"caption": "Set Writable",
"command": "set_writable"
}
]
35 changes: 35 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"caption": "Preferences",
"children": [
{
"caption": "Package Settings",
"children": [
{
"caption": "Toggle Readonly",
"children": [
{
"args": {
"file": "${packages}/toggle-readonly/toggle-readonly.sublime-settings"
},
"caption": "Settings \u2013 Default",
"command": "open_file"
},
{
"args": {
"file": "${packages}/User/toggle-readonly.sublime-settings"
},
"caption": "Settings \u2013 User",
"command": "open_file"
}
]
}
],
"id": "package-settings",
"mnemonic": "P"
}
],
"id": "preferences",
"mnemonic": "n"
}
]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Toggle file's read-only flag in Sublime Text 2
#Toggle file's read-only flag in Sublime Text 2/3

Basic plugin for [Sublime Text 2][1] that adds a "Toggle Readonly" command to the
Basic plugin for [Sublime Text][1] that adds a "Toggle Readonly" command to the
context menu.

##Install
Expand All @@ -17,6 +17,6 @@ Or you can download the package as zip file [][3] then copy it into your Sublime
To use the command right click on the file in the side bar or in the buffer area.


[1]: http://www.sublimetext.com/2
[1]: http://www.sublimetext.com/
[2]: http://wbond.net/sublime_packages/package_control
[3]: https://github.com/reflog/toggle-readonly/zipball/master
15 changes: 14 additions & 1 deletion Side Bar.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
[
{ "command": "toggle_readonly", "caption": "Toggle Readonly" }
{
"caption": "-",
"id": "separator"
},

{
"command": "set_readonly",
"caption": "Set Readonly"
},
{
"command": "set_writable",
"caption": "Set Writable"
}

]
15 changes: 14 additions & 1 deletion Tab Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
[
{ "command": "toggle_readonly", "caption": "Toggle Readonly", "id": "end" }
{
"caption": "-",
"id": "separator"
},

{
"command": "set_readonly",
"caption": "Set Readonly"
},
{
"command": "set_writable",
"caption": "Set Writable"
}

]
20 changes: 20 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"None are so hopelessly enslaved as those who falsely believe they are free."
Johann Wolfgang von Goethe
Original author: jbrooksuk

Copyright (C) 2012 Tito Bouzout <tito.bouzout@gmail.com>

This license apply to all the files inside this program unless noted
different for some files or portions of code inside these files.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation. http://www.gnu.org/licenses/gpl.html

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/gpl.html
2 changes: 1 addition & 1 deletion package-metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"url": "http://github.com/reflog/toggle-readonly/", "version": "1.0", "description": "Toggle file's readonly state from context menu."}
{"url": "http://github.com/reflog/toggle-readonly/", "version": "2.0", "description": "Toggle file's readonly state from context menu."}
3 changes: 3 additions & 0 deletions toggle-readonly.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"auto_remove_readonly_on_save": false,
}
66 changes: 52 additions & 14 deletions toggle_readonly.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
import sublime, sublime_plugin, os, stat

class ToggleReadonlyCommand(sublime_plugin.TextCommand):
s = sublime.load_settings('toggle-readonly.sublime-settings')


def changeReadonly(filename, state):
if (os.name == 'nt'):
flag = ~stat.S_IWRITE
if not state:
flag = ~flag;
os.chmod(filename, flag)
else:
flag = ~stat.UF_IMMUTABLE
if not state:
flag = flag;
os.chflags(filename, flag)

def isReadonly(filename):
if not filename or len(filename) <= 0:
return False

fileAtt = os.stat(filename)[0]

if (os.name == 'nt'):
return not (fileAtt & stat.S_IWRITE)
else:
return not (fileAtt & stat.UF_IMMUTABLE)


class ClearChangesCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
print (s.get('auto_remove_readonly_on_save'))
if s.get('auto_remove_readonly_on_save'):
if isReadonly(view.file_name()):
changeReadonly(view.file_name(), False)


class SetReadonlyCommand(sublime_plugin.TextCommand):
def run(self, edit):
myFile = self.view.file_name()
fileAtt = os.stat(myFile)[0]
myPlatform = os.name

if (myPlatform == 'nt'):
if (not fileAtt & stat.S_IWRITE):
sublime.status_message("Making "+myFile+" writable")
os.chmod(myFile, stat.S_IWRITE)
else:
if (fileAtt & stat.UF_IMMUTABLE):
sublime.status_message("Making "+myFile+" mutable")
os.chflags(myFile, not stat.UF_IMMUTABLE)
changeReadonly(self.view.file_name(), True)

def is_enabled(self):
return self.view.file_name() and len(self.view.file_name()) > 0
return not isReadonly(self.view.file_name())

def is_visible(self):
return self.is_enabled()


class SetWritableCommand(sublime_plugin.TextCommand):
def run(self, edit):
changeReadonly(self.view.file_name(), False)

def is_enabled(self):
return isReadonly(self.view.file_name())

def is_visible(self):
return self.is_enabled()
Binary file removed toggle_readonly.pyc
Binary file not shown.

0 comments on commit 39465e8

Please sign in to comment.