about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/slack-20180712.2222/slack-message-editor.el
blob: a1f8eb5ca06bfb74dfb670654f320ed434d42da9 (plain) (blame)
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
;;; slack-message-editor.el ---  edit message interface  -*- lexical-binding: t; -*-

;; Copyright (C) 2015  南優也

;; Author: 南優也 <yuyaminami@minamiyuunari-no-MacBook-Pro.local>
;; Keywords:

;; 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:

;;

;;; Code:
(require 'slack-util)
(require 'slack-room)
(require 'slack-message-sender)

(defconst slack-message-edit-url "https://slack.com/api/chat.update")
(defconst slack-message-edit-buffer-name "*Slack - Edit message*")
(defconst slack-message-write-buffer-name "*Slack - Write message*")
(defconst slack-message-share-buffer-name "*Slack - Share message*")
(defconst slack-share-url "https://slack.com/api/chat.shareMessage")
(defvar slack-buffer-function)
(defvar slack-target-ts)
(make-local-variable 'slack-target-ts)
(defvar slack-message-edit-buffer-type)
(make-local-variable 'slack-message-edit-buffer-type)

(defvar slack-edit-message-mode-map
  (let ((keymap (make-sparse-keymap)))
    (define-key keymap (kbd "C-s C-m") #'slack-message-embed-mention)
    (define-key keymap (kbd "C-s C-c") #'slack-message-embed-channel)
    (define-key keymap (kbd "C-c C-k") #'slack-message-cancel-edit)
    (define-key keymap (kbd "C-c C-c") #'slack-message-send-from-buffer)
    keymap))

(define-derived-mode slack-edit-message-mode fundamental-mode "Slack Edit Msg"
  ""
  (slack-buffer-enable-emojify))

(defun slack-message-share ()
  (interactive)
  (slack-if-let* ((buf slack-current-buffer))
      (slack-buffer-share-message buf (slack-get-ts))))

(defun slack-message-share--send (team room ts msg)
  (let* ((slack-room-list (or (and (object-of-class-p room 'slack-channel)
                                   (slack-message-room-list team))
                              (list (cons (slack-room-display-name room)
                                          room))))
         (share-channel-id (oref (slack-select-from-list
                                     (slack-room-list
                                      "Select Channel: "
                                      :initial
                                      (slack-room-name room)))
                                 id)))
    (cl-labels
        ((on-success (&key data &allow-other-keys)
                     (slack-request-handle-error
                      (data "slack-message-share"))))
      (slack-request
       (slack-request-create
        slack-share-url
        team
        :type "POST"
        :params (list (cons "channel" (oref room id))
                      (cons "timestamp" ts)
                      (cons "text" (slack-message-prepare-links
                                    (slack-escape-message msg)
                                    team))
                      (cons "share_channel" share-channel-id))
        :success #'on-success)))))

(defun slack-message-write-another-buffer ()
  (interactive)
  (slack-if-let* ((buf slack-current-buffer))
      (slack-buffer-display-message-compose-buffer buf)))

(defmethod slack-message-get-user-id ((m slack-user-message))
  (oref m user))

(defun slack-message-edit ()
  (interactive)
  (slack-if-let* ((buf slack-current-buffer))
      (slack-buffer-display-edit-message-buffer buf (slack-get-ts))))


(defun slack-message-cancel-edit ()
  (interactive)
  (let ((buffer (slack-buffer-buffer slack-current-buffer)))
    (with-current-buffer buffer
      (erase-buffer)
      (if (> (count-windows) 1) (delete-window)))))

(defun slack-message-send-from-buffer ()
  (interactive)
  (slack-if-let* ((buf slack-current-buffer)
            (text (buffer-substring-no-properties (point-min) (point-max))))
      (slack-buffer-send-message buf text)))

(defun slack-message--edit (channel team ts text)
  (cl-labels ((on-edit (&key data &allow-other-keys)
                       (slack-request-handle-error
                        (data "slack-message--edit"))))
    (slack-request
     (slack-request-create
      slack-message-edit-url
      team
      :type "POST"
      :params (list (cons "channel" channel)
                    (cons "ts" ts)
                    (cons "text" (slack-message-prepare-links
                                  (slack-escape-message text)
                                  team)))
      :success #'on-edit))))

(provide 'slack-message-editor)
;;; slack-message-editor.el ends here