File: m2.vim.plugin

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (132 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
" Author:       David Cook II <dcook@ms.uky.edu>
" Version:      0.1
" License:      Public Domain, 2010
" Description:  This plugin was developed to allow easier access to Macaulay 2 from within VIM.
" Caveat:       Many constants are hard-coded; to be fixed later.

" Author:       Manoj Kummini <mkummini@cmi.ac.in> 
" Description:  Modification to run on Apples, with Terminal.app
" Screen Session name
"let b:screens = printf("vim-M2-plugin-%s", localtime())
let b:screens ="vim-M2-plugin"
" Terminal geometry (WxH+X+Y)
let b:termgeom="80x25+0-0"

" Add Commands
command -nargs=0 -complete=shellcmd M2Start :call M2Start()
command -nargs=0 -complete=shellcmd M2Restart :call M2Restart()
command -nargs=0 -complete=shellcmd M2Exit :call M2Exit()
command -nargs=1 -complete=shellcmd M2SendString :call M2SendString(<args>)
command -nargs=0 -complete=shellcmd M2SendBuffer :call M2SendBuffer()
command -nargs=0 -range -complete=shellcmd M2Send :call M2Send(<line1>,<line2>)
    
" Check for all required executables
if !executable('screen')
    call M2Warning("Please install 'screen' to run vim-M2-plugin")
    sleep 2
    finish
elseif !executable('M2')
    call M2Warning("Please install 'M2' to run vim-M2-plugin")
    sleep 2
    finish
endif

" Output a meaningful warning.
function! M2Warning(msg)
    echohl WarningMsg
    echo a:msg
    echohl Normal
endfunction

" Load M2 in a new terminal, named with b:screens
function! M2Start()
    let cwd = getcwd() 
    let cmd = printf("/usr/bin/osascript ~/local/Applications/VimM2.scpt %s %s" , cwd, b:screens)
    let log = system(cmd)
    if v:shell_error
        call M2Warning(log)
        return
    endif
endfunction

" Restart M2
function! M2Restart()
    call M2SendString('restart')
endfunction

" Exit M2
function! M2Exit()
    call M2SendString('exit')
endfunction

" Send a string command to M2
function! M2SendString(str)
    " fix single quotes and then carets
    let sstr = substitute(a:str, "'", "'\\\\''", "g")
    let sstr = substitute(sstr, '\^', '\\\^', "g")
    let sstr = substitute(sstr, '\"', '\\\"', "g")
    " send the command: notice \015 is just newline
    let cmd = printf("screen -S %s -X eval 'stuff \"%s\015\"'", b:screens, sstr)
    let log = system(cmd)
    if v:shell_error
        call M2Warning(log)
    endif
endfunction

" Send the current buffer to M2
function! M2SendBuffer()
    call M2Send("1", "$")
endfunction

" Fancy-schmancy general send
" Accepts inputs of either string, list<string>, or line1,line2
" Modified from screen.vim
function! M2Send(...)
    " parse inputs
    if a:0 == 0
        let lines = getline(".")
    elseif a:0 == 1
        let ta1 = type(a:1)
        if ta1 == 1
            " strings: break up on newlines
            let lines = split(a:1, "\n")
        elseif ta1 == 3
            " lists: take-as-is
            let lines = a:1
        else
            call M2Warning('vim-M2-plugin: Argument must be a string or a list.')
            return
        endif
    elseif a:0 == 2
        if type(a:1) <= 1 && type(a:2) <= 1
            " integers/strings
            let lines = getline(a:1, a:2)
            let mode = visualmode(1)
            if mode != '' && line("'<") == a:1
                if mode == "v"
                    let start = col("'<") - 1
                    let end = col("'>") - 1
                    " slice in end before start in case the selection is only one line
                    let lines[-1] = lines[-1][: end]
                    let lines[0] = lines[0][start :]
                elseif mode == "\<c-v>"
                    let start = col("'<")
                    if col("'>") < start
                        let start = col("'>")
                    endif
                    let start = start - 1
                    call map(lines, 'v:val[start :]')
                endif
            endif
        else
            call M2Warning('vim-M2-plugin: Arguments must be a pair of strings/integers.')
            return
        endif
    else
        call M2Warning('vim-M2-plugin: Invalid number of arguments.')
    endif
    " send them on!
    for lin in lines
        call M2SendString(lin)
    endfor
endfunction