Home Download Buy Blog Forum Support

Sort Open Files

Re: Sort Open Files

Postby agibsonsw on Mon Mar 12, 2012 10:15 pm

skube wrote:How does one use this? Do you copy and save to a file call OrderedFiles.py? Does it have to go in any particular folder in the packages directory?


The file doesn't have to be named 'OrderedFiles.py', although it seems sensible to do so.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby skube on Tue Mar 13, 2012 2:55 pm

Great thanks. But how do I actually sort? I've tried adding the key bindings:
Code: Select all
{ "keys": ["ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 }  },
{ "keys": ["ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 }  }

But nothing happens.
skube
 
Posts: 88
Joined: Tue Feb 28, 2012 8:42 pm

Re: Sort Open Files

Postby agibsonsw on Tue Mar 13, 2012 3:04 pm

@skube. Every key-binding must be separated by a comma, except the last one.

Code: Select all
{ "keys": ["ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 }  },
{ "keys": ["ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 }  }    << bet you need to add a comma!


Otherwise, press the key-combination and then Ctrl-apostrophe to open the Console and see if there is an error message.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby desperado70 on Thu Aug 30, 2012 12:14 pm

I managed to get this running. However, this opens a new list/dopdown with all the open files. Would it be possible to use this to re-order the "open files" list on the left?
desperado70
 
Posts: 5
Joined: Thu Aug 30, 2012 11:39 am

Re: Sort Open Files

Postby agibsonsw on Thu Aug 30, 2012 1:11 pm

desperado70 wrote:I managed to get this running. However, this opens a new list/dopdown with all the open files. Would it be possible to use this to re-order the "open files" list on the left?


That is beyond the scope of my little utility. I think there may be a package/plug-in that does what you are after: check here. Maybe SidebarEnhancements(?) but I don't really know.

I suppose you could use my SortTabs.py:

Code: Select all
import sublime_plugin
from os import path
from operator import itemgetter

# A simple command to sort current tabs alphabetically (returning focus to the
# original tab).
# Does not work with different groups or windows. Not catered for unsaved views
# (although it seems to work okay if there are any). It could be modified to
# work in these circumstances.
#   { "keys": ["ctrl+alt+b"], "command": "sort_tabs" },
class SortTabsCommand(sublime_plugin.WindowCommand):
   def run(self):
      file_views = []
      win = self.window
      curr_view = win.active_view()
      for vw in win.views():
         _, tail = path.split(vw.file_name() or path.sep)
         group, _ = win.get_view_index(vw)
         file_views.append((tail.lower(), vw, group))
      file_views.sort(key = itemgetter(2, 0))
      moving_index = 0
      for index, (_, vw, group) in enumerate(file_views):
         if index == 0 or group > prev_group:
            moving_index = 0
            prev_group = group
         else:
            moving_index += 1
         win.set_view_index(vw, group, moving_index)
      win.focus_view(curr_view)

Sorting the tabs will also sort the list in the sidebar. It works within the current group - not across all groups.

Code: Select all
# suggested key binding
{ "keys": ["ctrl+alt+b"], "command": "sort_tabs" }
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby desperado70 on Fri Aug 31, 2012 12:47 pm

thanks. that works as expected. Didn't notice that it would also sort the list in the sidebar before.

Could this somehow be linked to the opening of a file, so that this plugin is automatically run when a file is opened or saved?
desperado70
 
Posts: 5
Joined: Thu Aug 30, 2012 11:39 am

Re: Sort Open Files

Postby bizoo on Fri Aug 31, 2012 2:12 pm

https://github.com/bizoo/SortTabs
However automatic sorting is not bullet proof. There are some case when it's not triggered (drag'n'drop of files for example).
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Sort Open Files

Postby pez on Tue Oct 09, 2012 5:30 am

agibsonsw: For some reason using your script I get a non-C collation order (case sensitive sort)

This happens even if I set the collation explicity to C with locale.setLocale("LC_COLLATE", "C") and use locale.strcoll as a sorting comparator. :(

Image
pez
 
Posts: 1
Joined: Tue Oct 09, 2012 5:25 am

Re: Sort Open Files

Postby agibsonsw on Tue Oct 09, 2012 11:33 am

@pez This is not something I know much about I'm afraid, although I'm guessing you need to check your character encoding as well(?). Hopefully someone else here may have experience on this topic. Here's my original code:

Code: Select all
import sublime_plugin
from os import path
from operator import itemgetter
from datetime import datetime

# Lists open files in a quick panel for jumping to,
# ordered alphabetically or by modified date: index, 0, for alphabetical.
#    { "keys": ["ctrl+alt+x"], "command": "ordered_files", "args": { "index": 0 }  },
#    { "keys": ["ctrl+alt+c"], "command": "ordered_files", "args": { "index": 2 }  },
# Does not work with different groups, windows, or unsaved views (although it could
# be modified so that it does).

class OrderedFilesCommand(sublime_plugin.WindowCommand):
   def run(self, index):
      OF = OrderedFilesCommand
      OF.file_views = []
      win = self.window
      for vw in win.views():
         if vw.file_name() is not None:
            _, tail = path.split(vw.file_name())
            modified = path.getmtime(vw.file_name())
            OF.file_views.append((tail, vw, modified))
         else:
            pass      # leave new/untitled files (for the moment)
      if index == 0:      # sort by file name (case-insensitive)
         OF.file_views.sort(key = lambda (tail, _, Doh): tail.lower())
         win.show_quick_panel([x for (x, y, z) in OF.file_views], self.on_chosen)
      else:            # sort by modified date (index == 2)
         OF.file_views.sort(key = itemgetter(2))
         win.show_quick_panel([
            (datetime.fromtimestamp(z)).strftime("%d-%m-%y %H:%M ") + x \
            for (x, y, z) in OF.file_views], self.on_chosen)
   def on_chosen(self, index):
      if index != -1:
         self.window.focus_view(OrderedFilesCommand.file_views[index][1])

My original code, though, includes tail.lower() within the sort-key, so this would need to be just tail.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sort Open Files

Postby moose on Fri Feb 14, 2014 7:06 am

How can I make use of these Python scripts? Where (and with which name) should I store them for ST2 on a Linux machine?
moose
 
Posts: 5
Joined: Sun Feb 09, 2014 4:03 pm

PreviousNext

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 9 guests