This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
weechat.el
1807 lines (1560 loc) · 68.9 KB
/
weechat.el
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
;;; weechat --- Chat via WeeChat's relay protocol in Emacs ;; -*- lexical-binding: t -*-
;; Copyright (C) 2013 Moritz Ulrich
;; Author: Moritz Ulrich <[email protected]>
;; Rüdiger Sonderfeld <[email protected]>
;; Aristid Breitkreuz <[email protected]>
;; Keywords: irc chat network weechat
;; URL: https://github.com/the-kenny/weechat.el
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a way to chat via WeeChat's relay protocol in
;; Emacs.
;; Please see README.org on how to use it.
;;; Code:
(require 'weechat-core)
(require 'weechat-relay)
(require 'weechat-color)
(require 'cl-lib)
(require 'format-spec)
(require 's)
(defcustom weechat-host-default "localhost"
"Default host for `weechat-connect'."
:type 'string
:group 'weechat)
(defcustom weechat-port-default 9000
"Default port for `weechat-connect'."
:type 'string
:group 'weechat)
(defcustom weechat-mode-default 'plain
"Wether to connect via SSL by default.
Null or 'plain: Plain socket.
t or 'ssl: TLS socket.
String: command to run."
:type '(choice
(const :tag "Plain" 'plain)
(const :tag "SSL/TLS" 'ssl)
(string :tag "Command to run"))
:group 'weechat)
(defcustom weechat-modules '(weechat-button weechat-complete)
"Modules loaded when weechat.el is loaded.
Each module must be in `load-path' and must have a call to
provide in order to be loaded correctly.
To unload modules, use (unload-feature FEATURE)."
:type '(repeat symbol)
:group 'weechat)
(defcustom weechat-read-only t
"Whether to make text in weechat buffers read-only."
:type 'boolean
:group 'weechat)
(defcustom weechat-initial-lines 100
"Number of lines to show when initializing a channel buffer."
:type 'integer
:group 'weechat)
(defcustom weechat-more-lines-amount 10
"Number of extra lines `weechat-get-more-lines' will retieve."
:type 'integer
:group 'weechat)
(defcustom weechat-prompt "[%n] "
"The Weechat prompt."
:type 'string
:group 'weechat)
(defcustom weechat-buffer-line-limit 1000
"Number of max. lines per buffer."
:type '(choice integer
(const :tag "Unlimited" nil))
:group 'weechat)
(defcustom weechat-return-always-replace-input t
"Always replace current input with line on return.
If set to t, pressing return will always copy the current line to
the input prompt. If nil, only copy when the input line is
empty."
:type 'boolean
:group 'weechat)
(defcustom weechat-auto-move-cursor-to-prompt t
"Automatically move the cursor to the prompt when typing."
:type 'boolean
:group 'weechat)
(defcustom weechat-auto-recenter t
"Wether the prompt will always stay at the bottom"
:type 'boolean
:group 'weechat)
(defcustom weechat-hidden-text-hidden t
"Wether weechat.el should hide or show hidden text. "
:type 'boolean
:group 'weechat)
(defcustom weechat-connect-hook nil
"Hook run when successfully connected and authenticated."
:type '(choice (const :tag "Off" nil)
(function :tag "Hook"))
:group 'weechat)
(defcustom weechat-auto-reconnect-buffers t
"Automatically re-monitor channel buffers which were opened on a prior connection."
:type 'boolean
:group 'weechat)
(defcustom weechat-auto-reconnect-retries 5
"Number of max. retries when reconnecting"
:type '(choice (integer :tag "Number of retries")
(const :tag "No auto-reconnect" nil))
:group 'weechat)
(defcustom weechat-auto-monitor-buffers ()
"List of buffer names to auto-monitor on connect.
If value is a list, buffers corresponding the names will be
monitored on connect. If value is a string, monitor all buffers
matching the string as regexp. A value of t will monitor all
available buffers. Be warned, a too long list will use much
bandwidth on connect."
:type '(choice (const :tag "All" t)
(repeat :tag "List" string)
string)
:group 'weechat)
(defcustom weechat-auto-monitor-new-buffers 'silent
"Wether to auto-monitor new WeeChat buffers.
Value can be t, silent or nil. If t, new Emacs buffers will be
created when a new buffer in WeeChat is opened. If value
is (quote silent), new buffers will be opened in background. If
nil, no action will be taken for new WeeChat buffers."
:type '(choice (const :tag "Popup new buffer" t)
(const :tag "Open in background" 'silent)
(const :tag "Do nothing" nil))
:group 'weechat)
(defcustom weechat-monitor-buffer-function 'message
"Function called when a new buffer is monitored. Useful to
display notifications.
Value is either 'message or a function taking one argument (a
buffer-ptr). "
:type '(choice (const :tag "Message in the mode line" 'message)
hook)
:group 'weechat)
(defcustom weechat-auto-close-buffers nil
"Wether to auto-close closed WeeChat buffers."
:type 'boolean
:group 'weechat)
(defcustom weechat-time-format "%H:%M:%S"
"How to format time stamps.
See `format-time-string' for format description."
:type 'string
:group 'weechat)
(defcustom weechat-text-column 22
"Column after which text will be inserted.
If `(length (concat nick timestamp))' is longer than this value,
text-column will be increased for that line."
:type 'integer
:group 'weechat)
(defcustom weechat-max-nick-length nil
"Maximum length of nicknames. Longer nicks will be truncated.
Note that this option will apply to all prefixes, not just
nicknames."
:type '(choice
(integer :tag "Max length")
(const :tag "Off" nil))
:group 'weechat)
(defcustom weechat-fill-text t
"Wether weechat should fill text paragraphs automatically."
:type 'boolean
:group 'weechat)
(defcustom weechat-fill-column 'frame-width
"Column used for filling text in buffers."
:type '(choice
(const :tag "Frame width" 'frame-width)
(integer :tag "Number")
(const :tag "Default" t))
:group 'weechat)
(defcustom weechat-notification-mode :monitored
"When to notify the user.
Possible values are nil (Never), :monitored (Only monitored
buffers) and t (All buffers)."
:type '(choice
(const :tag "Never" nil)
(const :tag "Monitored buffers" :monitored)
(const :tag "All Buffers" t))
:group 'weechat)
(defcustom weechat-notification-types '(:highlight :disconnect :query)
"Events for which a notification should be shown."
:type '(repeat symbol)
:group 'weechat)
(defcustom weechat-header-line-format "%n on %c/%s: %t"
"Header line format.
Set to nil to disable header line. Supported options are:
- %n nick name
- %s server name
- %c channel name
- %N buffer name
- %t topic"
:type '(choice (const :tag "Disabled" nil)
string)
:set (lambda (sym val)
(set sym val)
(when (fboundp 'weechat-update-header-line)
(weechat-update-header-line)))
:group 'weechat)
(defcustom weechat-input-ring-size 20
"Size for the input ring."
:type 'integer
:group 'weechat)
(defcustom weechat-insert-modify-hook nil
"The hook will be called after new text is inserted into the buffer.
It is called with narrowing in the correct buffer."
:type 'hook
:group 'weechat)
(defcustom weechat-message-post-receive-functions nil
"List of function called after a new line was received for a buffer.
This hook is useful in conjunction with
`weechat-last-background-message-date' or
`weechat-last-background-highlight-date'.
Functions must take one argument: The buffer-ptr.
If the weechat-buffer is currently associated with an emacs
buffer, the functions will get called with the active buffer set
to it."
:type 'hook
:group 'weechat)
(defcustom weechat-message-filter-functions nil
"List of functions called in sequence before a line will be sent to the server.
The functions (a b c) are applied like (a (b (c input-string))).
If a function returns nil, evaluation will stop and the line is
ignored."
:type '(repeat :tag "List" function)
:group 'weechat)
(defcustom weechat-message-filter-require-double-ret nil
"If t, pressing RET once will show the result of the filter.
If nil, filtering will take place transparently when sending the
message."
:type 'boolean
:group 'weechat)
(defcustom weechat-complete-order-nickname t
"If non-nil nicknames are completed in order of most recent speaker."
:type 'boolean
:group 'weechat)
(defcustom weechat-password-callback 'weechat-password-auth-source-callback
"Function called to get the relay password. Set to nil if no
password is needed.
Value must be a function with two arguments: Hostname and port.
The return value must be either a string, a function which
returns a string, or nil."
:type 'function
:group 'weechat)
(defcustom weechat-buffer-activity-types '(:irc/privmsg :irc/action :irc/notice)
"List of types which will contribute to buffer activity."
:type '(repeat :tag "List" symbol)
:group 'weechat)
(defcustom weechat-buffer-kill-buffers-on-disconnect nil
"Kill buffers if the connection is disconnected by the user."
:type 'boolean
:group 'weechat)
(defcustom weechat-sync-active-buffer nil
"Sync currently visible buffer with the relay (one-way).
When set to t, weechat.el will switch the currently active
weechat buffer on the relay server when visiting a buffer in
weechat.
This is useful when setting irc.msgbuffer.* to 'current'.
Syncing is done when sending a command/message to the buffer."
:type 'boolean
:group 'weechat)
(defcustom weechat-sync-buffer-read-status nil
"Mark buffers as read in the relay, when read with weechat.el.
When set to t, weechat will automatically mark buffers as read in
the relay, when they are visited in the client.
Be aware, that this setting can loose highlights: If the
highlight occurred more than `weechat-initial-lines' before Emacs
connects to the relay, reading the Emacs buffer will not show the
highlight, but mark the buffer as read.
Also, weechat >= 1.0 is required for this to work."
:type 'boolean
:group 'weechat)
(defcustom weechat-completing-read-function 'weechat--try-ido
"Function to prompt for channel names.
The function must comply to the interface of `completing-read'.
Possible choices would be `ido-completing-read' or
`completing-read'."
:type '(choice
(const :tag "Ido" weechat--try-ido)
(const :tag "Ivy" weechat--try-ivy)
(const :tag "Default" completing-read)
(function :tag "Other"))
:group 'weechat)
(defvar weechat--buffer-hashes (make-hash-table :test 'equal))
(defvar weechat--connected nil)
(defvar weechat-host-history nil
"List of recently connected hosts.")
(defvar weechat-last-port nil
"Last port connected to.")
(defvar weechat-mode-history nil
"List of recently used connection modes.")
(defvar weechat-version nil)
(add-to-list 'version-regexp-alist '("^[-_+ ]dev$" . -3))
(defvar weechat-buffer-opened-functions nil
"Hook ran when a WeeChat buffer opens.")
(defvar weechat-buffer-closed-functions nil
"Hook ran when a WeeChat buffer closes.")
(defvar weechat-notification-handler-functions nil
"List of functions called to display notificiations.
The functions are called with the following arguments:
TYPE, a symbol from `weechat-notification-types' Other optional
arguments are SENDER, TEXT, DATE, and BUFFER-PTR.")
(defvar weechat-inhibit-notifications nil
"Non-nil means don't display any weechat notifications.")
(defun weechat-load-modules-maybe ()
"Load all modules listed in `weechat-modules'"
;; Inspired by `org-load-modules-maybe'
(dolist (module weechat-modules)
(condition-case nil (load-library (symbol-name module))
(error (weechat-warn "Problems while trying to load feature `%s'" module)))))
;;; This is a hack to load modules after weechat.el is loaded
;;; completely
(eval-after-load 'weechat
'(weechat-load-modules-maybe))
;;; Add all hooks ending in -functions to
;;; `unload-feature-special-hooks' to make `unload-feature' remove the
;;; hooks on unload
(eval-after-load 'loadhist
'(setq unload-feature-special-hooks
(append unload-feature-special-hooks
'(weechat-buffer-opened-functions
weechat-buffer-closed-functions
weechat-notification-handler-functions
weechat-message-post-receive-functions))))
(defun weechat-connected-p ()
(and (weechat-relay-connected-p)
weechat--connected))
(defun weechat-buffer-hash (buffer-ptr)
(gethash buffer-ptr weechat--buffer-hashes))
(defun weechat--clear-buffer-store ()
(clrhash weechat--buffer-hashes))
(defun weechat--store-buffer-hash (ptr alist &optional replace)
(when (and (not replace) (weechat-buffer-hash ptr))
(error "Buffer '%s' already exists" ptr))
(let ((hash (make-hash-table :test 'equal)))
(dolist (x alist)
(puthash (car x) (cdr x) hash))
(puthash ptr hash weechat--buffer-hashes)))
(defun weechat--remove-buffer-hash (ptr)
(unless (weechat-buffer-hash ptr)
(error "Buffer '%s' doesn't exist" ptr))
(remhash ptr weechat--buffer-hashes))
(defun weechat--handle-buffer-list (response)
;; Remove all hashes not found in the new list
(let* ((hdata (car response))
(buffer-pointers (mapcar (lambda (x) (car (weechat--hdata-value-pointer-path x)))
(weechat--hdata-values hdata))))
(maphash (lambda (k _)
(unless (cl-find k buffer-pointers
:test 'equal)
(remhash k weechat--buffer-hashes)))
(copy-hash-table weechat--buffer-hashes))
;; Update all remaining values
(dolist (value (weechat--hdata-values hdata))
(let* ((buffer-ptr (car (weechat--hdata-value-pointer-path value)))
(buffer-hash (weechat-buffer-hash buffer-ptr))
(alist (weechat--hdata-value-alist value)))
(if (hash-table-p buffer-hash)
(dolist (v alist)
(puthash (car v) (cdr v) buffer-hash))
(weechat--store-buffer-hash buffer-ptr alist))))))
(defun weechat-update-buffer-list (&optional callback)
(weechat-relay-send-command
"hdata buffer:gui_buffers(*) number,name,short_name,title,local_variables"
(lambda (response)
(weechat--handle-buffer-list response)
(when (functionp callback)
(funcall callback)))))
(defun weechat--handle-buffer-opened (response)
(let* ((hdata (car response))
(value (car (weechat--hdata-values hdata)))
(buffer-ptr (car (weechat--hdata-value-pointer-path value))))
(when (weechat-buffer-hash buffer-ptr)
(error "Received '_buffer_opened' event for '%s' but the buffer exists already" buffer-ptr))
(weechat--store-buffer-hash buffer-ptr (weechat--hdata-value-alist value))
(when weechat-auto-monitor-new-buffers
(weechat-monitor-buffer
buffer-ptr
(not (eq weechat-auto-monitor-new-buffers 'silent))))
(run-hook-with-args 'weechat-buffer-opened-functions
buffer-ptr)))
(defun weechat--handle-buffer-closed (response)
(let* ((hdata (car response))
(value (car (weechat--hdata-values hdata)))
(buffer-ptr (car (weechat--hdata-value-pointer-path value)))
(emacs-buffer (weechat--emacs-buffer buffer-ptr)))
(unless (weechat-buffer-hash buffer-ptr)
(error "Received '_buffer_closed' event for '%s' but the buffer doesn't exist" buffer-ptr))
(when (buffer-live-p emacs-buffer)
;; Add text about quitting etc. bla
(weechat-print-line buffer-ptr :prefix "Buffer closed")
;; Close buffer if user wants this
(when weechat-auto-close-buffers
(kill-buffer emacs-buffer)))
;; Remove from buffer hash map
(weechat--remove-buffer-hash buffer-ptr)
;; Finally, run hook
(run-hook-with-args 'weechat-buffer-closed-functions
buffer-ptr)))
(defmacro weechat->> (&rest body)
(let ((result (pop body)))
(dolist (form body result)
(setq result (append form (list result))))))
(defmacro weechat-> (&rest body)
(let ((result (pop body)))
(dolist (form body result)
(setq result (append (list (car form) result)
(cdr form))))))
(defun weechat--handle-buffer-renamed (response)
(let* ((hdata (car response))
(value (car (weechat--hdata-values hdata)))
(buffer-ptr (car (weechat--hdata-value-pointer-path value)))
(hash (weechat-buffer-hash buffer-ptr)))
(unless hash
(error "Received '_buffer_renamed' event for '%s' but the buffer doesn't exist" buffer-ptr))
(puthash "number" (assoc-default "number" value) hash)
(puthash "full_name" (assoc-default "full_name" value) hash)
(puthash "short_name" (assoc-default "short_name" value) hash)
(puthash "local_variables" (assoc-default "local_variables" value) hash)))
(weechat-relay-add-id-callback "_buffer_opened" #'weechat--handle-buffer-opened nil 'force)
(weechat-relay-add-id-callback "_buffer_closing" #'weechat--handle-buffer-closed nil 'force)
(weechat-relay-add-id-callback "_buffer_renamed" #'weechat--handle-buffer-renamed nil 'force)
;;; Handle pong replies
(weechat-relay-add-id-callback "_buffer_renamed" #'weechat--handle-buffer-renamed nil 'force)
(defvar weechat-topic nil
"Topic of the channel buffer.")
(defun weechat--handle-buffer-title-changed (response)
(let* ((hdata (car response))
(value (car (weechat--hdata-values hdata)))
(buffer-ptr (car (weechat--hdata-value-pointer-path value)))
(hash (weechat-buffer-hash buffer-ptr))
(alist (weechat--hdata-value-alist value))
(buffer (gethash :emacs/buffer hash))
(new-title (or (cdr (assoc-string "title" alist)) "")))
(unless (weechat-buffer-hash buffer-ptr)
(error "Received '_buffer_title_changed' event for '%s' but the buffer doesn't exist" buffer-ptr))
(puthash "title" new-title hash)
(when (buffer-live-p buffer)
(with-current-buffer buffer
(setq weechat-topic new-title)
(weechat-update-header-line-buffer buffer)))))
(weechat-relay-add-id-callback "_buffer_title_changed" #'weechat--handle-buffer-title-changed nil 'force)
(defun weechat-merge-alists (old new)
(dolist (k new old)
(let ((to-remove (assoc-string (car k) old)))
(setq old (cons k (remove to-remove old))))))
(defun weechat--handle-localvar-changed (response)
(let* ((hdata (car response))
(value (car (weechat--hdata-values hdata)))
(buffer-ptr (car (weechat--hdata-value-pointer-path value)))
(hash (weechat-buffer-hash buffer-ptr))
(alist (weechat--hdata-value-alist value))
(buffer (gethash :emacs/buffer hash))
(old-local-variables (gethash "local_variables" hash))
(new-local-variables (cdr (assoc-string "local_variables" alist))))
(unless (weechat-buffer-hash buffer-ptr)
(error "Received '_buffer_localvar_changed' event for '%s' but the buffer doesn't exist"
buffer-ptr))
(puthash "local_variables"
(weechat-merge-alists old-local-variables new-local-variables)
hash)
(when buffer
(with-current-buffer buffer
(weechat-update-prompt)
(weechat-update-header-line-buffer buffer)))))
(weechat-relay-add-id-callback "_buffer_localvar_changed" #'weechat--handle-localvar-changed nil 'force)
(weechat-relay-add-id-callback "_buffer_localvar_added" #'weechat--handle-localvar-changed nil 'force)
(defun weechat-password-auth-source-callback (host port)
"Get password for HOST and PORT via `auth-source-search'.
Returns either a string or a function. See Info node `(auth) Top' for details."
(when (fboundp 'auth-source-search)
(weechat-message "Using auth-source to retrieve weechat relay password")
(plist-get
(car (auth-source-search
:max 1
:host host
:port port
:require '(:secret)))
:secret)))
(defun weechat-get-password (host port)
"Get password for HOST and PORT.
Return either a string, a function returning a string, or nil."
(when (functionp weechat-password-callback)
(funcall weechat-password-callback host port)))
(defvar weechat-mode-completion-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map "\t" 'minibuffer-complete)
(define-key map "?" 'minibuffer-completion-help)
map)
"Weechat mode selection: Local keymap for minibuffer input with completion.")
(defvar weechat-reconnect-timer nil)
(defun weechat-cancel-reconnect ()
(when (timerp weechat-reconnect-timer)
(cancel-timer weechat-reconnect-timer)
(setq weechat-reconnect-timer nil))
(unintern 'weechat-auto-reconnect-retries-left obarray))
;;;###autoload
(defun weechat-connect (&optional host port password mode force-disconnect)
"Connect to WeeChat.
HOST is the relay host, `weechat-host-default' by default.
PORT is the port where the relay listens, `weechat-port-default' by default.
PASSWORD is either a string, a function or nil.
MODE is null or 'plain for a plain socket, t or 'ssl for a TLS socket;
a string denotes a command to run. You can use %h and %p to interpolate host
and port number respectively."
(interactive
(let* ((host
(read-string
(format "Relay host (default '%s'): " weechat-host-default)
nil 'weechat-host-history weechat-host-default))
(port
(read-number "Port: " (or weechat-last-port weechat-port-default)))
(mode (let*
((minibuffer-local-completion-map weechat-mode-completion-map)
(modestr (completing-read
(format "Mode (`plain', `ssl' or command, default `%s'): "
weechat-mode-default)
'("plain" "ssl" "ssh -W localhost:%p %h")
nil nil nil 'weechat-mode-history
;; NOTE: `completing-read' is fine when
;; passed a symbol, but helm breaks.
;; The following ensures we always pass
;; a string.
(format "%s" weechat-mode-default))))
(cond
((string= modestr "") nil)
((string= modestr "plain") 'plain)
((string= modestr "ssl") 'ssl)
(t modestr)))))
(setq weechat-last-port port)
(list
host port
(or
(progn
(weechat-message "Trying to get password via `weechat-password-callback'...")
(weechat-get-password host port))
;; Use lexical-let to scramble password lambda in *Backtrace*
(read-passwd "Password: "))
mode
nil)))
;; Cancel the reconnect timer to prevent surprises
(weechat-cancel-reconnect)
;; Handle when the user is already connected etc.
(let* ((host (or host weechat-host-default))
(port (or port weechat-port-default))
(password (or password
(weechat-get-password host port)))
(mode (or mode weechat-mode-default)))
(weechat-message "Weechat connecting to %s:%d" host port)
(when (weechat-relay-connected-p)
(if (or force-disconnect
(y-or-n-p "Already connected. Disconnect other connection? "))
(weechat-relay-disconnect)
(error "Can't open two connections")))
(when (and (stringp host)
(integerp port))
(weechat-relay-connect
host
port
mode
(lambda ()
(weechat-relay-authenticate password)
(weechat-relay-send-command
"info version"
(lambda (data)
(let ((version-str (cdar data)))
(weechat-message "Connected to '%s', version %s" host
version-str)
(setq weechat-version version-str))
(weechat-update-buffer-list
(lambda ()
(weechat-relay-send-command "sync")
(setq weechat--connected t)
(weechat--relay-start-ping-timer)
(weechat-cancel-reconnect)
(run-hooks 'weechat-connect-hook))))))))))
(defvar weechat-auto-reconnect-retries-left)
(defun weechat-handle-reconnect-maybe ()
(weechat-cancel-reconnect)
(unless (boundp 'weechat-auto-reconnect-retries-left)
(setq weechat-auto-reconnect-retries-left
weechat-auto-reconnect-retries))
(when (> weechat-auto-reconnect-retries-left 0)
(let ((host (car weechat-host-history))
(port weechat-last-port)
(delay (lsh 1 (- weechat-auto-reconnect-retries
weechat-auto-reconnect-retries-left))))
(if (not (weechat-get-password host port))
(weechat-message "Not reconnecting: No password stored.")
(weechat-message "Reconnecting in %ds..." delay)
(cl-decf weechat-auto-reconnect-retries-left)
(setq weechat-reconnect-timer
(run-with-timer
delay nil
(lambda ()
(weechat-connect
host
port
(weechat-get-password host port)
(car weechat-mode-history)
'force-disconnect)))))
t)))
(defun weechat-handle-disconnect ()
(setq weechat--connected nil
weechat-version nil)
(unless (and weechat-auto-reconnect-retries
(weechat-handle-reconnect-maybe))
;; Print 'disconnected' message to all channel buffers
(maphash (lambda (k v)
(when (bufferp (gethash :emacs/buffer v))
(with-current-buffer (gethash :emacs/buffer v)
(weechat-print-line k
:prefix "!!!"
:text "Lost connection to relay server"
:date (current-time)
:line-type :irc/x-error))))
weechat--buffer-hashes)
(weechat-notify :disconnect
:date (current-time))))
(defun weechat-disconnect ()
(interactive)
;; It's safe to lexical-bind the retry limit to nil to disable
;; reconnects
(let ((weechat-auto-reconnect-retries nil))
;; Disconnect the relay. `weechat-relay-disconnect-hook' will NOT
;; run.
(weechat-relay-disconnect)
(weechat-handle-disconnect)
(when weechat-buffer-kill-buffers-on-disconnect
(weechat-do-buffers (kill-buffer)))
(clrhash weechat--buffer-hashes)
(setq weechat--connected nil)))
(add-hook 'weechat-relay-disconnect-hook 'weechat-handle-disconnect)
(defun weechat-buffer-name (buffer-ptr)
(let ((hash (weechat-buffer-hash buffer-ptr)))
(or (gethash "name" hash)
(gethash "full_name" hash)
;; NOTE: Short name isn't useful to identify the buffer
;; (gethash "short_name" hash)
)))
(defun weechat--find-buffer (name)
"Return buffer-ptr for channel NAME."
(let (ret)
(maphash
(lambda (ptr _)
(when (string= name (weechat-buffer-name ptr))
(setq ret ptr)))
weechat--buffer-hashes)
ret))
(defun weechat--channel-names-pred (l r)
"Compare channel name L and R.
Return non-nil if L < R. Names of actual channels should come first."
(let ((l? (s-contains? "#" l))
(r? (s-contains? "#" r))
(l<r? (string< l r)))
(or (and l? l<r?)
(and l? (not r?))
(and (not r?) l<r?))))
(defun weechat-channel-names (&optional arg sort)
"Return all available buffer names in WeeChat.
If ARG is non-nil, only return monitored buffers. If SORT is non-nil then sort
the channel list with actual channels coming first."
(let (ret)
(maphash
(lambda (k v)
(when (or (not arg) (buffer-live-p (gethash :emacs/buffer v)))
(setq ret (cons (weechat-buffer-name k) ret))))
weechat--buffer-hashes)
(if sort
(sort ret #'weechat--channel-names-pred)
ret)))
(defun weechat-buffer-list ()
"List all Weechat buffers."
(let (acc)
(maphash (lambda (_ v)
(when (buffer-live-p (gethash :emacs/buffer v))
(setq acc (cons (gethash :emacs/buffer v) acc))))
weechat--buffer-hashes)
acc))
(defmacro weechat-do-buffers (&rest body)
"Evaluate body in each WeeChat buffer."
`(maphash (lambda (_ v)
(let ((buffer (gethash :emacs/buffer v)))
(when (buffer-live-p buffer)
(with-current-buffer buffer
,@body))))
weechat--buffer-hashes))
(defun weechat-channel-names-unmonitored ()
(cl-remove-if
(lambda (name)
(weechat--emacs-buffer
(weechat--find-buffer name)))
(weechat-channel-names)))
(defun weechat--emacs-buffer (buffer-ptr)
(let ((hash (gethash buffer-ptr weechat--buffer-hashes)))
(gethash :emacs/buffer hash)))
(defun weechat-visible-buffers (&optional current-frame-only)
"Returns list of all visible weechat.el channel buffers.
Optional argument CURRENT-FRAME-ONLY limits list to current
frame."
(let (ret)
(weechat-do-buffers
(when (window-live-p (get-buffer-window
(current-buffer)
(not current-frame-only)))
(setq ret (cons (current-buffer) ret))))
ret))
;;; Buffer local variables
(defvar weechat-buffer-ptr)
(defvar weechat-server-buffer)
(defvar weechat-buffer-number)
(defvar weechat-local-prompt)
(defvar weechat-lines-received)
;;; The following functions handle buffer-hash-entries storing the
;;; last highlight and the last message. The entries will be cleared
;;; automatically when the buffer becomes visible. This is useful for
;;; a mode-line display of modified buffers etc.
(defvar weechat-buffer-background-message-hook nil
"Hook called when a message was received in a weechat buffer
which isn't currently visible. Called with the corresponding
buffer active.")
(defvar weechat-buffer-background-highlight-hook nil
"Same as `weechat-buffer-background-message-hook', only for highlights.")
(defvar weechat-buffer-visited-hook nil
"Hook called when a weechat-buffer is visited and the
background-data is reset.")
(defun weechat--reset-relay-read-status (buffer-ptr)
"Mark the buffer BUFFER-PTR as read in the relay."
(when (and weechat-version
(not (version< weechat-version "1.0"))
weechat-sync-buffer-read-status)
(weechat-relay-send-command (concat "input " buffer-ptr " /buffer set hotlist -1"))
(weechat-relay-send-command (concat "input " buffer-ptr " /input set_unread_current_buffer"))))
(defun weechat-reset-buffer-modified (buffer-ptr)
(let ((hash (weechat-buffer-hash buffer-ptr)))
(when (hash-table-p hash)
(weechat--reset-relay-read-status buffer-ptr)
(run-hooks 'weechat-buffer-visited-hook)
(remhash :background-message hash)
(remhash :background-highlight hash))))
(defun weechat-buffer-modified-update-hash (hash line-data)
(let ((hash (or hash (make-hash-table))))
(puthash :date (assoc-default "date" line-data) hash)
(puthash :sender (weechat--get-nick-from-line-data line-data) hash)
(puthash :count (1+ (gethash :count hash 0)) hash)
hash))
(defun weechat-update-buffer-modified (buffer-ptr line-data)
(let ((line-type (weechat-line-type line-data))
(line-date (assoc-default "date" line-data))
(nick (weechat--get-nick-from-line-data line-data))
(hash (weechat-buffer-hash buffer-ptr))
(emacs-buffer (weechat--emacs-buffer buffer-ptr)))
(unless (hash-table-p hash)
(error "Tried to update modification date for unknown buffer-ptr '%s'" buffer-ptr))
(if (and (buffer-live-p emacs-buffer)
(cl-find emacs-buffer (weechat-visible-buffers) :test 'equal))
;; Buffer is visible. Reset modification
(weechat-reset-buffer-modified buffer-ptr)
;; Buffer invisible. Store modifications.
(when (and line-type line-date nick)
(cond
;; Message from ourself. Reset.
((string= nick (weechat-get-local-var "nick" buffer-ptr))
(weechat-reset-buffer-modified buffer-ptr))
;; General activity
((memq line-type weechat-buffer-activity-types)
(puthash :background-message
(weechat-buffer-modified-update-hash
(gethash :background-message hash)
line-data)
hash)
(when (buffer-live-p emacs-buffer)
(with-current-buffer emacs-buffer
(run-hooks 'weechat-buffer-background-message-hook)))))
;; Highlight
(when (eq 1 (cdr (assoc-string "highlight" line-data)))
(puthash :background-highlight
(weechat-buffer-modified-update-hash
(gethash :background-highlight hash)
line-data)
hash)
(when (buffer-live-p emacs-buffer)
(with-current-buffer emacs-buffer
(run-hooks 'weechat-buffer-background-highlight-hook))))))))
(defun weechat-window-configuration-change ()
"Resets modification dates for all visible buffers."
(dolist (b (weechat-visible-buffers))
(with-current-buffer b
(weechat-reset-buffer-modified weechat-buffer-ptr))))
(add-hook 'window-configuration-change-hook 'weechat-window-configuration-change)
;;; Borrowed this behavior from rcirc
(defvar weechat-prompt-start-marker)
(defvar weechat-prompt-end-marker)
(defun weechat-update-prompt ()
(save-excursion
(let ((start (marker-position weechat-prompt-start-marker))
(inhibit-read-only t))
(setq weechat-local-prompt
(format-spec weechat-prompt
(format-spec-make ?n (weechat-get-local-var "nick"))))
(delete-region weechat-prompt-start-marker weechat-prompt-end-marker)
(goto-char weechat-prompt-end-marker)
(insert-before-markers weechat-local-prompt)
(set-marker weechat-prompt-start-marker start)
(unless (zerop (- weechat-prompt-end-marker
weechat-prompt-start-marker))
(add-text-properties weechat-prompt-start-marker
weechat-prompt-end-marker
(list 'face 'weechat-prompt-face
'read-only t
'field t
'rear-nonsticky t
'front-sticky t))))))
(cl-defun weechat-notify (type &key sender text date buffer-ptr)
(when (and (memq type weechat-notification-types)
(or (eq weechat-notification-mode t)
(and (eql weechat-notification-mode :monitored)
(local-variable-p 'weechat-buffer-ptr)
(buffer-live-p (weechat--emacs-buffer weechat-buffer-ptr)))))
(dolist (fn weechat-notification-handler-functions)
(with-demoted-errors
(funcall fn type sender text date buffer-ptr)))))
(defun weechat-buffer-p (&optional buffer)
"Return non-nil if buffer is a WeeChat buffer."
(eq 'weechat-mode (buffer-local-value 'major-mode
(or buffer (current-buffer)))))
(defun weechat-narrow-to-line ()
(interactive)
(unless (weechat-buffer-p)
(error "No weechat-mode buffer"))
(when (> (point) weechat-prompt-start-marker)
(error "Only narrowing to lines is supported"))
(narrow-to-region (point-at-bol) (min (point-at-eol)
weechat-prompt-start-marker)))
(defun weechat-truncate-buffer ()
(when (integerp weechat-buffer-line-limit)
(save-excursion
(save-restriction
(widen)
(let ((lines-to-delete (- (- weechat-buffer-line-limit
(count-lines (point-min) (point-max)))))
(inhibit-read-only t))
(when (> lines-to-delete 0)
(goto-char (point-min))
(forward-line lines-to-delete)
(delete-region (point-min) (point))))))))
(defun weechat-line-add-properties (nick date highlight invisible)
"Add various text properties (read-only, etc.) to a line.
Must be called with `weechat-narrow-to-line' active."
;; Add `date' and `highlighted' to the whole line
(add-text-properties (point-min) (point-max)