Skip to content

Commit 06b2651

Browse files
author
Philipp Schmidt
committed
VimChat exit handler. New cusomizable options like blink timeout, buddy list max width, custom time format
1 parent dc948d5 commit 06b2651

File tree

3 files changed

+94
-41
lines changed

3 files changed

+94
-41
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ __Optional ~/.vimrc Variables:__
5454
* let g:vimchat\_otr = (0 or 1) default is 0 -- enable otr or not
5555
* let g:vimchat\_logotr = (0 or 1) default is 1 -- log otr convos or not
5656
* let g:vimchat\_statusicon = (0 or 1) default is 1 -- use a gtk status icon?
57-
57+
* let g:vimchat\_blinktimeout = timeout in seconds default is -1
58+
* let g:vimchat\_buddylistmaxwidth = max width of buddy list window default ''
59+
* let g:vimchat\_timestampformat = format of the message timestamp default "[%H:%M]"

plugin/vimchat.vim

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
" g:vimchat_otr = (0 or 1) default is 0
2626
" g:vimchat_logotr = (0 or 1) default is 1
2727
" g:vimchat_statusicon = (0 or 1) default is 1
28+
" g:vimchat_blinktimeout = timeout in seconds default is -1
29+
" g:vimchat_buddylistmaxwidth = max width of buddy list window default ''
30+
" g:vimchat_timestampformat = format of the message timestamp default "[%H:%M]"
2831

2932

3033
python <<EOF
@@ -65,7 +68,6 @@ except:
6568
pyotr_enabled = False
6669
pyotr_logging = False
6770

68-
6971
gtk_enabled = False
7072
if 'DISPLAY' in os.environ:
7173
try:
@@ -88,6 +90,8 @@ class VimChatScope:
8890
rosterFile = '/tmp/vimChatRoster'
8991
statusIcon = None
9092
lastMessageTime = 0
93+
blinktimeout = -1
94+
timeformat = "[%H:%M]"
9195

9296
#{{{ init
9397
def init(self):
@@ -126,13 +130,8 @@ class VimChatScope:
126130
pyotr_enabled = False
127131
pyotr_logging = False
128132

129-
isStatusIcon = int(vim.eval('g:vimchat_statusicon'))
130-
if isStatusIcon != 1:
131-
self.gtk_enabled = False
132-
133-
if self.gtk_enabled:
134-
self.statusIcon = self.StatusIcon()
135-
self.statusIcon.start()
133+
# Timestamp format
134+
self.timeformat = vim.eval('g:vimchat_timestampformat')
136135

137136
# Signon to accounts listed in .vimrc
138137
if vim.eval("exists('g:vimchat_accounts')") == '1':
@@ -149,6 +148,19 @@ class VimChatScope:
149148
password = config.get('accounts', jid)
150149
self._signOn(jid, password)
151150

151+
if len(self.accounts) and self.statusIcon == None:
152+
isStatusIcon = int(vim.eval('g:vimchat_statusicon'))
153+
if isStatusIcon != 1:
154+
self.gtk_enabled = False
155+
if self.gtk_enabled:
156+
self.statusIcon = self.StatusIcon()
157+
self.statusIcon.start()
158+
self.blinktimeout = int(vim.eval('g:vimchat_blinktimeout'))
159+
160+
def stop(self):
161+
if self.statusIcon != None:
162+
self.statusIcon.stop()
163+
self.signOffAll()
152164
#}}}
153165

154166
#CLASSES
@@ -277,7 +289,7 @@ class VimChatScope:
277289
def still_secure(self, opdata=None, context=None, is_reply=0):
278290
# this is called when the OTR session was refreshed
279291
# (ie. new session keys have been created)
280-
# is_reply will be 0 when we started we started that refresh,
292+
# is_reply will be 0 when we started that refresh,
281293
# 1 when the contact started it
282294
try:
283295
connection = VimChat.accounts[context.accountname]
@@ -317,7 +329,6 @@ class VimChatScope:
317329
#}}}
318330
#{{{ class JabberConnection
319331
class JabberConnection(threading.Thread):
320-
321332
#{{{ class Variables
322333
_roster = {}
323334
_chats = {}
@@ -332,6 +343,7 @@ class VimChatScope:
332343
self._roster = roster
333344
threading.Thread.__init__ ( self )
334345
self.jabber = jabberClient
346+
self.online = 0
335347
self._protocol = 'xmpp'
336348
#}}}
337349
#{{{ run
@@ -344,18 +356,20 @@ class VimChatScope:
344356
RECV_BUF = 4096
345357
self.xmppS = self.jabber.Connection._sock
346358
socketlist = [self.xmppS]
347-
online = 1
359+
self.online = 1
348360

349361
#set up otr
350362
self.otrSetup()
351-
352-
while online:
363+
while self.online:
353364
(i , o, e) = select.select(socketlist,[],[],1)
354365
for each in i:
355366
if each == self.xmppS:
356367
self.jabber.Process(1)
357368
else:
358369
pass
370+
time.sleep(1)
371+
def stop(self):
372+
self.online = 0
359373
#}}}
360374

361375
#From Jabber Functions
@@ -716,21 +730,32 @@ class VimChatScope:
716730
# GTK StausIcon
717731
gtk.gdk.threads_init()
718732
self.status_icon = StatusIcon()
719-
self.status_icon.set_from_file(os.path.expanduser(
720-
'~/.vimchat/icon.gif'))
733+
self.status_icon.set_from_file(os.path.expanduser('~/.vimchat/icon.gif'))
721734
self.status_icon.set_tooltip("VimChat")
722735
self.status_icon.set_visible(True)
723736
gtk.main()
724737
def blink(self, value):
725738
self.status_icon.set_blinking(value)
726-
#}}}
727739

740+
def stop(self):
741+
self.status_icon.set_visible(False)
742+
gtk.main_quit()
743+
#}}}
744+
#{{{ class BlinkClearer
745+
class BlinkClearer(threading.Thread):
746+
def __init__(self, tt):
747+
self.timeoutTime = tt
748+
threading.Thread.__init__ ( self )
749+
def run(self):
750+
time.sleep(self.timeoutTime)
751+
VimChat.clearNotify()
752+
#}}}
728753
#CONNECTION FUNCTIONS
729754
#{{{ signOn
730755
def signOn(self):
731756
accounts = self.getAccountsFromConfig()
732757
if len(accounts) == 0:
733-
print 'No acounts found in the vimchat config %s.'\
758+
print 'No accounts found in the vimchat config %s.'\
734759
% (self.configFilePath)
735760
return
736761
for account in accounts:
@@ -778,29 +803,44 @@ class VimChatScope:
778803
self.accounts[accountJid] = self.JabberConnection(
779804
self, jid, jabberClient, roster)
780805
self.accounts[accountJid].start()
781-
782806
print "Connected with " + jid
783807
#}}}
784808
#{{{ signOff
785809
def signOff(self):
786810
accounts = self.accounts
787811
if len(accounts) == 0:
788-
print 'No acounts found'
812+
print 'No accounts found'
789813
return
790814
for account in accounts:
791815
print account
792816
account = vim.eval(
793817
'input("Enter the account from the above list: ")')
794818
self._signOff(account)
795819
#}}}
820+
#{{{ signOffAll
821+
def signOffAll(self):
822+
accounts = self.accounts
823+
if len(accounts) == 0:
824+
return
825+
size=len(accounts)
826+
while(size > 0):
827+
account = accounts.keys()[0]
828+
self._signOff(account)
829+
if len(accounts) >= size:
830+
print "Error while signing off"
831+
break
832+
else:
833+
size=len(accounts)
834+
#}}}
796835
#{{{ _signOff
797836
def _signOff(self, account):
798837
accounts = self.accounts
799838
if account in accounts:
800839
try:
801840
accounts[account].disconnect()
841+
accounts[account].stop()
802842
del accounts[account]
803-
print "Signed Off VimChat!"
843+
print "%s was signed off of VimChat!" % (account)
804844
except:
805845
print "Error signing off %s VimChat!" % (account)
806846
print sys.exc_info()[0:2]
@@ -837,7 +877,7 @@ class VimChatScope:
837877
#}}}
838878
#{{{ getTimestamp
839879
def getTimestamp(self):
840-
return time.strftime("[%H:%M]")
880+
return time.strftime(self.timeformat)
841881
#}}}
842882
#{{{ getBufByName
843883
def getBufByName(self, name):
@@ -892,7 +932,6 @@ class VimChatScope:
892932
print e
893933
vim.command("new " + self.rosterFile)
894934

895-
896935
commands = """
897936
setlocal foldtext=VimChatFoldText()
898937
setlocal nowrap
@@ -901,11 +940,13 @@ class VimChatScope:
901940
nnoremap <buffer> <silent> <Leader>l :py VimChat.openLogFromBuddyList()<CR>
902941
nnoremap <buffer> <silent> B :py VimChat.toggleBuddyList()<CR>
903942
nnoremap <buffer> <silent> q :py VimChat.toggleBuddyList()<CR>
943+
nnoremap <buffer> <silent> r :py VimChat.refreshBuddyList()<CR>
944+
nnoremap <buffer> <silent> R :py VimChat.refreshBuddyList()<CR>
945+
nnoremap <buffer> <silent> <Leader>n /{{{ (<CR>
904946
nnoremap <buffer> <silent> <Leader>c :py VimChat.openGroupChat()<CR>
905947
nnoremap <buffer> <silent> <Leader>ss :py VimChat.setStatus()<CR>
906-
nnoremap <buffer> <silent> <Space> :silent exec 'vertical resize ' . (winwidth('.') > g:vimchat_buddylistwidth ? (g:vimchat_buddylistwidth) : '')<CR>
948+
nnoremap <buffer> <silent> <Space> :silent exec 'vertical resize ' . (winwidth('.') > g:vimchat_buddylistwidth ? (g:vimchat_buddylistwidth) : (g:vimchat_buddylistmaxwidth))<CR>
907949
"""
908-
909950
vim.command(commands)
910951
self.setupLeaderMappings()
911952

@@ -926,7 +967,6 @@ class VimChatScope:
926967
vim.command("normal [z")
927968

928969
account = str(vim.current.line).split(' ')[2]
929-
930970
return account, toJid
931971
#}}}
932972
#{{{ beginChatFromBuddyList
@@ -939,11 +979,15 @@ class VimChatScope:
939979
#print "Error getting buddy info: " + jid
940980
return 0
941981

942-
943982
vim.command('sbuffer ' + str(buf.number))
944983
VimChat.toggleBuddyList()
945984
vim.command('wincmd K')
946985
#}}}
986+
#{{{ refreshBuddyList
987+
def refreshBuddyList(self):
988+
self.writeBuddyList()
989+
vim.command("silent e!")
990+
#}}}
947991
#{{{ writeBuddyList
948992
def writeBuddyList(self):
949993
#write roster to file
@@ -1001,7 +1045,6 @@ You can type \on to reconnect.
10011045
#CHAT BUFFERS
10021046
#{{{ beginChat
10031047
def beginChat(self, fromAccount, toJid, groupChat = False):
1004-
#return 0
10051048
#Set the ChatFile
10061049
connection = self.accounts[fromAccount]
10071050
if toJid in connection._chats.keys():
@@ -1013,7 +1056,6 @@ You can type \on to reconnect.
10131056
chatFile = 'chat:' + toJid
10141057

10151058
connection._chats[toJid] = chatFile
1016-
10171059
bExists = int(vim.eval('buflisted("' + chatFile + '")'))
10181060
if bExists:
10191061
#TODO: Need to call sbuffer only if buffer is hidden.
@@ -1031,7 +1073,6 @@ You can type \on to reconnect.
10311073
vim.command("let b:account = '" + fromAccount + "'")
10321074
self.setupChatBuffer(groupChat);
10331075
return vim.current.buffer
1034-
10351076
#}}}
10361077
#{{{ setupChatBuffer
10371078
def setupChatBuffer(self, isGroupChat=False):
@@ -1124,10 +1165,10 @@ You can type \on to reconnect.
11241165

11251166
#Get the first line
11261167
if resource:
1127-
line = tstamp + secureString + \
1168+
line = tstamp + " " + secureString + \
11281169
user + "/" + resource + ": " + lines.pop(0);
11291170
else:
1130-
line = tstamp + secureString + user + ": " + lines.pop(0);
1171+
line = tstamp + " " + secureString + user + ": " + lines.pop(0);
11311172

11321173
buf.append(line)
11331174
#TODO: remove these lines
@@ -1483,16 +1524,12 @@ You can type \on to reconnect.
14831524
try:
14841525
self.notify(jid, message, groupChat)
14851526
except:
1486-
print 'Error zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
1487-
print 'could not notify:', message, 'from:', jid
1527+
print 'Could not notify:', message, 'from:', jid
14881528
#}}}
14891529
#{{{ notify
14901530
def notify(self, jid, msg, groupChat):
1491-
# Important to keep this echo statement. As a side effect, it
1492-
# refreshes the buffer so the new message shows up. Need to find
1493-
# a better solution though.
1494-
vim.command("echo 'Message Received from: " + jid.replace("'", "''")
1495-
+ "'")
1531+
# refreshes the buffer so the new message shows up
1532+
vim.command("echo ");
14961533

14971534
if groupChat:
14981535
msgLowered = msg.lower()
@@ -1519,6 +1556,9 @@ You can type \on to reconnect.
15191556

15201557
if self.gtk_enabled:
15211558
self.statusIcon.blink(True)
1559+
if self.blinktimeout != -1:
1560+
thr1 = self.BlinkClearer(self.blinktimeout)
1561+
thr1.start()
15221562
#}}}
15231563
#{{{ clearNotify
15241564
def clearNotify(self):
@@ -1616,6 +1656,7 @@ endif
16161656
let g:vimchat_loaded = 1
16171657

16181658
com! VimChat py VimChat.init()
1659+
com! VimChatStop py VimChat.stop()
16191660
com! VimChatBuddyList py VimChat.toggleBuddyList()
16201661
com! VimChatViewLog py VimChat.openLogFromChat()
16211662
com! VimChatJoinGroupChat py VimChat.openGroupChat()
@@ -1625,6 +1666,7 @@ com! VimChatOtrGenerateKey py VimChat.otrGenerateKey()
16251666
com! -nargs=0 VimChatSetStatus py VimChat.setStatus(<args>)
16261667
com! VimChatShowStatus py VimChat.showStatus()
16271668
com! VimChatJoinChatroom py VimChat.joinChatroom()
1669+
autocmd! VIMLeave * :VimChatStop
16281670

16291671
set switchbuf=usetab
16301672

@@ -1634,6 +1676,9 @@ fu! VimChatCheckVars()
16341676
if !exists('g:vimchat_buddylistwidth')
16351677
let g:vimchat_buddylistwidth=30
16361678
endif
1679+
if !exists('g:vimchat_buddylistmaxwidth')
1680+
let g:vimchat_buddylistmaxwidth=''
1681+
endif
16371682
if !exists('g:vimchat_libnotify')
16381683
let g:vimchat_libnotify=1
16391684
endif
@@ -1652,6 +1697,12 @@ fu! VimChatCheckVars()
16521697
if !exists('g:vimchat_statusicon')
16531698
let g:vimchat_statusicon=1
16541699
endif
1700+
if !exists('g:vimchat_blinktimeout')
1701+
let g:vimchat_blinktimeout=-1
1702+
endif
1703+
if !exists('g:vimchat_timestampformat')
1704+
let g:vimchat_timestampformat="[%H:%M]"
1705+
endif
16551706

16561707
return 1
16571708
endfu

syntax/vimchat.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
syn match vimChatMsg /^\[\d\d:\d\d].\{-}:/ contains=vimChatTime,vimChatMe
2-
syn match vimChatTime /\[\d\d:\d\d\]/ contained nextgroup=vimChatMe
1+
syn match vimChatMsg /^\[\d\d\(:\d\d\)\{0,2\}].\{-}:/ contains=vimChatTime,vimChatMe
2+
syn match vimChatTime /\[\d\d\(:\d\d\)\{0,2\}]/ contained nextgroup=vimChatMe
33
syn match vimChatMe /Me:/ contained
44

55
" Comment, Type, String, Statement

0 commit comments

Comments
 (0)