most emacs major modes have “hooks” mechanism. It means, you can have emacs automatically run some code, whenever the mode is loaded.
Here’s a example:
(add-hook ‘org-mode-hook ‘soft-wrap-lines) ; make org-mode wrap long lines
(defun soft-wrap-lines ()
“Make lines wrap at window edge and on word boundary,
in current buffer.”
(setq truncate-lines nil)
(setq word-wrap t)
another example, adding some personal keys to a major mode.
(add-hook ‘org-mode-hook ‘xah-org-mode-keys)
(defun xah-org-mode-keys ()
“my keybindings for org-mode. For `org-mode-hook’.”
(local-set-key (kbd “<M-up>”) ‘org-metaup)
(local-set-key (kbd “<M-down>”) ‘org-metadown)
(local-set-key (kbd “<M-left>”) ‘org-metaleft)
(local-set-key (kbd “<M-right>”) ‘org-metaright)
typically, if a mode is named “xyz-mode”, its hook is usually named “xyz-mode-hook”. You can find out for sure by loading the mode first, then call “describe-variable” on the hook name. Or, call “describe-function” on “xyz-mode” and go to the code and do a “list-matching-lines” for the word “hook”.
to elisp, a hook is basically just a variable, pre-defined in a major mode. The value of this variable is a list of symbols (function names). When a major mode is loaded, emacs will run all the functions.
You can look at the value of a hook anytime by calling “describe-variable”.
now, whenever you want to remove some hooks, you can do this:
(remove-hook ‘org-mode-hook ‘soft-wrap-lines)
select the line, and call “eval-region”.
whenever you made changes to the hook, you usually need to reload the mode for it to be in effect. Fastest way is simply to re-open that file.