Welcome, guest | Sign In | My Account | Store | Cart

This does a search for the selected word when you double click it. Functionality similar to Notepad++. Faster that ctrl-f3.

JavaScript, 135 lines
  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
/**
 *
 * Original:
 * Copyright (c) 2009 Stan Angeloff http://blog.angeloff.name
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

if (typeof (extensions) === 'undefined')
        window.extensions = {};

(function() {

        const Cc = Components.classes;
        const Ci = Components.interfaces;

        // Get a reference to the script namespace
        var $self = extensions.findWordUnderCursor ||
                                (extensions.findWordUnderCursor = { events: {} });

        // Clean-up after previous execution
        if ($self.onViewClosed)
                window.removeEventListener('view_closed', $self.onViewClosed, false);
        if ($self.onViewOpened)
                window.removeEventListener('view_opened', $self.onViewOpened, false);
        if ($self.destroyAll)
                $self.destroyAll();

        $self.isSupportedView = function(view) {

                return (view && view.getAttribute('type') === 'editor');
        };

        $self.onViewOpened = function(e) {

                var view = e.originalTarget;

                if ($self.isSupportedView(view))
                        $self.apply(view);
        };

        $self.onViewClosed = function(e) {

                var view = e.originalTarget;

                if ($self.isSupportedView(view))
                        $self.destroy(view);
        };

        $self.destroy = function(view) {

                if (view.uid in $self.events) {

                        view.removeEventListener('dblclick', $self.events[view.uid], false);

                        delete $self.events[view.uid];
                }
        };

        $self.apply = function(view) {

                var fn = function(e) {

                        if (e.which === 1 &&
                                ! (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) &&
                                ! (view.scimoz._startDragDrop || view.scimoz._inDragDrop)) {

                                e.stopPropagation();
                                e.preventDefault();

                                $self.jumpToSearch(view);
                        }
                };

                //view.addEventListener('mouseup', fn, false);
                dblclick:view.addEventListener('dblclick', fn, false);

                $self.events[view.uid] = fn;
        };

        window.addEventListener('view_opened', $self.onViewOpened, false);
        window.addEventListener('view_closed', $self.onViewClosed, false);

        $self.forEach = function(fn) {

                var viewsByType = ko.views.manager.topView.getViewsByType(true, 'editor'),
                        view;

                for (var i = 0; i < viewsByType.length; i ++) {

                        view = viewsByType[i];

                        if ($self.isSupportedView(view))
                                fn.apply($self, [view]);
                }
        };

        $self.applyToAll = function() { $self.forEach($self.apply); };
        $self.destroyAll = function() { $self.forEach($self.destroy); };

        $self.jumpToSearch = function(view) {
            startLine = ko.views.manager.currentView.scimoz.firstVisibleLine;
            ko.commands.doCommand("cmd_findNextSelected");
            ko.commands.doCommand("cmd_findPrevious");
            endLine = ko.views.manager.currentView.scimoz.firstVisibleLine;
            changeLine = startLine - endLine;
            
            //in case it jumps to the top then back in the event that the next
            //selected is at the top of the screen.
            ko.views.manager.currentView.scimoz.lineScroll(0, changeLine);
            
        };

        $self.applyToAll();

})();

I found this here:

http://community.activestate.com/forum/macro-quick-find-highlight

But it was really old and didn't use the much simpler ko.commands.doCommand("cmd_findNextSelected");

Instead it used 2 pages of complicated something or other. Someone posted a modified version to use dblclick, but then didn't update the destroy method to unbind the dblclick which led to oddities at times.

So here's an updated version.

4 comments

Kai Risku 12 years, 6 months ago  # | flag

Thank you for this useful macro!

However, I have found that it also activates and pops up the general find-dialog if you double-click in the margin of the editor, i.e. if you folds and unfolds a section too quickly. Any idea how to fix that?

Irakli Kublashvil 12 years, 5 months ago  # | flag

Awesome! Thank you !

Piyush Chandra 12 years, 4 months ago  # | flag

thanks Adam. this macro is very nice. This works good for all languages but TCL. In TCL, when you double click on "$var_name", then the complete "$var_name" is selected. What modification needs to be done so that it highlights only "var_name", without "$" symbol.

Álvaro G. Vicario 12 years, 2 months ago  # | flag

This macro works on top of regular search feature. As a result, there's the side effect that the last search options are reused. In particular, if "[X] Regex" is set you cannot highlight PHP variables like $foo.

Created by Adam on Fri, 22 Jul 2011 (MIT)
JavaScript recipes (69)
Adam's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks