robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- Marks functionality 2--- - |:marks| command/completion 3 4local api =vim . api 5local util =require ( 'vim._core.util' ) 6local N_ =vim . fn . gettext 7 8local M ={} 9 10--- Names of the namespaces that have extmarks in the current buffer, sorted. 11--- @return string[] 12local function buf_namespaces () 13local names ={} ---@type string[] 14for name , id in pairs ( api . nvim_get_namespaces ()) do 15if # api . nvim_buf_get_extmarks ( 0 , id , 0 , - 1 , { limit =1 } ) > 0 then 16names [ # names + 1 ] =name 17end 18end 19table . sort ( names ) 20return names 21end 22 23--- Completion for ":marks {arg}": 24--- - set marks (buffer-local and global) 25--- - extmark namespaces (buffer-local) 26--- @return string[] 27function M . complete () 28local names ={} ---@type string[] 29for _ , list in ipairs ( { vim . fn . getmarklist ( api . nvim_get_current_buf ()), vim . fn . getmarklist () } ) do 30for _ , m in ipairs ( list ) do 31names [ # names + 1 ] =m . mark : sub ( 2 ) -- "'a" => "a" 32end 33end 34table . sort ( names ) 35return vim . list_extend ( names , buf_namespaces ()) 36end 37 38--- ":marks {ns}": lists the extmarks of namespace {ns} in the current buffer. 39--- @param ns string 40--- @return boolean handled false: {ns} is not a namespace (it is a set of mark names) 41function M . show ( ns ) 42local id =api . nvim_get_namespaces ()[ ns ] 43if not id then 44return false 45end 46local extmarks =api . nvim_buf_get_extmarks ( 0 , id , 0 , - 1 , {} ) 47if # extmarks == 0 then 48api . nvim_echo ( { { ( 'No extmarks in this buffer for namespace "%s"' ): format ( ns ) } } , false , {} ) 49return true 50end 51--- @type [string, string?][] 52local chunks ={ { ( '%6s %5s %4s %s' ): format ( 'id' , 'line' , 'col' , 'text' ), 'Title' } } 53for _ , m in ipairs ( extmarks ) do 54local text =api . nvim_buf_get_lines ( 0 , m [ 2 ], m [ 2 ] + 1 , false )[ 1 ] or '' 55chunks [ # chunks + 1 ] ={ ( '\n%6d %5d %4d %s' ): format ( m [ 1 ], m [ 2 ] + 1 , m [ 3 ], vim . trim ( text )) } 56end 57api . nvim_echo ( chunks , false , { kind ='list_cmd' } ) 58return true 59end 60 61--- The line at mark line {lnum}, sans leading whitespace, truncated to fit in the window 62--- (cells), like C mark_line(). 63--- @param lnum integer 64--- @return string 65local function mark_line ( lnum ) 66local line =lnum > 0 and api . nvim_buf_get_lines ( 0 , lnum - 1 , lnum , false )[ 1 ] 67if not line then 68return '-invalid-' 69end 70local text =line : gsub ( '^%s+' , '' ) 71local limit =vim . o . columns - 15 72if vim . fn . strdisplaywidth ( text ) < limit then 73return text 74end 75local keep ={} ---@type string[] 76local width =0 77for _ , c in ipairs ( vim . fn . split ( text , [[\zs]] )) do 78width =width + vim . fn . strdisplaywidth ( c ) 79if width >= limit then 80break 81end 82keep [ # keep + 1 ] =c 83end 84return table . concat ( keep ) 85end 86 87--- ":marks [arg]" (|ex_cmds.lua| "marks" => C ex_marks() => this). 88--- @param args vim._core.ExCmdArgs 89function M . ex_marks ( args ) 90local arg =args . args ~= '' and args . args or nil 91 92-- ":marks {ns}": list namespace's extmarks, or fallthrough if unknown. 93if arg and # arg > 1 and M . show ( arg ) then 94return 95end 96 97local curbuf =api . nvim_get_current_buf () 98local marks ={} ---@type table<string, vim.fn.getmarklist.ret.item> 99for _ , list in ipairs ( { vim . fn . getmarklist ( curbuf ), vim . fn . getmarklist () } ) do 100for _ , m in ipairs ( list ) do 101marks [ m . mark : sub ( 2 )] =m 102end 103end 104 105-- "<" and ">" are shown as where they will jump to: start before end. 106local vs , ve =marks [ '<' ], marks [ '>' ] 107if vs and ve then 108local s , e =vs . pos , ve . pos 109if s [ 2 ] > e [ 2 ] or ( s [ 2 ] == e [ 2 ] and s [ 3 ] > e [ 3 ]) then 110vs . pos , ve . pos =e , s 111end 112elseif ve and not vs then 113marks [ '<' ], marks [ '>' ] =ve , nil 114end 115 116--- The "file/text" column: the text at the mark if it is in the current buffer (highlighted 117--- as "Directory", like C show_one_mark()), else the file name. 118--- @param m vim.fn.getmarklist.ret.item 119--- @return string text, string? hl 120local function displayname ( m ) 121if m . pos [ 1 ] == curbuf then 122return mark_line ( m . pos [ 2 ]), 'Directory' 123elseif m . pos [ 1 ] == 0 then 124return m . file or '' , nil 125end 126return vim . fn . fnamemodify ( vim . fn . bufname ( m . pos [ 1 ]), ':~' ), nil 127end 128 129-- Fixed :marks order. 130local order ={ "'" } 131for _ , range in ipairs ( { { 'a' , 'z' } , { 'A' , 'Z' } , { '0' , '9' } } ) do 132for b =range [ 1 ]: byte (), range [ 2 ]: byte () do 133order [ # order + 1 ] =string . char ( b ) 134end 135end 136vim . list_extend ( order , { '"' , '[' , ']' , '^' , '.' , ':' , '<' , '>' } ) 137 138local filtered =require ( 'vim._core.ex_cmd' ). filter 139local chunks ={} ---@type [string, string?][] 140for _ , name in ipairs ( order ) do 141local m =marks [ name ] 142if m and ( not arg or arg : find ( name , 1 , true )) then 143local text , hl =displayname ( m ) 144if not filtered ( args . smods . filter , text ) then 145chunks [ # chunks + 1 ] ={ ( '\n %s %6d %4d ' ): format ( name , m . pos [ 2 ], m . pos [ 3 ] - 1 ) } 146if text ~= '' then 147chunks [ # chunks + 1 ] ={ text , hl } 148end 149end 150end 151end 152 153if # chunks == 0 then 154if arg then 155util . echo_err ( N_ ( 'E283: No marks matching "%s"' ): format ( arg )) 156return 157end 158api . nvim_echo ( { { N_ ( 'No marks set' ) } } , false , {} ) 159else 160table . insert ( chunks , 1 , { N_ ( '\nmark line col file/text' ), 'Title' } ) 161api . nvim_echo ( chunks , false , { kind ='list_cmd' } ) 162end 163 164if not arg then 165-- By default, list extmarks namespaces (but not their extmarks). 166local names =buf_namespaces () 167if # names > 0 then 168local msg =( 'Extmark namespaces (use ":marks {ns}"): %s' ): format ( table . concat ( names , ', ' )) 169api . nvim_echo ( { { msg } } , false , {} ) 170end 171end 172end 173 174return M