From 42f302ab616264348a22107d6fb4d41632ec52a2 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 14 Jan 2015 22:22:36 -0200 Subject: Move sx-question-mode--goto-property-change to sx.el --- sx.el | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 36ecfca..af4ab4d 100644 --- a/sx.el +++ b/sx.el @@ -259,6 +259,21 @@ whenever BODY evaluates to nil." :filter (lambda (&optional _) (when (progn ,@body) ,def))))) +(defun sx--goto-property-change (prop &optional direction) + "Move forward to the next change of text-property PROP. +Return the new value of PROP at point. + +If DIRECTION is negative, move backwards instead." + (let ((func (if (and (numberp direction) + (< direction 0)) + #'previous-single-property-change + #'next-single-property-change)) + (limit (if (and (numberp direction) + (< direction 0)) + (point-min) (point-max)))) + (goto-char (funcall func (point) prop nil limit)) + (get-text-property (point) prop))) + ;;; Printing request data (defvar sx--overlays nil -- cgit v1.2.3 From cd215e056fd092bef5c0867ecd151107516cac09 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 14 Jan 2015 22:47:44 -0200 Subject: Implement sx--find-in-buffer --- sx.el | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index af4ab4d..d6c5335 100644 --- a/sx.el +++ b/sx.el @@ -274,6 +274,26 @@ If DIRECTION is negative, move backwards instead." (goto-char (funcall func (point) prop nil limit)) (get-text-property (point) prop))) +(defun sx--find-in-buffer (type id) + "Move point to an object of TYPE and ID. +That is, move forward from beginning of buffer until +`sx--data-here' is an object of type TYPE with the respective id +ID. + +TYPE is either question, answer, or comment. +ID is an integer." + (goto-char (point-min)) + (let (done) + (while (not done) + (let-alist (sx--goto-property-change 'sx--data-here 1) + (setq done (or (eobp) + (= id (cl-case type + (answer .answer_id) + (comment .comment_id) + (question .question_id))))))) + (unless done + (sx-message "Can't find the specified %s" type)))) + ;;; Printing request data (defvar sx--overlays nil -- cgit v1.2.3 From acd7a66c79878275e7bda307ee07825877b85bbf Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 14 Jan 2015 23:13:28 -0200 Subject: Fix find in buffer --- sx.el | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index d6c5335..8ae7a65 100644 --- a/sx.el +++ b/sx.el @@ -282,17 +282,22 @@ ID. TYPE is either question, answer, or comment. ID is an integer." - (goto-char (point-min)) - (let (done) - (while (not done) - (let-alist (sx--goto-property-change 'sx--data-here 1) - (setq done (or (eobp) - (= id (cl-case type - (answer .answer_id) - (comment .comment_id) - (question .question_id))))))) - (unless done - (sx-message "Can't find the specified %s" type)))) + (let* ((id-symbol (cl-case type + (answer 'answer_id) + (comment 'comment_id) + (question 'question_id))) + (pos + (save-excursion + (goto-char (point-min)) + (while (not (or (eobp) + (let ((data (sx--data-here type t))) + (and data + (= id (or (cdr (assq id-symbol data)))))))) + (forward-char 1)) + (point)))) + (if (equal pos (point-max)) + (sx-message "Can't find the specified %s" type) + (goto-char pos)))) ;;; Printing request data -- cgit v1.2.3 From a36ab1aecfdf62d7d3763cff2490c61cf63d4930 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 15 Jan 2015 14:10:55 -0200 Subject: sx--find-in-buffer moves over end of line --- sx.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 8ae7a65..950bbf0 100644 --- a/sx.el +++ b/sx.el @@ -278,7 +278,7 @@ If DIRECTION is negative, move backwards instead." "Move point to an object of TYPE and ID. That is, move forward from beginning of buffer until `sx--data-here' is an object of type TYPE with the respective id -ID. +ID. If point is left at the of a line, move over the line break. TYPE is either question, answer, or comment. ID is an integer." @@ -297,7 +297,9 @@ ID is an integer." (point)))) (if (equal pos (point-max)) (sx-message "Can't find the specified %s" type) - (goto-char pos)))) + (goto-char pos) + (when (looking-at-p "$") + (forward-char 1))))) ;;; Printing request data -- cgit v1.2.3 From 96e421160c5e682cedb8786b8a4bdbf59d7790d5 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 15 Jan 2015 14:28:45 -0200 Subject: Add comment support on sx--link-to-data --- sx.el | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 950bbf0..ff481c1 100644 --- a/sx.el +++ b/sx.el @@ -93,6 +93,17 @@ with a `link' property)." (let ((result (list (cons 'site_par (sx--site link))))) ;; Try to strip a question or answer ID (when (or + ;; Comment + (and (string-match + ;; From inbox items + (rx "/posts/comments/" + ;; Comment ID + (group-n 1 (+ digit)) + ;; Stuff at the end + (or (sequence (any "?#") (* any)) "") + string-end) + link) + (push '(type . comment) result)) ;; Answer (and (or (string-match ;; From 'Share' button -- cgit v1.2.3 From 679894374c9c537ec4404b80d2828ef123695391 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 15 Jan 2015 15:11:12 -0200 Subject: Use cond in sx--link-to-data and better regexps --- sx.el | 106 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 57 insertions(+), 49 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index ff481c1..ab39202 100644 --- a/sx.el +++ b/sx.el @@ -92,57 +92,65 @@ with a `link' property)." "Convert string LINK into data that can be displayed." (let ((result (list (cons 'site_par (sx--site link))))) ;; Try to strip a question or answer ID - (when (or - ;; Comment - (and (string-match - ;; From inbox items - (rx "/posts/comments/" - ;; Comment ID - (group-n 1 (+ digit)) - ;; Stuff at the end - (or (sequence (any "?#") (* any)) "") - string-end) - link) - (push '(type . comment) result)) + (when (cond ;; Comment + ((string-match ;; From inbox items + (rx "/posts/comments/" + ;; Comment ID + (group-n 1 (+ digit)) + ;; Optional stuff at the end + (or (and (any "?#") (* any)) "") + string-end) + link) + (push '(type . comment) result)) ;; Answer - (and (or (string-match - ;; From 'Share' button - (rx "/a/" - ;; Question ID - (group (+ digit)) - ;; User ID - "/" (+ digit) - ;; Answer ID - (group (or (sequence "#" (* any)) "")) - string-end) link) - (string-match - ;; From URL - (rx "/questions/" (+ digit) "/" - (+ (not (any "/"))) "/" - ;; User ID - (optional (group (+ digit))) - (optional "/") - (group (or (sequence "#" (* any)) "")) - string-end) link)) - (push '(type . answer) result)) + ((or ;; If there's a #NUMBER at the end, we know it's an + ;; answer with that ID. + (string-match (rx "#" (group-n 1 (+ digit)) string-end) link) + ;; From 'Share' button + (string-match (rx "/a/" + ;; Answer ID + (group-n 1 (+ digit)) "/" + ;; User ID + (+ digit) + ;; Garbage at the end + (optional (and (any "?#") (* any))) + string-end) + link) + ;; From URL + (string-match (rx "/questions/" (+ digit) "/" + ;; Question title + (+ (not (any "/"))) "/" + ;; Answer ID. If this is absent, we match on + ;; Question clause below. + (group-n 1 (+ digit)) + (opt "/") + ;; Garbage at the end + (optional (and (any "?#") (* any))) + string-end) + link)) + (push '(type . answer) result)) ;; Question - (and (or (string-match - ;; From 'Share' button - (rx "/q/" - ;; Question ID - (group (+ digit)) - ;; User ID - (optional "/" (+ digit)) - ;; Answer or Comment ID - (group (or (sequence "#" (* any)) "")) - string-end) link) - (string-match - ;; From URL - (rx "/questions/" - ;; Question ID - (group (+ digit)) - "/") link)) - (push '(type . question) result))) + ((or ;; From 'Share' button + (string-match (rx "/q/" + ;; Question ID + (group-n 1 (+ digit)) + ;; User ID + (optional "/" (+ digit)) + ;; Garbage at the end + (optional (and (any "?#") (* any))) + string-end) + link) + ;; From URL + (string-match (rx "/questions/" + ;; Question ID + (group-n 1 (+ digit)) "/" + ;; Optional question title + (optional (+ (not (any "/"))) "/") + ;; Garbage at the end + (optional (and (any "?#") (* any))) + string-end) + link)) + (push '(type . question) result))) (push (cons 'id (string-to-number (match-string-no-properties 1 link))) result)) result)) -- cgit v1.2.3 From 872e2bea0220b36a6dc9a4fdd8c08b1fde3cf61f Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 15 Jan 2015 15:14:12 -0200 Subject: Add support for another type of comment on sx--link-to-data --- sx.el | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index ab39202..785e0c4 100644 --- a/sx.el +++ b/sx.el @@ -93,14 +93,19 @@ with a `link' property)." (let ((result (list (cons 'site_par (sx--site link))))) ;; Try to strip a question or answer ID (when (cond ;; Comment - ((string-match ;; From inbox items - (rx "/posts/comments/" - ;; Comment ID - (group-n 1 (+ digit)) - ;; Optional stuff at the end - (or (and (any "?#") (* any)) "") - string-end) - link) + ((or ;; If there's a #commentNUMBER_NUMBER at the end, we + ;; know it's a comment with that ID. + (string-match (rx "#comment" (group-n 1 (+ digit)) + "_" (+ digit) string-end) + link) + ;; From inbox items + (string-match (rx "/posts/comments/" + ;; Comment ID + (group-n 1 (+ digit)) + ;; Optional stuff at the end + (or (and (any "?#") (* any)) "") + string-end) + link)) (push '(type . comment) result)) ;; Answer ((or ;; If there's a #NUMBER at the end, we know it's an -- cgit v1.2.3