-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendSelectionExt.cs
More file actions
131 lines (112 loc) · 4.39 KB
/
ExtendSelectionExt.cs
File metadata and controls
131 lines (112 loc) · 4.39 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
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
/*
SamplePlugin - An Example KeePass Plugin
Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Security;
using KeePassLib.Utility;
// The namespace name must be the same as the file name of the
// plugin without its extension.
// For example, if you compile a plugin 'SamplePlugin.dll',
// the namespace must be named 'SamplePlugin'.
namespace ExtendSelection
{
// Namespace name 'SamplePlugin' + 'Ext' = 'SamplePluginExt'
public sealed class ExtendSelectionExt : Plugin
{
// The plugin remembers its host in this variable
private IPluginHost m_host = null;
private CustomRichTextBoxEx mainCustomRichTextBox;
/// <summary>
/// The <c>Initialize</c> method is called by KeePass when
/// you should initialize your plugin.
/// </summary>
/// <param name="host">Plugin host interface. Through this
/// interface you can access the KeePass main window, the
/// currently opened database, etc.</param>
/// <returns>You must return <c>true</c> in order to signal
/// successful initialization. If you return <c>false</c>,
/// KeePass unloads your plugin (without calling the
/// <c>Terminate</c> method of your plugin).</returns>
public override bool Initialize(IPluginHost host)
{
if(host == null) return false; // Fail; we need the host
m_host = host;
mainCustomRichTextBox = m_host.MainWindow.Controls.Find("m_richEntryView", true)[0] as CustomRichTextBoxEx;
mainCustomRichTextBox.DoubleClick += this.CustomRichTextBoxEx_DoubleClick;
return true; // Initialization successful
}
private void CustomRichTextBoxEx_DoubleClick(object sender, EventArgs e)
{
if (mainCustomRichTextBox.SelectionLength > 0)
{
int posStart = mainCustomRichTextBox.SelectionStart;
// Expand left side of selection
while (posStart > 0)
{
string previousChar = mainCustomRichTextBox.Text.Substring(posStart - 1, 1);
if (previousChar == "\n" | previousChar == "\r" | previousChar == " ")
break;
posStart = --mainCustomRichTextBox.SelectionStart;
mainCustomRichTextBox.SelectionLength++;
}
// Delete trailing white spaces
while(true)
{
if (mainCustomRichTextBox.Text.Substring(posStart + mainCustomRichTextBox.SelectionLength - 1, 1) == " ")
{
mainCustomRichTextBox.SelectionLength--;
}
else
{
break;
}
}
// Expand right side of selection
while (posStart + mainCustomRichTextBox.SelectionLength < mainCustomRichTextBox.Text.Length)
{
string nextChar = mainCustomRichTextBox.Text.Substring(posStart + mainCustomRichTextBox.SelectionLength, 1);
if (nextChar == " " | nextChar == "\n" | nextChar == "\r")
{
break;
}
mainCustomRichTextBox.SelectionLength++;
}
}
}
/// <summary>
/// The <c>Terminate</c> method is called by KeePass when
/// you should free all resources, close files/streams,
/// remove event handlers, etc.
/// </summary>
public override void Terminate()
{
mainCustomRichTextBox.DoubleClick -= this.CustomRichTextBoxEx_DoubleClick;
}
//Keepass update check
public override string UpdateUrl
{
get { return "https://raw.githubusercontent.com/georgoswonkos/ExtendSelectionPlugin/main/version.txt"; }
}
}
}