- Offizieller Beitrag
Ich hatte etwas Ähnliches bereits für das Suchen auf Seiten (AutoIt DE / ~EN und MSDN) erstellt. Aber dort war jedem Aufruf eine Funktion und somit ein Hotkey zugewiesen.
Mit dem neuen Skript stehen zwei Funktionen zur Verfügung:
InetSearch.Engine()
Bei Aufruf wird eine Liste verfügbarer (in properties festgelegter) Suchmaschinen angezeigt. Der Suchbegriff (markierter Text od. Text indem der Cursor steht) wird in der gewählten Maschine gesucht.
InetSearch.Site()
Bei Aufruf wird eine Liste verfügbarer (in properties festgelegter) Suchseiten angezeigt. Der Suchbegriff wird in der Standardsuchmaschine (in properties festlegbar) auf der ausgewählten Seite gesucht.
In einer weiteren Einstellung kann erlaubt werden, dass die Texterkennung auch Punkte findet, also Objektvariablen mit Eigenschaften oder Methoden.
Das Skript abspeichern und in der Lua Startdatei laden.
Beide Funktionen an Hotkeys eurer Wahl binden.
InetSearch.lua
-- TIME_STAMP 2018-08-29 22:31:47
--[[
This script publicates two functions
- InetSearch.Engine()
You can search for the selected/touched text in SciTE and can select a search engine of your choice.
- InetSearch.Site()
You can search for the selected/touched text in SciTE with a defined search engine and can select a search site of your choice.
Copy this script in your Lua scripts folder.
Call this file in the Lua startup: dofile("Full\\Path\\InetSearch.lua")
Add
in "SciTEUser.properties" the following properties with search engines and search sites of your choice:
#~ Inet Search Settings
#~ List of engines. "display-name,search-url" (if not set: google.com will used)
#~ If only one entry exists, the selection box will not shown.
Inet.Search.Engines= \
Google,https://www.google.de/search?q=| \
MetaGer,https://metager.de/meta/meta.ger3?focus=web&eingabe=| \
Bing,https://www.bing.com/search?q=| \
Ask,https://de.ask.com/web?q=| \
Yandex,https://www.yandex.com/search/?text=
#~ Engine for site based search
#~ Use display name from engines list (if not set: google.com will used)
Inet.Search.SearchOnSite.Engine=Google
#~ List of sites for site based search. "display-name,site-url"
#~ If not set, it searches without a special site.
#~ If only one entry exists, the selection box will not shown.
Inet.Search.SearchOnSite.Sites= \
AutoIt DE,autoit.de| \
AutoIt EN,autoitscript.com| \
MSDN,msdn.microsoft.com
#~ By default, words will detected without dots. (0)
#~ You can allow dots, to detect object variables with properties or methods too.
Inet.Search.Allow.Punctuation=1
#~ Search commands
#~ Search word from cursor with engine selection
command.name.9.*=Search with engine selection
command.9.*=dostring InetSearch.Engine()
command.mode.9.*=subsystem:lua,savebefore:no
command.shortcut.9.*=Ctrl+Shift+F8
#~ Search word from cursor with search site selection
command.name.5.*=Search with site selection
command.5.*=dostring InetSearch.Site()
command.mode.5.*=subsystem:lua,savebefore:no
command.shortcut.5.*=Ctrl+Shift+F9
--]]
InetSearch = {}
InetSearch.Handler = EventClass:new(Common)
do
------------------------------------------------------------------------------------------------
local engineFallback = "Google,https://www.google.com/search?q=" -- default search engine: "display-name,search-url"
local search_type = nil
local engine_url = nil
local search_item = nil
local search_site = nil
local tableSelection = nil
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------- Load library protected
local require_protected = function(_lib)
local loaded_lib
if pcall(function() loaded_lib = require(_lib) end) then return loaded_lib
else return nil end
end --> require_protected
------------------------------------------------------------------------------------------------
local shell = require_protected("shell") -- loads the shell library if exists
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------ Starts the search
local RunSearch = function(_item, _url, _site)
local sSite = ''
if _site ~= nil then sSite = "%20site:".._site end
local sCmd = _url.._item..sSite
if shell then shell.exec(sCmd)
else os.execute('start "" "'..sCmd..'"') end
end --> RunSearch
------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------- Show the user list
local ShowSelectionList = function()
-- create the selection list (display names from engines or sites)
local sep, list = '_', tableSelection[1][1]
for i = 2, #tableSelection do
list = list..sep..tableSelection[i][1]
end
local sep_tmp = editor.AutoCSeparator
editor.AutoCSeparator = string.byte(sep)
editor:UserListShow(16, list)
editor.AutoCSeparator = sep_tmp
end --> ShowSelectionList
------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------- Select the user list
function InetSearch.Handler:OnUserListSelection(_tp, _sel)
if _tp == 16 then
local url
for i = 1, #tableSelection do
if tableSelection[i][1] == _sel then
url = tableSelection[i][2]
break
end
end
if search_type == 'site' then
search_site = url
else
search_site = nil
engine_url = url
end
RunSearch(search_item, engine_url, search_site)
end
end
------------------------------------------------------------------------------------------------
--------------------------------------------------------- List of search engines from properties
local GetEngines = function()
local sEngines = props['Inet.Search.Engines']
-- without settings, the default search engine will used
if sEngines == '' then sEngines = engineFallback end
local tEngines = {}
for entry in sEngines:gmatch('([^|]+)|-') do
for name, url in entry:gmatch('([^,]+),(.+)') do
table.insert(tEngines, {name, url})
end
end
return tEngines
end --> GetEngines
------------------------------------------------------------------------------------------------
----------------------------------------------------------- List of search sites from properties
local GetSearchSites = function()
local sSites = props['Inet.Search.SearchOnSite.Sites']
if sSites == '' then return nil end
local tSites = {}
for entry in sSites:gmatch('([^|]+)|-') do
for name, url in entry:gmatch('([^,]+),(.+)') do
table.insert(tSites, {name, url})
end
end
return tSites
end --> GetSearchSites
------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------- Get search item
local GetSearchItem = function()
local sSelect = editor:GetSelText()
if sSelect ~= '' then return sSelect end
local allowDots = false
if props['Inet.Search.Allow.Punctuation'] == '1' then allowDots = true end
local caret = editor.CurrentPos
local iLine = editor:LineFromPosition(caret)
local iZero = editor:PositionFromLine(iLine)
editor:LineEnd()
local iLineEnd = editor.CurrentPos
editor.CurrentPos = caret
editor:SetSelection(caret,caret)
local iStart = editor:WordStartPosition(caret)
if allowDots then
while iStart > iZero and editor:textrange(iStart -1, iStart) == '.' do
iStart = editor:WordStartPosition(iStart -1)
end
end
local iEnd = editor:WordEndPosition(caret)
if allowDots then
while iEnd < iLineEnd and editor:textrange(iEnd, iEnd +1) == '.' do
iEnd = editor:WordEndPosition(iEnd +1)
end
end
sSelect = editor:textrange(iStart, iEnd):gsub('^([^%w]+)', ''):gsub('([^%w]+)$', '')
return sSelect
end --> GetSearchItem
------------------------------------------------------------------------------------------------
------------------------------------------------------------------- Search with engine selection
local SearchEngineBased = function()
search_type = 'engine'
search_item = GetSearchItem()
if search_item == '' then return nil end
tableSelection = GetEngines()
-- only one entry: selection box will not shown
if #tableSelection == 1 then
RunSearch(search_item, tableSelection[1][2], nil)
else
ShowSelectionList()
end
end --> SearchEngineBased
------------------------------------------------------------------------------------------------
-------------------------------------------------------------- Search with search site selection
local SearchSiteBased = function()
search_type = 'site'
search_item = GetSearchItem()
if search_item == '' then return nil end
-- get the default engine for site based searching
for _, url in engineFallback:gmatch('([^,]+),(.+)') do
engine_url = url
end
-- if is set, use search engine from properties
local sEngine = props['Inet.Search.SearchOnSite.Engine']
if sEngine ~= '' then
local tEngines = GetEngines()
for i = 1, #tEngines do
if tEngines[i][1] == sEngine then
engine_url = tEngines[i][2]
break
end
end
end
tableSelection = GetSearchSites()
-- if none entries for search sites, use simple search
if tableSelection == nil then
RunSearch(search_item, engine_url, nil)
else
-- only one entry: selection box will not shown
if #tableSelection == 1 then
RunSearch(search_item, engine_url, tableSelection[1][2])
else
ShowSelectionList()
end
end
end --> SearchSiteBased
------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------- Publicate functions
InetSearch.Engine = SearchEngineBased
InetSearch.Site = SearchSiteBased
------------------------------------------------------------------------------------------------
end
----------------------------------------------------------------------------------------------------
Alles anzeigen