5

Is there any way that I can limit the size of a buffer in Emacs? have checked this question already: Can I limit the length of the compilation buffer in Emacs? but it's not what I want. I want to restrict the size of a buffer that the user is working on and so he can't put more than the pre-defined size.

Community
  • 1
  • 1
Daniel
  • 611
  • 5
  • 15

2 Answers2

4

An example for message-mode (to put a buffer in this mode type M-x message-mode)

(define-key message-mode-map [remap self-insert-command]
  '(lambda () 
     (interactive)
     (let ((limit-buffer-size 30))
       (message "buffer-size is %i of %i" (buffer-size) limit-buffer-size)
       (if (< (buffer-size) limit-buffer-size)
       (call-interactively 'self-insert-command)
     (message "Maximum bufer size is %i characters" limit-buffer-size)))))

Note that user still can yank more characters. Another option is to remap save-buffer.

slitvinov
  • 5,693
  • 20
  • 31
3

I cannot see any way to access this directly, what you can do is this: hook pre-command-hook if the command is an self-insert-command you can check if buffer-size is bellow the length you want. Alternatively you can hook post-self-insert-hook to check if the hit the size limit, undo it and print a message. For all this you will need to write a minor-mode that is active in your buffer.

pmr
  • 58,701
  • 10
  • 113
  • 156