Skip to content

Commit

Permalink
feat(context): add context menu support for glossaries-extra
Browse files Browse the repository at this point in the history
refer: #3073
  • Loading branch information
lervag committed Mar 1, 2025
1 parent fd72f7f commit 7826ac7
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 0 deletions.
124 changes: 124 additions & 0 deletions autoload/vimtex/context/glossaries.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: [email protected]
"

function! vimtex#context#glossaries#new() abort " {{{1
return deepcopy(s:handler)
endfunction

" }}}1

let s:handler = {
\ 'name': 'glossaries handler',
\}
function! s:handler.match(cmd, word) abort dict " {{{1
if empty(a:cmd)
\ || a:cmd.name[1:] !~# '\v([cpdr]?(gls|Gls|GLS)|acr|Acr|ACR)\a*>'
\ || len(a:cmd.args) != 1
return v:false
endif

let l:keys = a:cmd.args->map({_, x -> x.text})->join(',')->split(',\s*')
let self.selected = index(l:keys, a:word) >= 0 ? a:word : l:keys[0]

return !empty(self.selected)
endfunction

" }}}1
function! s:handler.get_actions() abort dict " {{{1
let l:entry = s:get_entry(self.selected)

if empty(l:entry)
call vimtex#log#warning('Glossary key not found: ' .. self.selected)
return {}
endif

return s:actions.create(l:entry)
endfunction

" }}}1

let s:actions = {
\ 'menu': [
\ {'name': 'Go to entry',
\ 'func': 'goto'},
\ {'name': 'Show entry',
\ 'func': 'show'},
\ ],
\}
function! s:actions.create(entry) abort dict " {{{1
let l:new = deepcopy(self)
unlet l:new.create

let l:new.entry = deepcopy(a:entry)
let l:new.prompt = 'Context menu for glossary key: ' .. a:entry.key

return l:new
endfunction

" }}}1
function! s:actions.show() abort dict " {{{1
let l:entry = deepcopy(self.entry)

call vimtex#ui#echo([
\ ['Normal', '@'],
\ ['VimtexMsg', l:entry.type],
\ ['Normal', '{'],
\ ['Special', l:entry.key],
\ ['Normal', ','],
\])

for l:x in ['key', 'type', 'source_lnum', 'source_file']
if has_key(l:entry, l:x)
call remove(l:entry, l:x)
endif
endfor

for l:x in ['title', 'author', 'year']
if has_key(l:entry, l:x)
call vimtex#ui#echo([
\ ['VimtexInfoValue', ' ' .. l:x .. ': '],
\ ['Normal', remove(l:entry, l:x)]
\])
endif
endfor

for [l:key, l:val] in items(l:entry)
call vimtex#ui#echo([
\ ['VimtexInfoValue', ' ' .. l:key .. ': '],
\ ['Normal', l:val]
\])
endfor
call vimtex#ui#echo([['Normal', '}']])
endfunction

" }}}1
function! s:actions.goto() abort dict " {{{1
execute 'edit' self.entry.source_file
filetype detect

call vimtex#pos#set_cursor(self.entry.source_lnum, 0)
normal! zv
endfunction

" }}}1

function! s:get_entry(key) abort " {{{1
" Ensure we're at the root directory when locating bib files
call vimtex#paths#pushd(b:vimtex.root)
let l:entries = []
for l:file in b:vimtex.glossaries
let l:entries += vimtex#parser#bib(
\ l:file,
\ {'backend': has('nvim') ? 'lua' : 'vim'}
\)
endfor
call vimtex#paths#popd()

" Return entry with the given key
return l:entries->filter({_, x -> x.key ==# a:key})->get(0, {})
endfunction

" }}}1
14 changes: 14 additions & 0 deletions test/test-context-glossaries/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MYVIM ?= nvim --clean --headless

INMAKE := 1
export INMAKE

TESTS := $(wildcard test*.vim)
TESTS := $(TESTS:.vim=)

.PHONY: test $(TESTS)

test: $(TESTS)

$(TESTS):
@$(MYVIM) -u $@.vim
56 changes: 56 additions & 0 deletions test/test-context-glossaries/glossaries-1.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
% Encoding: UTF-8
@acronym{isbn,
short = {ISBN},
long = {international standard book number}
}

@acronym{isbn13,
short = {isbn-13},
long = {international standard book number}
}

@acronym{nasa,
short = {NASA},
long = {National Aeronautics and Space Administration}
}

@entry{abelian,
name = {Abelsch},
description = {Som}
}

@entry{codeword,
plural = {Codew\"orter},
name = {Codewort},
description = {Element eines Codes}
}

@entry{codingtheory,
name = {Codierungstheorie},
description = {Theorie der Codes}
}

@entry{plausibility-check,
name = {Plausibilit\"ats-Check},
description = {\"Uberpr\"ufung,
ob ein Objekt \enquote{plausibel}
ist; dies kann bedeuten zupr\"ufen,
ob es in dem erwarteten Raum ist},
}

@entry{dual-code,
name = {dualer Code},
description = {Code, der von der Paritycheckmatrix eines anderen Codes aufgespannt wird},
}

@entry{qr-code,
name = {QR-Code},
description = {\enquote{Quick Response}-Code},
}
@abbreviation{API,
title = {Application {{Programming Interface}}},
long = {Application Programming Interface},
prefix = {an\space},
prefixfirst = {an\space},
short = {API},
}
54 changes: 54 additions & 0 deletions test/test-context-glossaries/glossaries-1.glstex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
% This file was created by bib2gls v3.9.
% DO NOT edit this file. Any changes made will be lost next time bib2gls is run.
% This file was generated from data obtained from the following files:
% glossaries.aux and glossaries-2.bib
\glsnoexpandfields
\providecommand{\bibglspassimname}{passim}
\bibglstertiaryprefixlabel{tertiary.}
\bibglsdualprefixlabel{dual.}
\bibglsprimaryprefixlabel{}
\providecommand{\bibglsseesep}{, }

\providecommand{\bibglsseealsosep}{, }

\providecommand{\bibglsaliassep}{, }

\ifdef\glsxtrsetgrouptitle
{
\providecommand{\bibglslettergroup}[4]{#4#3}
\providecommand{\bibglslettergrouptitle}[4]{\unexpanded{#1}}
\providecommand{\bibglssetlettergrouptitle}[1]{%
\glsxtrsetgrouptitle{\bibglslettergroup#1}{\bibglslettergrouptitle#1}}
\providecommand{\bibglsunicodegroup}[4]{#4#3}
\providecommand{\bibglsunicodegrouptitle}[4]{\unexpanded{#1}}
\providecommand{\bibglssetunicodegrouptitle}[1]{%
\glsxtrsetgrouptitle{\bibglsunicodegroup#1}{\bibglsunicodegrouptitle#1}}
\providecommand{\bibglsothergroup}[3]{glssymbols}
\providecommand{\bibglsothergrouptitle}[3]{\protect\glssymbolsgroupname}
\providecommand{\bibglssetothergrouptitle}[1]{%
\glsxtrsetgrouptitle{\bibglsothergroup#1}{\bibglsothergrouptitle#1}}
\providecommand{\bibglsemptygroup}[1]{glssymbols}
\providecommand{\bibglsemptygrouptitle}[1]{\protect\glssymbolsgroupname}
\providecommand{\bibglssetemptygrouptitle}[1]{%
\glsxtrsetgrouptitle{\bibglsemptygroup#1}{\bibglsemptygrouptitle#1}}
\providecommand{\bibglsnumbergroup}[3]{glsnumbers}
\providecommand{\bibglsnumbergrouptitle}[3]{\protect\glsnumbersgroupname}
\providecommand{\bibglssetnumbergrouptitle}[1]{%
\glsxtrsetgrouptitle{\bibglsnumbergroup#1}{\bibglsnumbergrouptitle#1}}
}
{
\providecommand{\bibglslettergroup}[4]{#1}
\providecommand{\bibglsothergroup}[3]{glssymbols}
\providecommand{\bibglsnumbergroup}[3]{glsnumbers}
\providecommand{\bibglssetlettergrouptitle}[1]{}
\providecommand{\bibglssetothergrouptitle}[1]{}
\providecommand{\bibglssetnumbergrouptitle}[1]{}
\providecommand{\bibglssetdatetimegrouptitle}[1]{}
\providecommand{\bibglssetdategrouptitle}[1]{}
\providecommand{\bibglssettimegrouptitle}[1]{}
}

\providecommand{\bibglssetlastgrouptitle}[2]{}


\providecommand*{\bibglsflattenedhomograph}[2]{#1}
53 changes: 53 additions & 0 deletions test/test-context-glossaries/glossaries-2.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
\newglossary*{gloss}{Glossary}
\newglossary*{symbols}{List of symbols}

\makeglossaries

\newglossaryentry{kv}
{
type=gloss,
name=KV,
description={Kontrollvolumen}
}

\newglossaryentry{dgl}
{
type=gloss,
name=DGL,
description={Differentialgleichung}
}

\newglossaryentry{agl}
{
type=gloss,
name=AGL,
description={Algebraische Gleichung}
}

\newglossaryentry{dichte}
{
sort=1171,
type=symbols,
name={\ensuremath{\varrho}},
description={
Dichte, $[\varrho] = \frac{kg}{m^3}$
}
}
\newglossaryentry{c}
{
sort=0031,
type=symbols,
name={\ensuremath{c}},
description={
spezifische Wärmekapazität, $[c] = \frac{J}{kg\cdot K}$
}
}
\newglossaryentry{C}
{
sort=0030,
type=symbols,
name={\ensuremath{C_{th}}},
description={
Wärmekapazität, $[C_{th}] = \frac{J}{K}$
}
}
Loading

0 comments on commit 7826ac7

Please sign in to comment.