summary refs log tree commit diff stats
path: root/lisp/acdw-irc.el
blob: 0891f31bd1b9b05a1a7a08a3d0a6b559ae794df6 (plain)
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
;;; acdw-irc.el -*- lexical-binding: t; coding: utf-8-unix -*-

(require 's nil :noerror)

(defgroup acdw-irc nil
  "Customizations for IRC."
  :group 'applications)

(defcustom acdw-irc/left-margin 16
  "The size of the margin for nicks, etc. on the left."
  :type 'integer)

(defcustom acdw-irc/pre-nick ""
  "What to show before a nick."
  :type 'string)

(defcustom acdw-irc/post-nick " | "
  "What to show after a nick."
  :type 'string)

(defcustom acdw-irc/pre-my-nick "-"
  "What to show before the current user's nick."
  :type 'string)

(defcustom acdw-irc/post-my-nick "-> "
  "What to show after the current user's nick."
  :type 'string)

(defcustom acdw-irc/ellipsis "~"
  "The ellipsis for when a string is too long."
  :type 'string)


;;; Convenience functions (I don't want to /depend/ on s.el)

(if (fboundp 's-repeat)
    (defalias 'repeat-string 's-repeat)
  (defun repeat-string (num s)
    "Make a string of STR repeated NUM times.
Stolen from s.el."
    (declare (pure t) (side-effect-free t))
    (let (ss)
      (while (> num 0)
        (setq ss (cons str ss)
              num (1- num)))
      (apply #'concat ss))))

(if (fboundp 's-truncate)
    (defalias 'truncate-string 's-truncate)
  (defun truncate-string (len s &optional ellipsis)
    "If STR is longer than LEN, cut it down and add ELLIPSIS to the end.
When not specified, ELLIPSIS defaults to '...'."
    (declare (pure t) (side-effect-free t))
    (unless ellipsis
      (setq ellipsis "..."))
    (if (> (length s) len)
        (format "%s%s" (substring s 0 (- len (length ellipsis))) ellipsis)
      s)))


;;; IRC stuff

(defun acdw-irc/margin-format (str &optional before after alignment)
  "Print STR to fit in `acdw-irc/left-margin'.
Optional arguments BEFORE and AFTER specify strings to go
... before and after the string.  ALIGNMENT aligns left on nil
and right on t."
  (let* ((before (or before ""))
         (after (or after ""))
         (str-length (length str))
         (before-length (length before))
         (after-length (length after))
         (max-length (- acdw-irc/left-margin 1 (+ before-length after-length)))
         (left-over (max 0 (- max-length str-length))))
    (format "%s%s%s%s%s"
            before
            (if alignment (repeat-string left-over " ") "")
            (truncate-string max-length str acdw-irc/ellipsis)
            (if alignment "" (repeat-string left-over " "))
            after)))

(defun irc ()
  "Connect to all IRC networks in `circe-network-options'."
  (interactive)
  (dolist (network (mapcar #'car circe-network-options))
    (circe-maybe-connect network)))

(defun circe-network-connected-p (network)
  "Return non-nil if there's any Circe server-buffer whose
`circe-server-netwok' is NETWORK."
  (catch 'return
    (dolist (buffer (circe-server-buffers))
      (with-current-buffer buffer
        (if (string= network circe-server-network)
            (throw 'return t))))))

(defun circe-maybe-connect (network)
  "Connect to NETWORK, but ask user for confirmation if it's
already been connected to."
  (interactive "sNetwork: ")
  (if (or (not (circe-network-connected-p network))
          (y-or-n-p (format "Already connected to %s, reconnect?" network)))
      (circe network)))


(provide 'acdw-irc)
;;; acdw-irc.el ends here