FindAndOpen
From Niki
If you don't know where a file is but are reasonably sure of its name, this is a script for you. I often must work with a large heirarchy of non-ctags-friendly files--html files, template files, simple script files, and the like--and this script can help quickly reduce the hunt. An imported file name in code can also be a reasonable use case.
If you have selected text in the buffer, the script gives you quick access to it by having an exclamation mark expand into the selected value. For instance, if you see a file is imported, highlight the file name and either specify the extension ("!.py") or use a wildcard ("!*"). If only one match is found, it is immediately opened; otherwise, the script presents a dialog from which you may choose your desired file to open.
To install, simply put this in an external file loaded by your autoload.nm file, and then create a macro that calls "find_and_open()".
If you work with a large heirarchy of files, other scripts that you might find useful in this collection include OpenFileWithTag and SuperGrep.
define find_and_open {
selected = ""
msg = "file name to find (CWD is "$file_path")"
if ($selection_start != -1) {
selected = get_range($selection_start, $selection_end)
msg = msg "\n('!' will expand to " selected ")"
}
name = string_dialog(msg, "OK", "OK & Change Directory", "Cancel")
if (name != "" && selected != ""){
bang = search_string(name, "!", 0)
if (bang != -1)
name = substring(name, 0, bang) selected \
substring(name, bang+1, length(name))
}
dir = ""
if ($string_dialog_button == 2)
dir = string_dialog(\
"directory (relative from "$file_path" or absolute)", \
"OK", "Cancel")
if (name != "" && $string_dialog_button == 1) {
dircommand = ""
if (dir != "")
dircommand = "cd "dir"; "
res = shell_command(dircommand "find -name \""name"\"", "")
if (res == "")
dialog("No results found", " OK ")
else {
end = search_string(res, "\n", 0)
if (search_string(res, "\n", end+1) != -1){
myres = ""
while (myres == "" && $string_dialog_button == 1)
myres = list_dialog(\
"Select from results:", res, \
"OK", "Cancel")
res = myres
}
else
res = substring(res, 0, end)
if (res != "" && $string_dialog_button == 1){
if (substring(res, 0, 1) != "/"){
if (dir == "" || substring(dir, 0, 1) != "/")
dir = $file_path dir
res = dir substring(res, 1, length(res))
}
open(res)
}
}
}
}
