aboutsummaryrefslogtreecommitdiff
path: root/sx-user.el
blob: 975d5ef912262646aa9574d97b568d2789666b05 (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
;;; sx-user.el --- handling and printing user information  -*- lexical-binding: t; -*-

;; Copyright (C) 2014  Artur Malabarba

;; Author: Artur Malabarba <bruce.connor.am@gmail.com>

;; 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 'sx)
(require 'sx-button)

(defgroup sx-user nil
  "Customization group for sx-question-mode."
  :prefix "sx-user-"
  :tag "SX Question Mode"
  :group 'sx)

(defvar sx--user-format-property-alist
  '((?d face sx-question-mode-author)
    (?r face sx-question-mode-reputation)
    (?a face sx-question-mode-accept-rate))
  "Alist relating % constructs with text properties.
See `sx--user-format'.")

(defun sx--user-format (format-string user)
  "Use FORMAT-STRING to format the user object USER.
The value is a copy of FORMAT-STRING, but with certain constructs
replaced by text that describes the specified USER:

%d is the display name.
%l is the link to the profile.
%r is the reputation.
%a is the accept rate.

The returned string is additionally propertized as a button with
the `sx-button-user' category."
  (sx-assoc-let (append user sx-question-mode-fallback-user)
    (let* ((text (sx-format-replacements
                  format-string
                  `((?d . ,.display_name)
                    (?l . ,.link)
                    (?r . ,.reputation)
                    (?a . ,.accept_rate))
                  sx--format-user-property-alist)))
      (if link
          (insert-text-button text
                              ;; For visiting and stuff.
                              'sx-button-url  link
                              'sx-button-copy link
                              :type 'sx-button-user)
        text))))

(provide 'sx-user)
;;; sx-user.el ends here

;; Local Variables:
;; indent-tabs-mode: nil
;; End: