From 4365d8f3cd693350a722b49b0f791670c16db6a8 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 6 Jan 2015 23:22:52 -0200 Subject: Define sx--format-user Use FORMAT-STRING to format the user object USER. --- sx-question-print.el | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index e2db76f..d2c5249 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -402,6 +402,34 @@ URL is used as 'help-echo and 'url properties." 'sx-button-copy url :type 'sx-button-link)) +(defun sx--format-user (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." + (let-alist user + (let* ((link (or .link "")) + (text (sx-format-replacements + format-string + `((?d . ,(or .display_name "Unknown user")) + (?l . ,link) + (?r . ,(number-to-string (or .reputation 0))) + (?a . ,(number-to-string (or .accept_rate 0))))))) + (if link + (insert-text-button text + ;; For visiting and stuff. + 'sx-button-url link + 'sx-button-copy link + :type 'sx-button-user) + text)))) + (defun sx-question-mode-find-reference (id &optional fallback-id) "Find url identified by reference ID in current buffer. If ID is nil, use FALLBACK-ID instead." -- cgit v1.2.3 From 77c0fa3803d3f6827dd6287fdbee70405c7a146a Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:21:49 -0200 Subject: Change deleted-user to fallback-user --- sx-question-print.el | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index d2c5249..0bf5479 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -41,10 +41,16 @@ ;;; Faces and Variables -(defcustom sx-question-mode-deleted-user - '((display_name . "(deleted user)")) +(defconst sx-question-mode-fallback-user + '((display_name . "(unknown user)") + (link . "") + (reputation . -1) + (accept_rate . -1)) "The structure used to represent a deleted account." - :type '(alist :options ((display_name string))) + :type '(alist :options ((display_name string) + (link string) + (reputation integer) + (accept_rate integer))) :group 'sx-question-mode) (defface sx-question-mode-header @@ -217,7 +223,7 @@ DATA can represent a question or an answer." (format sx-question-mode-last-edit-format (sx-time-since .last_edit_date) (sx-question-mode--propertize-display-name - (or .last_editor sx-question-mode-deleted-user))))) + (or .last_editor sx-question-mode-fallback-user))))) 'sx-question-mode-date) (sx-question-mode--insert-header sx-question-mode-header-score -- cgit v1.2.3 From 5584905198e5f3b4db68cdd9373b4172885d0d43 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:22:28 -0200 Subject: Use fallback user in sx--format-user --- sx-question-print.el | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 0bf5479..e4b8850 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -420,14 +420,14 @@ replaced by text that describes the specified USER: The returned string is additionally propertized as a button with the `sx-button-user' category." - (let-alist user - (let* ((link (or .link "")) - (text (sx-format-replacements + (sx-assoc-let (append user sx-question-mode-fallback-user) + (let* ((text (sx-format-replacements format-string - `((?d . ,(or .display_name "Unknown user")) - (?l . ,link) - (?r . ,(number-to-string (or .reputation 0))) - (?a . ,(number-to-string (or .accept_rate 0))))))) + `((?d . ,.display_name) + (?l . ,.link) + (?r . ,.reputation) + (?a . ,.accept_rate)) + sx-user-property-alist))) (if link (insert-text-button text ;; For visiting and stuff. -- cgit v1.2.3 From ea5bd0a5a03976b8b5bf62645f1e5623e805db8b Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:30:33 -0200 Subject: Delete duplicate face definition --- sx-question-print.el | 5 ----- 1 file changed, 5 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index e4b8850..0f54e6e 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -73,11 +73,6 @@ :type 'string :group 'sx-question-mode) -(defface sx-question-mode-author - '((t :inherit font-lock-string-face)) - "Face used on the question author in the question buffer." - :group 'sx-question-mode-faces) - (defcustom sx-question-mode-header-author "\nAuthor: " "String used before the question author at the header." :type 'string -- cgit v1.2.3 From 3b86d82a1a199a98bdb1bae8cc991807afa6a035 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:31:19 -0200 Subject: Use sx--format-user instead of propertize-display-name --- sx-question-print.el | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 0f54e6e..0efea98 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -208,7 +208,7 @@ DATA can represent a question or an answer." (sx-question-mode--insert-header ;; Author sx-question-mode-header-author - (sx-question-mode--propertize-display-name .owner) + (sx--format-user .owner) 'sx-question-mode-author ;; Date sx-question-mode-header-date @@ -217,8 +217,7 @@ DATA can represent a question or an answer." (when .last_edit_date (format sx-question-mode-last-edit-format (sx-time-since .last_edit_date) - (sx-question-mode--propertize-display-name - (or .last_editor sx-question-mode-fallback-user))))) + (sx--format-user "%n" .last_editor)))) 'sx-question-mode-date) (sx-question-mode--insert-header sx-question-mode-header-score @@ -274,12 +273,6 @@ DATA can represent a question or an answer." :type 'sx-button-comment) (insert "\n"))))) -(defun sx-question-mode--propertize-display-name (author) - "Return display_name of AUTHOR with `sx-question-mode-author' face." - (sx-assoc-let author - (propertize (or .display_name "??") - 'face 'sx-question-mode-author))) - (defun sx-question-mode--print-comment (comment-data) "Print the comment described by alist COMMENT-DATA. The comment is indented, filled, and then printed according to @@ -292,9 +285,8 @@ The comment is indented, filled, and then printed according to (if (eq .upvoted t) "^" "") " ")) (insert - (format - sx-question-mode-comments-format - (sx-question-mode--propertize-display-name .owner) + (format sx-question-mode-comments-format + (sx--format-user "%n" .owner) (substring ;; We fill with three spaces at the start, so the comment is ;; slightly indented. -- cgit v1.2.3 From 304217c1ac70ee7dc6f7e4c16410fca73b11c515 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:44:24 -0200 Subject: Move user-printing to sx-user --- sx-question-print.el | 34 +----------------------- sx-user.el | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 sx-user.el (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 0efea98..a8604e0 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -26,6 +26,7 @@ (require 'sx) (require 'sx-question) (require 'sx-babel) +(require 'sx-user) (defgroup sx-question-mode nil "Customization group for sx-question-mode." @@ -93,11 +94,6 @@ "Face used on the question tags in the question buffer." :group 'sx-question-mode-faces) -(defface sx-question-mode-author - '((t :inherit font-lock-variable-name-face)) - "Face used for author names in the question buffer." - :group 'sx-question-mode-faces) - (defface sx-question-mode-score '((t)) "Face used for the score in the question buffer." @@ -395,34 +391,6 @@ URL is used as 'help-echo and 'url properties." 'sx-button-copy url :type 'sx-button-link)) -(defun sx--format-user (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-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)))) - (defun sx-question-mode-find-reference (id &optional fallback-id) "Find url identified by reference ID in current buffer. If ID is nil, use FALLBACK-ID instead." diff --git a/sx-user.el b/sx-user.el new file mode 100644 index 0000000..975d5ef --- /dev/null +++ b/sx-user.el @@ -0,0 +1,73 @@ +;;; sx-user.el --- handling and printing user information -*- lexical-binding: t; -*- + +;; Copyright (C) 2014 Artur Malabarba + +;; Author: Artur Malabarba + +;; 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 . + +;;; 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: -- cgit v1.2.3 From b61d953576c2e46678777d5838c54a92093485c7 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 14:44:42 -0200 Subject: Change sx-question-mode-header-author to sx-question-mode-header-author-format --- sx-question-print.el | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index a8604e0..3ae86fc 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -74,8 +74,9 @@ :type 'string :group 'sx-question-mode) -(defcustom sx-question-mode-header-author "\nAuthor: " - "String used before the question author at the header." +(defcustom sx-question-mode-header-author-format "\nAuthor: %n %r" + "String used to display the question author at the header. +% constructs have special meaning here. See `sx--user-format'." :type 'string :group 'sx-question-mode) @@ -201,11 +202,12 @@ DATA can represent a question or an answer." ;; Sections can be hidden with overlays (sx--wrap-in-overlay '(sx-question-mode--section-content t) + ;; Author + (sx--format-user + (propertize sx-question-mode-header-author-format + 'face 'sx-question-mode-header) + .owner) (sx-question-mode--insert-header - ;; Author - sx-question-mode-header-author - (sx--format-user .owner) - 'sx-question-mode-author ;; Date sx-question-mode-header-date (concat -- cgit v1.2.3 From d2e808eae7565cefb95becd05628ae69f2980e25 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 15:08:58 -0200 Subject: Move fallback-user to sx-user, and improve it --- sx-question-print.el | 12 -------- sx-user.el | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 12 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 3ae86fc..6ab7698 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -42,18 +42,6 @@ ;;; Faces and Variables -(defconst sx-question-mode-fallback-user - '((display_name . "(unknown user)") - (link . "") - (reputation . -1) - (accept_rate . -1)) - "The structure used to represent a deleted account." - :type '(alist :options ((display_name string) - (link string) - (reputation integer) - (accept_rate integer))) - :group 'sx-question-mode) - (defface sx-question-mode-header '((t :inherit font-lock-variable-name-face)) "Face used on the question headers in the question buffer." diff --git a/sx-user.el b/sx-user.el index 975d5ef..d09faf0 100644 --- a/sx-user.el +++ b/sx-user.el @@ -30,6 +30,86 @@ :tag "SX Question Mode" :group 'sx) +(defcustom sx-question-mode-fallback-user + '( + (about_me . "") + (accept_rate . -1) + (account_id . -1) + (age . -1) + (answer_count . -1) + (badge_counts . ((bronze . -1) (silver . -1) (gold . -1))) + (creation_date . -1) + (display_name . "(unknown user)") + (down_vote_count . -1) + (is_employee . :json-false) + (last_access_date . -1) + (last_modified_date . -1) + (link . "") + (location . "") + (profile_image . ":(") + (question_count . -1) + (reputation . -1) + (reputation_change_day . -1) + (reputation_change_month . -1) + (reputation_change_quarter . -1) + (reputation_change_week . -1) + (reputation_change_year . -1) + (timed_penalty_date . -1) + (up_vote_count . -1) + (user_id . -1) + (user_type . does_not_exist) + (view_count . -1) + (website_url . "") + ) + "The structure used to represent missing user information. +NOOTE: SX relies on this variable containing all necessary user +information. You may edit any of its fields, but you'll run into +errors if you remove them." + :type '(alist :options ((about_me string) + (accept_rate integer) + (account_id integer) + (age integer) + (answer_count integer) + (badge_counts alist) + (creation_date integer) + (display_name string) + (down_vote_count integer) + (is_employee boolean) + (last_access_date integer) + (last_modified_date integer) + (link string) + (location string) + (profile_image string) + (question_count integer) + (reputation integer) + (reputation_change_day integer) + (reputation_change_month integer) + (reputation_change_quarter integer) + (reputation_change_week integer) + (reputation_change_year integer) + (timed_penalty_date integer) + (up_vote_count integer) + (user_id integer) + (user_type symbol) + (view_count integer) + (website_url string))) + :group 'sx-user) + +(defface sx-user-name + '((t :inherit font-lock-variable-name-face)) + "Face used for user names." + :group 'sx-user) + +(defface sx-user-reputation + '((t :inherit font-lock-function-name-face)) + "Face used for user reputations." + :group 'sx-user) + +(defface sx-user-accept-rate + '((t)) + "Face used for user accept-rates." + :group 'sx-user) + (defvar sx--user-format-property-alist '((?d face sx-question-mode-author) (?r face sx-question-mode-reputation) -- cgit v1.2.3 From adbf1e311eaafbec2476fe08a5ba81672f25eb79 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 15:09:21 -0200 Subject: Fix some migration left-overs --- sx-question-print.el | 20 +++++++++++--------- sx-user.el | 29 ++++++++++++++++------------- 2 files changed, 27 insertions(+), 22 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 6ab7698..66ae922 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -34,8 +34,9 @@ :tag "SX Question Mode" :group 'sx) -(defgroup sx-question-mode-faces nil - "Customization group for the faces of `sx-question-mode'." +(defgroup sx-question-mode-faces '((sx-user custom-group)) + "Customization group for the faces of `sx-question-mode'. +Some faces of this mode might be defined in the `sx-user' group." :prefix "sx-question-mode-" :tag "SX Question Mode Faces" :group 'sx-question-mode) @@ -62,7 +63,7 @@ :type 'string :group 'sx-question-mode) -(defcustom sx-question-mode-header-author-format "\nAuthor: %n %r" +(defcustom sx-question-mode-header-author-format "\nAuthor: %d %r" "String used to display the question author at the header. % constructs have special meaning here. See `sx--user-format'." :type 'string @@ -191,10 +192,11 @@ DATA can represent a question or an answer." (sx--wrap-in-overlay '(sx-question-mode--section-content t) ;; Author - (sx--format-user - (propertize sx-question-mode-header-author-format - 'face 'sx-question-mode-header) - .owner) + (insert + (sx--user-format + (propertize sx-question-mode-header-author-format + 'face 'sx-question-mode-header) + .owner)) (sx-question-mode--insert-header ;; Date sx-question-mode-header-date @@ -203,7 +205,7 @@ DATA can represent a question or an answer." (when .last_edit_date (format sx-question-mode-last-edit-format (sx-time-since .last_edit_date) - (sx--format-user "%n" .last_editor)))) + (sx--user-format "%d" .last_editor)))) 'sx-question-mode-date) (sx-question-mode--insert-header sx-question-mode-header-score @@ -272,7 +274,7 @@ The comment is indented, filled, and then printed according to " ")) (insert (format sx-question-mode-comments-format - (sx--format-user "%n" .owner) + (sx--user-format "%d" .owner) (substring ;; We fill with three spaces at the start, so the comment is ;; slightly indented. diff --git a/sx-user.el b/sx-user.el index d09faf0..362f1e3 100644 --- a/sx-user.el +++ b/sx-user.el @@ -25,9 +25,9 @@ (require 'sx-button) (defgroup sx-user nil - "Customization group for sx-question-mode." + "How users are displayed by SX." :prefix "sx-user-" - :tag "SX Question Mode" + :tag "SX User" :group 'sx) (defcustom sx-question-mode-fallback-user @@ -111,12 +111,14 @@ errors if you remove them." :group 'sx-user) (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)) + '((?d face sx-user-name) + (?r face sx-user-reputation) + (?a face sx-user-accept-rate)) "Alist relating % constructs with text properties. See `sx--user-format'.") + +;;; Formatting function (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 @@ -132,16 +134,17 @@ 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 + `((?d . ,\.display_name) + (?n . ,\.display_name) + (?l . ,\.link) + (?r . ,\.reputation) + (?a . ,\.accept_rate)) + sx--user-format-property-alist))) + (if (> 0 (string-width .link)) (insert-text-button text ;; For visiting and stuff. - 'sx-button-url link - 'sx-button-copy link + 'sx-button-url .link + 'sx-button-copy .link :type 'sx-button-user) text)))) -- cgit v1.2.3 From 1509d09a5ed073688a316660b5e3a2222dbebfca Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 17:20:57 -0200 Subject: Fix outdated references --- sx-interaction.el | 2 +- sx-question-print.el | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-interaction.el b/sx-interaction.el index 4d71c17..e444248 100644 --- a/sx-interaction.el +++ b/sx-interaction.el @@ -272,7 +272,7 @@ TEXT is a string. Interactively, it is read from the minibufer." (setq text (read-string "Comment text: " (when .comment_id - (concat (sx--user-@name .owner) " ")))) + (concat (sx-user--format "%@" .owner) " ")))) (while (not (sx--comment-valid-p text 'silent)) (setq text (read-string "Comment text (between 16 and 600 characters): " text)))) ;; If non-interactive, `text' could be anything. diff --git a/sx-question-print.el b/sx-question-print.el index 66ae922..37fa238 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -65,7 +65,7 @@ Some faces of this mode might be defined in the `sx-user' group." (defcustom sx-question-mode-header-author-format "\nAuthor: %d %r" "String used to display the question author at the header. -% constructs have special meaning here. See `sx--user-format'." +% constructs have special meaning here. See `sx-user--format'." :type 'string :group 'sx-question-mode) @@ -193,7 +193,7 @@ DATA can represent a question or an answer." '(sx-question-mode--section-content t) ;; Author (insert - (sx--user-format + (sx-user--format (propertize sx-question-mode-header-author-format 'face 'sx-question-mode-header) .owner)) @@ -205,7 +205,7 @@ DATA can represent a question or an answer." (when .last_edit_date (format sx-question-mode-last-edit-format (sx-time-since .last_edit_date) - (sx--user-format "%d" .last_editor)))) + (sx-user--format "%d" .last_editor)))) 'sx-question-mode-date) (sx-question-mode--insert-header sx-question-mode-header-score @@ -274,7 +274,7 @@ The comment is indented, filled, and then printed according to " ")) (insert (format sx-question-mode-comments-format - (sx--user-format "%d" .owner) + (sx-user--format "%d" .owner) (substring ;; We fill with three spaces at the start, so the comment is ;; slightly indented. -- cgit v1.2.3