Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying twitter images #136

Open
bmp opened this issue May 9, 2017 · 9 comments
Open

Displaying twitter images #136

bmp opened this issue May 9, 2017 · 9 comments

Comments

@bmp
Copy link

bmp commented May 9, 2017

Does anyone know if twittering mode allows for images of a tweet can be displayed (Twitter allows us to attach images to a tweet)?

Similar to displaying user icons, if we can display these images.

I've attached a sample of such an image on twitter's interface (image1):
image1

This is how this is rendered on twittering mode based on my customisation file (image 2):
image2

@Mosesofmason
Copy link

Please support this!

@fuxialexander
Copy link

fuxialexander commented Mar 24, 2018

@bmp @Mosesofmason I managed to create the following ugly hack. It's ugly, but it works.

Demo:

unknown

(defun *twittering-generate-format-table (status-sym prefix-sym)
  `(("%" . "%")
    ("}" . "}")
    ("#" . (cdr (assq 'id ,status-sym)))
    ("'" . (when (cdr (assq 'truncated ,status-sym))
             "..."))
    ("c" .
     (let ((system-time-locale "C"))
       (format-time-string "%a %b %d %H:%M:%S %z %Y"
                           (cdr (assq 'created-at ,status-sym)))))
    ("d" . (cdr (assq 'user-description ,status-sym)))
    ("f" .
     (twittering-make-string-with-source-property
      (cdr (assq 'source ,status-sym)) ,status-sym))
    ("i" .
     (when (and twittering-icon-mode window-system)
       (let ((url
              (cond
               ((and twittering-use-profile-image-api
                     (eq twittering-service-method 'twitter)
                     (or (null twittering-convert-fix-size)
                         (member twittering-convert-fix-size '(48 73))))
                (let ((user (cdr (assq 'user-screen-name ,status-sym)))
                      (size
                       (if (or (null twittering-convert-fix-size)
                               (= 48 twittering-convert-fix-size))
                           "normal"
                         "bigger")))
                  (format "http://%s/%s/%s.xml?size=%s" twittering-api-host
                          (twittering-api-path "users/profile_image") user size)))
               (t
                (cdr (assq 'user-profile-image-url ,status-sym))))))
         (twittering-make-icon-string nil nil url))))
    ("I" .
     (let* ((entities (cdr (assq 'entity ,status-sym)))
            text)
       (mapc (lambda (url-info)
               (setq text (or (cdr (assq 'media-url url-info)) "")))
             (cdr (assq 'media entities)))
       (if (string-equal "" text)
           text
         (let ((twittering-convert-fix-size 360))
           (twittering-make-icon-string nil nil text)))))
    ("j" . (cdr (assq 'user-id ,status-sym)))
    ("L" .
     (let ((location (or (cdr (assq 'user-location ,status-sym)) "")))
       (unless (string= "" location)
         (concat " [" location "]"))))
    ("l" . (cdr (assq 'user-location ,status-sym)))
    ("p" . (when (cdr (assq 'user-protected ,status-sym))
             "[x]"))
    ("r" .
     (let ((reply-id (or (cdr (assq 'in-reply-to-status-id ,status-sym)) ""))
           (reply-name (or (cdr (assq 'in-reply-to-screen-name ,status-sym))
                           ""))
           (recipient-screen-name
            (cdr (assq 'recipient-screen-name ,status-sym))))
       (let* ((pair
               (cond
                (recipient-screen-name
                 (cons (format "sent to %s" recipient-screen-name)
                       (twittering-get-status-url recipient-screen-name)))
                ((and (not (string= "" reply-id))
                      (not (string= "" reply-name)))
                 (cons (format "in reply to %s" reply-name)
                       (twittering-get-status-url reply-name reply-id)))
                (t nil)))
              (str (car pair))
              (url (cdr pair))
              (properties
               (list 'mouse-face 'highlight 'face 'twittering-uri-face
                     'keymap twittering-mode-on-uri-map
                     'uri url
                     'front-sticky nil
                     'rear-nonsticky t)))
         (when (and str url)
           (concat " " (apply 'propertize str properties))))))
    ("R" .
     (let ((retweeted-by
            (or (cdr (assq 'retweeting-user-screen-name ,status-sym)) "")))
       (unless (string= "" retweeted-by)
         (concat " (retweeted by " retweeted-by ")"))))
    ("S" .
     (twittering-make-string-with-user-name-property
      (cdr (assq 'user-name ,status-sym)) ,status-sym))
    ("s" .
     (twittering-make-string-with-user-name-property
      (cdr (assq 'user-screen-name ,status-sym)) ,status-sym))
    ("U" .
     (twittering-make-fontified-tweet-unwound ,status-sym))
    ;; ("D" .
    ;;  (twittering-make-fontified-tweet-unwound ,status-sym))
    ("T" .
     ,(twittering-make-fontified-tweet-text
       `(twittering-make-fontified-tweet-text-with-entity ,status-sym)
       twittering-regexp-hash twittering-regexp-atmark))
    ("t" .
     ,(twittering-make-fontified-tweet-text
       `(twittering-make-fontified-tweet-text-with-entity ,status-sym)
       twittering-regexp-hash twittering-regexp-atmark))
    ("u" . (cdr (assq 'user-url ,status-sym)))))
  (advice-add #'twittering-generate-format-table :override #'*twittering-generate-format-table)

@Mosesofmason
Copy link

@fuxialexander Could you give me a hint where to put the code, please? I try to put it in the .emacs file but without luck. Thank you very much!

@fuxialexander
Copy link

@Mosesofmason wrap the code in a (with-eval-after-load 'twittering-mode ) block, and tweak the twittering-status-format to include an %I entry.

To replicate my setup:

(with-eval-after-load 'twittering-mode 
  (defun *twittering-generate-format-table (status-sym prefix-sym)
    `(("%" . "%")
      ("}" . "}")
      ("#" . (cdr (assq 'id ,status-sym)))
      ("'" . (when (cdr (assq 'truncated ,status-sym))
               "..."))
      ("c" .
       (let ((system-time-locale "C"))
         (format-time-string "%a %b %d %H:%M:%S %z %Y"
                             (cdr (assq 'created-at ,status-sym)))))
      ("d" . (cdr (assq 'user-description ,status-sym)))
      ("f" .
       (twittering-make-string-with-source-property
        (cdr (assq 'source ,status-sym)) ,status-sym))
      ("i" .
       (when (and twittering-icon-mode window-system)
         (let ((url
                (cond
                 ((and twittering-use-profile-image-api
                       (eq twittering-service-method 'twitter)
                       (or (null twittering-convert-fix-size)
                           (member twittering-convert-fix-size '(48 73))))
                  (let ((user (cdr (assq 'user-screen-name ,status-sym)))
                        (size
                         (if (or (null twittering-convert-fix-size)
                                 (= 48 twittering-convert-fix-size))
                             "normal"
                           "bigger")))
                    (format "http://%s/%s/%s.xml?size=%s" twittering-api-host
                            (twittering-api-path "users/profile_image") user size)))
                 (t
                  (cdr (assq 'user-profile-image-url ,status-sym))))))
           (twittering-make-icon-string nil nil url))))
      ("I" .
       (let* ((entities (cdr (assq 'entity ,status-sym)))
              text)
         (mapc (lambda (url-info)
                 (setq text (or (cdr (assq 'media-url url-info)) "")))
               (cdr (assq 'media entities)))
         (if (string-equal "" text)
             text
           (let ((twittering-convert-fix-size 360))
             (twittering-make-icon-string nil nil text)))))
      ("j" . (cdr (assq 'user-id ,status-sym)))
      ("L" .
       (let ((location (or (cdr (assq 'user-location ,status-sym)) "")))
         (unless (string= "" location)
           (concat " [" location "]"))))
      ("l" . (cdr (assq 'user-location ,status-sym)))
      ("p" . (when (cdr (assq 'user-protected ,status-sym))
               "[x]"))
      ("r" .
       (let ((reply-id (or (cdr (assq 'in-reply-to-status-id ,status-sym)) ""))
             (reply-name (or (cdr (assq 'in-reply-to-screen-name ,status-sym))
                             ""))
             (recipient-screen-name
              (cdr (assq 'recipient-screen-name ,status-sym))))
         (let* ((pair
                 (cond
                  (recipient-screen-name
                   (cons (format "sent to %s" recipient-screen-name)
                         (twittering-get-status-url recipient-screen-name)))
                  ((and (not (string= "" reply-id))
                        (not (string= "" reply-name)))
                   (cons (format "in reply to %s" reply-name)
                         (twittering-get-status-url reply-name reply-id)))
                  (t nil)))
                (str (car pair))
                (url (cdr pair))
                (properties
                 (list 'mouse-face 'highlight 'face 'twittering-uri-face
                       'keymap twittering-mode-on-uri-map
                       'uri url
                       'front-sticky nil
                       'rear-nonsticky t)))
           (when (and str url)
             (concat " " (apply 'propertize str properties))))))
      ("R" .
       (let ((retweeted-by
              (or (cdr (assq 'retweeting-user-screen-name ,status-sym)) "")))
         (unless (string= "" retweeted-by)
           (concat " (retweeted by " retweeted-by ")"))))
      ("S" .
       (twittering-make-string-with-user-name-property
        (cdr (assq 'user-name ,status-sym)) ,status-sym))
      ("s" .
       (twittering-make-string-with-user-name-property
        (cdr (assq 'user-screen-name ,status-sym)) ,status-sym))
      ("U" .
       (twittering-make-fontified-tweet-unwound ,status-sym))
      ;; ("D" .
      ;;  (twittering-make-fontified-tweet-unwound ,status-sym))
      ("T" .
       ,(twittering-make-fontified-tweet-text
         `(twittering-make-fontified-tweet-text-with-entity ,status-sym)
         twittering-regexp-hash twittering-regexp-atmark))
      ("t" .
       ,(twittering-make-fontified-tweet-text
         `(twittering-make-fontified-tweet-text-with-entity ,status-sym)
         twittering-regexp-hash twittering-regexp-atmark))
      ("u" . (cdr (assq 'user-url ,status-sym)))))
  (advice-add #'twittering-generate-format-table :override #'*twittering-generate-format-table)
  (defface twitter-divider
    `((t (:underline (:color "grey"))))
    "The vertical divider between tweets."
    :group 'twittering-mode)
  (setq twittering-icon-mode t
        twittering-use-icon-storage t
        twittering-convert-fix-size 40
        twittering-status-format "
  %i  %FACE[font-lock-function-name-face]{  @%s}  %FACE[italic]{%@}  %FACE[error]{%FIELD-IF-NONZERO[❤ %d]{favorite_count}}  %FACE[warning]{%FIELD-IF-NONZERO[↺ %d]{retweet_count}}

%FOLD[   ]{%FILL{%t}
%QT{
%FOLD[   ]{%FACE[font-lock-function-name-face]{@%s}\t%FACE[shadow]{%@}
%FOLD[ ]{%FILL{%t}}
}}}

    %I

%FACE[twitter-divider]{                                                                                                                                                                                  }
"))

@Mosesofmason
Copy link

Thanks, it works like a charm.

@bmp
Copy link
Author

bmp commented Apr 4, 2018

@fuxialexander, thanks for that, it works but unfortunately for me the images are not sized properly. They seem to be sized at less than the user image.

screen shot 2018-04-04 at 4 30 40 pm

And with my previous setup it was,

screen shot 2018-04-04 at 4 43 55 pm

I am using the following to display tweets,

(setq twittering-status-format "%RT{Retweeted by %S %FACE[glyphless-char]{%@}\n%FACE[bold]{RT }}%i %S %FACE[glyphless-char]{@%s tweeted at %@}\n%FOLD[ ]{%T \n%FACE[glyphless-char]{RT %FIELD-IF-NONZERO[%d]{retweet_count} FAV %FIELD-IF-NONZERO[%d]{favorite_count} %f%L%r}%QT{\n+----\n%FOLD[|]{%i %S %FACE[glyphless-char]{@%s tweeted at %@}\n%FOLD[ ]{%T \n%FACE[glyphless-char]{RT %FIELD-IF-NONZERO[%d]{retweet_count} FAV %FIELD-IF-NONZERO[%d]{favorite_count} %f%L%r}}}\n+----}}\n")

@fuxialexander
Copy link

fuxialexander commented Apr 4, 2018

@bmp The size is set in this line (twittering-convert-fix-size 360) which should work. Try execute this function

(defun +twitter/rerender-all ()
  (interactive)
  (dolist (buf (doom-buffers-in-mode 'twittering-mode (buffer-list)))
    (with-current-buffer buf
      (twittering-rerender-timeline-all buf t)
      (setq-local line-spacing 0.2)
      (goto-char (point-min))
      )))

to refresh the display. Sometimes I need to do that to make the image show at the right size.

@bmp
Copy link
Author

bmp commented Apr 4, 2018

Thanks, that works.

@ghost
Copy link

ghost commented Apr 9, 2019

This is such a great feature would love to see it added as a tuneable feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants