forked from Difrex/idec.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidec-mode.el
376 lines (308 loc) · 12 KB
/
idec-mode.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
;;; idec-mode.el --- This file part of GNU Emacs client for IDEC network
;; Copyright (c) 2017 Denis Zheleztsov
;; Author: Denis Zheleztsov <[email protected]>
;; Keywords: lisp,network,IDEC
;; Version: 0.1
;; 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:
;; In active developent.
;; Fetched node must be support modern IDEC extensions like /list.txt, /x/c, etc.
;;; Code:
(require 'idec)
(require 'idec-online)
(defgroup idec nil
"IDEC configuration."
:group 'network)
;; Not used
(defcustom idec-nodes-list ""
"List(comma separated) of IDEC nodes;
http://idec.spline-online.tk/,https://ii-net.tk/ii/ii-point.php?q=/;
Currently not used."
:type 'string
:group 'idec)
(defcustom idec-primary-node ""
"Primary node to send messages."
:type 'string
:group 'idec)
;; Never used at this time.
(defcustom idec-use-list-txt t
"Use /list.txt extension."
:type 'boolean
:group 'idec)
(defcustom idec-smart-fetch t
"Enable smart fetching;
Download only new messages; Not implemented."
:type 'boolean
:group 'idec)
(defcustom idec-download-limit "50"
"Limit of download messages;
Not used if `idec-smart-fetching' is not nil."
:type 'string
:group 'idec)
(defcustom idec-download-offset "-50"
"Offset of download messages;
Not used if `idec-smart-fetching' is not nil."
:type 'string
:group 'idec)
(defcustom idec-echo-subscriptions "idec.talks,pipe.2032"
"List(comma separated string) of subribes echoes."
:type 'string
:group 'idec)
(defcustom idec-mail-dir (concat user-emacs-directory "idec-mail")
"Directory to store mail."
:type 'string
:group 'idec)
(defcustom idec-online-download-limit "0"
"Download limit on online browsing;
Default to `idec-download-lmit'"
:type 'string
:group 'idec)
(defcustom idec-online-download-offset "0"
"Download limit on online browsing;
Default to `idec-download-offset'"
:type 'string
:group 'idec)
(defcustom idec-desc-local-echo-sort nil
"Sort order in local echo display mode."
:type 'boolean
:group 'idec)
(defcustom idec-message-signature "Sent from my GNU Emacs"
"Message signatue."
:type 'string
:group 'idec)
(defcustom idec-attach-message-signature nil
"Add or not signature to message."
:type 'boolean
:group 'idec)
(defgroup idec-accounts nil
"IDEC accounts settings."
:group 'idec)
(defcustom idec-account-nick ""
"Account nickname."
:type 'string
:group 'idec-accounts)
(defcustom idec-account-node ""
"Node to send messages."
:type 'string
:group 'idec-accounts)
(defcustom idec-account-auth ""
"Account authstring."
:type 'string
:group 'idec-accounts)
;; END OF CUSTOMIZATION
;; ;;;;;;;;;;;;;;;;;;;;
;; VARIABLES
;; ;;;;;;;;;
(defvar smart-download-limit nil
"Used with `idec-smart-fetch'.")
(defvar smart-download-offset nil
"Used with `idec-smart-fetch'.")
(defvar new-messages-list nil
"New messages for display.")
(setq idec-online-download-limit idec-download-limit)
(setq idec-online-download-offset idec-download-offset)
;; END OF VARIABLES
;; ;;;;;;;;;;;;;;;;
;; MODE
;; ;;;;
(defun idec-close-message-buffer ()
"Close buffer with message."
(kill-this-buffer))
(defvar idec-mode-hook nil)
(defvar idec-mode-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "n" 'idec-new-message)
map)
"Keymapping for IDEC mode.")
(defvar idec-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?# "<" st)
(modify-syntax-entry ?\n ">" st)
st))
(defconst idec-font-lock-keywords-1
(list
'("\\(ID:.*\\)" . font-lock-function-name-face)
'("\\<\\(\\(?:Echo\\|From\\|Subj\\|T\\(?:ime\\|o\\)\\):\\)\\>" . font-lock-function-name-face))
"Minimal highlighting expressions for IDEC mode.")
(defconst idec-font-lock-keywords-2
(append idec-font-lock-keywords-1 (list
'("\\<\\(>>?.*\\)\s\\>" . font-lock-comment-face)))
"Quotes highligting for IDEC mode.")
(defvar idec-font-lock-keywords idec-font-lock-keywords-1
;; '(("ID" (1 font-lock-function-name-face)))
"Default highlighting expressions for IDEC mode.")
;; Mode function
(define-generic-mode
'idec
'("//" ">" "ЗЫ")
'("ii://" "ID" "$subj" "сабж" "субж"
"Subject" "From" "To" "Echo" "At")
'(("=" . 'font-lock-operator)
(";" . 'font-lock-builtin))
'("\\.*idec\\.*|\\.*Idec\\.*|\\.*IDEC\\.*$")
nil)
(define-derived-mode org-idec text-mode "IDEC"
"Major mode for view and editing IDEC messages."
:syntax-table idec-mode-syntax-table
(setq-local comment-start "> ")
(setq-local font-lock-defaults
'(idec-font-lock-keywords))
(use-local-map idec-mode-map)
(setq-local indent-line-function 'org-indent-line)
(setq imenu-generic-expression "*IDEC")
(setq mode-name "[IDEC]")
(run-hooks 'idec-mode-hook))
(define-derived-mode idec-mode nil "idec-mode" ()
"Major mode for view and editing IDEC messages."
:syntax-table idec-mode-syntax-table
(kill-all-local-variables)
;; Mode definition
(set-syntax-table idec-mode-syntax-table)
(use-local-map idec-mode-map)
(setq major-mode 'idec-mode)
(setq mode-name "[IDEC]")
(setq imenu-generic-expression "*IDEC")
(run-hooks 'idec-mode-hook))
(defun idec-mark-all-as-read (&optional echo)
"Mark all messages in ECHO as read."
(interactive)
(if (not echo)
(mark-all-messages-as-read (read-string "Enter echo name: "))
(mark-all-messages-as-read echo)))
(defun idec-local-browse (&optional checkpoint)
"Browse local mail from `idec-mail-dir';
optionaly return cursor to CHECKPOINT."
(interactive)
(with-output-to-temp-buffer (get-buffer-create "*IDEC: INBOX*")
(switch-to-buffer "*IDEC: INBOX*")
(insert "* LOCAL ECHOAREAS\n")
(save-excursion
(dolist (echo (get-local-echoes))
(if echo
(let (unread start end)
;; Echo name with unread messages
;; ii.test.14 (5)*
(insert-button echo
'action (lambda (x) (idec-browse-local-echo (button-get x 'echo)))
'echo echo)
(beginning-of-line)
(setq start (point))
(end-of-line)
(setq end (point))
(put-text-property start end 'font-lock-face '(:foreground "light blue"))
(princ (concat (dots echo)
"("
(number-to-string (get-echo-messages-count echo))
")"))
(setq unread (get-echo-unread-messages echo))
;; (when (> unread 0)
;; (princ "*"))
(princ " ")
;; [New message] button
(princ " [")
(insert-button "New message"
'action (lambda (x) (idec-new-message (button-get x 'echo)))
'echo echo)
(princ "]\t[")
;; [Mark read] button
(insert-button "Mark read"
'action (lambda (x) (mark-all-as-read (button-get x 'echo) (button-get x 'point)))
'echo echo
'point (point))
(princ "]")
;; Unread messages count
(when (> unread 0)
(princ (concat " unread (" (number-to-string unread) ")")))
(princ "\n"))
(message (concat "IDEC: FUUUUUU <" echo ">")))))
(when checkpoint
(goto-char checkpoint)))
(idec-mode))
(defun idec-browse-local-echo (&optional echo)
"Get messages from local ECHO."
(interactive)
(if (not echo)
(setq echo (read-string "Enter echo name: ")))
(let (longest)
(setq longest (+ 1 (longest-local-echo-subj echo)))
(with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: INBOX->(" echo ")") )
(switch-to-buffer (concat "*IDEC: INBOX->(" echo ")"))
(dolist (msg (get-echo-messages echo))
(setq subj-length (length (gethash "subj" msg)))
(insert-button (gethash "subj" msg)
'action (lambda (x) (display-message-hash (button-get x 'msg-hash)))
'subj (gethash "subj" msg)
'help-echo (concat "Read message *" (gethash "subj" msg) "*")
'msg-hash msg)
;; Mark by asterisk unread message
(if (= 1 (gethash "unread" msg))
(and
(princ "*")
(if (> longest subj-length)
(setq subj-length (+ subj-length 1)))))
(princ (make-string (- longest subj-length) ? ))
(princ (concat " " (gethash "time" msg)))
(princ (concat "\t" (gethash "author" msg) "\n")))))
(idec))
;; NAVIGATION FUNCTIONS
;; ;;;;;;;;;;;;;;;;;;;;
(defun idec-next-message ()
"Show next message."
(interactive)
(kill-this-buffer)
(forward-button 1)
(push-button))
(defun idec-previous-message ()
"Show next message."
(interactive)
(kill-this-buffer)
(backward-button 1)
(push-button))
;; END OF NAVIGATION FUNCTIONS
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun idec-load-new-messages ()
"Load new messages from IDEC `idec-primary-node'."
(interactive)
(defvar current-echo nil)
(setq new-messages-list (make-hash-table :test 'equal))
(let (msgid-for-download)
(setq msgid-for-download (make-hash-table :test 'equal))
(dolist (line (split-string (download-subscriptions) "\n"))
(if (string-match "\\." line)
(and (setq current-echo line)
(store-echo-counter line))
(when (and ;; (check-message-in-echo line current-echo)
(> (length line) 1)
(check-message-in-db line current-echo))
(when (not (string= "" line))
(puthash line current-echo msgid-for-download)))))
(download-message msgid-for-download))
;; (print (hash-table-count new-messages-list))
;; (message (gethash "id" (nth 0 new-messages-list)))
(display-new-messages)
)
(defun idec-new-message (&optional echo)
"Make new message to ECHO."
(interactive)
(if (not echo) (edit-new-message (read-string "Echo: "))
(edit-new-message echo)))
;; Online
(defun idec-online-browse ()
"Load echoes list.txt from node `idec-primary-node'."
(interactive)
(idec-fetch-echo-list (concat idec-primary-node "list.txt")))
(defun idec-online-browse-hidden ()
"Browse hidden echo."
(interactive)
(load-echo-messages (read-string "Enter echo name: ") t))
;;; idec-mode.el ends here
(provide 'idec-mode)