]> git.rmz.io Git - dotfiles.git/blobdiff - weechat/python/autosort.py
nvim: add FPP copyright snippet
[dotfiles.git] / weechat / python / autosort.py
index 46a840c34a4d0573a5c007c05da8323728b3c629..4312cda767c919e66bf66506d85f9caff95e2f09 100644 (file)
 
 #
 # Changelog:
+# 3.9:
+#   * Remove `buffers.pl` from recommended settings.
+# 3,8:
+#   * Fix relative sorting on script name in default rules.
+#   * Document a useful property of stable sort algorithms.
+# 3.7:
+#   * Make default rules work with bitlbee, matrix and slack.
+# 3.6:
+#   * Add more documentation on provided info hooks.
+# 3.5:
+#   * Add ${info:autosort_escape,...} to escape arguments for other info hooks.
 # 3.4:
 #   * Fix rate-limit of sorting to prevent high CPU load and lock-ups.
 #   * Fix bug in parsing empty arguments for info hooks.
@@ -76,7 +87,7 @@ import weechat
 
 SCRIPT_NAME     = 'autosort'
 SCRIPT_AUTHOR   = 'Maarten de Vries <maarten@de-vri.es>'
-SCRIPT_VERSION  = '3.4'
+SCRIPT_VERSION  = '3.9'
 SCRIPT_LICENSE  = 'GPL3'
 SCRIPT_DESC     = 'Flexible automatic (or manual) buffer sorting based on eval expressions.'
 
@@ -169,22 +180,21 @@ class Config:
 
        default_rules = json.dumps([
                '${core_first}',
-               '${irc_last}',
-               '${buffer.plugin.name}',
+               '${info:autosort_order,${info:autosort_escape,${script_or_plugin}},core,*,irc,bitlbee,matrix,slack}',
+               '${script_or_plugin}',
                '${irc_raw_first}',
-               '${if:${plugin}==irc?${server}}',
-               '${if:${plugin}==irc?${info:autosort_order,${type},server,*,channel,private}}',
-               '${if:${plugin}==irc?${hashless_name}}',
+               '${server}',
+               '${info:autosort_order,${type},server,*,channel,private}',
+               '${hashless_name}',
                '${buffer.full_name}',
        ])
 
        default_helpers = json.dumps({
-               'core_first':     '${if:${buffer.full_name}!=core.weechat}',
-               'irc_first':      '${if:${buffer.plugin.name}!=irc}',
-               'irc_last':       '${if:${buffer.plugin.name}==irc}',
-               'irc_raw_first':  '${if:${buffer.full_name}!=irc.irc_raw}',
-               'irc_raw_last':   '${if:${buffer.full_name}==irc.irc_raw}',
-               'hashless_name':  '${info:autosort_replace,#,,${buffer.name}}',
+               'core_first':       '${if:${buffer.full_name}!=core.weechat}',
+               'irc_raw_first':    '${if:${buffer.full_name}!=irc.irc_raw}',
+               'irc_raw_last':     '${if:${buffer.full_name}==irc.irc_raw}',
+               'hashless_name':    '${info:autosort_replace,#,,${info:autosort_escape,${buffer.name}}}',
+               'script_or_plugin': '${if:${script_name}?${script_name}:${plugin}}',
        })
 
        default_signal_delay = 5
@@ -746,6 +756,17 @@ def parse_args(args, max = None):
                if args is None: break
        return result, args
 
+def on_info_escape(pointer, name, arguments):
+       result = ''
+       for c in arguments:
+               if c == '\\':
+                       result += '\\\\'
+               elif c == ',':
+                       result += '\\,'
+               else:
+                       result +=c
+       return result
+
 def on_info_replace(pointer, name, arguments):
        arguments, rest = parse_args(arguments, 3)
        if rest or len(arguments) < 3:
@@ -910,12 +931,45 @@ Rename a helper variable.
 Swap the expressions of two helper variables in the list.
 
 
+{*white}# Info hooks{reset}
+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 {cyan}${{info:some_hook,arguments}}{reset}.
+
+Commas and backslashes in arguments to autosort info hooks (except for {cyan}${{info:autosort_escape}}{reset}) must be escaped with a backslash.
+
+{*white}${{info:{brown}autosort_replace{white},{cyan}pattern{white},{cyan}replacement{white},{cyan}source{white}}}{reset}
+Replace all occurrences of {cyan}pattern{reset} with {cyan}replacement{reset} in the string {cyan}source{reset}.
+Can be used to ignore certain strings when sorting by replacing them with an empty string.
+
+For example: {cyan}${{info:autosort_replace,cat,dog,the dog is meowing}}{reset} expands to "the cat is meowing".
+
+{*white}${{info:{brown}autosort_order{white},{cyan}value{white},{cyan}option0{white},{cyan}option1{white},{cyan}option2{white},{cyan}...{white}}}
+Generate a zero-padded number that corresponds to the index of {cyan}value{reset} in the list of options.
+If one of the options is the special value {brown}*{reset}, 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: {cyan}${{info:autosort_order,${{server}},freenode,oftc,efnet}}{reset} will sort freenode before oftc, followed by efnet and then any remaining servers.
+Alternatively, {cyan}${{info:autosort_order,${{server}},freenode,oftc,*,efnet}}{reset} will sort any unlisted servers after freenode and oftc, but before efnet.
+
+{*white}${{info:{brown}autosort_escape{white},{cyan}text{white}}}{reset}
+Escape commas and backslashes in {cyan}text{reset} 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 {cyan}${{info:autosort_replace}}{reset} like so:
+{cyan}${{info:autosort_replace,##,#,${{info:autosort_escape,${{buffer.name}}}}}}{reset}.
+
+
 {*white}# Description
 Autosort is a weechat script to automatically 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.
+
 {*white}# Sort rules{reset}
 Autosort evaluates a list of eval expressions (see {*default}/help eval{reset}) and sorts the
 buffers based on evaluated result. Earlier rules will be considered first. Only
@@ -934,17 +988,6 @@ readable. They can be used in the main sort rules as variables. For example,
 a helper variable named `{cyan}foo{reset}` can be accessed in a main rule with the
 string `{cyan}${{foo}}{reset}`.
 
-{*white}# Replacing substrings{reset}
-There is no default method for replacing text inside eval expressions. However,
-autosort adds a `replace` info hook that can be used inside eval expressions:
-  {cyan}${{info:autosort_replace,from,to,text}}{reset}
-
-For example, to strip all hashes from a buffer name, you could write:
-  {cyan}${{info:autosort_replace,#,,${{buffer.name}}}}{reset}
-
-You can escape commas and backslashes inside the arguments by prefixing them with
-a backslash.
-
 {*white}# Automatic or manual sorting{reset}
 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
@@ -959,12 +1002,9 @@ If you remove all signals you can still sort your buffers manually with the
 {*white}# Recommended settings
 For the best visual effect, consider setting the following options:
   {*white}/set {cyan}irc.look.server_buffer{reset} {brown}independent{reset}
-  {*white}/set {cyan}buffers.look.indenting{reset} {brown}on{reset}
 
-The first setting allows server buffers to be sorted independently, which is
+This setting allows server buffers to be sorted independently, which is
 needed to create a hierarchical tree view of the server and channel buffers.
-The second one indents channel and private buffers in the buffer list of the
-`{*default}buffers.pl{reset}` script.
 
 If you are using the {*default}buflist{reset} plugin you can (ab)use Unicode to draw a tree
 structure with the following setting (modify to suit your need):
@@ -973,15 +1013,29 @@ structure with the following setting (modify to suit your need):
 
 command_completion = '%(plugin_autosort) %(plugin_autosort) %(plugin_autosort) %(plugin_autosort) %(plugin_autosort)'
 
-info_replace_description = 'Replace all occurences of `from` with `to` in the string `text`.'
-info_replace_arguments   = 'from,to,text'
+info_replace_description = (
+       '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. '
+       'See /help autosort for examples.'
+)
+info_replace_arguments = 'pattern,replacement,source'
 
 info_order_description = (
-       'Get a zero padded index of a value in a list of possible values.'
-       'If the value is not found, the index for `*` is returned.'
-       'If there is no `*` in the list, the highest index + 1 is returned.'
+       '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. '
+       'See /help autosort for examples.'
+)
+info_order_arguments = 'value,first,second,third,...'
+
+info_escape_description = (
+       '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.'
+       'See /help autosort for examples.'
 )
-info_order_arguments   = 'value,first,second,third,...'
+info_escape_arguments = 'text'
 
 
 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
@@ -1014,6 +1068,7 @@ if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
        weechat.hook_config('autosort.*', 'on_config_changed',  '')
        weechat.hook_completion('plugin_autosort', '', 'on_autosort_complete', '')
        weechat.hook_command('autosort', command_description.format(**colors), '', '', command_completion, 'on_autosort_command', '')
+       weechat.hook_info('autosort_escape',  info_escape_description,  info_escape_arguments,  'on_info_escape', '')
        weechat.hook_info('autosort_replace', info_replace_description, info_replace_arguments, 'on_info_replace', '')
        weechat.hook_info('autosort_order',   info_order_description,   info_order_arguments,   'on_info_order',   '')