Emacs specific region read only

emacs

Solution

Use text properties:

(defun set-region-read-only (begin end)
  (interactive "r")
  (add-text-properties begin end '(read-only t)))

Relevant Docs:

Text-Properties

Changing Properties

Special Properties (like read-only)

Problem

How does one make a specific region in a text file read only when using emacs. I know ctrl+X+Q to make the whole file a read only. I am currently writing a code and I do not want to modify by accident the first 40 lines of my code while working on lines 41 and upwards.

Original source