Gnus development mailing list
 help / color / mirror / Atom feed
* View docx/doc documents from Gnus in Docview
@ 2023-09-08 16:18 Björn Bidar
  2023-09-10 16:20 ` Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Björn Bidar @ 2023-09-08 16:18 UTC (permalink / raw)
  To: ding


Hey,

How can I make that I can view doc/docx document from the article buffer
in Docview?
Opening them from dired already works,

Thanks

Björn


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-08 16:18 View docx/doc documents from Gnus in Docview Björn Bidar
@ 2023-09-10 16:20 ` Eric Abrahamsen
  2023-09-10 19:50   ` Björn Bidar
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2023-09-10 16:20 UTC (permalink / raw)
  To: ding

Björn Bidar <bjorn.bidar@thaodan.de> writes:

> Hey,
>
> How can I make that I can view doc/docx document from the article buffer
> in Docview?
> Opening them from dired already works,

This is probably what you want. Use the prefix argument to select a
part.

K e runs the command gnus-article-view-part-externally (found in
gnus-summary-mode-map), which is an autoloaded interactive
byte-compiled Lisp function in ‘gnus-art.el’.

It is bound to K e.

(gnus-article-view-part-externally N)

View MIME part N externally, which is the numerical prefix.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-10 16:20 ` Eric Abrahamsen
@ 2023-09-10 19:50   ` Björn Bidar
  2023-09-10 21:43     ` Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Björn Bidar @ 2023-09-10 19:50 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: ding

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Björn Bidar <bjorn.bidar@thaodan.de> writes:
>
>> Hey,
>>
>> How can I make that I can view doc/docx document from the article
>> buffer
>> in Docview?
>> Opening them from dired already works,
>
> This is probably what you want. Use the prefix argument to select a
> part.
>
> K e runs the command gnus-article-view-part-externally (found in
> gnus-summary-mode-map), which is an autoloaded interactive
> byte-compiled Lisp function in ‘gnus-art.el’.
>
> It is bound to K e.
>
> (gnus-article-view-part-externally N)
>
> View MIME part N externally, which is the numerical prefix.

I tried to select that option from the graphic menu when I select the
docx attachment by clicking on it with mouse.
However the only option it offers is to safe the attachment.
What does externally mean, externally of Gnus or externally of Emacs?


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-10 19:50   ` Björn Bidar
@ 2023-09-10 21:43     ` Eric Abrahamsen
  2023-09-11  2:53       ` Andrew Cohen
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2023-09-10 21:43 UTC (permalink / raw)
  To: ding

Björn Bidar <bjorn.bidar@thaodan.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Björn Bidar <bjorn.bidar@thaodan.de> writes:
>>
>>> Hey,
>>>
>>> How can I make that I can view doc/docx document from the article
>>> buffer
>>> in Docview?
>>> Opening them from dired already works,
>>
>> This is probably what you want. Use the prefix argument to select a
>> part.
>>
>> K e runs the command gnus-article-view-part-externally (found in
>> gnus-summary-mode-map), which is an autoloaded interactive
>> byte-compiled Lisp function in ‘gnus-art.el’.
>>
>> It is bound to K e.
>>
>> (gnus-article-view-part-externally N)
>>
>> View MIME part N externally, which is the numerical prefix.
>
> I tried to select that option from the graphic menu when I select the
> docx attachment by clicking on it with mouse.
> However the only option it offers is to safe the attachment.
> What does externally mean, externally of Gnus or externally of Emacs?

It opens the file externally if it can find an external program to use,
otherwise it saves the file. Unfortunately the mm-* and mailcap-* stuff
is so perfectly impenetrable I don't think anyone has ever been able to
figure it out. I eventually did this to force Gnus to open absolutely
everything in `xdg-open`, no matter via which channel:

(setq mailcap-mime-data
  '(("application" (".*" (viewer . "xdg-open %s")))))

Then I had to install mimeo and xdg-utils-mimeo because xdg-open itself
is a horrible rat's maze. Then I had to uninstall Chromium because no
matter what it insisted on opening all image/* files.

That was one of those times I came very close to setting my computer on
fire and becoming a Franciscan friar.

Eric



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-10 21:43     ` Eric Abrahamsen
@ 2023-09-11  2:53       ` Andrew Cohen
  2023-09-11 17:01         ` Eric Abrahamsen
  2023-09-11 19:01         ` Björn Bidar
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Cohen @ 2023-09-11  2:53 UTC (permalink / raw)
  To: ding

Sorry for not replying sooner (I am swamped with real work and have
little time for other things); I have had this working for myself so I
thought I can provide some advice.

Firstly, telling gnus to use doc-view for these documents is easy: you
need to modify the variable 'mailcap-user-mime-data (which controls user
overrides for various mime types). Here is an example (this will use
doc-view-mode for mime types of ms-excel and
openxmlformats-officedocument.wordprocessingml.document, and use eww for
html.) You should modify this for your own needs:

(setq mailcap-user-mime-data
    '(((viewer . doc-view-mode)
       (test   . window-system)
       (type . "application/vnd.ms-excel"))
      ((viewer . doc-view-mode)
       (test   . window-system)
       (type . "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
      ((viewer . eww)
       (test   . (fboundp 'eww))
       (type   . "text/html"))))

But unfortunately this won't work properly due to a deficiency in
doc-view. Doc-view has only a fairly primitive mechanism for figuring
out the type of the document; since docx documents are mostly zip
archives, and many other file formats are also zip archives, doc-view
will notice they are zip files and treat them as epub (for me, at
least).  The right way to fix this is to smarten up doc-view to
correctly identify the file type. This isn't hard, but I don't have time
to do it right now (maybe someone else is willing?).  In the meantime
you can use the following hack which works for me: replace the function
'doc-view-set-doc-type with the modified version below.

(defun doc-view-set-doc-type ()
  "Figure out the current document type (`doc-view-doc-type')."
  (let* ((buffer-file-name (or buffer-file-name (buffer-name (current-buffer))))
         (name-types
	 (when buffer-file-name
	   (cdr (assoc-string
                 (file-name-extension buffer-file-name)
                 '(
                   ;; DVI
                   ("dvi" dvi)
                   ;; PDF
                   ("pdf" pdf) ("epdf" pdf)
                   ;; EPUB
                   ("epub" epub)
                   ;; PostScript
                   ("ps" ps) ("eps" ps)
                   ;; DjVu
                   ("djvu" djvu)
                   ;; OpenDocument formats.
                   ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
                   ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
                   ("ots" odf) ("otp" odf) ("otg" odf)
                   ;; Microsoft Office formats (also handled by the odf
                   ;; conversion chain).
                   ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
                   ("ppt" odf) ("pps" odf) ("pptx" odf) ("rtf" odf)
                   ;; CBZ
                   ("cbz" cbz)
                   ;; FB2
                   ("fb2" fb2)
                   ;; (Open)XPS
                   ("xps" xps) ("oxps" oxps))
		 t))))
	(content-types
	 (save-excursion
	   (goto-char (point-min))
	   (cond
	    ((looking-at "%!") '(ps))
	    ((looking-at "%PDF") '(pdf))
	    ((looking-at "\367\002") '(dvi))
	    ((looking-at "AT&TFORM") '(djvu))
            ;; The following pattern actually is for recognizing
            ;; zip-archives, so that this same association is used for
            ;; cbz files. This is fine, as cbz files should be handled
            ;; like epub anyway.
            ((looking-at "PK") '(epub odf))))))
    (setq-local
     doc-view-doc-type
     (car (or (nreverse (seq-intersection name-types content-types #'eq))
              (when (and name-types content-types)
                (error "Conflicting types: name says %s but content says %s"
                       name-types content-types))
              name-types content-types
              (error "Cannot determine the document type"))))))


-- 
Andrew Cohen



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-11  2:53       ` Andrew Cohen
@ 2023-09-11 17:01         ` Eric Abrahamsen
  2023-09-12  0:37           ` Andrew Cohen
  2023-09-11 19:01         ` Björn Bidar
  1 sibling, 1 reply; 9+ messages in thread
From: Eric Abrahamsen @ 2023-09-11 17:01 UTC (permalink / raw)
  To: ding

Andrew Cohen <acohen@ust.hk> writes:

> Sorry for not replying sooner (I am swamped with real work and have
> little time for other things); I have had this working for myself so I
> thought I can provide some advice.
>
> Firstly, telling gnus to use doc-view for these documents is easy: you
> need to modify the variable 'mailcap-user-mime-data (which controls user
> overrides for various mime types). Here is an example (this will use
> doc-view-mode for mime types of ms-excel and
> openxmlformats-officedocument.wordprocessingml.document, and use eww for
> html.) You should modify this for your own needs:
>
> (setq mailcap-user-mime-data
>     '(((viewer . doc-view-mode)
>        (test   . window-system)
>        (type . "application/vnd.ms-excel"))
>       ((viewer . doc-view-mode)
>        (test   . window-system)
>        (type . "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
>       ((viewer . eww)
>        (test   . (fboundp 'eww))
>        (type   . "text/html"))))
>
> But unfortunately this won't work properly due to a deficiency in
> doc-view. Doc-view has only a fairly primitive mechanism for figuring
> out the type of the document; since docx documents are mostly zip
> archives, and many other file formats are also zip archives, doc-view
> will notice they are zip files and treat them as epub (for me, at
> least).  The right way to fix this is to smarten up doc-view to
> correctly identify the file type. This isn't hard, but I don't have time
> to do it right now (maybe someone else is willing?).  In the meantime
> you can use the following hack which works for me: replace the function
> 'doc-view-set-doc-type with the modified version below.
>
> (defun doc-view-set-doc-type ()
>   "Figure out the current document type (`doc-view-doc-type')."
>   (let* ((buffer-file-name (or buffer-file-name (buffer-name (current-buffer))))
>          (name-types
> 	 (when buffer-file-name
> 	   (cdr (assoc-string
>                  (file-name-extension buffer-file-name)
>                  '(
>                    ;; DVI
>                    ("dvi" dvi)
>                    ;; PDF
>                    ("pdf" pdf) ("epdf" pdf)
>                    ;; EPUB
>                    ("epub" epub)
>                    ;; PostScript
>                    ("ps" ps) ("eps" ps)
>                    ;; DjVu
>                    ("djvu" djvu)
>                    ;; OpenDocument formats.
>                    ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
>                    ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
>                    ("ots" odf) ("otp" odf) ("otg" odf)
>                    ;; Microsoft Office formats (also handled by the odf
>                    ;; conversion chain).
>                    ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
>                    ("ppt" odf) ("pps" odf) ("pptx" odf) ("rtf" odf)
>                    ;; CBZ
>                    ("cbz" cbz)
>                    ;; FB2
>                    ("fb2" fb2)
>                    ;; (Open)XPS
>                    ("xps" xps) ("oxps" oxps))
> 		 t))))
> 	(content-types
> 	 (save-excursion
> 	   (goto-char (point-min))
> 	   (cond
> 	    ((looking-at "%!") '(ps))
> 	    ((looking-at "%PDF") '(pdf))
> 	    ((looking-at "\367\002") '(dvi))
> 	    ((looking-at "AT&TFORM") '(djvu))
>             ;; The following pattern actually is for recognizing
>             ;; zip-archives, so that this same association is used for
>             ;; cbz files. This is fine, as cbz files should be handled
>             ;; like epub anyway.
>             ((looking-at "PK") '(epub odf))))))
>     (setq-local
>      doc-view-doc-type
>      (car (or (nreverse (seq-intersection name-types content-types #'eq))
>               (when (and name-types content-types)
>                 (error "Conflicting types: name says %s but content says %s"
>                        name-types content-types))
>               name-types content-types
>               (error "Cannot determine the document type"))))))

Thanks for this!

The version of `doc-view-set-doc-type` in master looks almost exactly
like what you've posted here, with the exception of the let* for
(buffer-file-name (or buffer-file-name (buffer-name (current-buffer))))
at the top. Could someone have fixed it in the meantime?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-11  2:53       ` Andrew Cohen
  2023-09-11 17:01         ` Eric Abrahamsen
@ 2023-09-11 19:01         ` Björn Bidar
  1 sibling, 0 replies; 9+ messages in thread
From: Björn Bidar @ 2023-09-11 19:01 UTC (permalink / raw)
  To: Andrew Cohen; +Cc: ding

Andrew Cohen <acohen@ust.hk> writes:

> Sorry for not replying sooner (I am swamped with real work and have
> little time for other things); I have had this working for myself so I
> thought I can provide some advice.
>
> Firstly, telling gnus to use doc-view for these documents is easy: you
> need to modify the variable 'mailcap-user-mime-data (which controls user
> overrides for various mime types). Here is an example (this will use
> doc-view-mode for mime types of ms-excel and
> openxmlformats-officedocument.wordprocessingml.document, and use eww for
> html.) You should modify this for your own needs:
>
> (setq mailcap-user-mime-data
>     '(((viewer . doc-view-mode)
>        (test   . window-system)
>        (type . "application/vnd.ms-excel"))
>       ((viewer . doc-view-mode)
>        (test   . window-system)
>        (type . "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
>       ((viewer . eww)
>        (test   . (fboundp 'eww))
>        (type   . "text/html"))))
>
> But unfortunately this won't work properly due to a deficiency in
> doc-view. Doc-view has only a fairly primitive mechanism for figuring
> out the type of the document; since docx documents are mostly zip
> archives, and many other file formats are also zip archives, doc-view
> will notice they are zip files and treat them as epub (for me, at
> least).  The right way to fix this is to smarten up doc-view to
> correctly identify the file type. This isn't hard, but I don't have time
> to do it right now (maybe someone else is willing?).  In the meantime
> you can use the following hack which works for me: replace the function
> 'doc-view-set-doc-type with the modified version below.

Thanks that's exactly what I was looking for. I assumed since docview
can detect docx in dired in can do so in Gnus.

Docview is just ok, I would prefer something like pdf-tools for docx/odt
but I see those kind of file types so rare.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-11 17:01         ` Eric Abrahamsen
@ 2023-09-12  0:37           ` Andrew Cohen
  2023-09-12 17:59             ` Eric Abrahamsen
  0 siblings, 1 reply; 9+ messages in thread
From: Andrew Cohen @ 2023-09-12  0:37 UTC (permalink / raw)
  To: ding

>>>>> "EA" == Eric Abrahamsen <eric@ericabrahamsen.net> writes:

    EA> The version of `doc-view-set-doc-type` in master looks almost
    EA> exactly like what you've posted here, with the exception of the
    EA> let* for (buffer-file-name (or buffer-file-name (buffer-name
    EA> (current-buffer)))) at the top. Could someone have fixed it in
    EA> the meantime?

No, that small change is exactly the fix!

When viewing a mime part in gnus, the buffer holding the document
doesn't have an underlying file so 'buffer-file-name is nil (unlike when
invoking doc-view from dired). Thus the whole first part of the routine
is skipped, and only the first few bytes are looked at to determine the
file type (which just decides it is a zip file, and ends up with 'epub).

The change I made relies on the fact that the buffer that holds the
content for doc-view gets a name from the "filename" attribute of the
mime part in the message (i.e. the buffer gets named something like
"*mm* junk.docx") and this can be used to infer the file extension. Then
things work just like they would if doc-view had been invoked for a
buffer holding an actual file.

After some thought I believe that this is worth pushing to master, which
I'll try to do when I get some time.

Best,
Andy
-- 
Andrew Cohen



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: View docx/doc documents from Gnus in Docview
  2023-09-12  0:37           ` Andrew Cohen
@ 2023-09-12 17:59             ` Eric Abrahamsen
  0 siblings, 0 replies; 9+ messages in thread
From: Eric Abrahamsen @ 2023-09-12 17:59 UTC (permalink / raw)
  To: ding

Andrew Cohen <acohen@ust.hk> writes:

>>>>>> "EA" == Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>     EA> The version of `doc-view-set-doc-type` in master looks almost
>     EA> exactly like what you've posted here, with the exception of the
>     EA> let* for (buffer-file-name (or buffer-file-name (buffer-name
>     EA> (current-buffer)))) at the top. Could someone have fixed it in
>     EA> the meantime?
>
> No, that small change is exactly the fix!

Oh! Ha.

> When viewing a mime part in gnus, the buffer holding the document
> doesn't have an underlying file so 'buffer-file-name is nil (unlike when
> invoking doc-view from dired). Thus the whole first part of the routine
> is skipped, and only the first few bytes are looked at to determine the
> file type (which just decides it is a zip file, and ends up with 'epub).
>
> The change I made relies on the fact that the buffer that holds the
> content for doc-view gets a name from the "filename" attribute of the
> mime part in the message (i.e. the buffer gets named something like
> "*mm* junk.docx") and this can be used to infer the file extension. Then
> things work just like they would if doc-view had been invoked for a
> buffer holding an actual file.
>
> After some thought I believe that this is worth pushing to master, which
> I'll try to do when I get some time.

Cool, thank you. I can help push if you're not able to get to it.



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-09-12 17:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-08 16:18 View docx/doc documents from Gnus in Docview Björn Bidar
2023-09-10 16:20 ` Eric Abrahamsen
2023-09-10 19:50   ` Björn Bidar
2023-09-10 21:43     ` Eric Abrahamsen
2023-09-11  2:53       ` Andrew Cohen
2023-09-11 17:01         ` Eric Abrahamsen
2023-09-12  0:37           ` Andrew Cohen
2023-09-12 17:59             ` Eric Abrahamsen
2023-09-11 19:01         ` Björn Bidar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).