How can I limit the buffer size in Emacs?

Is there a way to limit the buffer size in Emacs? already checked this question: Can I limit the length of the compilation buffer in Emacs? but that is not what i want. I want to limit the size of the buffer that the user is working on, and therefore it cannot fit more than a given size.

+4
source share
2 answers

Example for message-mode (to place a buffer in this mode of type Mx 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))))) 

Please note that the user can still yank more characters. Another option is to reassign save-buffer .

+4
source

I do not see direct access to this, which you can do: hook pre-command-hook , if the command is self-insert-command , you can check if the buffer-size does not match the length you need. Alternatively, you can intercept post-self-insert-hook to check if it has reached the size limit, cancel it and print the message. To do this, you need to write minor-mode , which is active in your buffer.

+3
source

All Articles