help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to force auto-save of buffers not visiting files, right now?


From: hw
Subject: Re: how to force auto-save of buffers not visiting files, right now?
Date: Tue, 22 Mar 2022 08:06:26 +0100
User-agent: Evolution 3.42.4 (3.42.4-1.fc35)

On Sun, 2022-03-20 at 09:08 +0100, Emanuel Berg via Users list for the GNU 
Emacs text editor wrote:
> hw wrote:
> 
> > (defun my-perltidy-replace (_)
> >   "This function replaces the contents of the current buffer with
> > the output of perltidy, and makes a backup of the current buffer.
> > 
> > Before and after modifications to contents of buffers are being
> > made, all buffers that need to be auto-saved are being
> > auto-saved.
> > 
> > The major-mode of the buffer containing the backup is set to
> > 'cperl-mode'."
> 
> Emacs thinks
> 
>   the second line should not have indentation ...

Maybe it was altered during transfer?  It isn't indented.

>   the first line is not a complete sentence ....

To me, it's a complete sentence.  We'd have to ask some people who are
native speakers of English.

>   and probably "replaces" should be imperative "replace"!

Huh?  The function /does/ replace the contents.  It "shalln't" replace
them, if there is even such a word.

> >       (linum-mode -1)
> 
> This is the only one needed.

The wayland version of emacs screws up the display of the line numbers
when you have two buffers displayed in the same frame besides each
other, at least when you're using sway.  Disabling the mode and
re-enabling it is a workaround.

> 
> >       (auto-save-mode nil)
> 
> Yuk, not needed ...

That depends on whether you have auto-save-mode enabled by default or
not ...  In a function, you can either check whether auto-save-mode is
already enabled or not and enable it if it isn't, or just enable it.
In any case, it needs to be enabled for the function to do what I want
it to do, so enable it.

> BTW final args that could be nil (nil is in the
> function range) could always be optional and default to
> nil, right?
> 
> Here tho it is ugly by all means but also unnecessary since it
> _is_ an optional argument, so all good.

What do you mean?  Not all arguments default to nil, may they be
optional or not.

> >     (linum-mode 1)
> 
> Not as ugly! But as equally unnecessary still pretty ugl-
> I mean unnecessary.

When programming, I follow the principle that it's better to be more
explicit when it serves to make it more clear what the intention is.
And I picked '1' as argument rather than 'nil' because it's confusing
that something should be enabled rather disabled because the
equivalent of FALSE is given as an argument to it.  'Nil' should
disable stuff, not enable it.

Please think about this example:

perl -e 'print "no\n" unless false;'

That appears to work, but it leaves you to guess what the intention
is.  Using just (linum-mode) leaves me to guess and seems confusing.

As to optional arguments, I think they are more a disadvantage than an
advantage because the programmers have to make sure not to miss
specifying all arguments correctly every time they want to use a
function.  I rather have it that the computer checks the arguments for
me and tells me when there are missing ones, or too many.  In perl,
you can have that.

I also gave up statements like

array[iterator++] = 'foo';

in favour of either

array[iterator] = 'foo';
iterator++;

or

iterator++;
array[iterator] = 'foo';

a long time ago because they are unclear and cost a lot of time when
reading the code and thus are a nuisance.  If they're prettier, pretty
doesn't always win.

> > (defun my-transient-copy (_)
> >   "This function makes a copy of the current buffer to a new
> > buffer.  The new buffer does not visit a file.  Its name is based
> > on the name of the current buffer.
> > 
> > The 'auto-save-mode' is enabled for the new buffer, and all
> > buffers that need to be auto-saved are being auto-saved right
> > away, once before the copy is created and once after."
> 
> Emacs thinks
> 
> Second line should not have indentation
> 
> First line is not a complete sentence
> 
> Probably "makes" should be imperative "make"
> 
> >   (interactive "P")
> 
> Interesting, does that work and what does it do?!

The function works, yes.  Its description tells you what it does.

> >   (do-auto-save)
> >   (let ((transient_buffer (generate-new-buffer-name (concat 
> > "transient-copy-of-" (buffer-name)))))
> >     (copy-to-buffer transient_buffer (point-min) (point-max))
> 
> Here is another example - when the last two args a and b and
> 
>   (<= (point-min) a b (point-max))
> 
> then both formal parameters can be made optional if we had
> argument a defaulting to (point-min) and argument b defaulting
> to (point-max), right?

You're proving my point with this example:

With (copy-to-buffer buffer), you copy the region when omitting the
arguments.  Maybe it's prettier, but it isn't what I wanted.  Maybe
that's why the description tells you not to omit the arguments.

> >     (with-current-buffer transient_buffer (auto-save-mode
> >     nil))
> 
> Same.

Yes, explicitly :)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]