Git Product home page Git Product logo

weechat-autosort's Introduction

Autosort

Introduction

Autosort is a weechat script to automatically or manually keep your buffers sorted. The sort order can be customized by defining your own sort rules, but the default should be sane enough for most people. It can also group IRC channel/private buffers under their server buffer if you like.

Autosort uses a stable sorting algorithm, meaning that you can manually move buffers to change their relative order, if they sort equal with your rule set.

Sort rules

Autosort evaluates a list of eval expressions (see /help eval) and sorts the buffers based on evaluated result. Earlier rules will be considered first. Only if earlier rules produced identical results is the result of the next rule considered for sorting purposes.

You can debug your sort rules with the /autosort debug command, which will print the evaluation results of each rule for each buffer.

NOTE: The sort rules for version 3 are not compatible with version 2 or vice versa. You will have to manually port your old rules to version 3 if you have any.

Helper variables

You may define helper variables for the main sort rules to keep your rules readable. They can be used in the main sort rules as variables. For example, a helper variable named foo can be accessed in a main rule with the string ${foo}.

Automatic or manual sorting

By default, autosort will automatically sort your buffer list whenever a buffer is opened, merged, unmerged or renamed. This should keep your buffers sorted in almost all situations. However, you may wish to change the list of signals that cause your buffer list to be sorted. Simply edit the autosort.sorting.signals option to add or remove any signal you like. If you remove all signals you can still sort your buffers manually with the /autosort sort command. To prevent all automatic sorting, autosort.sorting.sort_on_config_change should also be set to off.

Recommended settings

For the best visual effect, consider setting the following options:

/set irc.look.server_buffer independent

This setting allows server buffers to be sorted independently, which is needed to create a hierarchical tree view of the server and channel buffers.

If you are using the buflist plugin you can (ab)use Unicode to draw a tree structure with the following setting (modify to suit your need):

/set buflist.format.indent "${color:237}${if:${buffer.next_buffer.local_variables.type}=~^(channel|private)$?├─:└─}"

Commands

Miscellaneous

/autosort sort

Manually trigger the buffer sorting.

/autosort debug

Show the evaluation results of the sort rules for each buffer.

Sorting rules

/autosort rules list

Print the list of sort rules.

/autosort rules add <expression>

Add a new rule at the end of the list.

/autosort rules insert <index> <expression>

Insert a new rule at the given index in the list.

/autosort rules update <index> <expression>

Update a rule in the list with a new expression.

/autosort rules delete <index>

Delete a rule from the list.

/autosort rules move <index_from> <index_to>

Move a rule from one position in the list to another.

/autosort rules swap <index_a> <index_b>

Swap two rules in the list

Helper variables

/autosort helpers list

Print the list of helpers variables.

/autosort helpers set <name> <expression>

Add or update a helper variable.

/autosort helpers delete <name>

Delete a helper variable.

/autosort helpers rename <old_name> <new_name>

Rename a helper variable.

/autosort helpers swap <name_a> <name_b>

Swap the expressions of two helper variables in the list.

Info hooks

Autosort comes with a number of info hooks to add some extra functionality to regular weechat eval strings. Info hooks can be used in eval strings in the form of ${info:some_hook,arguments}.

Commas and backslashes in arguments to autosort info hooks (except for ${info:autosort_escape}) must be escaped with a backslash.

${info:autosort_replace,pattern,replacement,source}

Replace all occurrences of pattern with replacement in the string source. Can be used to ignore certain strings when sorting by replacing them with an empty string.

For example: ${info:autosort_replace,cat,dog,the dog is meowing} expands to "the cat is meowing".

${info:autosort_order,value,option0,option1,option2,...}

Generate a zero-padded number that corresponds to the index of value in the list of options. If one of the options is the special value *, then any value not explicitly mentioned will be sorted at that position. Otherwise, any value that does not match an option is assigned the highest number available. Can be used to easily sort buffers based on a manual sequence.

For example: ${info:autosort_order,${server},freenode,oftc,efnet} will sort freenode before oftc, followed by efnet and then any remaining servers. Alternatively, ${info:autosort_order,${server},freenode,oftc,*,efnet} will sort any unlisted servers after freenode and oftc, but before efnet.

${info:autosort_escape,text}

Escape commas and backslashes in text by prepending them with a backslash. This is mainly useful to pass arbitrary eval strings as arguments to other autosort info hooks. Otherwise, an eval string that expands to something with a comma would be interpreted as multiple arguments.

For example, it can be used to safely pass buffer names to ${info:autosort_replace} like so: ${info:autosort_replace,##,#,${info:autosort_escape,${buffer.name}}}.

weechat-autosort's People

Contributors

alem0lars avatar de-vri-es avatar nicexe avatar ryzokuken avatar zofrex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

weechat-autosort's Issues

Buffers with spaces in the name

Currently, autosort uses /buffer swap <name> <target number> for applying a sort order. This breaks when <name> contains spaces.

It would be better to use /buffer swap <number> <number> to avoid the issue.

Unicode characters in buffer names

11:36 autosort: Individual evaluation results:
11:36 autosort: core.weechat: ['0', '0', '', '1', '', '1', 'weechat', 'core.weechat']
11:36 autosort: python.capitaines: ['1', '0', 'python', '1', '', '0', 'capitaines', 'python.capitaines']
11:36 autosort: python.capitainetrain: ['1', '0', 'python', '1', '', '0', 'capitainetrain', 'python.capitainetrain']
11:36 autosort: python.trainline: ['1', '0', 'python', '1', '', '0', 'trainline', 'python.trainline']
11:36 python: stdout/stderr: Traceback (most recent call last):
11:36 python: stdout/stderr:   File "/home/name/.weechat/python/autosort.py", line 688, in on_autosort_command
11:36 python: stdout/stderr:     'swap':   command_helper_swap,
11:36 python: stdout/stderr:   File "/home/name/.weechat/python/autosort.py", line 572, in call_command
11:36 python: stdout/stderr:     return child(buffer, command, tail)
11:36 python: stdout/stderr:   File "/home/name/.weechat/python/autosort.py", line 414, in command_debug
11:36 python: stdout/stderr:     log('{0}: {1}'.format(fullname, result))
11:36 python: stdout/stderr: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 21: ordinal not in range(128)
11:36 =!= python: error in function "on_autosort_command"

on_info_replace getting called with not enough arguments

I updated to WeeChat 2.4 [compiled on Feb 17 2019 08:49:01] from the weechat debian repo, and started getting a lot of the following message in the weechat buffer:

autosort: usage: ${info:autosort_replace,old,new,text}

on_info_replace is getting called with one argument, #.
I only have one other plugin, wee-slack.

So does anyone else see this?
If not, I'll try purging my settings and reinstalling.
If it's a general problem I can try to help debug.

'/help autosort' missing explanation of newer '${info:}' options

It appears that at least two of the newer ${info:} helper variables are not documented/explained in the output of /help autosort, leaving newer users to discover their existence from the embedded changelog in the script, and to divine the correct usage of these variables on their own.

3.5:
* Add ${info:autosort_escape,...} to escape arguments for other info hooks.
...
3.0:
...
* Add ${info:autosort_order,value,first,second,third} to ease writing sort rules.

Sorting an IRC channel after all other buffers results in odd behaviour.

The IRC buffer got sorted alphabetically with the IRC server buffers instead of appearing at the very end.

I could not reproduce it yet. The problem has appeared with irc.freenode.#i3, so I should test with that too.

Configuration where this appeared (with group_irc on):

autosort: Sorting rules:
   0: core = 0
   1: irc = 2
   2: *highmon* = 99
   3: *i3* = 99
   4: * = 1
   5: irc.server = 1
   6: irc.server.*.#* = 0
   7: irc.irc_raw = 0
   8: irc.server.*.#* = 0
autosort: Replacement patterns:
   0: perl.highmon -> highmon
   1: irc.freenode.#i3 -> i3

Renaming instead to Zi3 did sort it last, but that should not be required.

sorting by type - is it possible at all?

Hi!

I'm struggling with sorting rules configuration and was wondering if anyone can confirm if this is possible to setup with this plugin at all.

I'm trying to achieve following buffers order:

  1. All private chats/queries first
  2. Next, all channels starting with # (regardless of the network, or ordered by network - don't care)
  3. Next, all channels starting with &
  4. Next, all irc.server buffers
  5. Lastly the core window

My setup is as follows:

  1. Three IRC networks/servers, one of them is bitlbee
  2. Following rules configured:
0: irc.*.#* = 3
1: irc.*.&* = 4
2: irc.*.* = 2
3: irc.server = 15
4: core = 9999999999

group_irc is set to off.

Currently, above rules result in following order: [server buffers, 1st network, 2nd network, core window] which is not what I want.

Guessing from the source code, this does not work because python stops comparison on the first or second component, regardless how the last component is scored. For example, the sort() method receives following data:

12:25 autosort: Unsorted:
[[(9999999999, u'core'), (999999999, u'weechat')],
[(999999999, u'irc'), (15, u'server'), (999999999, u'bitlbee')],
[(999999999, u'irc'), (15, u'server'), (999999999, u'net1')],
 [(999999999, u'irc'), (15, u'server'), (999999999, u'znc-freenode')],
 [(999999999, u'irc'), (999999999, u'bitlbee'), (3, u'#twitter_abc')],
 [(999999999, u'irc'), (999999999, u'bitlbee'), (3, u'&bitlbee')],
 [(999999999, u'irc'), (999999999, u'bitlbee'), (3, u'&jabber')],
 [(999999999, u'irc'), (999999999, u'net1'), (3, u'#acsas')],
 [(999999999, u'irc'), (999999999, u'net1'), (3, u'#netdevops')],
 [(999999999, u'irc'), (999999999, u'net1'), (999999999, u'priv1')],
 [(999999999, u'irc'), (999999999, u'znc-freenode'), (3, u'#linux')],
 [(999999999, u'irc'), (999999999, u'znc-freenode'), (3, u'#salt')],
 [(999999999, u'irc'), (999999999, u'znc-freenode'), (3, u'#vim')],
 [(999999999, u'irc'), (999999999, u'znc-freenode'), (999999999, u'^priv3^')],
 [(999999999, u'irc'), (999999999, u'znc-freenode'), (999999999, u'someoneelse')]]

For reference, this was produced by adding following debug code:

def on_buffers_changed(*args, **kwargs):
    ''' Called whenever the buffer list changes. '''
    buffers = get_buffers()
    skey = buffer_sort_key(config.rules)
    unsorted = [skey(buf) for buf in buffers]
    log("Unsorted: {0}".format(unsorted))
    buffers.sort(key=buffer_sort_key(config.rules))
    for buf in buffers:
        log("result for %s: %s" % (buf, skey(buf)))
    apply_buffer_order(buffers)
    return weechat.WEECHAT_RC_OK

Is there any way to avoid it?

scripts buffer can't be sorted

I'd like to sort the "scripts" buffer to the top, right after core and fset buffer.
But for some reason it doesn't work. My ruleset is

0: core = 0
1: fset = 1
2: scripts = 2
3: irc.server = 3
4: irc = 4
5: * = 5

However, the scripts buffer is always sorted at the end of the list. I tried changing
that to

2: *scripts* = 2

but to no avail. What am I doing wrong?

Thanks,
Corinna

Getting lots of these in my logs after recent host upgrade

autosort: usage: ${info:autosort_replace,old,new,text}
autosort: usage: ${info:autosort_replace,old,new,text}
autosort: usage: ${info:autosort_replace,old,new,text}
autosort: usage: ${info:autosort_replace,old,new,text}
autosort: usage: ${info:autosort_replace,old,new,text}

Please advise!

Issues with custom sort when Slack channels are involved

autosort.py defaults works fine with Slack teams and channels.

But if I wish to sort my IRC servers like..
/autosort rules update 4 ${if:${plugin}==irc?${info:autosort_order,${server},bitlbee,lair,freenode,oftc,efnet,*}}
..all the Slack channels show up under the last Slack team.

See https://i.imgur.com/NwBc3Nc.png

I can fix this with another rule like:
/autosort rules update 5 ${script_or_plugin}==slack?${info:autosort_order,${server},googlecloud-community.slack.com,openobservatory.slack.com}

Perhaps I'm misreading the changelog or is this a bug?

3.7:
 * Make default rules work with bitlbee, matrix and slack.

Exception when running `/autosort helpers swap`

Running /autosort helpers swap causes a python exception:

2023-09-25 10:25 ▬!▬ python: error in function "on_autosort_command"
2023-09-25 10:26 autosort: Helper variables:
2023-09-25 10:26           core_first: ${info:autosort_replace,#,,${info:autosort_escape,${buffer.name}}}
2023-09-25 10:26        hashless_name: ${if:${buffer.full_name}!=core.weechat}
2023-09-25 10:26        irc_raw_first: ${if:${buffer.full_name}!=irc.irc_raw}
2023-09-25 10:26         irc_raw_last: ${if:${buffer.full_name}==irc.irc_raw}
2023-09-25 10:26     script_or_plugin: ${if:${script_name}?${script_name}:${plugin}}
2023-09-25 10:26 python: stdout/stderr (autosort): Traceback (most recent call last):
2023-09-25 10:26 python: stdout/stderr (autosort):   File "/home/maarten/.weechat/python/autoload/autosort.py", line 802, in on_autosort_command
2023-09-25 10:26 python: stdout/stderr (autosort):     return call_command(buffer, ['/autosort'], args, {
2023-09-25 10:26 python: stdout/stderr (autosort):            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-09-25 10:26 python: stdout/stderr (autosort):   File "/home/maarten/.weechat/python/autoload/autosort.py", line 635, in call_command
2023-09-25 10:26 python: stdout/stderr (autosort):     return call_command(buffer, command, tail, child)
2023-09-25 10:26 python: stdout/stderr (autosort):            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-09-25 10:26 python: stdout/stderr (autosort):   File "/home/maarten/.weechat/python/autoload/autosort.py", line 637, in call_command
2023-09-25 10:26 python: stdout/stderr (autosort):     return child(buffer, command, tail)
2023-09-25 10:26 python: stdout/stderr (autosort):            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-09-25 10:26 python: stdout/stderr (autosort):   File "/home/maarten/.weechat/python/autoload/autosort.py", line 619, in command_helper_swap
2023-09-25 10:26 python: stdout/stderr (autosort):     config.helpers.swap(index_a, index_b)
2023-09-25 10:26 python: stdout/stderr (autosort):     ^^^^^^^^^^^^^^^^^^^
2023-09-25 10:26 python: stdout/stderr (autosort): AttributeError: 'dict' object has no attribute 'swap'

Trying to sort my bitlbee stuff after everything else

Helpers:

16:15:25            weechat    | autosort: Helper variables:
16:15:25            weechat    |      bitlbee_last: ${if:${buffer.server}!=bitlbee}
16:15:25            weechat    |        core_first: ${if:${buffer.full_name}!=core.weechat}
16:15:25            weechat    |     hashless_name: ${info:autosort_replace,#,,${buffer.name}}
16:15:25            weechat    |         irc_first: ${if:${buffer.plugin.name}!=irc}
16:15:25            weechat    |          irc_last: ${if:${buffer.plugin.name}==irc}
16:15:25            weechat    |     irc_raw_first: ${if:${buffer.full_name}!=irc.irc_raw}
16:15:25            weechat    |      irc_raw_last: ${if:${buffer.full_name}==irc.irc_raw}

Rules:

16:15:36            weechat    | autosort: Sorting rules:
16:15:36            weechat    |     0: ${core_first}
16:15:36            weechat    |     1: ${bitlbee_last}
16:15:36            weechat    |     2: ${irc_first}
16:15:36            weechat    |     3: ${buffer.plugin.name}
16:15:36            weechat    |     4: ${irc_raw_first}
16:15:36            weechat    |     5: ${if:${plugin}==irc?${server}}
16:15:36            weechat    |     6: ${if:${plugin}==irc?${info:autosort_order,${type},server,*,channel,private}}
16:15:36            weechat    |     7: ${if:${plugin}==irc?${hashless_name}}
16:15:36            weechat    |     8: ${buffer.full_name}

However I still get bitlbee stuff first :(

Any help appreciated!

Default sort config does not support buffers with commas in their name

The default sort config contains ${info:autosort_replace,#,,${buffer.name}} in the hashless_name helper.
If a buffer contains a comma in its name, then info:autosort_replace is given more than three parameter and an error is displayed: autosort: usage: ${info:autosort_replace,old,new,text}
I could not find any reliable way to escape the comma.

Sorting rules don't eval properly

Hello,

I can't make autosort output something useful on debug, and it won't sort.

The install:

  • weechat version 1.6
  • autosort version 3.3

Here is an output of the rules (default I believe):

    0: ${core_first}
    1: ${irc_last}
    2: ${buffer.plugin.name}
    3: ${irc_raw_first}
    4: ${if:${plugin}==irc?${server}}
    5: ${if:${plugin}==irc?${info:autosort_order,${type},server,*,channel,private}}
    6: ${if:${plugin}==irc?${hashless_name}}
    7: ${buffer.full_name}

And here is a debug:

autosort: core.weechat: ['', '', '', '', '', '', '', 'core.weechat']
autosort: irc.s1.#c1: ['', '', 'irc', '', '', '', '', 'irc.s1.#c1']
autosort: irc.s1.q1: ['', '', 'irc', '', '', '', '', 'irc.s1.q1']
autosort: irc.s2.#c2: ['', '', 'irc', '', '', '', '', 'irc.s2.#c2']
autosort: irc.server.s1: ['', '', 'irc', '', '', '', '', 'irc.server.s1']
autosort: irc.server.s2: ['', '', 'irc', '', '', '', '', 'irc.server.s2']
autosort: irc.server.s3: ['', '', 'irc', '', '', '', '', 'irc.server.s3']
autosort: irc.s3.q2: ['', '', 'irc', '', '', '', '', 'irc.s3.q2']

The strings s1, s2, s3 are three servers weechat is connected to. The strings #c1 and #c2 are two canals weechat has joined. The stings q1 and q2 are two private chat with users (/query). They are anonymized but the output stays coherent except for alphabetical order.

Seems to me, the ${if:} expression isn't doing what it's supposed to do as the only two rules that output something are those who doesn't use the expression.

The other possibility being this is the normal behavior and then it's me who doesn't understand how to make the plugin work.

Thank you for you time.

Edit : I've played a bit with the rules. And I can confirm I can't make the if expression work. Doesn't output anything.

Plugin fails to load: AttributeError: 'str' object has no attribute 'decode'

When I try to load the plugin (using python3) I get the following error:

weechat     | python: loading script "/home/alem0lars/.weechat/python/autoload/autosort.py"
weechat     | python: registered script "autosort", version 2.7 (Automatically or manually keep your buffers sorted and grouped by server.)
weechat     | python: stdout/stderr: Traceback (most recent call last):
weechat     | python: stdout/stderr:   File "/home/alem0lars/.weechat/python/autoload/autosort.py", line 877, in <module>
weechat     | python: stdout/stderr:     on_config_changed()
weechat     | python: stdout/stderr:   File "/home/alem0lars/.weechat/python/autoload/autosort.py", line 691, in on_config_changed
weechat     | python: stdout/stderr:     on_buffers_changed()
weechat     | python: stdout/stderr:   File "/home/alem0lars/.weechat/python/autoload/autosort.py", line 675, in on_buffers_changed
weechat     | python: stdout/stderr:     buffers.sort(key=buffer_sort_key(config.rules))
weechat     | python: stdout/stderr:   File "/home/alem0lars/.weechat/python/autoload/autosort.py", line 450, in key
weechat     | python: stdout/stderr:     for word in preprocess(buffer[0].decode('utf-8'), config):
weechat     | python: stdout/stderr: AttributeError: 'str' object has no attribute 'decode'
weechat =!= | python: unable to parse file "/home/alem0lars/.weechat/python/autoload/autosort.py"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.