Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b17f9135e9
Fetching contributors…

Cannot retrieve contributors at this time

file 60 lines (53 sloc) 2.147 kb
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
# -*- coding: utf-8 -*-
#
# Kate/Pâté plugins for general use
# Copyright 2010-2012 by Alex Trubov <i.zaufi@gmail.com>
#
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
#
# Here is a short list of plugins in this file:
#
# Insert Color (Meta+Shift+C)
# open color choose dialog and insert selected color as #hex string
#

import kate

from PyQt4 import QtGui
from PyKDE4 import kdeui
from PyKDE4.ktexteditor import KTextEditor

from libkatepate import common

_LEFT_COLOR_BOUNDARY = set(' \t"\';[]{}():/\\,+=!?%^|&*~`')
_RIGHT_COLOR_BOUNDARY = set(' \t"\';[]{}():/\\,+=!?%^|&*~`#')

@kate.action('Insert Color', shortcut='Meta+Shift+C', menu='Tools')
def insertColor():
    document = kate.activeDocument()
    view = kate.activeView()

    if not view.selection():
        cursor = view.cursorPosition()
        colorRange = common.getBoundTextRangeSL(_LEFT_COLOR_BOUNDARY, _RIGHT_COLOR_BOUNDARY, cursor, document)
    else:
        colorRange = view.selectionRange()

    currentColor = document.text(colorRange)
    color = QtGui.QColor(currentColor)
    result = kdeui.KColorDialog.getColor(color)
    if result == kdeui.KColorDialog.Accepted:
        colorStr = color.name()
        document.startEditing()
        document.replaceText(colorRange, colorStr)
        document.endEditing()
        if view.selection():
            startPos = colorRange.start()
            endPos = KTextEditor.Cursor(startPos.line(), startPos.column() + len(colorStr))
            view.setSelection(KTextEditor.Range(startPos, endPos))
Something went wrong with that request. Please try again.