From c1764a2b6f22f2ec09af6de9ee5b30e1ef961dda Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Wed, 8 Feb 2023 15:52:45 +0000 Subject: [PATCH] Finish up ep13 --- docs/videos.org | 104 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/docs/videos.org b/docs/videos.org index 9faddb8..0c8bd28 100644 --- a/docs/videos.org +++ b/docs/videos.org @@ -778,4 +778,106 @@ We can clone a library somewhere on the disk and add the path to it to the =load and load the library files. But that would be tedious to do so for all the libraries. That's why we use a package manager -* Episode 13 - Hooks +* Episode 13 - Editing Modes, Part 1 +Emacs provides a concept called ~editing mode~ that allows +us to control different aspect of the editor. + +** Major Modes +Major modes are mutually exclusive, so each Buffer has exactly on +major mode and just one major mode can be active at any given +time. So, it is possible to switch between different major modes. + +Major modes control the main behaviour of your editor for each buffer. +For example, The active major mode might: + +- Provide syntax highlighter +- Control the indentation +- Provide a local =keymap= +- ... + +To put it simply, major modes are specialized to handle certain files +and buffers. + +*** Naming Convenstion +Usually, the name of a major mode is like =-mode= which is +an interactive function that we can call either directly or via =M-x= interface. +For example: + +- python-mode +- fundamental-mode +- emacs-lisp-mode +- ... + +**** Keymap +*We will take about Keymaps in the future* + +Major modes usually have a keymap to hold their local keybindings that has the +the =-map= suffix. + +**** Hooks +*We will take about Hooks in the future* + +Hooks are lists of functions that can be called on certain occasions. For example, +after a major mode activates. + +Usually major modes come with at least one hook called =-hook= that +runs after the major mode activates. E.g. =emacs-lisp-mode-hook= or =python-mode-hook=. + +*** How Emacs choose a major mode for a buffer? +When we open a file, Emacs goes through some hoops to figure out what major mode +should it choose for that buffer. + +To put it simply and avoid a lot of details, Emacs will try to match the buffer name +against the keys in ~auto-mode-alist~ and uses the mode provided by that key as a value. + +#+BEGIN_SRC emacs-lisp + (("\\`/tmp/fol/" . text-mode) + ("\\.texinfo\\'" . texinfo-mode) + ("\\.texi\\'" . texinfo-mode) + + ("\\.el\\'" . emacs-lisp-mode) + ("\\.c\\'" . c-mode) + ("\\.h\\'" . c-mode) + …) +#+END_SRC + +For more info check out: +https://www.gnu.org/software/emacs/manual/html_node/elisp/Auto-Major-Mode.html + +*** How to switch the major mode +Just call the other mode or +- ~major-mode-suspend~: Kills all the buffer local variables and record them +- ~major-mode-restore~: This function restores the major mode recorded by ~major-mode-suspend~ + +For more info: +https://www.gnu.org/software/emacs/manual/html_node/elisp/Major-Modes.html + +** Minor Modes +A minor mode provides optional features that users may enable or disable independently of +the choice of major mode. Minor modes can be enabled individually or in combination. + +The main difference with major modes is that minor modes are not mutually exclusive and +are not tied to buffers. They can operate globally or local to buffers. + +*** How to enable/disable minor modes? +In order to toggle a minor mode we just have to call its function with no argument +*interactively*. In order to disable a minor mode we can pass a negative integer, +and to enable it we can pass a positive integer. + + +#+BEGIN_SRC emacs-lisp + ;; Enable + (blah-mode 1) + + ;; Disable + (blah-mode -1) +#+END_SRC + +** Useful functions and variables +- ~describe-mode~: Display documentation of current major mode and minor modes and a brief + summary of the state of the current buffer. +- ~local-minor-modes~: This buffer-local variable lists the currently enabled minor modes + in the current buffer, and is a list of symbols. +- ~global-minor-modes~: This variable lists the currently enabled global minor modes, + and is a list of symbols. +- ~minor-mode-list~: The value of this variable is a list of all minor mode commands.