From a9a8ba18260383725ff30c1e4e9e8c5f8591e5ad Mon Sep 17 00:00:00 2001
From: A Farzat <a@farzat.xyz>
Date: Sun, 20 Oct 2024 19:54:46 +0900
Subject: Add weechat plugins

---
 .local/share/weechat/.gitignore                    |   2 +
 .local/share/weechat/python/autoload/bufsize.py    |   1 +
 .local/share/weechat/python/autoload/edit.py       |   1 +
 .local/share/weechat/python/autoload/go.py         |   1 +
 .../share/weechat/python/autoload/notify_send.py   |   1 +
 .local/share/weechat/python/bufsize.py             | 247 +++++++++
 .local/share/weechat/python/go.py                  | 572 +++++++++++++++++++++
 7 files changed, 825 insertions(+)
 create mode 100644 .local/share/weechat/.gitignore
 create mode 120000 .local/share/weechat/python/autoload/bufsize.py
 create mode 120000 .local/share/weechat/python/autoload/edit.py
 create mode 120000 .local/share/weechat/python/autoload/go.py
 create mode 120000 .local/share/weechat/python/autoload/notify_send.py
 create mode 100644 .local/share/weechat/python/bufsize.py
 create mode 100644 .local/share/weechat/python/go.py

(limited to '.local/share')

diff --git a/.local/share/weechat/.gitignore b/.local/share/weechat/.gitignore
new file mode 100644
index 0000000..0308619
--- /dev/null
+++ b/.local/share/weechat/.gitignore
@@ -0,0 +1,2 @@
+matrix/
+matrix-rust/
diff --git a/.local/share/weechat/python/autoload/bufsize.py b/.local/share/weechat/python/autoload/bufsize.py
new file mode 120000
index 0000000..ff72a18
--- /dev/null
+++ b/.local/share/weechat/python/autoload/bufsize.py
@@ -0,0 +1 @@
+../bufsize.py
\ No newline at end of file
diff --git a/.local/share/weechat/python/autoload/edit.py b/.local/share/weechat/python/autoload/edit.py
new file mode 120000
index 0000000..fcdd325
--- /dev/null
+++ b/.local/share/weechat/python/autoload/edit.py
@@ -0,0 +1 @@
+/usr/share/weechat/python/edit.py
\ No newline at end of file
diff --git a/.local/share/weechat/python/autoload/go.py b/.local/share/weechat/python/autoload/go.py
new file mode 120000
index 0000000..bdfb7dd
--- /dev/null
+++ b/.local/share/weechat/python/autoload/go.py
@@ -0,0 +1 @@
+../go.py
\ No newline at end of file
diff --git a/.local/share/weechat/python/autoload/notify_send.py b/.local/share/weechat/python/autoload/notify_send.py
new file mode 120000
index 0000000..0d7dd7b
--- /dev/null
+++ b/.local/share/weechat/python/autoload/notify_send.py
@@ -0,0 +1 @@
+/usr/share/weechat/python/notify_send.py
\ No newline at end of file
diff --git a/.local/share/weechat/python/bufsize.py b/.local/share/weechat/python/bufsize.py
new file mode 100644
index 0000000..153c9bc
--- /dev/null
+++ b/.local/share/weechat/python/bufsize.py
@@ -0,0 +1,247 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
+#                         and nesthib <nesthib@gmail.com>
+#
+# scroll indicator; displaying number of lines below last line, overall lines in buffer, number of current line and percent displayed
+#
+# 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; either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+#
+# 2017-08-17: nils_2 (freenode.#weechat)
+#        0.8: add support for buffer_filters_enabled and buffer_filters_disabled (WeeChat ≥ 2.0)
+# 2016-12-16: nils_2 (freenode.#weechat)
+#        0.7: add option show_scroll (idea by earnestly)
+# 2016-04-23: wdbw <tuturu@tutanota.com>
+#     0.6.2 : fix: type of filters_enabled
+# 2014-02-24: nesthib (freenode.#weechat)
+#     0.6.1 : fix: color tags for default format
+# 2013-11-19: nils_2 (freenode.#weechat)
+#       0.6 : fix: stdout/stderr warning
+# 2013-11-02: nils_2 (freenode.#weechat)
+#       0.5 : fix refresh on (un)zoomed buffer
+#           : add option 'count_filtered_lines' and format item "%F"
+# 2013-10-15: nils_2 (freenode.#weechat)
+#       0.4 : fix bug with root-bar
+#           : add support of eval_expression (weechat >= 0.4.2)
+# 2013-01-25: nils_2 (freenode.#weechat)
+#       0.3 : make script compatible with Python 3.x
+#           : internal changes
+# 2012-07-09: nils_2 (freenode.#weechat)
+#       0.2 : fix: display bug with more than one window
+#           : hide item when buffer empty
+# 2012-07-08: obiwahn
+#     0.1.1 : add hook for switch_buffer
+# 2012-01-11: nils_2, nesthib (freenode.#weechat)
+#       0.1 : initial release
+#
+# Development is currently hosted at
+# https://github.com/weechatter/weechat-scripts
+
+try:
+    import weechat, re
+
+except Exception:
+    print("This script must be run under WeeChat.")
+    print("Get WeeChat now at: http://www.weechat.org/")
+    quit()
+
+SCRIPT_NAME     = "bufsize"
+SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
+SCRIPT_VERSION  = "0.8"
+SCRIPT_LICENSE  = "GPL"
+SCRIPT_DESC     = "scroll indicator; displaying number of lines below last line, overall lines in buffer, number of current line and percent displayed"
+
+OPTIONS         = { 'format'            : ('${color:yellow}%P${color:default}⋅%{${color:yellow}%A${color:default}⇵${color:yellow}%C${color:default}/}${color:yellow}%L',
+                                           'format for items to display in bar, possible items: %P = percent indicator, %A = number of lines below last line, %L = lines counter, %C = current line %F = number of filtered lines (note: using WeeChat >= 0.4.2, content is evaluated, so you can use colors with format \"${color:xxx}\", see /help eval)'),
+                    'count_filtered_lines': ('on',
+                                           'filtered lines will be count in item.'),
+                    'show_scroll':('on','always show the scroll indicator number,even if its 0 (item %A), if option is off the scroll indicator will be hidden like the item "scroll"'),
+                   }
+# ================================[ weechat item ]===============================
+# regexp to match ${color} tags
+regex_color=re.compile('\$\{([^\{\}]+)\}')
+
+# regexp to match ${optional string} tags
+regex_optional_tags=re.compile('%\{[^\{\}]+\}')
+
+filter_status = 0
+
+def show_item (data, item, window):
+    # check for root input bar!
+    if not window:
+       window = weechat.current_window()
+
+    ptr_buffer = weechat.window_get_pointer(window,'buffer')
+    if ptr_buffer == '':
+        return ''
+
+    if weechat.buffer_get_string(ptr_buffer,'name') != 'weechat':                         # not weechat core buffer
+        if weechat.buffer_get_string(ptr_buffer,'localvar_type') == '':                   # buffer with free content?
+          return ''
+
+    lines_after, lines_count, percent, current_line, filtered, filtered_before, filtered_after = count_lines(window,ptr_buffer)
+    lines_after_bak = lines_after
+
+    if lines_count == 0:                                                                  # buffer empty?
+        return ''
+
+    if filtered == 0:
+        filtered = ''
+
+    if lines_after == 0 and (OPTIONS['show_scroll'].lower() == 'off'):
+        lines_after = ''
+
+    tags = {'%C': str(current_line),
+            '%A': str(lines_after),
+            '%F': str(filtered),
+            '%L': str(lines_count),
+            '%P': str(percent)+'%'}
+
+    bufsize_item = substitute_colors(OPTIONS['format'])
+
+    # replace mandatory tags
+    for tag in list(tags.keys()):
+#    for tag in tags.keys():
+        bufsize_item = bufsize_item.replace(tag, tags[tag])
+
+    # replace optional tags
+    # %{…} only if lines after (e.g. %A > 0)
+    if lines_after_bak > 0:
+        for regex_tag in regex_optional_tags.findall(bufsize_item):
+            bufsize_item = bufsize_item.replace(regex_tag, regex_tag.lstrip('%{').rstrip('}'))
+    else:
+        bufsize_item = regex_optional_tags.sub('', bufsize_item)
+
+    return bufsize_item
+
+def substitute_colors(text):
+    if int(version) >= 0x00040200:
+        return weechat.string_eval_expression(text,{},{},{})
+    # substitute colors in output
+    return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text)
+
+def count_lines(ptr_window,ptr_buffer):
+    global filter_status
+
+    hdata_buf = weechat.hdata_get('buffer')
+    hdata_lines = weechat.hdata_get('lines')
+    lines = weechat.hdata_pointer(hdata_buf, ptr_buffer, 'lines') # own_lines, mixed_lines
+    lines_count = weechat.hdata_integer(hdata_lines, lines, 'lines_count')
+
+    hdata_window = weechat.hdata_get('window')
+    hdata_winscroll = weechat.hdata_get('window_scroll')
+    window_scroll = weechat.hdata_pointer(hdata_window, ptr_window, 'scroll')
+    lines_after = weechat.hdata_integer(hdata_winscroll, window_scroll, 'lines_after')
+    window_height = weechat.window_get_integer(weechat.current_window(), 'win_chat_height')
+
+    filtered = 0
+    filtered_before = 0
+    filtered_after = 0
+    # if filter is disabled, don't count.
+    if (OPTIONS['count_filtered_lines'].lower() == 'off') and filter_status == 1:
+        filtered, filtered_before,filtered_after = count_filtered_lines(ptr_buffer,lines_count,lines_after)
+        lines_count = lines_count - filtered
+#        lines_after = lines_after - filtered_after
+
+    if lines_count > window_height:
+        differential = lines_count - window_height
+        percent = max(int(round(100. * (differential - lines_after) / differential)), 0)
+    else:
+        percent = 100
+
+    # get current position
+    current_line = lines_count - lines_after
+
+    return lines_after,lines_count,percent,current_line, filtered, filtered_before, filtered_after
+
+def count_filtered_lines(ptr_buffer,lines_count,lines_after):
+    filtered_before = 0
+    filtered_after = 0
+    filtered = 0
+
+    lines = weechat.hdata_pointer(weechat.hdata_get('buffer'), ptr_buffer, 'own_lines')
+    counter = 0
+    current_position = lines_count - lines_after
+
+    if lines:
+        line = weechat.hdata_pointer(weechat.hdata_get('lines'), lines, 'first_line')
+        hdata_line = weechat.hdata_get('line')
+        hdata_line_data = weechat.hdata_get('line_data')
+
+        while line:
+            data = weechat.hdata_pointer(hdata_line, line, 'data')
+            if data:
+#                message = weechat.hdata_string(hdata_line_data, data, 'message')
+                displayed = weechat.hdata_char(hdata_line_data, data, 'displayed')
+                if displayed == 0:
+#                    weechat.prnt('','%d - %s - %s' % (counter, displayed, message))
+                    if counter < current_position:
+                        filtered_before += 1
+                    else:
+                        filtered_after += 1
+            counter += 1
+            line = weechat.hdata_move(hdata_line, line, 1)
+
+    filtered = filtered_before + filtered_after
+    return filtered,filtered_before,filtered_after
+
+def update_cb(data, signal, signal_data):
+    weechat.bar_item_update(SCRIPT_NAME)
+    return weechat.WEECHAT_RC_OK
+
+def filtered_update_cb(data, signal, signal_data):
+    global filter_status
+    if signal == 'filters_disabled':
+        filter_status = 0
+    if signal == 'filters_enabled':
+        filter_status = 1
+    weechat.bar_item_update(SCRIPT_NAME)
+    return weechat.WEECHAT_RC_OK
+# ================================[ weechat options and description ]===============================
+def init_options():
+    for option,value in OPTIONS.items():
+        if not weechat.config_is_set_plugin(option):
+            weechat.config_set_plugin(option, value[0])
+            OPTIONS[option] = value[0]
+        else:
+            OPTIONS[option] = weechat.config_get_plugin(option)
+        weechat.config_set_desc_plugin(option, "%s (default: '%s')" % (value[1], value[0]))
+
+def toggle_refresh(pointer, name, value):
+    global OPTIONS
+    option = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]        # get optionname
+    OPTIONS[option] = value                                               # save new value
+    weechat.bar_item_update(SCRIPT_NAME)
+    return weechat.WEECHAT_RC_OK
+# ================================[ main ]===============================
+if __name__ == "__main__":
+#    global filter_status
+    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
+        version = weechat.info_get("version_number", "") or 0
+
+        if int(version) >= 0x00030600:
+            filter_status = int(weechat.info_get('filters_enabled',''))
+            bar_item = weechat.bar_item_new(SCRIPT_NAME, 'show_item','')
+            weechat.bar_item_update(SCRIPT_NAME)
+            weechat.hook_signal('buffer_line_added','update_cb','')
+            weechat.hook_signal('window_scrolled','update_cb','')
+            weechat.hook_signal('buffer_switch','update_cb','')
+            weechat.hook_signal('*filters*','filtered_update_cb','')
+            weechat.hook_command_run('/buffer clear*','update_cb','')
+            weechat.hook_command_run('/window page*','update_cb','')
+            weechat.hook_command_run('/input zoom_merged_buffer','update_cb','')
+            weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )
+            init_options()
+        else:
+            weechat.prnt('','%s%s %s' % (weechat.prefix('error'),SCRIPT_NAME,': needs version 0.3.6 or higher'))
diff --git a/.local/share/weechat/python/go.py b/.local/share/weechat/python/go.py
new file mode 100644
index 0000000..77c5774
--- /dev/null
+++ b/.local/share/weechat/python/go.py
@@ -0,0 +1,572 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2009-2014 Sébastien Helleu <flashcode@flashtux.org>
+# Copyright (C) 2010 m4v <lambdae2@gmail.com>
+# Copyright (C) 2011 stfn <stfnmd@googlemail.com>
+#
+# 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; either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+#
+
+#
+# History:
+#
+# 2021-05-25, Tomáš Janoušek <tomi@nomi.cz>:
+#     version 2.7: add new option to prefix short names with server names
+# 2019-07-11, Simmo Saan <simmo.saan@gmail.com>
+#     version 2.6: fix detection of "/input search_text_here"
+# 2017-04-01, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 2.5: add option "buffer_number"
+# 2017-03-02, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 2.4: fix syntax and indentation error
+# 2017-02-25, Simmo Saan <simmo.saan@gmail.com>
+#     version 2.3: fix fuzzy search breaking buffer number search display
+# 2016-01-28, ylambda <ylambda@koalabeast.com>
+#     version 2.2: add option "fuzzy_search"
+# 2015-11-12, nils_2 <weechatter@arcor.de>
+#     version 2.1: fix problem with buffer short_name "weechat", using option
+#                  "use_core_instead_weechat", see:
+#                  https://github.com/weechat/weechat/issues/574
+# 2014-05-12, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 2.0: add help on options, replace option "sort_by_activity" by
+#                  "sort" (add sort by name and first match at beginning of
+#                  name and by number), PEP8 compliance
+# 2012-11-26, Nei <anti.teamidiot.de>
+#     version 1.9: add auto_jump option to automatically go to buffer when it
+#                  is uniquely selected
+# 2012-09-17, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 1.8: fix jump to non-active merged buffers (jump with buffer name
+#                  instead of number)
+# 2012-01-03 nils_2 <weechatter@arcor.de>
+#     version 1.7: add option "use_core_instead_weechat"
+# 2012-01-03, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 1.6: make script compatible with Python 3.x
+# 2011-08-24, stfn <stfnmd@googlemail.com>:
+#     version 1.5: /go with name argument jumps directly to buffer
+#                  Remember cursor position in buffer input
+# 2011-05-31, Elián Hanisch <lambdae2@gmail.com>:
+#     version 1.4: Sort list of buffers by activity.
+# 2011-04-25, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 1.3: add info "go_running" (used by script input_lock.rb)
+# 2010-11-01, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 1.2: use high priority for hooks to prevent conflict with other
+#                  plugins/scripts (WeeChat >= 0.3.4 only)
+# 2010-03-25, Elián Hanisch <lambdae2@gmail.com>:
+#     version 1.1: use a space to match the end of a string
+# 2009-11-16, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 1.0: add new option to display short names
+# 2009-06-15, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.9: fix typo in /help go with command /key
+# 2009-05-16, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.8: search buffer by number, fix bug when window is split
+# 2009-05-03, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.7: eat tab key (do not complete input, just move buffer
+#                  pointer)
+# 2009-05-02, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.6: sync with last API changes
+# 2009-03-22, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.5: update modifier signal name for input text display,
+#                  fix arguments for function string_remove_color
+# 2009-02-18, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.4: do not hook command and init options if register failed
+# 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.3: case insensitive search for buffers names
+# 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.2: add help about Tab key
+# 2009-02-08, Sébastien Helleu <flashcode@flashtux.org>:
+#     version 0.1: initial release
+#
+
+"""
+Quick jump to buffers.
+(this script requires WeeChat 0.3.0 or newer)
+"""
+
+from __future__ import print_function
+
+SCRIPT_NAME = 'go'
+SCRIPT_AUTHOR = 'Sébastien Helleu <flashcode@flashtux.org>'
+SCRIPT_VERSION = '2.7'
+SCRIPT_LICENSE = 'GPL3'
+SCRIPT_DESC = 'Quick jump to buffers'
+
+SCRIPT_COMMAND = 'go'
+
+IMPORT_OK = True
+
+try:
+    import weechat
+except ImportError:
+    print('This script must be run under WeeChat.')
+    print('Get WeeChat now at: http://www.weechat.org/')
+    IMPORT_OK = False
+
+import re
+
+# script options
+SETTINGS = {
+    'color_number': (
+        'yellow,magenta',
+        'color for buffer number (not selected)'),
+    'color_number_selected': (
+        'yellow,red',
+        'color for selected buffer number'),
+    'color_name': (
+        'black,cyan',
+        'color for buffer name (not selected)'),
+    'color_name_selected': (
+        'black,brown',
+        'color for a selected buffer name'),
+    'color_name_highlight': (
+        'red,cyan',
+        'color for highlight in buffer name (not selected)'),
+    'color_name_highlight_selected': (
+        'red,brown',
+        'color for highlight in a selected buffer name'),
+    'message': (
+        'Go to: ',
+        'message to display before list of buffers'),
+    'short_name': (
+        'off',
+        'display and search in short names instead of buffer name'),
+    'short_name_server': (
+        'off',
+        'prefix short names with server names for search and display'),
+    'sort': (
+        'number,beginning',
+        'comma-separated list of keys to sort buffers '
+        '(the order is important, sorts are performed in the given order): '
+        'name = sort by name (or short name), ',
+        'hotlist = sort by hotlist order, '
+        'number = first match a buffer number before digits in name, '
+        'beginning = first match at beginning of names (or short names); '
+        'the default sort of buffers is by numbers'),
+    'use_core_instead_weechat': (
+        'off',
+        'use name "core" instead of "weechat" for core buffer'),
+    'auto_jump': (
+        'off',
+        'automatically jump to buffer when it is uniquely selected'),
+    'fuzzy_search': (
+        'off',
+        'search buffer matches using approximation'),
+    'buffer_number': (
+        'on',
+        'display buffer number'),
+}
+
+# hooks management
+HOOK_COMMAND_RUN = {
+    'input': ('/input *', 'go_command_run_input'),
+    'buffer': ('/buffer *', 'go_command_run_buffer'),
+    'window': ('/window *', 'go_command_run_window'),
+}
+hooks = {}
+
+# input before command /go (we'll restore it later)
+saved_input = ''
+saved_input_pos = 0
+
+# last user input (if changed, we'll update list of matching buffers)
+old_input = None
+
+# matching buffers
+buffers = []
+buffers_pos = 0
+
+
+def go_option_enabled(option):
+    """Checks if a boolean script option is enabled or not."""
+    return weechat.config_string_to_boolean(weechat.config_get_plugin(option))
+
+
+def go_info_running(data, info_name, arguments):
+    """Returns "1" if go is running, otherwise "0"."""
+    return '1' if 'modifier' in hooks else '0'
+
+
+def go_unhook_one(hook):
+    """Unhook something hooked by this script."""
+    global hooks
+    if hook in hooks:
+        weechat.unhook(hooks[hook])
+        del hooks[hook]
+
+
+def go_unhook_all():
+    """Unhook all."""
+    go_unhook_one('modifier')
+    for hook in HOOK_COMMAND_RUN:
+        go_unhook_one(hook)
+
+
+def go_hook_all():
+    """Hook command_run and modifier."""
+    global hooks
+    priority = ''
+    version = weechat.info_get('version_number', '') or 0
+    # use high priority for hook to prevent conflict with other plugins/scripts
+    # (WeeChat >= 0.3.4 only)
+    if int(version) >= 0x00030400:
+        priority = '2000|'
+    for hook, value in HOOK_COMMAND_RUN.items():
+        if hook not in hooks:
+            hooks[hook] = weechat.hook_command_run(
+                '%s%s' % (priority, value[0]),
+                value[1], '')
+    if 'modifier' not in hooks:
+        hooks['modifier'] = weechat.hook_modifier(
+            'input_text_display_with_cursor', 'go_input_modifier', '')
+
+
+def go_start(buf):
+    """Start go on buffer."""
+    global saved_input, saved_input_pos, old_input, buffers_pos
+    go_hook_all()
+    saved_input = weechat.buffer_get_string(buf, 'input')
+    saved_input_pos = weechat.buffer_get_integer(buf, 'input_pos')
+    weechat.buffer_set(buf, 'input', '')
+    old_input = None
+    buffers_pos = 0
+
+
+def go_end(buf):
+    """End go on buffer."""
+    global saved_input, saved_input_pos, old_input
+    go_unhook_all()
+    weechat.buffer_set(buf, 'input', saved_input)
+    weechat.buffer_set(buf, 'input_pos', str(saved_input_pos))
+    old_input = None
+
+
+def go_match_beginning(buf, string):
+    """Check if a string matches the beginning of buffer name/short name."""
+    if not string:
+        return False
+    esc_str = re.escape(string)
+    if re.search(r'^#?' + esc_str, buf['name']) \
+            or re.search(r'^#?' + esc_str, buf['short_name']):
+        return True
+    return False
+
+
+def go_match_fuzzy(name, string):
+    """Check if string matches name using approximation."""
+    if not string:
+        return False
+
+    name_len = len(name)
+    string_len = len(string)
+
+    if string_len > name_len:
+        return False
+    if name_len == string_len:
+        return name == string
+
+    # Attempt to match all chars somewhere in name
+    prev_index = -1
+    for i, char in enumerate(string):
+        index = name.find(char, prev_index+1)
+        if index == -1:
+            return False
+        prev_index = index
+    return True
+
+
+def go_now(buf, args):
+    """Go to buffer specified by args."""
+    listbuf = go_matching_buffers(args)
+    if not listbuf:
+        return
+
+    # prefer buffer that matches at beginning (if option is enabled)
+    if 'beginning' in weechat.config_get_plugin('sort').split(','):
+        for index in range(len(listbuf)):
+            if go_match_beginning(listbuf[index], args):
+                weechat.command(buf,
+                                '/buffer ' + str(listbuf[index]['full_name']))
+                return
+
+    # jump to first buffer in matching buffers by default
+    weechat.command(buf, '/buffer ' + str(listbuf[0]['full_name']))
+
+
+def go_cmd(data, buf, args):
+    """Command "/go": just hook what we need."""
+    global hooks
+    if args:
+        go_now(buf, args)
+    elif 'modifier' in hooks:
+        go_end(buf)
+    else:
+        go_start(buf)
+    return weechat.WEECHAT_RC_OK
+
+
+def go_matching_buffers(strinput):
+    """Return a list with buffers matching user input."""
+    global buffers_pos
+    listbuf = []
+    if len(strinput) == 0:
+        buffers_pos = 0
+    strinput = strinput.lower()
+    infolist = weechat.infolist_get('buffer', '', '')
+    while weechat.infolist_next(infolist):
+        pointer = weechat.infolist_pointer(infolist, 'pointer')
+        short_name = weechat.infolist_string(infolist, 'short_name')
+        server = weechat.buffer_get_string(pointer, 'localvar_server')
+        if go_option_enabled('short_name'):
+            if go_option_enabled('short_name_server') and server:
+                name = server + '.' + short_name
+            else:
+                name = short_name
+        else:
+            name = weechat.infolist_string(infolist, 'name')
+        if name == 'weechat' \
+                and go_option_enabled('use_core_instead_weechat') \
+                and weechat.infolist_string(infolist, 'plugin_name') == 'core':
+            name = 'core'
+        number = weechat.infolist_integer(infolist, 'number')
+        full_name = weechat.infolist_string(infolist, 'full_name')
+        if not full_name:
+            full_name = '%s.%s' % (
+                weechat.infolist_string(infolist, 'plugin_name'),
+                weechat.infolist_string(infolist, 'name'))
+        matching = name.lower().find(strinput) >= 0
+        if not matching and strinput[-1] == ' ':
+            matching = name.lower().endswith(strinput.strip())
+        if not matching and go_option_enabled('fuzzy_search'):
+            matching = go_match_fuzzy(name.lower(), strinput)
+        if not matching and strinput.isdigit():
+            matching = str(number).startswith(strinput)
+        if len(strinput) == 0 or matching:
+            listbuf.append({
+                'number': number,
+                'short_name': short_name,
+                'name': name,
+                'full_name': full_name,
+                'pointer': pointer,
+            })
+    weechat.infolist_free(infolist)
+
+    # sort buffers
+    hotlist = []
+    infolist = weechat.infolist_get('hotlist', '', '')
+    while weechat.infolist_next(infolist):
+        hotlist.append(
+            weechat.infolist_pointer(infolist, 'buffer_pointer'))
+    weechat.infolist_free(infolist)
+    last_index_hotlist = len(hotlist)
+
+    def _sort_name(buf):
+        """Sort buffers by name (or short name)."""
+        return buf['name']
+
+    def _sort_hotlist(buf):
+        """Sort buffers by hotlist order."""
+        try:
+            return hotlist.index(buf['pointer'])
+        except ValueError:
+            # not in hotlist, always last.
+            return last_index_hotlist
+
+    def _sort_match_number(buf):
+        """Sort buffers by match on number."""
+        return 0 if str(buf['number']) == strinput else 1
+
+    def _sort_match_beginning(buf):
+        """Sort buffers by match at beginning."""
+        return 0 if go_match_beginning(buf, strinput) else 1
+
+    funcs = {
+        'name': _sort_name,
+        'hotlist': _sort_hotlist,
+        'number': _sort_match_number,
+        'beginning': _sort_match_beginning,
+    }
+
+    for key in weechat.config_get_plugin('sort').split(','):
+        if key in funcs:
+            listbuf = sorted(listbuf, key=funcs[key])
+
+    if not strinput:
+        index = [i for i, buf in enumerate(listbuf)
+                 if buf['pointer'] == weechat.current_buffer()]
+        if index:
+            buffers_pos = index[0]
+
+    return listbuf
+
+
+def go_buffers_to_string(listbuf, pos, strinput):
+    """Return string built with list of buffers found (matching user input)."""
+    string = ''
+    strinput = strinput.lower()
+    for i in range(len(listbuf)):
+        selected = '_selected' if i == pos else ''
+        buffer_name = listbuf[i]['name']
+        index = buffer_name.lower().find(strinput)
+        if index >= 0:
+            index2 = index + len(strinput)
+            name = '%s%s%s%s%s' % (
+                buffer_name[:index],
+                weechat.color(weechat.config_get_plugin(
+                    'color_name_highlight' + selected)),
+                buffer_name[index:index2],
+                weechat.color(weechat.config_get_plugin(
+                    'color_name' + selected)),
+                buffer_name[index2:])
+        elif go_option_enabled("fuzzy_search") and \
+                go_match_fuzzy(buffer_name.lower(), strinput):
+            name = ""
+            prev_index = -1
+            for char in strinput.lower():
+                index = buffer_name.lower().find(char, prev_index+1)
+                if prev_index < 0:
+                    name += buffer_name[:index]
+                    name += weechat.color(weechat.config_get_plugin(
+                        'color_name_highlight' + selected))
+                if prev_index >= 0 and index > prev_index+1:
+                    name += weechat.color(weechat.config_get_plugin(
+                        'color_name' + selected))
+                    name += buffer_name[prev_index+1:index]
+                    name += weechat.color(weechat.config_get_plugin(
+                        'color_name_highlight' + selected))
+                name += buffer_name[index]
+                prev_index = index
+
+            name += weechat.color(weechat.config_get_plugin(
+                'color_name' + selected))
+            name += buffer_name[prev_index+1:]
+        else:
+            name = buffer_name
+        string += ' '
+        if go_option_enabled('buffer_number'):
+            string += '%s%s' % (
+                weechat.color(weechat.config_get_plugin(
+                    'color_number' + selected)),
+                str(listbuf[i]['number']))
+        string += '%s%s%s' % (
+            weechat.color(weechat.config_get_plugin(
+                'color_name' + selected)),
+            name,
+            weechat.color('reset'))
+    return '  ' + string if string else ''
+
+
+def go_input_modifier(data, modifier, modifier_data, string):
+    """This modifier is called when input text item is built by WeeChat.
+
+    This is commonly called after changes in input or cursor move: it builds
+    a new input with prefix ("Go to:"), and suffix (list of buffers found).
+    """
+    global old_input, buffers, buffers_pos
+    if modifier_data != weechat.current_buffer():
+        return ''
+    names = ''
+    new_input = weechat.string_remove_color(string, '')
+    new_input = new_input.lstrip()
+    if old_input is None or new_input != old_input:
+        old_buffers = buffers
+        buffers = go_matching_buffers(new_input)
+        if buffers != old_buffers and len(new_input) > 0:
+            if len(buffers) == 1 and go_option_enabled('auto_jump'):
+                weechat.command(modifier_data, '/wait 1ms /input return')
+            buffers_pos = 0
+        old_input = new_input
+    names = go_buffers_to_string(buffers, buffers_pos, new_input.strip())
+    return weechat.config_get_plugin('message') + string + names
+
+
+def go_command_run_input(data, buf, command):
+    """Function called when a command "/input xxx" is run."""
+    global buffers, buffers_pos
+    if command.startswith('/input search_text') or command.startswith('/input jump'):
+        # search text or jump to another buffer is forbidden now
+        return weechat.WEECHAT_RC_OK_EAT
+    elif command == '/input complete_next':
+        # choose next buffer in list
+        buffers_pos += 1
+        if buffers_pos >= len(buffers):
+            buffers_pos = 0
+        weechat.hook_signal_send('input_text_changed',
+                                 weechat.WEECHAT_HOOK_SIGNAL_STRING, '')
+        return weechat.WEECHAT_RC_OK_EAT
+    elif command == '/input complete_previous':
+        # choose previous buffer in list
+        buffers_pos -= 1
+        if buffers_pos < 0:
+            buffers_pos = len(buffers) - 1
+        weechat.hook_signal_send('input_text_changed',
+                                 weechat.WEECHAT_HOOK_SIGNAL_STRING, '')
+        return weechat.WEECHAT_RC_OK_EAT
+    elif command == '/input return':
+        # switch to selected buffer (if any)
+        go_end(buf)
+        if len(buffers) > 0:
+            weechat.command(
+                buf, '/buffer ' + str(buffers[buffers_pos]['full_name']))
+        return weechat.WEECHAT_RC_OK_EAT
+    return weechat.WEECHAT_RC_OK
+
+
+def go_command_run_buffer(data, buf, command):
+    """Function called when a command "/buffer xxx" is run."""
+    return weechat.WEECHAT_RC_OK_EAT
+
+
+def go_command_run_window(data, buf, command):
+    """Function called when a command "/window xxx" is run."""
+    return weechat.WEECHAT_RC_OK_EAT
+
+
+def go_unload_script():
+    """Function called when script is unloaded."""
+    go_unhook_all()
+    return weechat.WEECHAT_RC_OK
+
+
+def go_main():
+    """Entry point."""
+    if not weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
+                            SCRIPT_LICENSE, SCRIPT_DESC,
+                            'go_unload_script', ''):
+        return
+    weechat.hook_command(
+        SCRIPT_COMMAND,
+        'Quick jump to buffers', '[name]',
+        'name: directly jump to buffer by name (without argument, list is '
+        'displayed)\n\n'
+        'You can bind command to a key, for example:\n'
+        '  /key bind meta-g /go\n\n'
+        'You can use completion key (commonly Tab and shift-Tab) to select '
+        'next/previous buffer in list.',
+        '%(buffers_names)',
+        'go_cmd', '')
+
+    # set default settings
+    version = weechat.info_get('version_number', '') or 0
+    for option, value in SETTINGS.items():
+        if not weechat.config_is_set_plugin(option):
+            weechat.config_set_plugin(option, value[0])
+        if int(version) >= 0x00030500:
+            weechat.config_set_desc_plugin(
+                option, '%s (default: "%s")' % (value[1], value[0]))
+    weechat.hook_info('go_running',
+                      'Return "1" if go is running, otherwise "0"',
+                      '',
+                      'go_info_running', '')
+
+
+if __name__ == "__main__" and IMPORT_OK:
+    go_main()
-- 
cgit v1.2.3-70-g09d2