From 95e479dd665615926c0618b2d44e4990830fdaa4 Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Sun, 5 May 2013 13:19:59 -0400 Subject: [PATCH 1/8] basic st3 support. Can't get jpeg dimensions to work. --- autofilename.py | 17 +++++++------- getimageinfo.py | 60 ++++++++++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/autofilename.py b/autofilename.py index 004b929..363f687 100644 --- a/autofilename.py +++ b/autofilename.py @@ -1,7 +1,7 @@ import sublime import sublime_plugin import os -from getimageinfo import getImageInfo +from .getimageinfo import getImageInfo class InsertDimensionsCommand(sublime_plugin.TextCommand): this_dir = '' @@ -39,8 +39,8 @@ def run(self, edit): if '= 10) and data[:6] in ('GIF87a', 'GIF89a'): + if (size >= 10) and data[:6] == b'GIF89a': # Check to see if content_type is correct content_type = 'image/gif' w, h = struct.unpack("= 24) and data.startswith('\211PNG\r\n\032\n') - and (data[12:16] == 'IHDR')): + elif ((size >= 24) and data[:8] == b'\211PNG\r\n\032\n' + and (data[12:16] == b'IHDR')): content_type = 'image/png' w, h = struct.unpack(">LL", data[16:24]) width = int(w) height = int(h) # Maybe this is for an older PNG version. - elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'): + elif (size >= 16) and data[:8] == b'\211PNG\r\n\032\n': + print("we have a png2") # Check to see if we have the right content type content_type = 'image/png' w, h = struct.unpack(">LL", data[8:16]) @@ -35,27 +38,28 @@ def getImageInfo(data): height = int(h) # handle JPEGs - elif (size >= 2) and data.startswith('\377\330'): - content_type = 'image/jpeg' - jpeg = StringIO.StringIO(data) - jpeg.read(2) - b = jpeg.read(1) - try: - while (b and ord(b) != 0xDA): - while (ord(b) != 0xFF): b = jpeg.read(1) - while (ord(b) == 0xFF): b = jpeg.read(1) - if (ord(b) >= 0xC0 and ord(b) <= 0xC3): - jpeg.read(3) - h, w = struct.unpack(">HH", jpeg.read(4)) - break - else: - jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) - b = jpeg.read(1) - width = int(w) - height = int(h) - except struct.error: - pass - except ValueError: - pass + # Can't get this method to work. + # elif (size >= 2) and data[:2] == b'\377\330': + # content_type = 'image/jpeg' + # jpeg = io.StringIO(olddata) + # jpeg.read(2) + # b = jpeg.read(1) + # try: + # while (b and ord(b) != 0xDA): + # while (ord(b) != 0xFF): b = jpeg.read(1) + # while (ord(b) == 0xFF): b = jpeg.read(1) + # if (ord(b) >= 0xC0 and ord(b) <= 0xC3): + # jpeg.read(3) + # h, w = struct.unpack(">HH", jpeg.read(4)) + # break + # else: + # jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) + # b = jpeg.read(1) + # width = int(w) + # height = int(h) + # except struct.error: + # pass + # except ValueError: + # pass return content_type, width, height \ No newline at end of file From c300b1f3ac3797402baf50c170854beafce92600 Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Fri, 24 May 2013 16:24:41 -0400 Subject: [PATCH 2/8] windows bug fixes; project root works as expected; optional keybinding this should fix almost all the current bugs. Still looking into overriding default autocompletion issues --- Default.sublime-commands | 12 ++++ Default.sublime-keymap | 2 +- README.md | 59 +++++++++++------- autofilename.py | 112 ++++++++++++++++++++++++++++------ autofilename.sublime-settings | 33 +++++++--- 5 files changed, 170 insertions(+), 48 deletions(-) create mode 100644 Default.sublime-commands diff --git a/Default.sublime-commands b/Default.sublime-commands new file mode 100644 index 0000000..6864255 --- /dev/null +++ b/Default.sublime-commands @@ -0,0 +1,12 @@ +[ + { + "caption": "AutoFileName: Default Settings", + "command": "open_file", + "args": {"file": "${packages}/AutoFileName/autofilename.sublime-settings"} + }, + { + "caption": "AutoFileName: Quick Settings", + "command": "afn_settings_panel" + } + +] \ No newline at end of file diff --git a/Default.sublime-keymap b/Default.sublime-keymap index 6098f74..fa5e0c7 100644 --- a/Default.sublime-keymap +++ b/Default.sublime-keymap @@ -1,5 +1,5 @@ [ - { "keys": ["tab"], "command": "insert_dimensions", + { "keys": ["tab"], "command": "insert_dimensions", "context": [ { "key": "setting.auto_complete_commit_on_tab" }, diff --git a/README.md b/README.md index 138fe10..6ab1214 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,41 @@ -AutoFileName: Autocomplete Filenames in Sublime Text -===================================================== +AutoFileName +============ +Autocomplete Filenames in Sublime Text +-------------------------------------- + Do you ever find yourself sifting through folders in the sidebar trying to remember what you named that file? Can't remember if it was a jpg or a png? Maybe you just wish you could type filenames faster. *No more.* -Whether your making a `img` tag in html, setting a background image in css, or linking a `.js` file to your html (or whatever else people use filename paths for these days...), you can now autocomplete the filename. Plus, it uses the built-in autocomplete, so no need to learn another *pesky* shortcut. +Whether you're making a `img` tag in html, setting a background image in css, or linking a `.js` file to your html (or whatever else people use filename paths for these days...), you can now autocomplete the filename. Plus, it uses the built-in autocomplete, so no need to learn another pesky shortcut. + +Features +-------- + +- Display filenames and folders +- Show dimensions next to image files +- Autoinsert dimensions in img tags (can be disabled in settings) +- Support for both '/' and '\' for all you Windows hooligans +- Usage -===== +----- +**Nothing!** + +For example: + If you are looking to autocomplete an image path in an HTML `` tag: - - -Pressing control+space, will activate AutoFileName. I list of available files where be ready to select. - -*Looking for an even more automatic and seemless completion?* Add the following to your User Settings file: - - "auto_complete_triggers": - [ - { - "characters": "<", - "selector": "text.html" - }, - { - "characters": "/", - "selector": "string.quoted.double.html,string.quoted.single.html, source.css" - } - ] - -With this, there's no need to worry about pressing control+space, autocompletion with appear upon pressing /. \ No newline at end of file + `` + +AutoFileName will display a list of files without you having to do anything. As you type, the results will narrow. The list will automatically update as you enter a new directory so you don't have to do a thing. + +Settings +-------- +There are some options now. + +Perhaps you're working on a website and all the image files are relative to the project root instead of the Computer's root directory. No worries. Just tell AutoFileName the project root. (More info in the settings file.) + +Additionally, if you hate awesomeness, you can turn off some of the automagicalness and use a boring keybinding to activate AutoFileName. + +How Can I help? +--------------- +- If you're feeling really nice, I need some help getting jpg dimensions. All the bytes and binary mumbo-jumbo makes my head spin so if you're experienced with that stuff, mind lending me a pull request? +- **Got a feature request? Something bugging you and you're about to uninstall it?** Submit a bug report with all your fears, desires, and vulgarity. I'll heartily try to fix the plugin to your specifications... well, I'll consider it. \ No newline at end of file diff --git a/autofilename.py b/autofilename.py index 363f687..598fae7 100644 --- a/autofilename.py +++ b/autofilename.py @@ -3,6 +3,48 @@ import os from .getimageinfo import getImageInfo +class AfnShowFilenames(sublime_plugin.TextCommand): + def run(self, edit): + FileNameComplete.is_active = True + self.view.run_command('auto_complete', + {'disable_auto_insert': True, + 'next_completion_if_showing': False}) + +class AfnSettingsPanel(sublime_plugin.WindowCommand): + def run(self): + use_pr = '✗ Stop using project root' if self.get_setting('afn_use_project_root') else '✓ Use Project Root' + use_dim = '✗ Disable HTML Image Dimension insertion' if self.get_setting('afn_insert_dimensions') else '✓ Auto-insert Image Dimensions in HTML' + p_root = self.get_setting('afn_proj_root') + + menu = [ + [use_pr, p_root], + [use_dim, ''] + ] + self.window.show_quick_panel(menu, self.on_done) + + def on_done(self, value): + settings = sublime.load_settings('autofilename.sublime-settings') + if value == 0: + use_pr = settings.get('afn_use_project_root') + settings.set('afn_use_project_root', not use_pr) + if value == 1: + use_dim = settings.get('afn_use_project_root') + settings.set('afn_use_project_root', not use_dim) + + def get_setting(self,string,view=None): + if view and view.settings().get(string): + return view.settings().get(string) + else: + return sublime.load_settings('autofilename.sublime-settings').get(string) + +# Used to remove the / or \ when autocompleting a Windows drive (eg. /C:/path) +class AfnDeletePrefixedSlash(sublime_plugin.TextCommand): + def run(self, edit): + sel = self.view.sel()[0].a + reg = sublime.Region(sel-4,sel-3) + self.view.erase(edit, reg) + +# inserts width and height dimensions into img tags. HTML only class InsertDimensionsCommand(sublime_plugin.TextCommand): this_dir = '' @@ -34,13 +76,13 @@ def run(self, edit): if path.startswith(("'","\"","(")): path = path[1:-1] - path = path[path.rfind('/'):] if '/' in path else '' + path = path[path.rfind(FileNameComplete.sep):] if FileNameComplete.sep in path else path full_path = self.this_dir + path if ' Date: Fri, 24 May 2013 16:27:28 -0400 Subject: [PATCH 3/8] quick readme fix --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 6ab1214..6c08d4f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ AutoFileName ============ + Autocomplete Filenames in Sublime Text -------------------------------------- - Do you ever find yourself sifting through folders in the sidebar trying to remember what you named that file? Can't remember if it was a jpg or a png? Maybe you just wish you could type filenames faster. *No more.* Whether you're making a `img` tag in html, setting a background image in css, or linking a `.js` file to your html (or whatever else people use filename paths for these days...), you can now autocomplete the filename. Plus, it uses the built-in autocomplete, so no need to learn another pesky shortcut. @@ -14,7 +14,6 @@ Features - Show dimensions next to image files - Autoinsert dimensions in img tags (can be disabled in settings) - Support for both '/' and '\' for all you Windows hooligans -- Usage ----- From b510c04b0e411bd9374338683eb0c8ca51c1c03d Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Sat, 6 Jul 2013 20:35:47 -0400 Subject: [PATCH 4/8] Blacklist scopes setting --- autofilename.py | 12 ++++++++---- autofilename.sublime-settings | 3 +++ getimageinfo.py | 8 ++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/autofilename.py b/autofilename.py index 598fae7..b49524e 100644 --- a/autofilename.py +++ b/autofilename.py @@ -81,8 +81,8 @@ def run(self, edit): if '= 24) and data[:8] == b'\211PNG\r\n\032\n' and (data[12:16] == b'IHDR')): - content_type = 'image/png' w, h = struct.unpack(">LL", data[16:24]) width = int(w) height = int(h) # Maybe this is for an older PNG version. elif (size >= 16) and data[:8] == b'\211PNG\r\n\032\n': - print("we have a png2") # Check to see if we have the right content type - content_type = 'image/png' w, h = struct.unpack(">LL", data[8:16]) width = int(w) height = int(h) # handle JPEGs - # Can't get this method to work. # elif (size >= 2) and data[:2] == b'\377\330': - # content_type = 'image/jpeg' # jpeg = io.StringIO(olddata) + # print(bytes(ord(jpeg.read(1)))) # jpeg.read(2) # b = jpeg.read(1) # try: @@ -62,4 +58,4 @@ def getImageInfo(data): # except ValueError: # pass - return content_type, width, height \ No newline at end of file + return width, height From c3c08d4fea1723f877064937b5de3f9f7c8598d7 Mon Sep 17 00:00:00 2001 From: Liam Cain Date: Sat, 20 Jul 2013 17:53:10 -0400 Subject: [PATCH 5/8] fixed some bugs; now supports ~; should no longer override default completions --- autofilename.py | 41 +++++++++++++++++++---------------- autofilename.sublime-settings | 2 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/autofilename.py b/autofilename.py index b49524e..8e284aa 100644 --- a/autofilename.py +++ b/autofilename.py @@ -143,7 +143,7 @@ def on_modified(self, view): self.showing_win_drives = False view.run_command('afn_delete_prefixed_slash') - def on_selection_modified(self,view): + def on_selection_modified_async(self,view): if not view.window(): return sel = view.sel()[0] @@ -191,32 +191,34 @@ def on_query_completions(self, view, prefix, locations): uses_keybinding = self.get_setting('afn_use_keybinding', view) sel = view.sel()[0].a + this_dir = "" completions = [] if uses_keybinding and not FileNameComplete.is_active: return - if not any(s in view.scope_name(sel) for s in valid_scopes): return - if any(s in view.scope_name(sel) for s in blacklist): - return [] - - cur_path = self.get_cur_path(view, sel) + return - if is_proj_rel and cur_path.startswith('/') or cur_path.startswith('\\'): - proot = self.get_setting('afn_proj_root', view) - if proot: - cur_path = os.path.join(proot, cur_path[1:]) - else: - for f in sublime.active_window().folders(): - if f in view.file_name(): - cur_path = f + cur_path = os.path.expanduser(self.get_cur_path(view, sel)) + + + if cur_path.startswith('/') or cur_path.startswith('\\'): + if is_proj_rel: + proot = self.get_setting('afn_proj_root', view) + if proot: + if not view.file_name() and not os.path.isabs(proot): + proot = "/" + cur_path = os.path.join(proot, cur_path[1:]) + else: + for f in sublime.active_window().folders(): + if f in view.file_name(): + cur_path = f + elif not view.file_name(): + return else: - if not view.file_name(): - return - this_dir = os.path.split(view.file_name())[0] - + this_dir = os.path.split(view.file_name())[0] this_dir = os.path.join(this_dir, cur_path) try: @@ -232,7 +234,8 @@ def on_query_completions(self, view, prefix, locations): completions.append((self.fix_dir(this_dir,d), d)) if completions: InsertDimensionsCommand.this_dir = this_dir - return completions + return completions + return except OSError: print("AutoFileName: could not find " + this_dir) return diff --git a/autofilename.sublime-settings b/autofilename.sublime-settings index 1d0af0f..1e21b13 100644 --- a/autofilename.sublime-settings +++ b/autofilename.sublime-settings @@ -8,7 +8,7 @@ // Changing this setting allows for absolute paths on a project level. // This is useful for web designers and developers who want to use the // root of their site. - "afn_use_project_root": true, + "afn_use_project_root": false, // Override the project root. Will only work // if "auto_file_name_use_project_root" is true. From 46bc97ab90c055a5ffa72dad9b20485dfd5b16b8 Mon Sep 17 00:00:00 2001 From: Brad Carson Date: Mon, 25 Nov 2013 23:45:28 -0500 Subject: [PATCH 6/8] Update JPG dimension checking for Python3. --- getimageinfo.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/getimageinfo.py b/getimageinfo.py index e1e56f8..e63afff 100644 --- a/getimageinfo.py +++ b/getimageinfo.py @@ -35,27 +35,28 @@ def getImageInfo(data): height = int(h) # handle JPEGs - # elif (size >= 2) and data[:2] == b'\377\330': - # jpeg = io.StringIO(olddata) - # print(bytes(ord(jpeg.read(1)))) - # jpeg.read(2) - # b = jpeg.read(1) - # try: - # while (b and ord(b) != 0xDA): - # while (ord(b) != 0xFF): b = jpeg.read(1) - # while (ord(b) == 0xFF): b = jpeg.read(1) - # if (ord(b) >= 0xC0 and ord(b) <= 0xC3): - # jpeg.read(3) - # h, w = struct.unpack(">HH", jpeg.read(4)) - # break - # else: - # jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) - # b = jpeg.read(1) - # width = int(w) - # height = int(h) - # except struct.error: - # pass - # except ValueError: - # pass + elif (size >= 2) and data[:2] == b'\377\330': + jpeg = io.BytesIO(data) + + jpeg.read(2) + b = jpeg.read(1) + + try: + while (b != b''): + while (b != b'\xFF'): b = jpeg.read(1) + while (b == b'\xFF'): b = jpeg.read(1) + if (b >= b'\xC0' and b <= b'\xC3'): + jpeg.read(3) + h, w = struct.unpack(">HH", jpeg.read(4)) + break + else: + jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2) + b = jpeg.read(1) + width = int(w) + height = int(h) + except struct.error: + pass + except ValueError: + pass return width, height From 7b55fa5ea33fd18942ba509e5ce04ee0ba34cb2a Mon Sep 17 00:00:00 2001 From: Brad Carson Date: Thu, 9 Jan 2014 14:02:47 -0500 Subject: [PATCH 7/8] Add image dimension support for template languages like Slim that omit brackets on img tags --- autofilename.py | 40 +++++++++++++++++++++++++++-------- autofilename.sublime-settings | 11 +++++++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/autofilename.py b/autofilename.py index 8e284aa..952687f 100644 --- a/autofilename.py +++ b/autofilename.py @@ -5,7 +5,7 @@ class AfnShowFilenames(sublime_plugin.TextCommand): def run(self, edit): - FileNameComplete.is_active = True + FileNameComplete.is_active = True self.view.run_command('auto_complete', {'disable_auto_insert': True, 'next_completion_if_showing': False}) @@ -51,6 +51,7 @@ class InsertDimensionsCommand(sublime_plugin.TextCommand): def insert_dimension(self,edit,dim,name,tag_scope): view = self.view sel = view.sel()[0].a + if name in view.substr(tag_scope): reg = view.find('(?<='+name+'\=)\s*\"\d{1,5}', tag_scope.a) view.replace(edit, reg, '"'+str(dim)) @@ -64,13 +65,37 @@ def get_setting(self,string,view=None): else: return sublime.load_settings('autofilename.sublime-settings').get(string) + + def insert_dimensions(self, edit, scope, w, h): + view = self.view + + if self.get_setting('afn_insert_width_first',view): + self.insert_dimension(edit,h,'height', scope) + self.insert_dimension(edit,w,'width', scope) + else: + self.insert_dimension(edit,w,'width', scope) + self.insert_dimension(edit,h,'height', scope) + + + # determines if there is a template tag in a given region. supports HTML and template languages. + def img_tag_in_region(self, region): + view = self.view + + # handle template languages but template languages like slim may also contain HTML so + # we do a check for that as well + return view.substr(region).strip().startswith('img') | (' Date: Thu, 9 Jan 2014 15:17:56 -0500 Subject: [PATCH 8/8] =?UTF-8?q?Remove=20jpg=20dimension=20from=20README=20?= =?UTF-8?q?-=20it=E2=80=99s=20been=20added.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 6c08d4f..9e026e4 100644 --- a/README.md +++ b/README.md @@ -36,5 +36,4 @@ Additionally, if you hate awesomeness, you can turn off some of the automagicaln How Can I help? --------------- -- If you're feeling really nice, I need some help getting jpg dimensions. All the bytes and binary mumbo-jumbo makes my head spin so if you're experienced with that stuff, mind lending me a pull request? - **Got a feature request? Something bugging you and you're about to uninstall it?** Submit a bug report with all your fears, desires, and vulgarity. I'll heartily try to fix the plugin to your specifications... well, I'll consider it. \ No newline at end of file