Gnus development mailing list
 help / color / mirror / Atom feed
* Save an article with its subject as default name
@ 2016-05-27 13:38 Dmitry Alexandrov
  2016-05-29  2:33 ` Dmitry Alexandrov
  2016-06-03 19:48 ` Reiner Steib
  0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Alexandrov @ 2016-05-27 13:38 UTC (permalink / raw)
  To: ding

Hello.

I’m quite new to Gnus, and I was quite surprised when found that in
contrast to any modern MUA, when I save a single message (‘article’) in
a file (‘o’), Gnus does not suggest to name it after subject.  The
manual [0] does not help a lot.

[0] https://www.gnu.org/software/emacs/manual/html_node/gnus/Saving-Articles.html

Ok, I have to implement it myself (am I right or am I trying to
re-invent a wheel?), that should not be so hard, probably something like
that:

(require 'subr-x)

(defvar gnus-rmail-save-name-extension ".eml")

(defun gnus-subject-save-name (_newsgroup headers &optional _last-file)
  "Generate file name from subject."
  (let ((subject (mail-header-subject headers)))
    (expand-file-name
     (*file-name-sanitize
      (concat
       (if (string-blank-p subject)
           (int-to-string (mail-header-number headers)) ; fallback to number
         subject)
       gnus-rmail-save-name-extension))
     gnus-article-save-directory)))

(setq gnus-rmail-save-name 'gnus-subject-save-name)

One problem though: ‘*file-name-sanitize’ is just a placeholder yet,
i. e. I need a function that takes an arbitrary string and return string
that is safe to use as a filename.  It is not hard to implement either,
of course, but that looks to me as a pretty common need for any program
that works with files, so I suppose GNU Emacs itself or some library
should have it already.  I cannot find it though.  Could you turn me in
right direction, please?




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

* Re: Save an article with its subject as default name
  2016-05-27 13:38 Save an article with its subject as default name Dmitry Alexandrov
@ 2016-05-29  2:33 ` Dmitry Alexandrov
  2016-05-29  6:11   ` Emanuel Berg
  2016-06-03 19:48 ` Reiner Steib
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry Alexandrov @ 2016-05-29  2:33 UTC (permalink / raw)
  To: ding

> One problem though: ‘*file-name-sanitize’ is just a placeholder yet,
> i. e. I need a function that takes an arbitrary string and return string
> that is safe to use as a filename.  It is not hard to implement either,
> of course, but that looks to me as a pretty common need for any program
> that works with files, so I suppose GNU Emacs itself or some library
> should have it already.  I cannot find it though.  Could you turn me in
> right direction, please?

OK.  Still could not find anything, so my ‘gnus.el’ has just got the
following:

(require 'subr-x)

(defconst sanitize-file-name--unsafe-chars-unix "[/]")
(defconst sanitize-file-name--unsafe-chars-windows "[\\/:*?\"<>|]")

(defvar sanitize-file-name--unsafe-chars
  (if (memq system-type '(ms-dos windows-nt cygwin))
       sanitize-file-name--unsafe-chars-windows
     sanitize-file-name--unsafe-chars-unix))

(defvar sanitize-file-name--max-bytes 255) ; most probable

(defun sanitize-file-name--truncate-to-bytes (string bytes)
  (if (> (string-bytes string) bytes)
      (sanitize-file-name--truncate-to-bytes (substring string 0 -1) bytes)
    string))

(defun sanitize-file-name (string &optional spare-bytes)
  (replace-regexp-in-string
   sanitize-file-name--unsafe-chars
   "-"
   (sanitize-file-name--truncate-to-bytes
    string
    (- sanitize-file-name--max-bytes
       (if (number-or-marker-p spare-bytes) spare-bytes 0)))
   t 'literal))

(defvar gnus-rmail-save-name-extension ".eml")

(defun gnus-subject-save-name (_newsgroup headers &optional _last-file)
  "Generate file name from subject."
  (let ((subject (mail-header-subject headers)))
    (expand-file-name
     (concat
      (sanitize-file-name
       (if (string-blank-p subject)
           (int-to-string (mail-header-number headers)) ; fallback to number
         subject)
       (string-bytes gnus-rmail-save-name-extension))
      gnus-rmail-save-name-extension)
     gnus-article-save-directory)))

(setq gnus-rmail-save-name 'gnus-subject-save-name)

Not being an Elisp programmer, I have no idea whether it neat or
horrible, but at first sight it works.




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

* Re: Save an article with its subject as default name
  2016-05-29  2:33 ` Dmitry Alexandrov
@ 2016-05-29  6:11   ` Emanuel Berg
  0 siblings, 0 replies; 6+ messages in thread
From: Emanuel Berg @ 2016-05-29  6:11 UTC (permalink / raw)
  To: ding

Dmitry Alexandrov <321942@gmail.com> writes:

> Not being an Elisp programmer, I have no idea
> whether it neat or horrible, but at first
> sight it works.

In terms of Elisp it looks good.

I suspect the Unix filename washing is around
already but it might take more time to find it
(and be more boring) than to write it yourself
so it is 50/50 what to do in those cases :)

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 42 Blogomatic articles -                   




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

* Re: Save an article with its subject as default name
  2016-05-27 13:38 Save an article with its subject as default name Dmitry Alexandrov
  2016-05-29  2:33 ` Dmitry Alexandrov
@ 2016-06-03 19:48 ` Reiner Steib
  2016-06-04 11:18   ` Emanuel Berg
  2016-06-08 12:33   ` Dmitry Alexandrov
  1 sibling, 2 replies; 6+ messages in thread
From: Reiner Steib @ 2016-06-03 19:48 UTC (permalink / raw)
  To: ding

On Fri, May 27 2016, Dmitry Alexandrov wrote:

> I’m quite new to Gnus, and I was quite surprised when found that in
> contrast to any modern MUA, when I save a single message (‘article’) in
> a file (‘o’), Gnus does not suggest to name it after subject.  

I guess most users save (interesting) articles in groups (e.g. nnml)
and read them with Gnus; No need for a file name based on the subject.

> One problem though: ‘*file-name-sanitize’ is just a placeholder yet,
> i. e. I need a function that takes an arbitrary string and return
> string that is safe to use as a filename.  [...] Could you turn me
> in right direction, please?

See `mm-file-name-rewrite-functions'?

(`gnus-sticky-article' might be useful as well.)

Bye, Reiner.



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

* Re: Save an article with its subject as default name
  2016-06-03 19:48 ` Reiner Steib
@ 2016-06-04 11:18   ` Emanuel Berg
  2016-06-08 12:33   ` Dmitry Alexandrov
  1 sibling, 0 replies; 6+ messages in thread
From: Emanuel Berg @ 2016-06-04 11:18 UTC (permalink / raw)
  To: ding

Reiner Steib <Reiner.Steib@gmx.de> writes:

> I guess most users save (interesting)
> articles in groups (e.g. nnml) and read them
> with Gnus; No need for a file name based on
> the subject.

Including the original groups: `gnus-summary-tick-article-forward'.

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 47 Blogomatic articles -                   




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

* Re: Save an article with its subject as default name
  2016-06-03 19:48 ` Reiner Steib
  2016-06-04 11:18   ` Emanuel Berg
@ 2016-06-08 12:33   ` Dmitry Alexandrov
  1 sibling, 0 replies; 6+ messages in thread
From: Dmitry Alexandrov @ 2016-06-08 12:33 UTC (permalink / raw)
  To: ding

Reiner Steib <Reiner.Steib@gmx.de> writes:

> On Fri, May 27 2016, Dmitry Alexandrov wrote:
>
>> I’m quite new to Gnus, and I was quite surprised when found that in
>> contrast to any modern MUA, when I save a single message (‘article’) in
>> a file (‘o’), Gnus does not suggest to name it after subject.
>
> I guess most users save (interesting) articles in groups (e.g. nnml)
> and read them with Gnus;

Articles saved in files named after subject are fully readable with
Gnus.  I use the following XDG association:

$ cat ~/.local/share/applications/gnus.desktop
[Desktop Entry]
Name=Gnus
MimeType=message/rfc822;message/news;
Exec=gnus %F
TryExec=gnus
Icon=/usr/share/icons/hicolor/scalable/apps/emacs24.svg
Type=Application
Terminal=false
Categories=Internet;

where ‘gnus’ is a script:

$ cat ~/.bin/gnus
#!/bin/bash

if [[ ! $1 ]]; then
    exec emacsclient --create-frame --eval '(gnus)'
else
    exec emacsclient \
         --create-frame \
         --eval "(require 'gnus-group)" \
         --eval "(gnus-group-read-ephemeral-group \"\" '(nndoc \"$1\"))"
fi

> No need for a file name based on the subject.

Why?  Is not this the most natural way?  With Gnus it’s especially
convenient, since unlike other MUAs it supports saving / reading whole
tread in / from one file.

>> One problem though: ‘*file-name-sanitize’ is just a placeholder yet,
>> i. e. I need a function that takes an arbitrary string and return
>> string that is safe to use as a filename.  [...] Could you turn me
>> in right direction, please?
>
> See `mm-file-name-rewrite-functions'?

Thanks, but it seems that these functions [0] are for making already
safe filenames ‘safer’, not turning arbitrary string to a sane filename.
In particular, there is nothing about complying a requirement that file
name must be less than 255 *bytes* long.

Not a problem, though.  While I fully admit that the solution I wrote
[1] — recursively strip characters one by one until string fit the limit
— is not the best one, as long as it works okay, I’ll stick to it.

However the idea to ensure that file name contain no control characters
and does not start or end with a whitespace looks reasonable.  I will add
it to my ‘sanitize-file-name’.

[0] (info "(emacs-mime) Files and Directories")
    https://www.gnu.org/software/emacs/manual/html_node/emacs-mime/Files-and-Directories.html

[1] <87inxxegou.fsf@gmail.com>
    http://comments.gmane.org/gmane.emacs.gnus.general/87070

> (`gnus-sticky-article' might be useful as well.)

Sorry, but I probably missed the point.  The Manual [2] says:

    When you select an article the current article buffer will be reused
    according to the value of the variable ‘gnus-single-article-buffer’.
    If its value is non-‘nil’ (the default) all articles reuse the same
    article buffer.  Else each group has its own article buffer.

    This implies that it’s not possible to have more than one article
    buffer in a group at a time.  But sometimes you might want to
    display all the latest emails from your mother, your father, your
    aunt, your uncle and your 17 cousins to coordinate the next
    Christmas party.

    That’s where sticky articles come in handy.  A sticky article buffer
    basically is a normal article buffer, but it won’t be reused when
    you select another article.

This indeed might be useful, but how is it related to saving articles?

[2] (info "(gnus) Sticky Articles")
    https://www.gnu.org/software/emacs/manual/html_node/gnus/Sticky-Articles.html



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

end of thread, other threads:[~2016-06-08 12:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-27 13:38 Save an article with its subject as default name Dmitry Alexandrov
2016-05-29  2:33 ` Dmitry Alexandrov
2016-05-29  6:11   ` Emanuel Berg
2016-06-03 19:48 ` Reiner Steib
2016-06-04 11:18   ` Emanuel Berg
2016-06-08 12:33   ` Dmitry Alexandrov

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).