Script: grep_filter.py

Filter buffers automatically while searching them.
Author: Simmo Saan — Version: 0.11 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.5.
Tags: filter, py2, py3
Added: 2016-04-09 — Updated: 2021-11-06

Download GitHub Repository

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 by Simmo Saan <simmo.saan@gmail.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-11-06, Sébastien Helleu <flashcode@flashtux.org>
#   version 0.11: make script compatible with WeeChat >= 3.4
#                 (new parameters in function hdata_search)
# 2019-06-07, Trygve Aaberge <trygveaa@gmail.com>
#   version 0.10: remove newlines from command completion
# 2015-10-04, Simmo Saan <simmo.saan@gmail.com>
#   version 0.9: fix text search imitation in filter
# 2015-08-27, Simmo Saan <simmo.saan@gmail.com>
#   version 0.8: add documentation
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.7: mute filter add/del
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.6: imitate search settings in filter
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.5: option for bar item text
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.4: option for default state
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.3: allow toggling during search
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.2: add bar item for indication
# 2015-08-25, Simmo Saan <simmo.saan@gmail.com>
#   version 0.1: initial script
#

"""
Filter buffers automatically while searching them
"""

from __future__ import print_function

SCRIPT_NAME = "grep_filter"
SCRIPT_AUTHOR = "Simmo Saan <simmo.saan@gmail.com>"
SCRIPT_VERSION = "0.11"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Filter buffers automatically while searching them"

SCRIPT_REPO = "https://github.com/sim642/grep_filter"

SCRIPT_COMMAND = SCRIPT_NAME
SCRIPT_BAR_ITEM = SCRIPT_NAME
SCRIPT_LOCALVAR = SCRIPT_NAME

IMPORT_OK = True

try:
    import weechat
except ImportError:
    print("This script must be run under WeeChat.")
    print("Get WeeChat now at: https://weechat.org/")
    IMPORT_OK = False

import re  # re.escape

SETTINGS = {
    "enable": ("off", "enable automatically start filtering when searching"),
    "bar_item": ("grep", "text to show in bar item when filtering"),
}

KEYS = {"ctrl-G": "/%s toggle" % SCRIPT_COMMAND}


def get_merged_buffers(ptr):
    """
    Get a list of buffers which are merged with "ptr".
    """

    weechat_version = int(weechat.info_get("version_number", "") or 0)

    hdata = weechat.hdata_get("buffer")
    buffers = weechat.hdata_get_list(hdata, "gui_buffers")
    if weechat_version >= 0x03040000:
        buffer = weechat.hdata_search(
            hdata,
            buffers,
            "${buffer.number} == ${value}",
            {},
            {"value": str(weechat.hdata_integer(hdata, ptr, "number"))},
            {},
            1,
        )
    else:
        buffer = weechat.hdata_search(
            hdata,
            buffers,
            "${buffer.number} == %i"
            % weechat.hdata_integer(hdata, ptr, "number"),
            1,
        )
    nbuffer = weechat.hdata_move(hdata, buffer, 1)

    ret = []
    while buffer:
        ret.append(weechat.hdata_string(hdata, buffer, "full_name"))

        if weechat.hdata_integer(
            hdata, buffer, "number"
        ) == weechat.hdata_integer(hdata, nbuffer, "number"):
            buffer = nbuffer
            nbuffer = weechat.hdata_move(hdata, nbuffer, 1)
        else:
            buffer = None

    return ret


def filter_exists(name):
    """
    Check whether a filter named "name" exists.
    """

    weechat_version = int(weechat.info_get("version_number", "") or 0)

    hdata = weechat.hdata_get("filter")
    filters = weechat.hdata_get_list(hdata, "gui_filters")
    if weechat_version >= 0x03040000:
        filter = weechat.hdata_search(
            hdata,
            filters,
            "${filter.name} == ${name}",
            {},
            {"name": name},
            {},
            1,
        )
    else:
        filter = weechat.hdata_search(
            hdata,
            filters,
            "${filter.name} == %s" % name,
            1,
        )

    return bool(filter)


def filter_del(name):
    """
    Delete a filter named "name".
    """

    weechat.command(
        weechat.buffer_search_main(), "/mute filter del %s" % name
    )


def filter_addreplace(name, buffers, tags, regex):
    """
    Add (or replace if already exists) a filter named "name" with specified argumets.
    """

    if filter_exists(name):
        filter_del(name)

    weechat.command(
        weechat.buffer_search_main(),
        "/mute filter add %s %s %s %s" % (name, buffers, tags, regex),
    )


def buffer_searching(buffer):
    """
    Check whether "buffer" is in search mode.
    """

    hdata = weechat.hdata_get("buffer")

    return bool(weechat.hdata_integer(hdata, buffer, "text_search"))


def buffer_filtering(buffer):
    """
    Check whether "buffer" should be filtered.
    """

    local = weechat.buffer_get_string(buffer, "localvar_%s" % SCRIPT_LOCALVAR)
    return {"": None, "0": False, "1": True}[local]


def buffer_build_regex(buffer):
    """
    Build a regex according to "buffer"'s search settings.
    """

    hdata = weechat.hdata_get("buffer")
    input = weechat.hdata_string(hdata, buffer, "input_buffer")
    exact = weechat.hdata_integer(hdata, buffer, "text_search_exact")
    where = weechat.hdata_integer(hdata, buffer, "text_search_where")
    regex = weechat.hdata_integer(hdata, buffer, "text_search_regex")

    if not regex:
        input = re.escape(input)

    if exact:
        input = "(?-i)%s" % input

    filter_regex = None
    if where == 1:  # message
        filter_regex = input
    elif where == 2:  # prefix
        filter_regex = "%s\\t" % input
    else:  # prefix | message
        filter_regex = input  # TODO: impossible with current filter regex

    return "!%s" % filter_regex


def buffer_update(buffer):
    """
    Refresh filtering in "buffer" by updating (or removing) the filter and update the bar item.
    """

    hdata = weechat.hdata_get("buffer")

    buffers = ",".join(get_merged_buffers(buffer))
    name = "%s_%s" % (SCRIPT_NAME, buffers)

    if buffer_searching(buffer):
        if buffer_filtering(buffer):
            filter_addreplace(name, buffers, "*", buffer_build_regex(buffer))
        elif not buffer_filtering(buffer) and filter_exists(name):
            filter_del(name)
    elif filter_exists(name):
        filter_del(name)

    where = weechat.hdata_integer(hdata, buffer, "text_search_where")
    weechat.buffer_set(
        buffer,
        "localvar_set_%s_warn" % SCRIPT_LOCALVAR,
        "1" if where == 3 else "0",
    )  # warn about incorrect filter

    weechat.bar_item_update(SCRIPT_BAR_ITEM)


def input_search_cb(data, signal, buffer):
    """
    Handle "input_search" signal.
    """

    if buffer_searching(buffer) and buffer_filtering(buffer) is None:
        enable = weechat.config_string_to_boolean(
            weechat.config_get_plugin("enable")
        )
        weechat.buffer_set(
            buffer,
            "localvar_set_%s" % SCRIPT_LOCALVAR,
            "1" if enable else "0",
        )
        weechat.buffer_set(
            buffer, "localvar_set_%s_warn" % SCRIPT_LOCALVAR, "0"
        )
    elif not buffer_searching(buffer):
        weechat.buffer_set(buffer, "localvar_del_%s" % SCRIPT_LOCALVAR, "")
        weechat.buffer_set(
            buffer, "localvar_del_%s_warn" % SCRIPT_LOCALVAR, ""
        )

    buffer_update(buffer)

    return weechat.WEECHAT_RC_OK


def input_text_changed_cb(data, signal, buffer):
    """
    Handle "input_text_changed" signal.
    """

    if buffer_searching(buffer) and buffer_filtering(buffer):
        buffers = ",".join(get_merged_buffers(buffer))
        name = "%s_%s" % (SCRIPT_NAME, buffers)

        filter_addreplace(name, buffers, "*", buffer_build_regex(buffer))

    return weechat.WEECHAT_RC_OK


def command_cb(data, buffer, args):
    """
    Handle command.
    """

    if args == "enable":
        weechat.buffer_set(buffer, "localvar_set_%s" % SCRIPT_LOCALVAR, "1")
    elif args == "disable":
        weechat.buffer_set(buffer, "localvar_set_%s" % SCRIPT_LOCALVAR, "0")
    elif args == "toggle":
        weechat.buffer_set(
            buffer,
            "localvar_set_%s" % SCRIPT_LOCALVAR,
            "0" if buffer_filtering(buffer) else "1",
        )
    else:
        pass

    buffer_update(buffer)

    return weechat.WEECHAT_RC_OK


def bar_item_cb(data, item, window, buffer, extra_info):
    """
    Build the bar item's content for "buffer".
    """

    buffers = ",".join(get_merged_buffers(buffer))
    name = "%s_%s" % (SCRIPT_NAME, buffers)

    if filter_exists(name):
        warn = int(
            weechat.buffer_get_string(
                buffer, "localvar_%s_warn" % SCRIPT_LOCALVAR
            )
        )

        return "%s%s%s" % (
            weechat.color("input_text_not_found" if warn else "bar_fg"),
            weechat.config_get_plugin("bar_item"),
            weechat.color("reset"),
        )
    else:
        return ""


if __name__ == "__main__" and IMPORT_OK:
    if weechat.register(
        SCRIPT_NAME,
        SCRIPT_AUTHOR,
        SCRIPT_VERSION,
        SCRIPT_LICENSE,
        SCRIPT_DESC,
        "",
        "",
    ):
        weechat.hook_signal("input_search", "input_search_cb", "")
        weechat.hook_signal("input_text_changed", "input_text_changed_cb", "")

        weechat.hook_command(
            SCRIPT_COMMAND,
            SCRIPT_DESC,
            """enable || disable || toggle""",
            """ enable: enable {0} in current buffer
disable: disable {0} in current buffer
 toggle: toggle {0} in current buffer

By default a bind in "search" context is added to toggle with "ctrl-G".
To see {0} status during search, add "{1}" item to some bar. On default configuration you can do it with:
    /set weechat.bar.input.items "[input_prompt]+(away),[{1}],[input_search],[input_paste],input_text"

Due to technical reasons with /filter it is not possible to exactly {0} in "pre|msg" search mode, thus the bar item is shown in warning color.""".format(
                SCRIPT_NAME, SCRIPT_BAR_ITEM
            ),
            """enable || disable || toggle""",
            "command_cb",
            "",
        )

        weechat.bar_item_new("(extra)%s" % SCRIPT_BAR_ITEM, "bar_item_cb", "")

        for option, value in SETTINGS.items():
            if not weechat.config_is_set_plugin(option):
                weechat.config_set_plugin(option, value[0])

            weechat.config_set_desc_plugin(
                option, '%s (default: "%s")' % (value[1], value[0])
            )

        weechat.key_bind("search", KEYS)