2020 let s: TERM = ' !'
2121endif
2222let s: hardware_dirs = {}
23+ let s: SKETCHFILE = v: null
2324python3 import json
2425
2526" Initialization {{{1
@@ -82,6 +83,7 @@ function! arduino#InitializeConfig() abort
8283 echoerr ' arduino-cli: command not found'
8384 endif
8485 call arduino#ReloadBoards ()
86+ call s: ReadSketchJson ()
8587endfunction
8688
8789function ! arduino#RunCmd (cmd) abort
@@ -196,10 +198,13 @@ function! arduino#GetBuildPath() abort
196198endfunction
197199
198200function ! arduino#GetCLICompileCommand (... ) abort
199- let cmd = ' arduino-cli compile -b ' . g: arduino_board
200- let port = arduino#GetPort ()
201- if ! empty (port)
202- let cmd = cmd . ' -p ' . port
201+ let cmd = ' arduino-cli compile'
202+ if s: SKETCHFILE == v: null
203+ let cmd = cmd . ' -b ' . g: arduino_board
204+ let port = arduino#GetPort ()
205+ if ! empty (port)
206+ let cmd = cmd . ' -p ' . port
207+ endif
203208 endif
204209 if ! empty (g: arduino_programmer )
205210 let cmd = cmd . ' -P ' . g: arduino_programmer
@@ -420,6 +425,31 @@ function! s:ChooserItemOrder(i1, i2) abort
420425 return l1 == l2 ? 0 : l1 > l2 ? 1 : -1
421426endfunction
422427
428+ function ! arduino#Attach (... ) abort
429+ if ! s: has_cli
430+ echoerr ' ArduinoAttach requires arduino-cli'
431+ return
432+ end
433+ let port = v: null
434+ if a: 0
435+ let port = a: 1
436+ function PostAttach () abort
437+ call s: ReadSketchJson ()
438+ call s: notify (' Arduino attached to board ' . g: arduino_board )
439+ endfunction
440+ call arduino#job#run ([' arduino-cli' , ' board' , ' attach' , ' -p' , port], funcref (' PostAttach' ))
441+ else
442+ let ports = arduino#GetPorts ()
443+ if empty (ports)
444+ echoerr ' No likely serial ports detected!'
445+ elseif len (ports) == 1
446+ call arduino#Attach (ports[0 ])
447+ else
448+ call arduino#chooser#Choose (' Select Port' , ports, ' arduino#Attach' )
449+ endif
450+ endif
451+ endfunction
452+
423453" Port selection {{{2
424454
425455function ! arduino#ChoosePort (... ) abort
@@ -437,6 +467,7 @@ endfunction
437467
438468function ! arduino#SelectPort (port) abort
439469 let g: arduino_serial_port = a: port
470+ call s: WriteSketchKey (' port' , ' serial://' . g: arduino_serial_port )
440471endfunction
441472
442473" Board selection {{{2
@@ -465,6 +496,9 @@ function! arduino#SelectBoard(board) abort
465496 \}
466497 " Have to delay this to give the previous chooser UI time to clear
467498 call timer_start (10 , {tid - > arduino#ChooseBoardOption ()})
499+ if empty (options )
500+ call s: WriteSketchKey (' fqbn' , g: arduino_board )
501+ endif
468502endfunction
469503
470504" Prompt user for the next unselected board option
@@ -474,17 +508,21 @@ function! arduino#ChooseBoardOption() abort
474508 if ! has_key (s: callback_data .opts, opt .option )
475509 let s: callback_data .active_option = opt .option
476510 call arduino#chooser#Choose (opt .option_label, opt .values , ' arduino#SelectOption' )
477- return
511+ return v: true
478512 endif
479513 endfor
514+ return v: false
480515endfunction
481516
482517" Callback from option selection
483518function ! arduino#SelectOption (value) abort
484519 let opt = s: callback_data .active_option
485520 let s: callback_data .opts[opt ] = a: value
486521 call arduino#SetBoard (s: callback_data .board, s: callback_data .opts)
487- call arduino#ChooseBoardOption ()
522+ let choosing = arduino#ChooseBoardOption ()
523+ if ! choosing
524+ call s: WriteSketchKey (' fqbn' , g: arduino_board )
525+ endif
488526endfunction
489527
490528" Programmer selection {{{2
@@ -626,6 +664,49 @@ endfunction
626664
627665" Utility functions {{{1
628666
667+ function ! s: ReadSketchJson () abort
668+ let dir = getcwd ()
669+ while v: true
670+ let sketch = dir . ' /sketch.json'
671+ if filereadable (sketch)
672+ let data = json_decode (join (readfile (sketch)))
673+ let cpu = get (data, ' cpu' , {})
674+ if ! empty (cpu)
675+ let s: SKETCHFILE = sketch
676+ let board = get (cpu, ' fqbn' , ' ' )
677+ if ! empty (board)
678+ let g: arduino_board = board
679+ endif
680+ let port = get (cpu, ' port' , ' ' )
681+ if ! empty (port)
682+ if port = ~? ' ^serial://'
683+ let port = strcharpart (port, 9 )
684+ endif
685+ let g: arduino_serial_port = port
686+ endif
687+ endif
688+ return
689+ endif
690+ let next_dir = fnamemodify (dir , ' :h' )
691+ if next_dir == dir
692+ break
693+ else
694+ let dir = next_dir
695+ endif
696+ endwhile
697+ let s: SKETCHFILE = v: null
698+ endfunction
699+
700+ function s: WriteSketchKey (key , value) abort
701+ if s: SKETCHFILE == v: null
702+ return
703+ endif
704+ let data = json_decode (join (readfile (s: SKETCHFILE )))
705+ let cpu = get (data, ' cpu' , {})
706+ let cpu[a: key ] = a: value
707+ call writefile ([json_encode (data)], s: SKETCHFILE )
708+ endfunction
709+
629710function ! s: CacheLine (lines , varname) abort
630711 if exists (a: varname )
631712 let value = eval (a: varname )
@@ -675,7 +756,15 @@ function! arduino#GetInfo() abort
675756 if g: arduino_use_cli
676757 echo ' Verify command: ' . arduino#GetCLICompileCommand ()
677758 else
678- echo " Verify command: " . arduino#GetArduinoCommand (" --verify" )
759+ echo ' Verify command: ' . arduino#GetArduinoCommand (' --verify' )
760+ endif
761+ endfunction
762+
763+ function ! s: notify (msg) abort
764+ if has (' nvim' )
765+ call luaeval (' vim.notify(_A)' , a: msg )
766+ else
767+ echo a: msg
679768 endif
680769endfunction
681770
0 commit comments