Repository navigation
Expand file tree
/
Copy pathteams4e-compose.el
More file actions
549 lines (510 loc) · 26.9 KB
/
Copy pathteams4e-compose.el
File metadata and controls
549 lines (510 loc) · 26.9 KB
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
;;; teams4e-compose.el --- Org and Markdown Teams composition -*- lexical-binding: t; -*-
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; Real authoring modes share the existing Teams draft and send pipeline.
;; Only the outgoing body is converted; the draft always remains source text.
;;; Code:
(require 'teams4e-advanced)
(require 'ox-html)
(declare-function markdown-mode "markdown-mode" ())
(declare-function evil-insert-state "evil-states" (&optional arg))
(defvar evil-local-mode)
(defvar teams4e-compose-edit-mode)
(autoload 'teams4e-suggest-reply "teams4e-reply-agent" nil t)
(defvar teams4e-compose--register-image nil
"During immediate send, function mapping a local image path to a hosted ID.")
(defun teams4e-compose--image-directory ()
"Return the directory for inline images owned by this draft."
(concat (or teams4e-compose--draft-file (teams4e-compose--path)) ".images"))
(defun teams4e-compose--import-image (extension writer)
"Insert an Org image link after WRITER saves image bytes to a new file."
(unless (and (teams4e-compose-p) (derived-mode-p 'org-mode))
(user-error "Open an Org Teams draft to insert an inline image"))
(unless (member extension '("png" "jpg" "jpeg" "gif"))
(user-error "Choose a PNG, JPEG or GIF; use C-c C-a for other files"))
(let ((directory (teams4e-compose--image-directory)) path done)
(make-directory directory t)
(set-file-modes directory #o700)
(setq path (make-temp-file (expand-file-name "image-" directory) nil
(concat "." extension)))
(unwind-protect
(progn
(funcall writer path)
;; Terminal-only Emacs may not have an image decoder.
(unless (with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally path nil 0 8)
(goto-char (point-min))
(looking-at-p (regexp-opt '("\211PNG\r\n\032\n" "\377\330\377"
"GIF87a" "GIF89a"))))
(user-error "No PNG, JPEG or GIF image found; draft unchanged"))
(insert (org-link-make-string (concat "file:" path)))
(setq done t)
(when (display-images-p) (org-display-inline-images)))
(unless done (delete-file path)))))
(defun teams4e-compose-insert-image (file)
"Insert a local image at point and keep a private copy with the Org draft."
(interactive "fInsert image: ")
(when (or (file-remote-p file) (not (file-regular-p file)))
(user-error "Choose a local image file"))
(teams4e-compose--import-image
(downcase (or (file-name-extension file) ""))
(lambda (path) (copy-file file path t) (set-file-modes path #o600))))
(defun teams4e-compose-paste-inline-image ()
"Insert the desktop clipboard image at point in this Org draft."
(interactive)
(let* ((args (cond ((eq system-type 'darwin) '("pngpaste" "-"))
((getenv "WAYLAND_DISPLAY") '("wl-paste" "-t" "image/png"))
((getenv "DISPLAY") '("xclip" "-selection" "clipboard" "-t" "image/png" "-o"))
(t (user-error "No desktop clipboard; use C-c i to insert a file"))))
(program (or (executable-find (car args))
(user-error "Clipboard paste requires %s" (car args)))))
(teams4e-compose--import-image
"png" (lambda (path)
(with-temp-buffer
(set-buffer-multibyte nil)
(let ((coding-system-for-read 'no-conversion)
(coding-system-for-write 'no-conversion))
(unless (equal 0 (apply #'call-process program nil (list t nil) nil (cdr args)))
(user-error "Could not read an image from the clipboard"))
(write-region (point-min) (point-max) path nil 'silent)))))))
(defcustom teams4e-compose-editor 'org
"Editor for new Teams drafts.
Org uses the built-in HTML exporter. Markdown requires markdown-mode and
Pandoc. Text keeps the original plain text/direct HTML composer.
Saved drafts retain their editor; drafts predating this option use text."
:type '(choice (const org) (const markdown) (const text))
:group 'teams4e)
(defcustom teams4e-compose-pandoc-program "pandoc"
"Pandoc executable used to render Markdown messages to Teams HTML."
:type 'file
:group 'teams4e)
(defcustom teams4e-compose-window-height 0.4
"Fraction of the reader window used for the reply editor.
The transcript remains above it. On small frames, use normal buffer
display if there is insufficient room to split."
:type 'float
:group 'teams4e)
(defvar-local teams4e-compose--window nil
"Window created for this composer, if any.")
(defvar-local teams4e-compose--command-maps nil
"Compose commands above Org/Evil emulation maps in this draft only.")
;; A draft belongs to its buffer, not to one initialization of its editor.
;; In particular, losing schedule/reply metadata must never change delivery.
(dolist (variable '(teams4e-compose-edit-mode
teams4e-compose--target teams4e-compose--origin
teams4e-compose--reply-to teams4e-compose--attachments
teams4e-compose--mentions teams4e-compose--content-type
teams4e-compose--editor teams4e-compose--draft-file
teams4e-compose--draft-timer teams4e-compose--discarded
teams4e-compose--schedule teams4e-compose--outbox-id
teams4e-compose--outbox-revision teams4e-compose--outbox-busy
teams4e-compose--window))
(put variable 'permanent-local t))
(defun teams4e-compose--current-editor ()
"Return the supported authoring format of the current major mode."
(cond ((derived-mode-p 'org-mode) 'org)
((derived-mode-p 'markdown-mode) 'markdown)
((derived-mode-p 'text-mode) 'text)))
(defun teams4e-compose--after-major-mode ()
"Restore draft commands and hooks after the authoring mode is reinitialized."
(when teams4e-compose-edit-mode
(when-let ((editor (teams4e-compose--current-editor)))
(setq teams4e-compose--editor editor))
(teams4e-compose-edit-mode 1)
(setq-local require-final-newline nil
buffer-file-coding-system 'utf-8-unix)
(visual-line-mode 1)
(add-hook 'kill-buffer-hook #'teams4e-compose--close-window nil t)
(when teams4e-compose--target
(teams4e-compose--update-header))))
(put 'teams4e-compose--after-major-mode 'permanent-local-hook t)
(defun teams4e-compose--install-bindings ()
"Install draft commands without shadowing the authoring mode's other keys."
(add-hook 'after-change-major-mode-hook
#'teams4e-compose--after-major-mode t t)
(let ((map (copy-keymap teams4e-compose-mode-map))
(commands (make-sparse-keymap)))
(set-keymap-parent map nil)
(when (derived-mode-p 'org-mode)
(define-key map (kbd "C-c i") #'teams4e-compose-insert-image)
(define-key map (kbd "C-c C-p") #'teams4e-compose-paste-inline-image)
(define-key commands [remap org-download-clipboard] #'teams4e-compose-paste-inline-image))
(define-key map (kbd "C-<return>") #'teams4e-compose-send)
(setq-local minor-mode-overriding-map-alist
(cons (cons 'teams4e-compose-edit-mode map)
(assq-delete-all 'teams4e-compose-edit-mode
minor-mode-overriding-map-alist)))
;; Ordinary minor-mode overrides lose to Org/Evil emulation bindings.
;; Elevate only the draft command prefix, not text entry or Vim keys.
(define-key commands (kbd "C-c") (lookup-key map (kbd "C-c")))
(define-key commands (kbd "C-<return>") #'teams4e-compose-send)
(define-key commands (kbd "M-.") #'teams4e-suggest-reply)
(define-key commands [remap org-ctrl-c-ctrl-c] #'teams4e-compose-send)
(setq-local teams4e-compose--command-maps
(list (cons 'teams4e-compose-edit-mode commands)))
(setq-local emulation-mode-map-alists
(cons 'teams4e-compose--command-maps
(remq 'teams4e-compose--command-maps emulation-mode-map-alists)))))
(define-minor-mode teams4e-compose-edit-mode
"Teams send, draft and attachment commands over an authoring major mode."
:lighter " Teams"
:keymap (make-sparse-keymap)
(if teams4e-compose-edit-mode
(progn
(teams4e-compose--install-bindings)
(unless (derived-mode-p 'teams4e-compose-mode)
(run-hooks 'teams4e-compose-mode-hook)))
(setq-local teams4e-compose--command-maps nil
emulation-mode-map-alists
(remq 'teams4e-compose--command-maps emulation-mode-map-alists))
(remove-hook 'after-change-major-mode-hook
#'teams4e-compose--after-major-mode t)
(remove-hook 'after-change-functions #'teams4e-compose--schedule-draft t)
(remove-hook 'kill-buffer-hook #'teams4e-compose--save-draft t)
(setq minor-mode-overriding-map-alist
(assq-delete-all 'teams4e-compose-edit-mode
minor-mode-overriding-map-alist))))
;;;###autoload
(defun teams4e-compose-repair ()
"Restore Teams state and keys without changing the current draft's text.
If a mode reset erased the target, offer metadata recovery from exactly one
matching saved draft, or ask which recovery to use. Never guess a recipient,
send, replace the text, reopen the buffer, or change its major mode."
(interactive)
(when (or teams4e-compose--target
(teams4e-compose--recover-metadata))
(teams4e-compose-edit-mode 1)
(teams4e-compose--update-header)
(message "Teams draft keys restored: C-c C-c or C-RET sends; C-c C-k aborts")))
(defun teams4e-compose--saved-metadata ()
"Return saved (FILE PAYLOAD TARGET REPLY) records with consistent identities."
(let (records)
(when (file-directory-p teams4e-draft-directory)
(dolist (file (directory-files teams4e-draft-directory t
"\\`[[:xdigit:]]\\{32\\}\\.json\\'"))
(when-let* ((payload (teams4e-compose--read-draft file))
(target (teams4e-compose--record-target
(teams4e--get payload 'target))))
(let ((reply (teams4e--get payload 'replyTo)))
(when (equal (file-name-nondirectory file)
(concat (md5 (teams4e--compose-target-key target reply)) ".json"))
(push (list file payload target reply) records))))))
records))
(defun teams4e-compose--matching-metadata (records)
"Find RECORDS matching the stable draft hash, independent of display names."
(when (string-match "\\`\\*Teams Compose: .* \\[\\([[:xdigit:]]\\{6\\}\\)\\]\\*\\(?:<[0-9]+>\\)?\\'"
(buffer-name))
(let ((hash (match-string 1 (buffer-name))))
(cl-remove-if-not
(lambda (record)
(string-prefix-p hash (file-name-nondirectory (car record))))
records))))
(defun teams4e-compose--recover-metadata ()
"Recover lost draft metadata only after matching and confirming a saved draft.
The buffer name is a lookup hint, never authority to send to a recipient.
Scheduled drafts need a fresh outbox hold and are deliberately not recovered
through this synchronous command."
(unless (string-prefix-p "*Teams Compose: " (buffer-name))
(user-error "Not a recognized draft; teams4e-compose-recover offers explicit recovery"))
(let* ((records (teams4e-compose--saved-metadata))
(matches (teams4e-compose--matching-metadata records)))
(if (= (length matches) 1)
(teams4e-compose--restore-metadata (car matches))
(teams4e-compose--choose-recovery records))))
(defun teams4e-compose--other-draft-buffer (target &optional reply)
"Return another live buffer owning TARGET and REPLY, if any."
(let ((here (current-buffer))
(key (teams4e--compose-target-key target reply)))
(cl-find-if
(lambda (buffer)
(and (not (eq buffer here))
(with-current-buffer buffer
(and teams4e-compose--target
(equal key (teams4e-compose--target-key))))))
(buffer-list))))
(defun teams4e-compose--restore-metadata (record)
"Confirm and restore RECORD's metadata, never its saved body; return t."
(pcase-let* ((`(,file ,payload ,target ,reply) record)
(editor (pcase (teams4e--get payload 'editor)
("org" 'org) ("markdown" 'markdown) (_ 'text))))
(when (or (teams4e--get payload 'schedule)
(teams4e--get payload 'outboxId))
(user-error "Scheduled draft needs an outbox hold; text retained, recovery cannot enable immediate send"))
(unless (eq editor (teams4e-compose--current-editor))
(user-error "Saved draft uses %s; switch to that authoring mode before repair" editor))
(when (teams4e-compose--other-draft-buffer target reply)
(user-error "That draft is already open in another buffer; no metadata replaced"))
;; Validate the complete destination and reply ID without sending.
(teams4e--send-args target "" reply)
(unless (yes-or-no-p
(format "Restore saved destination %s [%s]%s and %d attachment(s), keeping this text? "
(teams4e--target-label target)
(teams4e--compose-target-key target)
(if reply (format " (reply %s)" (teams4e--get reply 'id)) "")
(length (teams4e--get payload 'attachments))))
(user-error "Recovery canceled; draft unchanged"))
(when (or teams4e-compose--target
(teams4e-compose--other-draft-buffer target reply))
(user-error "Draft ownership changed during confirmation; nothing replaced"))
(setq-local teams4e-compose--target target
teams4e-compose--reply-to reply
teams4e-compose--attachments (teams4e--get payload 'attachments)
teams4e-compose--mentions (teams4e--get payload 'mentions)
teams4e-compose--content-type
(or (teams4e--get payload 'contentType) "text")
teams4e-compose--editor editor
teams4e-compose--draft-file file
teams4e-compose--discarded nil)
t))
;;;###autoload
(defun teams4e-compose-recover ()
"Explicitly recover this buffer's Teams destination, without sending.
Choose saved metadata or explicitly start a NEW message to a selected chat.
The latter cannot recover old reply, attachment, mention or scheduling data."
(interactive)
(when (or teams4e-compose--target
(teams4e-compose--choose-recovery (teams4e-compose--saved-metadata)))
(teams4e-compose-repair)))
(defun teams4e-compose--choose-recovery (records)
"Ask which of RECORDS to restore, or offer explicit new-message recovery."
(unless (teams4e-compose--current-editor)
(user-error "Recovery requires an Org, Markdown or Text buffer"))
(when buffer-file-name
(user-error "Recovery is for draft buffers, not files; no destination attached"))
(when (or teams4e-compose--schedule teams4e-compose--outbox-id
teams4e-compose--outbox-revision teams4e-compose--outbox-busy
(string-prefix-p "*Teams scheduled:" (buffer-name))
(cl-some (lambda (record)
(or (teams4e--get (nth 1 record) 'schedule)
(teams4e--get (nth 1 record) 'outboxId)))
(teams4e-compose--matching-metadata records)))
(user-error "Scheduled draft needs an outbox hold; recovery cannot enable immediate send"))
(let* ((cancel "Cancel; keep this buffer unchanged")
(new "NEW message: choose conversation; old reply/attachments not restored")
(pairs (mapcar
(lambda (record)
(list (format "Saved: %s%s [%s]"
(teams4e--target-label (nth 2 record))
(if (nth 3 record)
(format " / reply %s" (teams4e--get (nth 3 record) 'id)) "")
(file-name-base (car record)))
record)) records))
(choice (completing-read "Recover Teams destination: "
(append (list cancel new) (mapcar #'car pairs))
nil t nil nil cancel)))
(cond ((equal choice new) (teams4e-compose--recover-new-message) nil)
((assoc choice pairs)
(teams4e-compose--restore-metadata (cadr (assoc choice pairs))))
(t (user-error "Recovery canceled; draft unchanged")))))
(defun teams4e-compose--recover-new-message ()
"Select and confirm a new chat destination without replacing current text."
(let ((buffer (current-buffer))
(name (buffer-name))
(editor (teams4e-compose--current-editor)))
(teams4e--select-chat
(lambda (target)
(when (buffer-live-p buffer)
(with-current-buffer buffer
(unless (and (not teams4e-compose--target)
(not teams4e-compose--schedule)
(not teams4e-compose--outbox-id)
(not teams4e-compose--outbox-revision)
(not teams4e-compose--outbox-busy)
(equal name (buffer-name))
(eq editor (teams4e-compose--current-editor)))
(user-error "Draft changed while choosing its destination; nothing replaced"))
(teams4e--send-args target "")
(let* ((destination (teams4e--compose-buffer-name target))
(existing (get-buffer destination))
(file (expand-file-name
(concat (md5 (teams4e--compose-target-key target)) ".json")
teams4e-draft-directory)))
(when (or (and existing (not (eq existing buffer)))
(teams4e-compose--other-draft-buffer target)
(file-exists-p file))
(user-error "A draft for that chat already exists; choose saved metadata or keep both drafts separate"))
(unless (yes-or-no-p
(format "Use this text as a NEW message to %s [%s], WITHOUT the old reply/attachments/mentions/schedule? "
(teams4e--target-label target)
(teams4e--compose-target-key target)))
(user-error "Recovery canceled; draft unchanged"))
;; Minibuffer confirmation can dispatch asynchronous callbacks.
(when (or teams4e-compose--target
(not (equal name (buffer-name)))
(not (eq editor (teams4e-compose--current-editor)))
teams4e-compose--schedule teams4e-compose--outbox-id
teams4e-compose--outbox-revision teams4e-compose--outbox-busy
(teams4e-compose--other-draft-buffer target)
(let ((other (get-buffer destination)))
(and other (not (eq other buffer))))
(file-exists-p file))
(user-error "Draft ownership changed during confirmation; nothing replaced"))
(setq-local teams4e-compose--target target
teams4e-compose--reply-to nil
teams4e-compose--attachments nil
teams4e-compose--mentions nil
teams4e-compose--origin nil
teams4e-compose--editor editor
teams4e-compose--content-type teams4e-default-content-type
teams4e-compose--draft-file file
teams4e-compose--discarded nil)
(rename-buffer destination)
(teams4e-compose-repair)
(teams4e-compose--save-draft)
(message "NEW Teams draft recovered; review destination before C-c C-c. Old reply/attachments were not restored."))))))))
(defun teams4e-compose--start-editor (target reply-to)
"Start the appropriate editor for TARGET and REPLY-TO, including drafts."
(let* ((file (expand-file-name
(concat (md5 (teams4e--compose-target-key target reply-to))
".json")
teams4e-draft-directory))
(draft (and (file-readable-p file)
(teams4e-compose--read-draft file)))
(editor (if draft
(pcase (teams4e--get draft 'editor)
("org" 'org)
("markdown" 'markdown)
(_ 'text))
teams4e-compose-editor))
;; Major-mode initialization clears window ownership.
(window teams4e-compose--window))
(pcase editor
('org (org-mode))
('markdown
(unless (require 'markdown-mode nil t)
(user-error "Install markdown-mode to compose Teams Markdown"))
(unless (executable-find teams4e-compose-pandoc-program)
(user-error "Install Pandoc to send Teams Markdown"))
(markdown-mode))
('text (teams4e-compose-mode))
(_ (user-error "Unknown Teams compose editor: %s" editor)))
(setq-local teams4e-compose--editor editor
teams4e-compose--window window
require-final-newline nil
buffer-file-coding-system 'utf-8-unix)
(teams4e-compose-edit-mode 1)
(visual-line-mode 1)
(add-hook 'kill-buffer-hook #'teams4e-compose--close-window nil t)
(when (bound-and-true-p evil-local-mode)
(evil-insert-state))))
(defun teams4e-compose--html-link (link desc info)
"Render inline file images as hosted content; delegate other links to Org."
(if (and (equal "file" (org-element-property :type link))
(org-export-inline-image-p link org-html-inline-image-rules))
(progn
(unless teams4e-compose--register-image
(user-error "Inline images require immediate send; scheduled draft retained"))
(format "<img src=\"../hostedContents/%d/$value\" />"
(funcall teams4e-compose--register-image (org-element-property :path link))))
(org-html-link link desc info)))
(org-export-define-derived-backend 'teams4e-message 'html
:translate-alist '((link . teams4e-compose--html-link)))
(defun teams4e-compose--org-html (source &optional messagep)
"Render SOURCE as a Teams HTML fragment without executing Org code.
External includes, setup files, BIND/CALL directives and macros are not
supported in messages. Refuse them rather than silently reading files
or evaluating content pasted into a draft."
(let ((case-fold-search t))
(when (or (string-match-p
"^[ \t]*#\\+\\(?:INCLUDE\\|SETUPFILE\\|BIND\\|CALL\\|MACRO\\):"
source)
(string-match-p (regexp-quote "{{{") source))
(user-error "Teams Org messages cannot use includes, setup files, macros or calls")))
(let ((org-export-use-babel nil)
(org-export-allow-bind-keywords nil)
(org-export-global-macros nil)
(org-export-before-processing-functions nil)
(org-export-before-parsing-functions nil)
(org-export-filter-final-output-functions nil)
(org-html-htmlize-output-type nil))
(org-export-string-as
source (if messagep 'teams4e-message 'html) t
'(:with-toc nil :section-numbers nil :with-author nil :with-date nil
:with-title nil :with-sub-superscript nil :with-latex nil
:html-preamble nil :html-postamble nil))))
(defun teams4e-compose--markdown-html (source)
"Render Markdown SOURCE with Pandoc, without shell interpolation."
(let ((program (executable-find teams4e-compose-pandoc-program)))
(unless program (user-error "Install Pandoc to send Teams Markdown"))
(with-temp-buffer
(insert source)
(let ((coding-system-for-read 'utf-8-unix)
(coding-system-for-write 'utf-8-unix)
(errors (make-temp-file "teams4e-pandoc-")))
(unwind-protect
(let ((status
(call-process-region
(point-min) (point-max) program t (list t errors) nil
"--from=gfm" "--to=html5" "--wrap=none")))
(unless (eq status 0)
(user-error "Markdown conversion failed; draft retained: %s"
(string-trim
(with-temp-buffer
(insert-file-contents errors)
(buffer-string)))))
(buffer-string))
(delete-file errors))))))
(defun teams4e-compose--render-body (source)
"Return (CONTENT-TYPE . BODY) for SOURCE in the current Teams editor."
(when (and teams4e-compose-edit-mode
(not (teams4e-compose--current-editor)))
(user-error "Switch this draft back to Org, Markdown or Text mode before sending"))
(pcase teams4e-compose--editor
('org (cons "html" (if (string-empty-p source) ""
(teams4e-compose--org-html source t))))
('markdown (cons "html" (if (string-empty-p source) ""
(teams4e-compose--markdown-html source))))
(_ (cons teams4e-compose--content-type source))))
(defun teams4e-compose--close-window ()
"Remove only the split owned by this composer, never a frame."
(when (and (window-live-p teams4e-compose--window)
(eq (window-buffer teams4e-compose--window) (current-buffer))
(not (one-window-p t)))
(condition-case nil
(delete-window teams4e-compose--window)
(error nil))))
(defun teams4e-compose--display (buffer origin target)
"Show BUFFER below the matching transcript from ORIGIN for TARGET."
(let* ((existing (get-buffer-window buffer))
(reader
(cond
((with-current-buffer origin
(derived-mode-p 'teams4e-read-mode))
origin)
((and (with-current-buffer origin
(derived-mode-p 'teams4e-recent-mode))
(teams4e--chat-id target))
(teams4e-open-chat target)
(get-buffer teams4e--read-buffer-name))))
(window (and reader (get-buffer-window reader))))
(cond
(existing (select-window existing))
(window
(let ((editor-window
(condition-case nil
(split-window
window
(- (max window-min-height
(floor (* (window-total-height window)
(max 0.2 (min 0.7
teams4e-compose-window-height)))))))
(error nil))))
(if (not editor-window)
(pop-to-buffer buffer)
(set-window-buffer editor-window buffer)
(with-selected-window window
(goto-char (point-max))
(recenter -1))
(select-window editor-window)
(with-current-buffer buffer
(setq teams4e-compose--window editor-window)
(add-hook 'kill-buffer-hook #'teams4e-compose--close-window nil t)))))
(t (pop-to-buffer buffer)))))
;; Reloading this file repairs already-open Org/Markdown drafts too.
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (bound-and-true-p teams4e-compose-edit-mode)
(teams4e-compose--install-bindings))))
(provide 'teams4e-compose)
;;; teams4e-compose.el ends here