当前位置:网站首页>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>)
边栏推荐
- Database transactions, JDBC operations and data types
- Basemap and Seaborn
- 汇编实现冒泡排序
- 高手云集、丰富活动,斩获佳绩,超过2万名开发者参与的AI社团邀你加入!
- IP池设计思考(面试点)[通俗易懂]
- [100 Solidity Skills] 1. Contract reentrancy attack
- 还在用Swagger?我推荐这款零代码侵入的接口管理神器
- 数据库脏读、不可重复读、幻读以及对应的隔离级别
- Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
- PyQt5 - draw text on window
猜你喜欢

【HMS core】【FAQ】HMS Toolkit Typical Questions Collection 1

自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律

SST-Calib: A lidar-visual extrinsic parameter calibration method combining semantics and VO for spatiotemporal synchronization calibration (ITSC 2022)

系统设计精选 | 基于FPGA的CAN总线控制器的设计(附代码)
![[Qualcomm][Network] 网络拨号失败和netmgrd服务分析](/img/76/49054ff8c7215eca98cc479ab1d986.png)
[Qualcomm][Network] 网络拨号失败和netmgrd服务分析

NLP领域的最新研究进展

Neural Ordinary Differential Equations

FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)

Nacos configuration in the project of battle

【AGC】增长服务2-应用内消息示例
随机推荐
kubernetes的一些命令
图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
Log4j有哪几种日志级别呢?
Matplotlib--plot markers
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
[100 Solidity Skills] 1. Contract reentrancy attack
IP池设计思考(面试点)[通俗易懂]
【HMS core】【FAQ】HMS Toolkit典型问题合集1
安全提示:Qt中的FreeType
系统设计精选 | 基于FPGA的CAN总线控制器的设计(附代码)
Scrapy crawler website image crawling
第3章 信息收集
Drools 规则引擎一文读懂
第2章 常用安全工具
Telerik2022 R2,有效的自动化测试
360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
WebAPI 复习
MySQL之数据库维护
【梦想起航】