当前位置:网站首页>Vim plugin GrepIt
Vim plugin GrepIt
2022-07-30 11:11:00 【hjjdebug】
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Filename: grepit.vim
"功能: By file type, Finds key strings in a series of files of the same type
"author: hjjdebug
"date: 2022年 07月 29日 星期五 10:07:02 CST
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists("g:grepit_loaded")
finish
endif
let g:grepit_loaded=1 "避免重复加载
"设置3个全局bool变量,Whether to highlight search terms,Whether to jump to the first match,是否打开quickfix窗口
function! s:CfgHightlightSearch()
if exists('g:grepit_hlsearch')
return g:grepit_hlsearch
endif
return 1
endfunction
function! s:CfgGotoFirstMatch()
if exists('g:grepit_goto_first_match')
return g:grepit_goto_first_match
endif
return 1
endfunction
function! s:CfgOpenQuickFix()
if exists('g:grepit_open_quickfix')
return g:grepit_open_quickfix
endif
return 1
endfunction
"Get options for searching
function! s:CfgOption()
if exists('g:grepit_option')
return g:grepit_option
endif
return ""
endfunction
"Set search options
function! s:CfgSetOption()
if !exists('g:grepit_option')
let g:grepit_option=''
endif
let g:grepit_option = input("grep option:-w -i etc:",g:grepit_option)
echo "\nGrepIt option:" . g:grepit_option
endfunction
"constitute a primitive languagemap
"Each language corresponds to a series of file suffixes
"language : extentions
let s:lang_map = {
\ "cpp" : "h,cpp,c,s,S" ,
\ "S" : "s,S" ,
\ }
"获取语言map
function! s:CfgLangeMap()
let l:lang_map = s:lang_map
if exists("g:grepit_lang_map")
extend(l:lang_map, g:grepit_lang_map)
endif
return l:lang_map
endfunction
"According to the file suffix and languagemap, 获取语言(搜索)
function! s:GetLanguage(extension, lang_map)
for [l:language, l:extensions] in items(a:lang_map)
for l:candidate in split(l:extensions, ",")
if l:candidate == a:extension
return l:language
endif
endfor
endfor
return ""
endfunction
"according to language andmap, Build a list of all suffixes
function! s:GetExtensions(languages, lang_map)
let l:extensions = ""
for l:lang in a:languages "Form all search file suffixes from a range of languages
let l:lang_extensions = get(a:lang_map, l:lang, "") "Get the file suffix corresponding to the language
if l:lang_extensions == ""
let l:lang_extensions = tolower(expand("%:e")) "Without language suffix,Let the file suffix be the language suffix
endif
if strlen(l:extensions) > 0
let l:lang_extensions = "," . l:lang_extensions
endif
let l:extensions = l:extensions . l:lang_extensions "Synthesize the file suffix
endfor
return l:extensions
endfunction
"Build search parameters from a list of suffixes
function! s:GetGrepParams(needle, extensions)
let l:params = "-R"
let l:params = l:params . ' ' . s:CfgOption()
for l:extension in split(a:extensions, ",")
let l:params = l:params . " --include=*." . l:extension
endfor
return l:params . " " . shellescape(a:needle ) . " ."
endfunction
"Search based on a list of suffixes and search terms
function! s:GrepItInExtensions(extensions, needle)
let l:params = s:GetGrepParams(a:needle, a:extensions)
let l:commandline = ""
if s:CfgGotoFirstMatch()
let l:commandline = "grep"
else
let l:commandline = "grep!" "Do not jump to the first entry
endif
let l:extlist = join(split(a:extensions, ","), "|")
echo "Searhing for" shellescape(a:needle) "in *.(" . l:extlist . ")"
silent execute l:commandline . " " . l:params
if s:CfgHightlightSearch()
let @/ = a:needle
set hlsearch
endif
if len(getqflist()) == 0
echo "Nothing found"
return
endif
if s:CfgOpenQuickFix()
botright copen " Quickfix always occupies the entire bottom of the window
endif
endfunction
"Find from the specified suffix
function! s:GrepItCmdExt(extensions, ...)
let l:needle = join(a:000)
if strlen(l:needle) == 0
echoerr "usage: GrepItExt <extensions> <needle>"
return
endif
call s:GrepItInExtensions(a:extensions, l:needle)
endfunction
"Find from the specified language
function! s:GrepItLangCmd(languages, ...)
let l:needle = join(a:000)
if strlen(l:needle) == 0
echoerr "usage: GrepItLang <langs> <needle>"
return
endif
let l:lang_map = s:CfgLangeMap()
let l:languages = split(a:languages, ",")
let l:extensions = s:GetExtensions(l:languages, l:lang_map)
call s:GrepItInExtensions(l:extensions, l:needle)
endfunction
"Mainstream uses commands,Because typing one more character is a waste
function! s:GrepItCmd(...)
let l:needle = join(a:000)
if strlen(l:needle) == 0
echoerr "usage: GrepIt <needle>"
return
endif
let l:lang_map = s:CfgLangeMap()
let l:extension = tolower(expand("%:e")) "获取文件后缀
let l:language = s:GetLanguage(l:extension, l:lang_map) "找到语言
let l:extensions = s:GetExtensions([l:language], l:lang_map) "Expand from language to a series of suffix files
call s:GrepItInExtensions(l:extensions, l:needle) "Find in a series of suffixed files
endfunction
"Interface with the user,Set search options
function! GrepItSetOption()
call s:CfgSetOption()
endfunction
"Interface with the user,Select Pattern Search
function! GrepItOperator(type)
let l:old_register_value = @@
if a:type ==# 'v'
normal! `<v`>y
elseif a:type ==# 'char'
normal! `[v`]y
else
return
endif
let l:needle = @@
let @@ = l:old_register_value
call s:GrepItCmd(l:needle)
endfunction
"Define three search commands
"常用的方式,It is up to the program to search from those defined file suffixes
command! -nargs=+ GrepIt call <SID>GrepItCmd(<f-args>)
"Directly specify the file type suffix to find
command! -nargs=+ GrepItExt call <SID>GrepItCmdExt(<f-args>)
"Specify the language to look for
command! -nargs=+ GrepItLang call <SID>GrepItLangCmd(<f-args>)
边栏推荐
猜你喜欢
[Deep Learning] (Problem Record)
- Linear Regression - Small Batch Stochastic Gradient Descent 加密和安全
Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court
Beyond Stream Processing!The 4th real-time computing Flink challenge is launched, and 490,000 prizes are waiting for you!
【HMS core】【FAQ】HMS Toolkit典型问题合集1
Flink_CDC construction and simple use
vscode中写markdown格式笔记的配置过程和相关语法
HJY-F931A/YJ三相电压继电器
真正懂经营管理的CIO具备哪些特质
【HMS core】【FAQ】HMS Toolkit Typical Questions Collection 1
随机推荐
salesforce使用方法(salesforce authenticator下载)
张量篇-初步
Swift common extension classes and simple encapsulation
Js array operating mobile for encapsulation
Transfer Learning技术研修
原生js 创建表格
208. 实现 Trie (前缀树)
单片机开发之ADC0808/9信号采集
系统设计精选 | 基于FPGA的CAN总线控制器的设计(附代码)
The method of parameter passing
梅科尔工作室-看鸿蒙设备开发实战笔记七——网络应用开发
美团内推+校招笔试题+知识点总结
R语言怎么绘图(一个r语言完整的命令有什么)
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
OC- about alloc and dealloc (haven't started writing yet)
AIX shell获取前几个月时间
MySQL之数据库维护
Neural Ordinary Differential Equations
安全提示:Qt中的FreeType
TestNg整合Retry代码