Programmers are divided into two categories:
1) Those who are already using Vim.
2) Those already using Emacs.
3) Those who have not yet used.Foreword
Somehow the idea came to put Emacs a second time, to make sure once again that this is some kind of wrong editor with a bunch of different games, but no functions for working with text. So stayed on it.
')
Add mode
Emacs has many different modes that add functionality to it. As a rule, when you need some kind of feature, it is downloaded as a package consisting of .el files (Emacs Lisp), and they are already connected to the built-in .el files that are responsible for loading the editor.
At first, it's all convenient to use, but then something starts to be missed and you have to think about adding your own functionality.
When you need to add one keyboard shortcut, you can do it directly in any of the configuration files. At first, it works and brings solid joy, but soon it starts to work not as expected: some keys will climb on to others, others - on the first, third - on the second. You have to redo everything or refuse something.
The longer you work with Emacs, the more he likes it. Therefore, the motivation did not take long to wait - the task appeared: “I need to have some kind of box with tools for automatic formatting of answers to the forum.”
How to solve (of course, without turning off Emacs)?
The answer is simple: you need to add your own mode, which you can turn on when you need it, and turn it off otherwise.
Reverse engineering
Naturally, to add a mode, you need to read a bunch of documentation, see how other modes are made, study the Lisp and not accidentally mess up something in a working Emacs.
Began the search for documentation. First tutorial articles ... no articles. Then examples of modes ... examples are filled with unnecessary constructions. Then the documentation ... read it and check for several days. Then ready-made modes ... nothing is clear, you need to learn Lisp.
The circle is closed. I had to use everything at once and a lot.
First approach
Information:
1) Modes are main (major) and side (minor).
2) Modes can be inherited from existing modes.
3) The main mode can be selected only one, while the side - a lot.
4) To the main mode, you can attach a lot of side.
First, the most minimal mode was compiled:
The file test-mode.el with this text is placed in a convenient folder, and then the mode is connected somewhere in the initialization file, like any other.
(add-to-list 'load-path "~/.emacs.d/packages/modes/test-mode/") (require 'test-mode)
Explanation of content:Create a mode that inherits everything from the Python mode:
(define-derived-mode test-mode python-mode
This line will be displayed in the mode name bar:
"Test"
This is a comment that is shown when you open help for a mode:
"Major mode for editing Test source text."
This is a variable that is created when entering the mode and is destroyed when exiting it:
(set (make-local-variable 'test-variable) "Test variable value"))
This assigns files with the .test extension to this mode automatically:
(add-to-list 'auto-mode-alist (cons "\\.test\\'" 'test-mode))
This rassharivaet mode, so that it can be connected from other files:
(provide 'test-mode)
With a minimum finished.
How to use it all?
Well, we can inherit, and then override any part. For example, one of the keyboard shortcuts to substitute for another, or a syntactic word that is painted in one color, repainted into another.
Add a function and assign a key for it: Here, only the mode name and the file extension are changed, in which it will be included automatically. And two things added: function and key mapping.
When we create the file file.testfunc and open it in Emacs, it sets the TestFunc mode. Since the mode is inherited from Piton, syntax highlighting and other things work for us. But when you press Ctrl + c + 1, we have a message that this combination is pressed, and the name of the function that has been triggered.
Now we can create a mode directory in the yasnippet directory with the name of our mode test-mode or test-func-mode and put some snippet there that will be separated from the usual Python and even overlapping will only complement each other.
Minimum binding
After some time, you realize that this is all well and good, of course, but when you are already in the correct mode, you need to stay in it. And the current mode with its own chips may not coincide with the one that was inherited initially.
Therefore, to add functionality to the current mode, a side mode (small or minor) comes to the rescue. He has all the same things as the main mode, but he does not force the current mode to drop the processed data and immediately switch somewhere.
We remake the main mode with the function in the side As you can see, everything is not as happy here as when creating the main mode. There were some strange constructions, not at all clear.
If explained in a simple way, the side mode is a switchable design. It can be turned on or off, so it must be able to determine its state so that it does not turn on again when it is already turned on. Moreover, unlike the main mode, it does not clean its variables behind itself, therefore, it is required to have such a branch that works when it is turned off.
If to explain in a complicated way, in Emacs a certain behavior is wired to work with such modes. It was possible to make them better, but it's too late.
The main thing to remember about small modes is that they have four variables at the top. There you can accidentally miss this, and he will put an expression that must be executed when the mode is turned on instead of the last variable that doesn’t say anything about it - you’ll sit in the documentation for two hours to understand why the expression does not work.
The solution of the problem
Here, a couple of functions have been added to wrap the selected text in <quote> </ quote> and <code> </ code> tags. In addition, we had to add a mode to yasnippet, because it detects only the main modes.
Here it should be noted that the mode can be tied to a file, launched manually through the Alt + x + name, or hooked to another mode via a hook.
Here is the connection to the Piton:
(add-hook 'python-mode-hook 'forum-minor-mode)
Now you can start writing more advanced text processing functions. Modes can be added and removed without affecting the rest of the settings.
Conclusion
When I put Emacs for the first time, I did not understand it, it seemed to me difficult, inconvenient and ugly (due to ancient fonts). Now, having put it a second time, I did not recognize it. It turned out so many wonderful applications, and convenience rolls over.
Before him, I used Vim, it was good and interesting, but his settings seemed to be non-native, all the time you had to read something to understand mnemonics. Because of this, I then programmed for a long time in kwrite, because you don’t need to learn anything, and the syntax aligns and tints well.
But only when I installed and merged with Emacs, I realized what power and simplicity are (for the time being, of course, until you try to open the file with 20Mb).
PS: If something is wrong in my given code or approach - well.