forked from vim-scripts/VimIRC.vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimirc.vim
6533 lines (5764 loc) · 140 KB
/
vimirc.vim
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" An IRC client plugin for Vim
" Maintainer: Madoka Machitani <madokam@zag.att.ne.jp>
" Created: Tue, 24 Feb 2004
" Last Change: Mon, 10 Jan 2005 17:29:23 +0900 (JST)
" License: Distributed under the same terms as Vim itself
"
" Credits:
" ircII the basics of IRC
" X-Chat ideas for DCC implementation specifically
" ERC ideas for netsplit handling, auto-away feature etc.
" KoRoN creator of a BBS viewer for Vim, Chalice (very popular
" among Japanese vim community)
" Ilya Sher an idea for mini-buffers for cmdline editing
" morbuz pointing out the error "E28: No such highlight group"
" with s:Hilite* functions
" alexander pointing out the excessive CPU-time consumption,
" feature of multiple server-connection on startup, etc
"
" Features:
" * real-time message receiving with user interaction (many of the normal
" mode commands available)
" * multiple server/channel connectivity
" * DCC SEND/CHAT functionalities
" * logging
" * command aliasing
" * auto-join channels on logon
" * auto-reconnect/rejoin after disconnect
" * auto-away
" * netsplit detection
"
" A Drawback:
" VimIRC achieves real-time message reception by implementing its own main
" loop. Therefore, while you are out of it, VimIRC has no way to get new
" messages.
"
" So my recommendation is, use a shortcut which creates a VimIRC-dedicated
" instance of Vim, where you do not initiate normal editing sessions.
"
" Requirements:
" * Vim 6.2 or later with perl interface enabled
" * Perl 5.6 or later (5.8 or later if you want multibyte feature)
"
" Options:
"
" Basic settings:
" let g:vimirc_nick ="nickname"
" let g:vimirc_user ="username"
" let g:vimirc_realname ="full name"
" let g:vimirc_server ="irc.foobar.com:6667"
" Your favorite IRC server
" (default: irc.freenode.net)
" let g:vimirc_umode ="user modes" set upon logon (e.g.: "+i")
"
" Misc.:
"
" let g:vimirc_autojoin ="#chan1,#chan2"
" ="#chan1|#chan2@irc.foo.com,#chan3@irc.bar.com"
"
" List of channels to join upon logon
"
" let g:vimirc_nickpass ="pass@irc.foo.com"
" ="nick:pass@irc.foo.com"
" ="nick1:pass1|nick2:pass2@irc.foo.com"
"
" List of passwords to identify yourself to
" NickServ.
"
" The first form can be used if you are using
" the same pass for different nicks, or you
" have only one nick registered.
"
" Use commas to separate settings for each
" server. (Only irc.freenode.net is
" supported currently)
"
" let g:vimirc_log =NUMBER
" Set this to non-zero to enable logging
" feature. (default: zero)
" Logs will be taken for each channel and
" server independently.
"
" Since server buffers are usually not
" interesting (is it?), they'll be logged only
" if NUMBER is greater than 1.
" let g:vimirc_logdir ="where log files are saved"
" (default: ~/.vimirc)
" The specified directory will be created
" automatically.
"
" let g:vimirc_partmsg ="message sent with QUIT/PART"
"
" let g:vimirc_winmode ="single" is the only valid value.
" (default: multi-windows mode)
"
" Auto-away feature:
" let g:vimirc_autoaway =NUMBER
" Set to non-zero to enable the feature.
" (default: zero)
" let g:vimirc_autoawaytime =NUMBER
" Threshold time in seconds after which you
" will be marked as `away'
" (default: 1800 (30 minutes))
"
" DCC-related:
" let g:vimirc_dccdir ="where files are downloaded"
" (default: ~/.vimirc/dcc)
" let g:vimirc_dccport =NUMBER
" Port-number to watch at when you set up
" a dcc server. maybe necessary to set if you
" are behind firewall or something.
"
" Runtime Options:
" You can pass following options to the command :VimIRC, overriding the
" vim variables above
"
" -n nickname
" -u username
" -s server:port
"
" Long option(s):
"
" --realname="full name"
"
" Startup:
" Type
" :VimIRC<CR>
"
" You will be prompted for several user information (nick etc.) if you have
" not set options listed above.
"
" Usage:
"
" Normal mode: This is a pseudo normal mode. Try your favorite Vim normal
" mode commands as usual.
"
" Typing "i" or "I" will let you in the `command mode'
" described below, just as you do in Vim to go insert mode.
"
" ":", "/" and "?" keys will prompt you to enter ex-commands
" or search strings as usual.
"
" Hit <Ctrl-C> to get out of control and freely move around
" or do ex commands. Hit <Space> to re-enter the normal
" (online) mode again.
"
" Special cases:
"
" * In a channels list window (which should open up with /list
" command), you can type "o" to sort the list, "O" to
" reverse it (I took these mappings from Mutt the e-mail
" client).
" Hitting <CR> will prompt you whether to join the channel
" where the cursor is.
"
" * In a nicks window, you can hit <CR> to choose an action to
" take against the one whom the cursor is on.
"
" * <C-N> / <C-P> keys are available in all buffers, mapped to
" cycle forward/backward through channel/server buffers.
" Useful in single-window mode.
"
" Command mode: This is just a normal buffer opened (at the bottom of the
" screen|below the current window). Enter IRC commands here.
" Hitting <CR>, both in insert and normal mode, will send out
" the cursor line instantly either as a command or a message.
"
" Every IRC command starts with "/". E.g.: /join #vim,#c
"
" Line without a leading slash will be sent as a message,
" normaly to the current channel.
"
" Type /help<CR> to see the list of available commands. It's
" far from complete, though.
"
" Quit:
" Type
" /quit<CR>
" in the IRC command line to disconnect with the current server.
"
" Or to totally exit from the script, type
" VimIRCQuit<CR>
" on the VIM command line.
"
" TODOs:
" * multibyte support (done? I don't think so)
" * authentication (just add one line to send PASS)
" * flood protection
" * handling of channel-mode changes
" * IPv6
" * SSL
" * nicks auto-identification (done for freenode)
" * command-line completion (with tab key)
" * handling of control characters (bold, underline etc.)
" * scripting (?)
" * help (both /help command and local help file)
" * menus (I personally never use menus)
" * etc. etc.
"
" Done:
" - command-line history (?)
" - separate listing of channels with sorting facilities
" - auto reconnect/rejoin
" - ctcp, including dcc stuffs (well, mostly)
" - timer (it hasn't been in todo list though)
" - auto-away
" - logging
" - netsplit detection (?)
" - command abbreviation (aliasing)
"
" Tips:
" 1. If you see extreme slowness in vim startup time due to this plugin,
" put "let loaded_vimirc=1" in your .vimrc to avoid loading this in
" normal editing sessions. Create a VimIRC-dedicated rc file (.vimircrc
" or something) and put necessary settings into it. Then set up an
" alias (shortcut) which runs VimIRC, specifying the rc file you
" created. Like this:
" alias irc='gvim -i NONE -u ~/.vimircrc -c VimIRC'
"
" 2. In single-window mode, you'll see buffer numbers appear next to the
" server/channel names in the `info-bar'. With them, you can easily
" navigate servers/channels like this:
" :sb1
if exists('g:loaded_vimirc') || &compatible
finish
endif
" Set this to zero when releasing, which I'll occasionally forget, for sure
let s:debug = 0
if !s:debug
let g:loaded_vimirc = 1
endif
let s:save_cpoptions = &cpoptions
set cpoptions&
let s:version = '0.8.8'
let s:client = 'VimIRC '.s:version
"
" Developing functions
"
"
" Start/Exit
"
if !exists(':VimIRC') || s:debug
command -nargs=* VimIRC :call s:StartVimIRC(<q-args>)
endif
function! s:ObtainUserInfo(args)
" Maybe called more than once
let retval = !strlen(a:args) && (exists('s:nick') && exists('s:user')
\ && exists('s:realname'))
if !retval
let s:nick = s:StrMatched(a:args, '-n\s*\(\S\+\)', '\1')
if !strlen(s:nick)
let s:nick = s:GetVimVar('g:vimirc_nick')
if !strlen(s:nick)
let s:nick = s:GetEnv('$IRCNICK')
if !strlen(s:nick)
let s:nick = s:Input('Enter your nickname')
endif
endif
endif
let s:user = s:StrMatched(a:args, '-u\s*\(\S\+\)', '\1')
if !strlen(s:user)
let s:user = s:GetVimVar('g:vimirc_user')
if !strlen(s:user)
let s:user = s:GetEnv('$USER')
if !strlen(s:user)
let s:user = s:Input('Enter your username')
endif
endif
endif
let s:realname = s:StrMatched(a:args,
\"--real\\%(name\\)\\==\\(['\"]\\)\\(.\\{-\\}\\)\\1", '\2')
if !strlen(s:realname)
let s:realname = s:GetVimVar('g:vimirc_realname')
if !strlen(s:realname)
let s:realname = s:GetEnv('$NAME')
if !strlen(s:realname)
let s:realname = s:GetEnv('$IRCNAME')
if !strlen(s:realname)
let s:realname = s:Input('Enter your full name')
endif
endif
endif
endif
let s:umode = s:StrMatched(a:args, '-m\s*\(\S\+\)', '\1')
if !strlen(s:umode)
let s:umode = s:GetVimVar('g:vimirc_umode')
if !strlen(s:umode)
let s:umode = s:GetEnv('$IRCUMODE')
if !strlen(s:umode)
let s:umode = 0
endif
endif
endif
endif
let retval = (strlen(s:nick) && strlen(s:user) && strlen(s:realname))
if retval
let s:server = s:StrMatched(a:args, '-s\s*\(\S\+\)', '\1')
if !strlen(s:server)
let s:server = s:GetVimVar('g:vimirc_server')
if !strlen(s:server)
let s:server = 'irc.freenode.net:6667'
endif
endif
else
unlet s:nick s:user s:realname s:umode
endif
return retval
endfunction
function! s:InitVars()
if exists('s:sid') " already inited
return
endif
map <SID>xx <SID>xx
let s:sid = substitute(maparg('<SID>xx'), 'xx$', '', '')
unmap <SID>xx
let s:bufname_prefix = '_VimIRC_'
let s:bufname_info = s:bufname_prefix.'INFO_'
let s:bufname_server = s:bufname_prefix.'SERVER_'
let s:bufname_list = s:bufname_prefix.'LIST_'
let s:bufname_channel = s:bufname_prefix.'CHANNEL_'
let s:bufname_nicks = s:bufname_prefix.'NICKS_'
let s:bufname_command = s:bufname_prefix.'COMMAND_'
let s:bufname_chat = s:bufname_prefix.'CHAT_'
let s:autocmd_disable = 0
" When timer was last triggered
let s:lasttime = localtime()
" When user did some action most recently
let s:lastactive = s:lasttime
" User options below
" Favorite farewell message
call s:SetVarIntern('vimirc_partmsg', (s:debug ? 'Testing ' : '').
\s:client.' (IRC client for Vim)')
" Prepend a leading colon
let s:partmsg = substitute(s:partmsg, '^[^:]', ':&', '')
" Preferred language. Encoding name which Perl's Encode module can accept
call s:SetVarIntern('vimirc_preflang')
" On/off logging feature
call s:SetVarIntern('vimirc_log', 0)
" Log directory
call s:SetVarIntern('vimirc_logdir', expand('$HOME').'/.vimirc')
let s:logdir = s:RegularizePath(s:logdir)
call s:SetVarIntern('vimirc_rcfile', s:logdir.'/.vimircrc')
" Directory where incoming dcc files should go
call s:SetVarIntern('vimirc_dccdir', s:logdir.'/dcc')
let s:dccdir = s:RegularizePath(s:dccdir)
" Preferred port you want to listen to
call s:SetVarIntern('vimirc_dccport')
" Window-related
" Window mode
call s:SetVarIntern('vimirc_winmode')
let s:singlewin = (s:winmode ==? 'single')
call s:SetVarIntern('vimirc_infowidth')
if s:infowidth <= 1
let s:infowidth = 20
endif
" On/off auto-away feature
call s:SetVarIntern('vimirc_autoaway', 0)
" Threshold time to be set `away'. MUST be in seconds
call s:SetVarIntern('vimirc_autoawaytime')
if (s:autoawaytime + 0) <= 0
let s:autoawaytime = 60 * 30
endif
endfunction
" Internalize user variables (for safety)
function! s:SetVarIntern(var, ...)
let var = substitute(a:var, '^vimirc_', '', '')
let s:{var} = s:GetVimVar('g:'.a:var)
if !strlen(s:{var}) && a:0
let s:{var} = a:1
endif
unlet! g:{a:var}
endfunction
function! s:SetGlobVars()
let s:eadirection = &eadirection
set eadirection=ver
let s:equalalways = &equalalways
set equalalways
let s:lazyredraw = &lazyredraw
"set nolazyredraw
set lazyredraw
let s:showbreak = &showbreak
let &showbreak = ' '
let s:statusline = &statusline
let &statusline = '%{'.s:sid.'GetStatus()}%=%l/%L'
let s:titlestring = &titlestring
let s:winminheight = &winminheight
set winminheight=1
let s:winwidth = &winwidth
set winwidth=12
endfunction
function! s:ResetGlobVars()
let &eadirection = s:eadirection
let &equalalways = s:equalalways
let &lazyredraw = s:lazyredraw
let &showbreak = s:showbreak
let &statusline = s:statusline
let &titlestring = s:titlestring
let &winminheight = s:winminheight
let &winwidth = s:winwidth
endfunction
function! s:StartVimIRC(...)
if !has('perl')
echoerr 'To use this, you have to build vim with perl interface. Exiting.'
return
endif
if !exists('s:opened')
let s:opened = 0
endif
if s:opened
return
endif
call s:InitVars()
if !s:ObtainUserInfo(a:0 ? a:1 : '')
return
endif
call s:RC_Load()
call s:SetGlobVars()
call s:SetCmds()
call s:SetAutocmds()
call s:SetEncoding()
call s:PerlIRC()
call s:StartServer(s:server)
let s:opened = 1
call s:MainLoop()
endfunction
function! s:QuitVimIRC()
call s:ResetCmds()
call s:ResetAutocmds()
call s:ResetGlobVars()
call s:Send_GQUIT('QUIT', '')
call s:ResetPerlVars()
call s:CloseVimIRC()
let s:opened = 0
endfunction
function! s:SetCmds()
delcommand VimIRC
command! VimIRCQuit :call s:QuitVimIRC()
endfunction
function! s:ResetCmds()
delcommand VimIRCQuit
command! -nargs=* VimIRC :call s:StartVimIRC()
endfunction
function! s:SetAutocmds()
augroup VimIRC
autocmd!
" NOTE: Cannot use CursorHold to auto re-enter the loop: getchar() won't
" get a char since key inputs will never be waited after that event.
execute 'autocmd CursorHold' s:bufname_prefix.'* call s:OfflineMsg()'
execute 'autocmd BufDelete' s:bufname_channel.'* call s:DelChanServ()'
execute 'autocmd BufDelete' s:bufname_chat.'* call s:DelChanServ()'
execute 'autocmd BufDelete' s:bufname_server.'* call s:DelChanServ()'
execute 'autocmd BufHidden' s:bufname_channel.'* call s:PreCloseBuf_Channel()'
execute 'autocmd BufHidden' s:bufname_chat.'* call s:PreCloseBuf_Chat()'
execute 'autocmd BufHidden' s:bufname_server.'* call s:PreCloseBuf_Server()'
execute 'autocmd BufWinEnter' s:bufname_channel.'* call s:PostOpenBuf_Channel()'
execute 'autocmd BufWinEnter' s:bufname_chat.'* call s:PostOpenBuf_Chat()'
execute 'autocmd BufWinEnter' s:bufname_server.'* call s:PostOpenBuf_Server()'
augroup END
endfunction
function! s:ResetAutocmds()
autocmd! VimIRC
endfunction
function! s:MainLoop()
" NOTE: Take care of the recursion, it may cause some obscure troubles
if !(s:GetVimVar('s:opened') && s:IsSockOpen())
return
endif
" Clearing the vim command line
echo ""
while 1
try
let key = getchar(0)
if ''.key != '0'
call s:HandleKey(key)
" Giving precedence to key-handling (?)
continue
endif
if s:DoTimer(!s:RecvData())
break
endif
catch /^IMGONNA/
" Get out of the loop
" NOTE: You cannot see new messages posted while posting
break
catch /^Vim:Interrupt$/
match none
if 1 && s:IsBufCommand()
startinsert
endif
break
endtry
endwhile
endfunction
"
" .vimircrc manipulation
"
function! s:RC_Load()
if filereadable(s:rcfile)
execute 'source' s:rcfile
endif
endfunction
function! s:RC_Open(force)
return (a:force || filereadable(s:rcfile)) && s:OpenBuf('1split', s:rcfile)
endfunction
function! s:RC_Close()
if s:SelectWindow(bufnr(s:rcfile)) >= 0
if &modified
silent! write!
endif
silent! close!
endif
endfunction
function! s:RC_Varname(type, name)
return 'vimirc_'.a:type.'_'.(a:name =~ '[^_[:alnum:]]'
\ ? '{"'.s:EscapeQuote(a:name).'"}' : a:name)
endfunction
function! s:RC_Section(section)
if !search('^" '.a:section, 'w')
if strlen(getline('$'))
call append('$', '')
endif
call append('$', '" '.a:section)
$
endif
endfunction
function! s:RC_Set(type, name, value)
call s:RC_Unset(a:type, a:name)
let varname = s:RC_Varname(a:type, a:name)
call append('.', 'let '.varname.' = "'.s:EscapeQuote(a:value).'"')
let g:{varname} = a:value
endfunction
function! s:RC_Unset(type, name)
let varname = s:RC_Varname(a:type, a:name)
while search('\m'.s:EscapeMagic(varname).'\s*=', 'w')
delete _
endwhile
unlet! g:{varname}
endfunction
"
" Buffer manipulation
"
" I'm using buffer numbers to access buffers: accessing by name will soon fail
" if user changes directory or something.
" NOTE: I removed the `server' argument from the functions below, just for
" ease of typing (esp. on the perl's side).
function! s:GetBufNum(bufname)
let bufnum = -1
let varname = 's:bufnum_'.a:bufname
if exists('{varname}')
if bufloaded({varname})
let bufnum = {varname}
else
unlet {varname}
endif
endif
return bufnum
endfunction
function! s:GetBufNum_Info()
return s:GetBufNum(s:GenBufName_Info())
endfunction
function! s:GetBufNum_Server(...)
return s:GetBufNum(s:GenBufName_Server(a:0 ? a:1 : s:server))
endfunction
function! s:GetBufNum_List(...)
return s:GetBufNum(s:GenBufName_List(a:0 ? a:1 : s:server))
endfunction
function! s:GetBufNum_Channel(channel, ...)
return s:GetBufNum(s:GenBufName_Channel((a:0 ? a:1 : s:server), a:channel))
endfunction
function! s:GetBufNum_Nicks(channel, ...)
return s:GetBufNum(s:GenBufName_Nicks((a:0 ? a:1 : s:server), a:channel))
endfunction
function! s:GetBufNum_Command(channel, ...)
return s:GetBufNum(s:GenBufName_Command((a:0 ? a:1 : s:server), a:channel))
endfunction
function! s:GetBufNum_Chat(nick, server)
return s:GetBufNum(s:GenBufName_Chat(a:server, a:nick))
endfunction
function! s:SetBufNum(bufname, bufnum)
let s:bufnum_{a:bufname} = a:bufnum
endfunction
function! s:DeleteBufNum(bufnum)
unlet! s:bufnum_{bufname(a:bufnum)}
endfunction
function! s:GenBufName_Info()
return s:bufname_info
endfunction
function! s:GenBufName_Server(server)
return s:bufname_server.a:server
endfunction
function! s:GenBufName_List(server)
return s:bufname_list.a:server
endfunction
function! s:GenBufName_Channel(server, channel)
return s:bufname_channel.s:SecureChannel(a:channel).'@'.a:server
endfunction
function! s:GenBufName_Nicks(server, channel)
return s:bufname_nicks.s:SecureChannel(a:channel).'@'.a:server
endfunction
function! s:GenBufName_Command(server, channel)
return s:bufname_command.s:SecureChannel(a:channel).'@'.a:server
endfunction
function! s:GenBufName_Chat(server, nick)
return s:bufname_chat.s:EscapeFName(a:nick).'@'.a:server
endfunction
function! s:SecureChannel(channel)
return s:EscapeFName(tolower(a:channel))
endfunction
function! s:IsBufIRC(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_prefix)
\ && exists('b:server')
endfunction
function! s:IsBufInfo(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_info)
endfunction
function! s:IsBufServer(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_server)
endfunction
function! s:IsBufList(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_list)
endfunction
function! s:IsBufChannel(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_channel)
endfunction
function! s:IsBufNicks()
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_nicks)
endfunction
function! s:IsBufCommand(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_command)
endfunction
function! s:IsBufChat(...)
return !match(bufname(a:0 && a:1 ? a:1 : '%'), s:bufname_chat)
endfunction
function! s:IsChannel(channel)
return !match(a:channel, '[&#+!]')
endfunction
function! s:CanOpenChannel(...)
let bufnum = a:0 && a:1 ? a:1 : bufnr('%')
return (s:IsBufChannel(bufnum) || s:IsBufChat(bufnum)
\ || s:IsBufServer(bufnum) || s:IsBufList(bufnum))
endfunction
function! s:VisitBuf_ChanServ()
if !s:CanOpenChannel()
let i = 1
while 1
let bufnum = winbufnr(i)
if bufnum < 0 || s:CanOpenChannel(bufnum)
if bufnum >= 0
call s:SelectWindow(bufnum)
endif
break
endif
let i = i + 1
endwhile
endif
return s:CanOpenChannel()
endfunction
function! s:VisitBuf_Info()
return (s:SelectWindow(s:GetBufNum_Info()) >= 0)
endfunction
function! s:VisitBuf_Server(...)
return (s:SelectWindow(s:GetBufNum_Server(a:0 ? a:1 : s:server)) >= 0)
endfunction
function! s:VisitBuf_List(...)
return (s:SelectWindow(s:GetBufNum_List(a:0 ? a:1 : s:server)) >= 0)
endfunction
function! s:VisitBuf_Channel(channel, ...)
return (s:SelectWindow(s:GetBufNum_Channel(a:channel,
\ (a:0 ? a:1 : s:server))) >= 0)
endfunction
function! s:VisitBuf_Nicks(channel, ...)
return (s:SelectWindow(s:GetBufNum_Nicks(a:channel,
\ (a:0 ? a:1 : s:server))) >= 0)
endfunction
function! s:VisitBuf_Chat(nick, ...)
return (s:SelectWindow(s:GetBufNum_Chat(a:nick, (a:0 ? a:1 : s:server))) >= 0)
endfunction
"
" Opening buffers
"
function! s:OpenBuf(comd, ...)
try
" Avoid "not enough room" error
let winminheight= &winminheight
let winminwidth = &winminwidth
set winminheight=0 winminwidth=0
let v:errmsg = ''
silent! execute a:comd.(a:0 && strlen(a:1) ? ' '.a:1 : '')
setlocal noswapfile modifiable
catch /^Vim:Interrupt$/
finally
let &winminheight = winminheight
let &winminwidth = winminwidth
endtry
return !strlen(v:errmsg)
endfunction
function! s:OpenBuf_Info()
let bufnum = s:GetBufNum_Info()
if !(bufnum >= 0 && s:SelectWindow(bufnum) >= 0)
let comd = 'vertical topleft '.s:infowidth.'split'
if bufnum >= 0
call s:OpenBuf(comd, '+'.bufnum.'buffer')
else
let bufname = s:GenBufName_Info()
call s:OpenBuf(comd, bufname)
call s:InitBuf_Info(bufname)
endif
endif
return 1
endfunction
function! s:OpenBuf_Server()
let bufnum = s:GetBufNum_Server()
let loaded = (bufnum >= 0)
let singlewin = (s:singlewin || !s:opened)
if !(loaded && s:SelectWindow(bufnum) >= 0)
let comd = singlewin ? (loaded ? 'buffer' : 'edit').'!' : 'split'
call s:VisitBuf_ChanServ()
if loaded
if singlewin
call s:OpenBuf(comd, bufnum)
else
call s:OpenBuf(comd, '+'.bufnum.'buffer')
endif
if exists('b:dead')
unlet b:dead
endif
else
let bufname = s:GenBufName_Server(s:server)
call s:OpenBuf(comd, bufname)
call s:InitBuf_Server(bufname)
endif
endif
endfunction
function! s:OpenBuf_List()
let bufnum = s:GetBufNum_List()
let loaded = (bufnum >= 0)
if !(loaded && s:SelectWindow(bufnum) >= 0)
let comd = s:singlewin ? (loaded ? 'buffer' : 'edit') : 'vertical split'
if s:singlewin
call s:VisitBuf_ChanServ()
else
" Open it next to the server window
call s:SelectWindow(s:GetBufNum_Server())
endif
if loaded
if s:singlewin
call s:OpenBuf(comd, bufnum)
else
call s:OpenBuf(comd, '+'.bufnum.'buffer')
endif
else
let bufname = s:GenBufName_List(s:server)
call s:OpenBuf(comd, bufname)
call s:InitBuf_List(bufname)
endif
endif
endfunction
function! s:OpenBuf_Channel(channel)
let bufnum = s:GetBufNum_Channel(a:channel)
let loaded = (bufnum >= 0)
if !(loaded && s:SelectWindow(bufnum) >= 0)
let comd = s:singlewin ? (loaded ? 'buffer' : 'edit') : 'botright split'
call s:VisitBuf_ChanServ()
if loaded
if s:singlewin
call s:OpenBuf(comd, bufnum)
else
call s:OpenBuf(comd, '+'.bufnum.'buffer')
endif
else
let bufname = s:GenBufName_Channel(s:server, a:channel)
call s:OpenBuf(comd, bufname)
call s:InitBuf_Channel(bufname, a:channel)
endif
" I don't remember why this is necessary
if &l:winfixheight
let &l:winfixheight = 0
endif
endif
endfunction
function! s:OpenBuf_Nicks(channel, ...)
if !s:VisitBuf_Channel(a:channel, (a:0 ? a:1 : s:server))
return 0
endif
let server = a:0 ? a:1 : s:server
let bufnum = s:GetBufNum_Nicks(a:channel, server)
let loaded = (bufnum >= 0)
if !(loaded && s:SelectWindow(bufnum) >= 0)
let comd = 'vertical belowright 12split'
if loaded
call s:OpenBuf(comd, '+'.bufnum.'buffer')
else
let bufname = s:GenBufName_Nicks(server, a:channel)
call s:OpenBuf(comd, bufname)
call s:InitBuf_Nicks(bufname, a:channel)
endif
endif
return 1
endfunction
function! s:OpenBuf_Command()
if !(s:GetVimVar('s:opened') && s:IsBufIRC())
return
endif
" Set the current server appropriately, so the command/message will be sent
" to the one user intended
if exists('b:server')
call s:SetCurServer(b:server)
endif
let channel = s:GetVimVar('b:channel')
let chattin = strlen(channel) && !s:IsChannel(channel)
let bufnum = s:GetBufNum_Command(channel)
" TODO: I think I should make it configurable where to open
if 0
let comd = 'botright 1split'
else
let comd = 'belowright 1split'
if !(chattin || bufnum == bufnr('%'))
call s:SelectWindow(strlen(channel) ? s:GetBufNum_Channel(channel)
\ : s:GetBufNum_Server())
endif
endif
" &equalalways will be restored when the command window is closed
let &equalalways = 0
if bufnum >= 0
if s:SelectWindow(bufnum) < 0
call s:OpenBuf(comd, '+'.bufnum.'buffer')
endif
else
let bufname = s:GenBufName_Command(s:server, channel)
call s:OpenBuf(comd, bufname)
call s:InitBuf_Command(bufname, channel, chattin)
endif
if 1 && !&l:winfixheight
setlocal winfixheight
endif
match none
if strlen(getline('$'))
call append('$', '')
endif
$
startinsert
endfunction
function! s:EnterChanServ(split)
let rx = '^\s*[-+]\=\[-\=\d\+\]\(\S\+\)\%(\s\+\(\S\+\)\)\=$'
let line = getline('.')
if line =~ rx
let channel= s:StrMatched(line, rx, '\1')
let server = s:StrMatched(line, rx, '\2')
let save_server = s:server
let s:server = strlen(server) ? server : channel
if s:singlewin && a:split
call s:VisitBuf_ChanServ()
split
endif
if !strlen(server) " Must be a server
call s:OpenBuf_Server()
elseif s:IsChannel(channel)
call s:OpenBuf_Channel(channel)
else
call s:OpenBuf_Chat(channel, server)
endif
let s:server = save_server
endif
endfunction
function! s:OpenBuf_Chat(nick, server)
let bufnum = s:GetBufNum_Chat(a:nick, a:server)
let loaded = (bufnum >= 0)
if !(loaded && s:SelectWindow(bufnum) >= 0)
let comd = s:singlewin ? (loaded ? 'buffer' : 'edit') : 'botright split'
call s:VisitBuf_ChanServ()
if loaded
if s:singlewin
call s:OpenBuf(comd, bufnum)
else
call s:OpenBuf(comd, '+'.bufnum.'buffer')
endif
else
let bufname = s:GenBufName_Chat(a:server, a:nick)
call s:OpenBuf(comd, bufname)
call s:InitBuf_Chat(bufname, a:nick, a:server)