2
2
" FILE: vimshell.vim
3
3
" AUTHOR: Janakiraman .S <[email protected] >(Original)
4
4
" Shougo Matsushita <[email protected] >(Modified)
5
- " Last Modified: 05 Oct 2009
5
+ " Last Modified: 12 Oct 2009
6
6
" Usage: Just source this file.
7
7
" License: MIT license {{{
8
8
" Permission is hereby granted, free of charge, to any person obtaining
27
27
" Version: 5.35, for Vim 7.0
28
28
" =============================================================================
29
29
30
+ if ! exists (' g:VimShell_Prompt' )
31
+ let s: prompt = ' vimshell% '
32
+ else
33
+ let s: prompt = g: VimShell_Prompt
34
+ endif
35
+ if ! exists (' g:VimShell_SecondaryPrompt' )
36
+ let s: secondary_prompt = ' %% '
37
+ else
38
+ let s: secondary_prompt = g: VimShell_SecondaryPrompt
39
+ endif
30
40
if ! exists (' g:VimShell_UserPrompt' )
31
41
let s: user_prompt = ' '
32
42
else
33
43
let s: user_prompt = g: VimShell_UserPrompt
34
44
endif
35
45
46
+ if ! exists (' g:VimShell_ExecuteFileList' )
47
+ let g: VimShell_ExecuteFileList = {}
48
+ endif
49
+
36
50
" Helper functions.
37
51
function ! vimshell#set_execute_file (exts, program)" {{{
38
52
for ext in split (a: exts , ' ,' )
@@ -196,12 +210,12 @@ function! vimshell#execute_command(program, args, fd, other_info)"{{{
196
210
return 0
197
211
endfunction " }}}
198
212
function ! vimshell#process_enter ()" {{{
199
- if getline (' .' ) !~ vimshell#escape_match (g: VimShell_Prompt )
213
+ if getline (' .' ) !~ vimshell#escape_match (s: prompt )
200
214
" Prompt not found
201
215
202
- if match (getline (' $' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
216
+ if match (getline (' $' ), vimshell#escape_match (s: prompt )) < 0
203
217
" Create prompt line.
204
- call append (line (' $' ), g: VimShell_Prompt )
218
+ call append (line (' $' ), s: prompt )
205
219
endif
206
220
207
221
" Search cursor file.
@@ -213,15 +227,15 @@ function! vimshell#process_enter()"{{{
213
227
normal ! G$
214
228
" Execute cursor file.
215
229
if isdirectory (l: filename )
216
- call setline (line (' $' ), g: VimShell_Prompt . l: filename )
230
+ call setline (line (' $' ), s: prompt . l: filename )
217
231
else
218
- call setline (line (' $' ), g: VimShell_Prompt . ' vim ' . l: filename )
232
+ call setline (line (' $' ), s: prompt . ' vim ' . l: filename )
219
233
endif
220
234
endif
221
235
222
236
if line (' .' ) != line (' $' )
223
237
" History execution.
224
- if match (getline (' $' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
238
+ if match (getline (' $' ), vimshell#escape_match (s: prompt )) < 0
225
239
" Insert prompt line.
226
240
call append (line (' $' ), getline (' .' ))
227
241
else
@@ -236,7 +250,7 @@ function! vimshell#process_enter()"{{{
236
250
endif
237
251
238
252
" Delete prompt string.
239
- let l: line = substitute (getline (' .' ), ' ^' . g: VimShell_Prompt , ' ' , ' ' )
253
+ let l: line = substitute (getline (' .' ), ' ^' . s: prompt , ' ' , ' ' )
240
254
241
255
" Delete comment.
242
256
let l: line = substitute (l: line , ' #.*$' , ' ' , ' ' )
@@ -254,7 +268,7 @@ function! vimshell#process_enter()"{{{
254
268
let l: line = substitute (l: line , ' ^\s\+' , ' ' , ' ' )
255
269
if l: line = ~ ' ^\s*$'
256
270
if g: VimShell_EnableAutoLs
257
- call setline (line (' .' ), g: VimShell_Prompt . ' ls' )
271
+ call setline (line (' .' ), s: prompt . ' ls' )
258
272
call vimshell#execute_internal_command (' ls' , [], {}, {})
259
273
260
274
call vimshell#print_prompt ()
@@ -263,7 +277,7 @@ function! vimshell#process_enter()"{{{
263
277
call vimshell#start_insert ()
264
278
else
265
279
" Ignore empty command line.
266
- call setline (line (' .' ), g: VimShell_Prompt )
280
+ call setline (line (' .' ), s: prompt )
267
281
268
282
call vimshell#start_insert ()
269
283
endif
@@ -407,7 +421,7 @@ function! vimshell#print_prompt()"{{{
407
421
let b: vimshell_commandline_stack = []
408
422
endif
409
423
if empty (b: vimshell_commandline_stack )
410
- let l: new_prompt = g: VimShell_Prompt
424
+ let l: new_prompt = s: prompt
411
425
else
412
426
let l: new_prompt = b: vimshell_commandline_stack [-1 ]
413
427
call remove (b: vimshell_commandline_stack , -1 )
@@ -495,46 +509,67 @@ endfunction"}}}
495
509
" VimShell key-mappings functions."{{{
496
510
function ! vimshell#push_current_line ()" {{{
497
511
" Check current line.
498
- if match (getline (' .' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
512
+ if match (getline (' .' ), vimshell#escape_match (s: prompt )) < 0
499
513
return
500
514
endif
501
515
502
516
call add (b: vimshell_commandline_stack , getline (' .' ))
503
517
504
518
" Set prompt line.
505
- call setline (line (' .' ), g: VimShell_Prompt )
519
+ call setline (line (' .' ), s: prompt )
506
520
507
521
startinsert !
508
522
endfunction " }}}
523
+ function ! vimshell#push_and_execute (command )" {{{
524
+ " Check current line.
525
+ if match (getline (' .' ), vimshell#escape_match (s: prompt )) < 0
526
+ return
527
+ endif
528
+
529
+ call add (b: vimshell_commandline_stack , getline (' .' ))
530
+
531
+ " Set prompt line.
532
+ call setline (line (' .' ), s: prompt . a: command )
533
+
534
+ call vimshell#process_enter ()
535
+ endfunction " }}}
509
536
510
537
function ! vimshell#previous_prompt ()" {{{
511
- call search (' ^' . vimshell#escape_match (g: VimShell_Prompt ), ' bWe' )
538
+ call search (' ^' . vimshell#escape_match (s: prompt ), ' bWe' )
512
539
endfunction " }}}
513
540
function ! vimshell#next_prompt ()" {{{
514
- call search (' ^' . vimshell#escape_match (g: VimShell_Prompt ), ' We' )
541
+ call search (' ^' . vimshell#escape_match (s: prompt ), ' We' )
515
542
endfunction " }}}
516
- function ! vimshell#switch_shell (split_flag)" {{{
543
+ function ! vimshell#switch_shell (split_flag, directory )" {{{
517
544
if &filetype == ' vimshell'
518
545
if winnr (' $' ) != 1
519
546
close
520
547
else
521
548
buffer #
522
549
endif
523
550
551
+ if a: directory != ' '
552
+ " Change current directory.
553
+ lcd `= fnamemodify (a: directory , ' :p' )`
554
+ call vimshell#print_prompt ()
555
+ endif
556
+ call vimshell#start_insert ()
524
557
return
525
558
endif
526
559
527
560
" Search VimShell window.
528
561
let l: cnt = 1
529
562
while l: cnt <= winnr (' $' )
530
563
if getwinvar (l: cnt , ' &filetype' ) == ' vimshell'
531
- let l: current = getcwd ()
532
564
533
565
execute l: cnt . ' wincmd w'
534
566
535
- " Change current directory.
536
- let b: vimshell_save_dir = l: current
537
- lcd `= fnamemodify (l: current , ' :p' )`
567
+ if a: directory != ' '
568
+ " Change current directory.
569
+ lcd `= fnamemodify (a: directory , ' :p' )`
570
+ call vimshell#print_prompt ()
571
+ endif
572
+ call vimshell#start_insert ()
538
573
return
539
574
endif
540
575
@@ -545,29 +580,30 @@ function! vimshell#switch_shell(split_flag)"{{{
545
580
let l: cnt = 1
546
581
while l: cnt <= bufnr (' $' )
547
582
if getbufvar (l: cnt , ' &filetype' ) == ' vimshell'
548
- let l: current = getcwd ()
549
-
550
583
if a: split_flag
551
584
execute ' sbuffer' . l: cnt
552
585
execute ' resize' . winheight (0 )* g: VimShell_SplitHeight / 100
553
586
else
554
587
execute ' buffer' . l: cnt
555
588
endif
556
589
557
- " Change current directory.
558
- let b: vimshell_save_dir = l: current
559
- lcd `= fnamemodify (l: current , ' :p' )`
590
+ if a: directory != ' '
591
+ " Change current directory.
592
+ lcd `= fnamemodify (a: directory , ' :p' )`
593
+ call vimshell#print_prompt ()
594
+ endif
595
+ call vimshell#start_insert ()
560
596
return
561
597
endif
562
598
563
599
let l: cnt += 1
564
600
endwhile
565
601
566
602
" Create window.
567
- call vimshell#create_shell (a: split_flag )
603
+ call vimshell#create_shell (a: split_flag, a: directory )
568
604
endfunction " }}}
569
605
function ! vimshell#delete_previous_output ()" {{{
570
- let l: prompt = vimshell#escape_match (g: VimShell_Prompt )
606
+ let l: prompt = vimshell#escape_match (s: prompt )
571
607
if s: user_prompt != ' '
572
608
let l: nprompt = ' ^\[%\] '
573
609
else
@@ -609,13 +645,13 @@ function! vimshell#insert_last_word()"{{{
609
645
startinsert !
610
646
endfunction " }}}
611
647
function ! vimshell#run_help ()" {{{
612
- if match (getline (' .' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
648
+ if match (getline (' .' ), vimshell#escape_match (s: prompt )) < 0
613
649
startinsert !
614
650
return
615
651
endif
616
652
617
653
" Delete prompt string.
618
- let l: line = substitute (getline (' .' ), ' ^' . vimshell#escape_match (g: VimShell_Prompt ), ' ' , ' ' )
654
+ let l: line = substitute (getline (' .' ), ' ^' . vimshell#escape_match (s: prompt ), ' ' , ' ' )
619
655
if l: line = ~ ' ^\s*$'
620
656
startinsert !
621
657
return
@@ -640,11 +676,11 @@ function! vimshell#run_help()"{{{
640
676
startinsert !
641
677
endfunction " }}}
642
678
function ! vimshell#paste_prompt ()" {{{
643
- if match (getline (' .' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
679
+ if match (getline (' .' ), vimshell#escape_match (s: prompt )) < 0
644
680
return
645
681
endif
646
682
647
- if match (getline (' $' ), vimshell#escape_match (g: VimShell_Prompt )) < 0
683
+ if match (getline (' $' ), vimshell#escape_match (s: prompt )) < 0
648
684
" Insert prompt line.
649
685
call append (line (' $' ), getline (' .' ))
650
686
else
@@ -654,7 +690,7 @@ function! vimshell#paste_prompt()"{{{
654
690
normal ! G
655
691
endfunction " }}}
656
692
function ! vimshell#move_head ()" {{{
657
- call search (vimshell#escape_match (g: VimShell_Prompt ), ' be' , line (' .' ))
693
+ call search (vimshell#escape_match (s: prompt ), ' be' , line (' .' ))
658
694
if col (' .' ) != col (' $' )-1
659
695
normal ! l
660
696
endif
@@ -663,7 +699,7 @@ endfunction"}}}
663
699
function ! vimshell#delete_line ()" {{{
664
700
let l: col = col (' .' )
665
701
let l: mcol = col (' $' )
666
- call setline (line (' .' ), g: VimShell_Prompt . getline (' .' )[l: col :])
702
+ call setline (line (' .' ), s: prompt . getline (' .' )[l: col :])
667
703
call vimshell#move_head ()
668
704
if l: col == l: mcol- 1
669
705
startinsert !
@@ -698,7 +734,7 @@ function! vimshell#clear()"{{{
698
734
endif
699
735
endfunction " }}}
700
736
701
- function ! vimshell#create_shell (split_flag)" {{{
737
+ function ! vimshell#create_shell (split_flag, directory )" {{{
702
738
let l: bufname = ' vimshell'
703
739
let l: cnt = 2
704
740
while bufexists (l: bufname )
@@ -718,8 +754,10 @@ function! vimshell#create_shell(split_flag)"{{{
718
754
setlocal bufhidden = hide
719
755
let &l: omnifunc = ' vimshell#complete#history_complete'
720
756
721
- " Save current directory.
757
+ " Change current directory.
722
758
let b: vimshell_save_dir = getcwd ()
759
+ let l: current = (a: directory != ' ' )? a: directory : getcwd ()
760
+ lcd `= fnamemodify (l: current , ' :p' )`
723
761
724
762
" Load history.
725
763
if ! filereadable (g: VimShell_HistoryPath )
0 commit comments