Home

Script: unhighlight.py

Allow per-buffer specification of a regex that prevents highlights. (for WeeChat ≥ 1.0)
Author: xiagu, version 0.1.0, GPL3 — added: 2016-10-30
  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
#
# Copyright (C) 2016 Andrew Rodgers-Schatz <me@andrew.rs>
#
# 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/>.

try:
    import weechat
except Exception:
    print 'This script must be run under WeeChat.'
    print 'Get WeeChat now at: http://www.weechat.org/'
    import_ok = False

import time
import re

SCRIPT_NAME     = 'unhighlight'
SCRIPT_AUTHOR   = 'xiagu'
SCRIPT_VERSION  = '0.1.0'
SCRIPT_LICENSE  = 'GPL3'
SCRIPT_DESC     = 'Allows per-buffer specification of a regex that prevents highlights.'


def matches_unhighlight_strings(msg, regex):
    return weechat.string_has_highlight_regex(msg, regex)


def unhighlight_cb(data, modifier, modifier_data, message):
    """Check if the line matches the unhighlight regular expression, and if it does, clear the message and reprint it with the no_highlight tag added."""

    # Buffer names can have ; in them, but not plugins or tags (I HOPE),
    # so just split(';') would have weird edgecases with channels that have a ;
    # in the name.
    m = re.match("^(?P<plugin>\S+);(?P<full_name>\S+);(?P<tags>\S*)$", modifier_data)

    tags = m.group('tags')
    if 'no_highlight' in tags or 'notify_none' in tags:
        return message

    plugin = m.group('plugin')
    full_name = m.group('full_name')
    buffer = weechat.buffer_search(plugin, full_name)

    unhighlight_regex = weechat.buffer_get_string(buffer, 'localvar_unhighlight_regex')
    if not matches_unhighlight_strings(message, unhighlight_regex):
        return message

    # inspired by https://weechat.org/scripts/source/mass_hl_blocker.pl.html/
    # this is terrible and gross but afaik there is no way to change the
    # highlight message once it's set and no way to interact with a message's
    # tags before highlights are checked.
    weechat.prnt_date_tags(buffer, 0, "%s,no_highlight" % tags, message)
    return ''


def command_cb(data, buffer, args):
    args = args.strip().lower().split(' ')

    if args[0] == 'list':
        weechat.command('', '/set *.localvar_set_unhighlight_regex')
    else:
        weechat.command('', '/help %s' % SCRIPT_NAME)

    return weechat.WEECHAT_RC_OK


def main():
    hook = weechat.hook_modifier('weechat_print', 'unhighlight_cb', '')

    description = """
{script_name} lets you set up a regex for things to never highlight.

To use this, set the localvar 'unhighlight_regex' on a buffer. Lines in
that buffer which match will never be highlighted, even if they have
your nick or match highlight_words or highlight_regex.

You will need the script 'buffer_autoset.py' installed to make local
variables persistent; see the examples below.

Examples:
 Temporarily block highlights in the current buffer for lines matching 'banana':
   /buffer set localvar_set_unhighlight_regex banana
 Unhighlight SASL authentication messages for double logins:
   /buffer weechat
   /buffer set localvar_set_unhighlight_regex SaslServ
   /autosetbuffer add core.weechat localvar_set_unhighlight_regex SaslServ
 List buffers with autoset unhighlights:
   /{script_name} list
 Show this help:
   /{script_name}
 Display local variables for current buffer:
   /buffer localvar
""".format(script_name = SCRIPT_NAME)

    weechat.hook_command(SCRIPT_NAME, SCRIPT_DESC, 'list', description, 'list %-', 'command_cb', '')


if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
    main()