about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/slack-20180913.651/slack-file-info-buffer.el
blob: da0f62ec6eba24777db8e64c0d691cb45c1269da (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; slack-file-info-buffer.el ---                    -*- lexical-binding: t; -*-

;; Copyright (C) 2017

;; Author:  <yuya373@yuya373>
;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'eieio)
(require 'slack-util)
(require 'slack-buffer)
(require 'slack-message)

(define-derived-mode slack-file-info-buffer-mode slack-buffer-mode  "Slack File Info"
  (add-hook 'lui-post-output-hook 'slack-display-image t t))

(defclass slack-file-info-buffer (slack-buffer)
  ((file :initarg :file :type slack-file)))

(defmethod slack-buffer-name :static ((class slack-file-info-buffer) file team)
  (format "*Slack - %s File: %s"
          (oref team name)
          (or (oref file title)
              (oref file name)
              (oref file id))))

(defun slack-create-file-info-buffer (team file)
  (slack-if-let* ((buffer (slack-buffer-find 'slack-file-info-buffer
                                             file
                                             team)))
      (progn
        (oset buffer file file)
        buffer)
    (slack-file-info-buffer :team team :file file)))

(defmethod slack-buffer-init-buffer :after ((this slack-file-info-buffer))
  (with-slots (file team) this
    (let ((class (eieio-object-class-name this)))
      (slack-buffer-push-new-3 class file team))))

(defmethod slack-buffer-name ((this slack-file-info-buffer))
  (with-slots (file team) this
    (slack-buffer-name (eieio-object-class-name this)
                       file
                       team)))

(defmethod slack-buffer-buffer ((this slack-file-info-buffer))
  (slack-if-let* ((buf (get-buffer (slack-buffer-name this))))
      (progn
        (with-current-buffer buf
          (let ((inhibit-read-only t))
            (slack-buffer-insert this)))
        buf)
    (slack-buffer-init-buffer this)))

(defmethod slack-buffer-init-buffer ((this slack-file-info-buffer))
  (let ((buf (call-next-method)))
    (with-current-buffer buf
      (slack-file-info-buffer-mode)
      (slack-buffer-set-current-buffer this)
      (slack-buffer-insert this))
    buf))

(defmethod slack-buffer-file-to-string ((this slack-file-info-buffer))
  (with-slots (file team) this
    (let* ((header (format "%s %s\n"
                           (slack-message-put-header-property (oref file title))
                           (if (oref file is-starred) ":star:" "")))
           (body (slack-message-body-to-string file team))
           (thumb (or (and (slack-file-image-p file)
                           (slack-message-large-image-to-string file))
                      (slack-message-image-to-string file)))
           (comments (mapconcat #'(lambda (comment)
                                    (slack-message-to-string comment team))
                                (oref file comments)
                                "\n")))
      (propertize (slack-format-message header
                                        body
                                        thumb
                                        "\n"
                                        comments)
                  'file-id (oref file id)))))

(defmethod slack-buffer-insert ((this slack-file-info-buffer))
  (delete-region (point-min) lui-output-marker)
  (with-slots (file team) this
    (let ((lui-time-stamp-position nil))
      (lui-insert-with-text-properties
       (slack-buffer-file-to-string this)
       ;; saved-text-properties not working??
       'file-id (oref file id)
       'ts (slack-ts file)))))

(defmethod slack-buffer-add-reaction-to-message
  ((this slack-file-info-buffer) reaction _ts)
  (with-slots (file team) this
    (slack-file-add-reaction (oref file id) reaction team)))

(defmethod slack-buffer-add-star ((this slack-file-info-buffer) _ts)
  (let ((url slack-message-stars-add-url))
    (with-slots (file team) this
      (slack-message-star-api-request url
                                      (list (slack-message-star-api-params file))
                                      team))))

(defmethod slack-buffer-remove-star ((this slack-file-info-buffer) _ts)
  (let ((url slack-message-stars-remove-url))
    (with-slots (file team) this
      (slack-message-star-api-request url
                                      (list (slack-message-star-api-params
                                             file))
                                      team))))

(defmethod slack-buffer--replace ((this slack-file-info-buffer) _ts)
  (slack-if-let* ((buffer (get-buffer (slack-buffer-name this))))
      (with-current-buffer buffer
        (let ((inhibit-read-only t))
          (slack-buffer-insert this)))))

(defmethod slack-buffer-update ((this slack-file-info-buffer))
  (with-slots (file team) this
    (let ((buffer (get-buffer (slack-buffer-name this))))
      (with-current-buffer buffer
        (let ((inhibit-read-only t))
          (slack-buffer-insert this))))))

(provide 'slack-file-info-buffer)
;;; slack-file-info-buffer.el ends here