Skip to content

Commit

Permalink
Mapping and commands available only for supported filetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaldot committed Nov 9, 2024
1 parent 1240728 commit 22d4fd1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 84 deletions.
1 change: 1 addition & 0 deletions ftplugin/julia.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vim9script

import autoload "../lib/highlight.vim"
import "../lib/ftcommands_mappings.vim"

# The following variable won't change during run-time
b:kernel_name = g:replica_kernels[&filetype]
Expand Down
1 change: 1 addition & 0 deletions ftplugin/python.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vim9script

import autoload "../lib/highlight.vim"
import "../lib/ftcommands_mappings.vim"

# The following variable won't change during run-time
b:kernel_name = g:replica_kernels[&filetype]
Expand Down
80 changes: 80 additions & 0 deletions lib/ftcommands_mappings.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
vim9script

import autoload "../lib/repl.vim"

# ------------
# Mappings
# ------------
# TODO: make imap and tmap to work.
noremap <unique> <script> <Plug>ReplicaConsoleToggle
\ <ScriptCmd>repl.ConsoleToggle()<cr>
tnoremap <unique> <script> <Plug>ReplicaConsoleToggle
\ <ScriptCmd>repl.ConsoleToggle()<cr>
noremap <unique> <script> <Plug>ReplicaSendLines
\ <ScriptCmd>repl.SendLines(line('.'), line('.'))<cr>
noremap <unique> <script> <Plug>ReplicaSendFile
\ <ScriptCmd>repl.SendFile()<cr>
noremap <unique> <script> <Plug>ReplicaSendCell
\ <ScriptCmd>repl.SendCell()<cr>
if g:replica_use_default_mapping == true
if !hasmapto('<Plug>ReplicaConsoleToggle') || empty(mapcheck("<F2>", "nt"))
nnoremap <silent> <F2> <Plug>ReplicaConsoleToggle
# imap <silent> <F2> <Plug>ReplicaConsoleToggle<cr>
tnoremap <F2> <silent> <c-w><Plug>ReplicaConsoleToggle
endif

if !hasmapto('<Plug>ReplicaSendLines') || empty(mapcheck("<F9>", "nx"))
nnoremap <silent> <unique> <F9> <Plug>ReplicaSendLines
# imap <silent> <unique> <F9> <Plug>ReplicaSendLines<cr>
xnoremap <silent> <unique> <F9> :ReplicaSendLines<cr>j
endif

if !hasmapto('<Plug>ReplicaSendFile') || empty(mapcheck("<F5>", "n"))
nnoremap <silent> <F5> <Plug>ReplicaSendFile
# imap <silent> <F5> <Plug>ReplicaSendFile<cr>
endif

if !hasmapto('<Plug>ReplicaSendCell') || empty(mapcheck("<c-enter>", "n"))
nnoremap <silent> <c-enter> <Plug>ReplicaSendCell
# imap <silent> <c-enter> <Plug>ReplicaSendCell<cr>j
endif
endif


# -----------------------------
# Commands
# -----------------------------
if !exists(":ReplicaConsoleToggle")
command -buffer ReplicaConsoleToggle silent repl.ConsoleToggle()
endif

if !exists(":ReplicaConsoleRestart" )
command -buffer ReplicaConsoleRestart silent repl.ConsoleShutoff() |
\ repl.ConsoleToggle()
endif

if !exists(":ReplicaConsoleShutoff")
command -buffer ReplicaConsoleShutoff repl.ConsoleShutoff()
endif

if !exists(":ReplicaSendLines")
command -buffer -range ReplicaSendLines
\ silent repl.SendLines(<line1>, <line2>)
endif

if !exists(":ReplicaSendCell")
command -buffer ReplicaSendCell silent repl.SendCell()
endif

if !exists(":ReplicaSendFile")
command -buffer -nargs=? -complete=file ReplicaSendFile
\ silent repl.SendFile(<f-args>)
endif

if !exists(":ReplicaRemoveCells")
command -buffer ReplicaRemoveCells repl.RemoveCells()
endif
18 changes: 13 additions & 5 deletions lib/repl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ var console_geometry = {}
# Functions for dealing with the console
# ---------------------------------------

def Echoerr(msg: string)
echohl ErrorMsg | echom $'[vim-replica]: {msg}' | echohl None
enddef

def Echowarn(msg: string)
echohl WarningMsg | echom $'[vim-replica]: {msg}' | echohl None
enddef

def Init()
if !exists('g:replica_console_position')
g:replica_console_position = "L"
elseif index(["H", "J", "K", "L"], g:replica_console_position) == -1
echoerr "g:replica_console_position must be one of HJKL"
Echoerr("'g:replica_console_position' must be one of 'HJKL'")
endif

if !exists('g:replica_console_width')
Expand Down Expand Up @@ -162,7 +170,7 @@ export def RemoveCells()
echo "Cells removed."

else
echo "vim-replica: filetype not supported!"
Echowarn("filetype not supported!")
endif
enddef

Expand All @@ -182,7 +190,7 @@ export def SendLines(firstline: number, lastline: number)
# TODO: avoid the following when firstline and lastline are passed
norm! j^
else
echo "vim-replica: filetype not supported!"
Echowarn("filetype not supported!")
endif
enddef

Expand All @@ -205,7 +213,7 @@ export def SendCell()
term_sendkeys(bufnr('^' .. b:console_name .. '$'),
\ b:run_command .. "\n")
else
echo "vim-replica: filetype not supported!"
Echowarn("filetype not supported!")
endif
enddef

Expand All @@ -229,7 +237,7 @@ export def SendFile(...filename: list<string>)
term_sendkeys(bufnr('^' .. b:console_name .. '$'),
\ b:run_command .. "\n")
else
echo "vim-replica: filetype not supported!"
Echowarn("filetype not supported!")
endif

# Remove temp buffer
Expand Down
79 changes: 0 additions & 79 deletions plugin/replica.vim
Original file line number Diff line number Diff line change
Expand Up @@ -97,85 +97,6 @@ g:replica_run_commands = replica_run_commands_default
# \ "python": "source ~/pippo && ",
# \ "julia": ""}

# -----------------------------
# Default mappings
# -----------------------------
#
import autoload "../lib/repl.vim"

# TODO: make imap and tmap to work.
noremap <unique> <script> <Plug>ReplicaConsoleToggle
\ <ScriptCmd>repl.ConsoleToggle()<cr>
tnoremap <unique> <script> <Plug>ReplicaConsoleToggle
\ <ScriptCmd>repl.ConsoleToggle()<cr>
noremap <unique> <script> <Plug>ReplicaSendLines
\ <ScriptCmd>repl.SendLines(line('.'), line('.'))<cr>
noremap <unique> <script> <Plug>ReplicaSendFile
\ <ScriptCmd>repl.SendFile()<cr>
noremap <unique> <script> <Plug>ReplicaSendCell
\ <ScriptCmd>repl.SendCell()<cr>
if g:replica_use_default_mapping == true
if !hasmapto('<Plug>ReplicaConsoleToggle') || empty(mapcheck("<F2>", "nt"))
nnoremap <silent> <F2> <Plug>ReplicaConsoleToggle
# imap <silent> <F2> <Plug>ReplicaConsoleToggle<cr>
tnoremap <F2> <silent> <c-w><Plug>ReplicaConsoleToggle
endif

if !hasmapto('<Plug>ReplicaSendLines') || empty(mapcheck("<F9>", "nx"))
nnoremap <silent> <unique> <F9> <Plug>ReplicaSendLines
# imap <silent> <unique> <F9> <Plug>ReplicaSendLines<cr>
xnoremap <silent> <unique> <F9> :ReplicaSendLines<cr>j
endif

if !hasmapto('<Plug>ReplicaSendFile') || empty(mapcheck("<F5>", "n"))
nnoremap <silent> <F5> <Plug>ReplicaSendFile
# imap <silent> <F5> <Plug>ReplicaSendFile<cr>
endif

if !hasmapto('<Plug>ReplicaSendCell') || empty(mapcheck("<c-enter>", "n"))
nnoremap <silent> <c-enter> <Plug>ReplicaSendCell
# imap <silent> <c-enter> <Plug>ReplicaSendCell<cr>j
endif
endif


# -----------------------------
# Commands
# -----------------------------
if !exists(":ReplicaConsoleToggle")
command ReplicaConsoleToggle silent repl.ConsoleToggle()
endif

if !exists(":ReplicaConsoleRestart" )
command ReplicaConsoleRestart silent repl.ConsoleShutoff() |
\ repl.ConsoleToggle()
endif

if !exists(":ReplicaConsoleShutoff")
command ReplicaConsoleShutoff repl.ConsoleShutoff()
endif

if !exists(":ReplicaSendLines")
command -range ReplicaSendLines
\ silent repl.SendLines(<line1>, <line2>)
endif

if !exists(":ReplicaSendCell")
command ReplicaSendCell silent repl.SendCell()
endif

if !exists(":ReplicaSendFile")
command -nargs=? -complete=file ReplicaSendFile
\ silent repl.SendFile(<f-args>)
endif

if !exists(":ReplicaRemoveCells")
command ReplicaRemoveCells repl.RemoveCells()
endif

augroup delete_tmp_file
autocmd VimLeave * delete(g:replica_tmp_filename)
Expand Down

0 comments on commit 22d4fd1

Please sign in to comment.