In-Editor Web Searching

This forum allows you to share scripts with other Zeus users. Please do not post bug reports, feature requests or questions to this forum, but rather use it exclusively for posting scripts or for the discussion of scripts that have been posted.
Post Reply
jussij
Site Admin
Posts: 2498
Joined: Fri Aug 13, 2004 5:10 pm

In-Editor Web Searching

Post by jussij »

The macro below can be used to initiate an in-editor Google search:

Code: Select all

--
--        Name: Web Search
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This Lua macro takes the current word or marked area and tries
--              to determine if the text should be loaded as a url or the text
--              details used in a web search.
--
--  How To Run: All Zeus macros can be run using the macro execute menu
--              item or they can be bound to the keyboard or they can
--              be attached to the macro drop down or popup menus.
--

local utils = require "utils"

function encodeChar(chr)
	return string.format("%%%X",string.byte(chr))
end

function encodeString(str)
	local output, t = string.gsub(str,"[^%w]",encodeChar)
	return output
end

function getLanguage()
    local language = ""
    local extension = string.lower(macro_tag("$Ext"))

    if (extension == ".ada") then
       language = "ada"
    elseif ((extension == ".asax"  ) or
            (extension == ".ascx"  ) or
            (extension == ".asmx"  ) or
            (extension == ".asp"   ) or
            (extension == ".aspx"  )) then
            language = "asp.net"
    elseif (extension == ".bat"   ) then
            language = "cmd"
    elseif ((extension == ".c"    ) or
            (extension == ".h"    )) then
            language = "clang"
    elseif ((extension == ".cpp"  ) or
            (extension == ".cxx"  ) or
            (extension == ".hpp"  ) or
            (extension == ".hxx"  ) or
            (extension == ".cppm" )) then
            language = "c++"
    elseif ((extension == ".cs"   )) then
            language = "c#"
    elseif ((extension == ".d"    )) then
            language = "dlang"
    elseif ((extension == ".f"    ) or
            (extension == ".for"  ) or
            (extension == ".f77"  ) or
            (extension == ".f90"  ) or
            (extension == ".f95"  ) or
            (extension == ".ftn"  ) or
            (extension == ".fort" )) then
            language = "fortran"
    elseif ((extension == ".go"   )) then
             language = "golang"
    elseif ((extension == ".html" ) or
            (extension == ".htm"  )) then
            language = "html"
    elseif ((extension == ".java" )) then
             language = "java"
    elseif ((extension == ".js" )) then
             language = "javascript"
    elseif ((extension == ".php"  ) or
            (extension == ".php3" ) or
            (extension == ".php4" ) or
            (extension == ".phtml")) then
            language = "php"
    elseif ((extension == ".bas"  ) or
            (extension == ".vba"  ) or
            (extension == ".vbs"  ) or
            (extension == ".frm"  )) then
            language = "VisualBasic"
    elseif (extension == ".lua") then
             language = "lua"
    elseif ((extension == ".rc"   ) or
            (extension == ".rh"   ) or
            (extension == ".dlg"  )) then
            language = "resource"
    elseif (extension == ".pas"  ) then
            language = "pascal"
    elseif ((extension == ".py"   ) or
            (extension == ".pl"   ) or
            (extension == ".pm"   )) then
            language = "python"
    elseif (extension == ".sql"   ) then
            language = "sql"
    end

    -- local message_text = ""
    -- message_text = message_text  .. "extension: " .. extension .. "\n"
    -- message_text = message_text  .. "language: " .. language .. "\n\n"
    -- message_box(1, message_text, "Language Details")

    return language
end

function key_macro()
    -- get the current word or marked area
    local current_word = macro_tag("$WEX")

    if (string.len(current_word) == 0) then
        message("To run a search place the cursor on a word or mark a region of text.")
        beep()
        return
    end

    if is_marked() == 1 then
        if string.starts(current_word, "https://", 1) or string.starts(current_word, "http://", 1) then
            message("Opening URL: " .. current_word)
            shell_execute(current_word)
            return
        end
    end

    local query = ""
    local language = getLanguage()

    if (string.len(language) > 0) then
        query = "\"" .. encodeString(language) .. "\"+"
    end

    query = query .. "\"" .. encodeString(current_word) .. "\""

    engines = { "https://www.google.com/search?q=",
                "https://duckduckgo.com/?q=",
                "https://www.bing.com/search?q=",
                "https://search.yahoo.com/search?q=" }

    for key, engine in ipairs(engines)
    do
        url = engine .. query
        message("Search URL: " .. url)
        shell_execute(url)
    end

    -- limit search to stackoverflow using google
    query = encodeString("site:stackoverflow.com") .. "+" .. query
    url = engines[1] .. query
    shell_execute(url)
end

key_macro() -- run the macro
Post Reply