aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH Durer <h.duerer@gmail.com>2018-03-19 15:54:23 +0000
committerJohnson Denen <johnson.denen@gmail.com>2018-08-10 22:20:04 -0400
commit9f9a6e8c45e329d58a772dcf25942f7093ba5fc0 (patch)
treee8b3c59f0ec14b110cf93251c95a71b9324b4ad4
parentdf32beb187aa7cc360324a75c802165aba016aeb (diff)
Give mastodon-media--inline-images a saner interface. (#191)
Instead of making it search the whole buffer every time to find images to load, give it a range where this work should be done. We then call this immediately after inserting a single status, notification, ... There should be no big noticible difference - images might load a tiny bit sooner although I doubt you can see that. This should be more efficient on large buffers although Alex didn't notice any problems when testing streamed buffers. We should still do it as it make things easier to understand. I was always worried about these global operations.
-rw-r--r--lisp/mastodon-inspect.el2
-rw-r--r--lisp/mastodon-media.el42
-rw-r--r--lisp/mastodon-notifications.el12
-rw-r--r--lisp/mastodon-profile.el6
-rw-r--r--lisp/mastodon-tl.el21
-rw-r--r--test/mastodon-media-tests.el2
6 files changed, 45 insertions, 40 deletions
diff --git a/lisp/mastodon-inspect.el b/lisp/mastodon-inspect.el
index 351c92a..61cb1e3 100644
--- a/lisp/mastodon-inspect.el
+++ b/lisp/mastodon-inspect.el
@@ -75,7 +75,7 @@
(goto-char (point-min))
(while (search-forward "\n\n\n | " nil t)
(replace-match "\n | "))
- (mastodon-media--inline-images)))
+ (mastodon-media--inline-images (point-min) (point-max))))
(switch-to-buffer-other-window buffer)
(mastodon-mode)))
diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el
index 2decce4..d06a6eb 100644
--- a/lisp/mastodon-media.el
+++ b/lisp/mastodon-media.el
@@ -193,8 +193,8 @@ MEDIA-TYPE is a symbol and either 'avatar or 'media-link."
'loading-failed)
:loading-failed))))))
-(defun mastodon-media--select-next-media-line ()
- "Find coordinates of the next media to load.
+(defun mastodon-media--select-next-media-line (end-pos)
+ "Find coordinates of the next media to load before END-POS.
Returns the list of (`start' . `end', `media-symbol') points of
that line and string found or nil no more media links were
@@ -206,7 +206,7 @@ found."
(null (get-text-property next-pos 'media-type))))
;; do nothing - the loop will proceed
)
- (when next-pos
+ (when (and next-pos (< next-pos end-pos))
(let ((media-type (get-text-property next-pos 'media-type)))
(cond
;; Avatars are just one character in the buffer
@@ -225,23 +225,25 @@ not been returned."
(or (string= "http://" (substring link 0 7))
(string= "https://" (substring link 0 8)))))
-(defun mastodon-media--inline-images ()
- "Find all `Media_Links:' in the buffer replacing them with the referenced image."
- (interactive)
- (goto-char (point-min))
- (let (line-details)
- (while (setq line-details (mastodon-media--select-next-media-line))
- (let* ((start (car line-details))
- (end (cadr line-details))
- (media-type (cadr (cdr line-details)))
- (image-url (get-text-property start 'media-url)))
- (if (not (mastodon-media--valid-link-p image-url))
- ;; mark it at least as not needing loading any more
- (put-text-property start end 'media-state 'invalid-url)
- ;; proceed to load this image asynchronously
- (put-text-property start end 'media-state 'loading)
- (mastodon-media--load-image-from-url
- image-url media-type start (- end start)))))))
+(defun mastodon-media--inline-images (search-start search-end)
+ "Find all `Media_Links:' in the range from SEARCH-START to SEARCH-END
+replacing them with the referenced image."
+ (save-excursion
+ (goto-char search-start)
+ (let (line-details)
+ (while (setq line-details (mastodon-media--select-next-media-line
+ search-end))
+ (let* ((start (car line-details))
+ (end (cadr line-details))
+ (media-type (cadr (cdr line-details)))
+ (image-url (get-text-property start 'media-url)))
+ (if (not (mastodon-media--valid-link-p image-url))
+ ;; mark it at least as not needing loading any more
+ (put-text-property start end 'media-state 'invalid-url)
+ ;; proceed to load this image asynchronously
+ (put-text-property start end 'media-state 'loading)
+ (mastodon-media--load-image-from-url
+ image-url media-type start (- end start))))))))
(defun mastodon-media--get-avatar-rendering (avatar-url)
"Returns the string to be written that renders the avatar at AVATAR-URL."
diff --git a/lisp/mastodon-notifications.el b/lisp/mastodon-notifications.el
index ddaaaf4..01068cb 100644
--- a/lisp/mastodon-notifications.el
+++ b/lisp/mastodon-notifications.el
@@ -111,15 +111,17 @@
(defun mastodon-notifications--by-type (note)
"Filters NOTE for those listed in `mastodon-notifications--types-alist'."
(let* ((type (mastodon-tl--field 'type note))
- (fun (cdr (assoc type mastodon-notifications--types-alist))))
- (when fun (funcall fun note))))
+ (fun (cdr (assoc type mastodon-notifications--types-alist)))
+ (start-pos (point)))
+ (when fun
+ (funcall fun note)
+ (when mastodon-tl--display-media-p
+ (mastodon-media--inline-images start-pos (point))))))
(defun mastodon-notifications--timeline (json)
"Format JSON in Emacs buffer."
(mapc #'mastodon-notifications--by-type json)
- (goto-char (point-min))
- (when mastodon-tl--display-media-p
- (mastodon-media--inline-images)))
+ (goto-char (point-min)))
(defun mastodon-notifications--get ()
"Display NOTIFICATIONS in buffer."
diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el
index 9dc5a82..fca1bd8 100644
--- a/lisp/mastodon-profile.el
+++ b/lisp/mastodon-profile.el
@@ -76,7 +76,8 @@
" ------------\n")
'success))
(setq mastodon-tl-update-point (point))
- (mastodon-tl--timeline json)))
+ (mastodon-media--inline-images (point-min) (point))
+ (mastodon-tl--timeline json)))
(mastodon-tl--goto-next-toot)))
(defun mastodon-profile--get-toot-author ()
@@ -112,8 +113,7 @@ FIELD is used to identify regions under 'account"
'byline 't
'toot-id (cdr (assoc 'id toot)) 'toot-json toot)
"\n"))
- tootv))
- (mastodon-media--inline-images))
+ tootv)))
(defun mastodon-profile--get-following ()
"Request a list of those who the user under point follows."
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el
index 4d6d0b6..5411d42 100644
--- a/lisp/mastodon-tl.el
+++ b/lisp/mastodon-tl.el
@@ -635,13 +635,16 @@ the byline that takes one variable. By default it is `mastodon-tl--byline-author
ACTION-BYLINE is also an optional function for adding an action, such as boosting
favouriting and following to the byline. It also takes a single function. By default
it is `mastodon-tl--byline-boosted'"
- (insert
- (propertize
- (concat body
- (mastodon-tl--byline toot author-byline action-byline))
- 'toot-id (cdr (assoc 'id toot))
- 'toot-json toot)
- "\n\n"))
+ (let ((start-pos (point)))
+ (insert
+ (propertize
+ (concat body
+ (mastodon-tl--byline toot author-byline action-byline))
+ 'toot-id (cdr (assoc 'id toot))
+ 'toot-json toot)
+ "\n\n")
+ (when mastodon-tl--display-media-p
+ (mastodon-media--inline-images start-pos (point)))))
(defun mastodon-tl--toot(toot)
"Formats TOOT and insertes it into the buffer."
@@ -657,9 +660,7 @@ it is `mastodon-tl--byline-boosted'"
(defun mastodon-tl--timeline (toots)
"Display each toot in TOOTS."
(mapc 'mastodon-tl--toot toots)
- (goto-char (point-min))
- (when mastodon-tl--display-media-p
- (mastodon-media--inline-images)))
+ (goto-char (point-min)))
(defun mastodon-tl--get-update-function (&optional buffer)
"Get the UPDATE-FUNCTION stored in `mastodon-tl--buffer-spec'"
diff --git a/test/mastodon-media-tests.el b/test/mastodon-media-tests.el
index 7031e90..a586be9 100644
--- a/test/mastodon-media-tests.el
+++ b/test/mastodon-media-tests.el
@@ -189,7 +189,7 @@
;; stub for the actual test:
(stub mastodon-media--load-image-from-url)
- (mastodon-media--inline-images)
+ (mastodon-media--inline-images (point-min) (point-max))
(should (eq 'loading (get-text-property marker-media-link 'media-state)))
(should (eq 'invalid-url (get-text-property marker-media-link-bad-url 'media-state)))