-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
101 lines (81 loc) · 4.01 KB
/
extension.js
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
/* exported enable, disable, init, main */
// Search command history by prefix in Gnome-shell's prompts.
// Copyright (C) 2011 Miroslav Sustek
// 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/>.
import Clutter from 'gi://Clutter';
import * as History from 'resource:///org/gnome/shell/misc/history.js';
import {Extension, InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js';
export default class HistoryManagerPrefixSearchExtension extends Extension {
enable() {
this._settings = this.getSettings();
const _settings = this._settings;
this._injectionManager = new InjectionManager();
this._injectionManager.overrideMethod(History.HistoryManager.prototype, 'prevItemPrefix', () => {
return function (text, prefix) {
/* eslint-disable no-invalid-this */
for (let i = this._historyIndex - 1; i >= 0; i--) {
if (this._history[i].indexOf(prefix) === 0 && this._history[i] !== text) {
this._historyIndex = i;
return this._indexChanged();
}
}
return text;
/* eslint-enable no-invalid-this */
};
});
this._injectionManager.overrideMethod(History.HistoryManager.prototype, 'nextItemPrefix', () => {
return function (text, prefix) {
/* eslint-disable no-invalid-this */
for (let i = this._historyIndex + 1; i < this._history.length; i++) {
if (this._history[i].indexOf(prefix) === 0 && this._history[i] !== text) {
this._historyIndex = i;
return this._indexChanged();
}
}
return text;
/* eslint-enable no-invalid-this */
};
});
this._injectionManager.overrideMethod(History.HistoryManager.prototype, '_onEntryKeyPress', () => {
return function (entry, event) {
/* eslint-disable no-invalid-this */
let symbol = event.get_key_symbol();
let prevKey = _settings.get_int('key-previous');
let nextKey = _settings.get_int('key-next');
if (symbol === prevKey) {
let pos = entry.get_cursor_position() !== -1 ? entry.get_cursor_position() : entry.get_text().length;
if (pos > 0)
this.prevItemPrefix(entry.get_text(), entry.get_text().slice(0, pos));
else
this._setPrevItem(entry.get_text().trim());
entry.set_selection(pos, pos);
return true;
} else if (symbol === nextKey) {
let pos = entry.get_cursor_position() !== -1 ? entry.get_cursor_position() : entry.get_text().length;
if (pos > 0)
this.nextItemPrefix(entry.get_text(), entry.get_text().slice(0, pos));
else
this._setNextItem(entry.get_text().trim());
entry.set_selection(pos, pos);
return true;
}
return Clutter.EVENT_PROPAGATE;
/* eslint-enable no-invalid-this */
};
});
}
disable() {
this._injectionManager.clear();
this._injectionManager = null;
this._settings = null;
}
}