6

Is there a way to force Emacs to automatically save the current file (if it is not already saved) after typing n characters (n is a number determined by the user).

Edit: After the comment of @Dan and the suggestion of @lawlist it seems that the solution below is less risky:

   (setq auto-save-interval 10) ;;; n=10

    (defun force-backup-of-buffer ()
      (setq buffer-backed-up nil))

    (add-hook 'before-save-hook  'force-backup-of-buffer)

I should mention that the above solution is based on http://www.emacswiki.org/emacs/ForceBackups.

Name
  • 7,689
  • 4
  • 38
  • 84
  • 5
    Some unsolicited advise (I know, hooray!): be cautious with automatic saves. You run the risk that accidental changes to the buffer (unintentional deletes, mis-keyed yanks, etc.) could trigger a save, which will overwrite your file. `auto-save-mode` gets you most of the way there without actually overwriting your file; it seems better to reserve the explicit save for human intervention. As an alternative, maybe consider a function that puts a message in the echo area every *n* characters such as "You've changed a lot of text, please consider saving!", which you can heed or ignore at will. – Dan Jan 31 '15 at 12:06
  • 1
    @Dan thank you for the advice. You are right, I think I should use the build-in auto-save feature. – Name Jan 31 '15 at 14:50

1 Answers1

8

Emacs has an auto-save feature that can be adapted to suit your needs. You can customize auto-save-interval to determine how many characters:

auto-save-interval is a variable defined in `C source code'. Its value is 300

Documentation: Number of input events between auto-saves. Zero means disable autosaving due to number of characters typed.

You can customize this variable.

Dan
  • 32,584
  • 6
  • 98
  • 168
  • auto-saving backups the file but it doesn't save the file itself. So my question remains unanswered. Thank you anyway. – Name Jan 31 '15 at 03:23
  • 4
    How about using `save-buffer` (or a custom function that essentially uses `save-buffer` when there is a valid `buffer-file-name`) in conjunction with the `auto-save-hook`? -- *This normal hook is run whenever an auto-save is about to happen.* https://www.gnu.org/software/emacs/manual/html_node/elisp/Auto_002dSaving.html – lawlist Jan 31 '15 at 04:39
  • 1
    +1 why not incorporate the comment of @lawlist in the current answer of Dan or write a separate answer. – Name Jan 31 '15 at 10:11
  • 1
    @lawlist: good idea -- do you want to write that up as an answer? – Dan Jan 31 '15 at 11:59
  • @Dan -- the idea was primarily yours -- I just looked into whether it was a standard Emacs concept where modes generally have hooks, or a run-hooks statement at the end of a base function. The answer / credit belongs to you. :) As you know, there are certain modes / functions that place a `buffer-file-name` on a non-file-visiting-buffer -- I'm not sure how you are going to deal with that situation because `save-buffer` may prompt/offer the user confirmation to save . . . there are probably other exceptions also where a file hasn't been saved yet. Maybe a valid-file check would be needed. – lawlist Jan 31 '15 at 16:15