Preamble
I, as a rule, edit the source code in the mcedit editor embedded in mc (midnight commander) (do not ask why not in vim, it happened so historically). And for quite some time in mcedit you can use the function of recording and playing macros.
I use macros all the time, but these are usually fairly simple things, like replacing one text with another, or repeating some single-type operations. But in this case, I had to deal with writing a more complex macro, with the implementation of which I would like to introduce the potential user of the wonderful editor mcedit.
Given
The task was to open for editing a file containing a problem in which the hotkey, i.e. putting a minimum of effort.
Ie, the editor opened a file with a list of warnings that was generated by the doxygen utility, it was a list containing the full path to the problem file as well as the line number.
The file with warnings looked like this (only the lines were much larger):
/home/smind/dev/mc/lib/vfs/path.c:1046: warning: argument 'element' of command @param is not found in the argument list of vfs_path_serialize(const vfs_path_t *vpath, GError **error) /home/smind/dev/mc/lib/vfs/path.c:1212: warning: The following parameters of vfs_path_append_new(const vfs_path_t *vpath, const char *first_element,...) are not documented: parameter 'first_element' /home/smind/dev/mc/lib/vfs/path.c:1246: warning: The following parameters of vfs_path_append_vpath_new(const vfs_path_t *first_vpath,...) are not documented: parameter 'first_vpath'
')
Decision
First of all, the task was divided into two subtasks:
1) select the desired part of the text from the beginning of the line to the colon, including the line number;
2) process the selected fragment in the external program;
External macros (external macros)
In other words, you need to write an external macro that will accept the line and open the desired file on the desired line.
You can find out where mcedit stores external macros with the command:
smind@darkstar$ mc -F
as a result we will see the following text
[User data]
mcedit macros: /home/smind/.local/share/mc/mc.macros
mcedit external macros: /home/smind/.local/share/mc/mcedit/macros.d/macro.*
Next, you need to write the code of the external handler and save it with the name /home/smind/.local/share/mc/mcedit/macros.d/macro.<NUMBER>.sh
This <NUMBER> will be required later when writing a macro (in my case the file was called macro.42.sh).
The external macro file should look something like this.
e edit file mcedit `cat %b`
% b - the name of the file containing the selected text in the editor.
Editor Macros (mcedit macros)
To record a macro, simply press the ctrl-r combination, after which further key presses will be recorded and can be repeated.
To finish, you need to press ctrl-r again and then click the hotkey to invoke the macro.
Now we need to write the following macro:
Shift-F7 ( ) Ctrl-Right ( ) Ctrl-Right ( ) Shift-Home ( )
... and after the end of the macro recording, hang it on the ctrl-P combination, after which you must open the file that stores the macros
/home/smind/.local/share/mc/mc.macros [editor] ctrl-T=ExecuteScript:2; ctrl-F=ExecuteScript:0; ctrl-A=MarkAll:-1; ctrl-W=Paste:-1;Down:-1; ctrl-P=SearchContinue:-1;WordRight:-1;WordRight:-1;MarkToHome:-1;
In the line containing the desired macro, you need to add a call to an external macro -
ExecuteScript: 42;It should be something like this:
[editor] ctrl-T=ExecuteScript:2; ctrl-F=ExecuteScript:0; ctrl-A=MarkAll:-1; ctrl-W=Paste:-1;Down:-1; ctrl-P=SearchContinue:-1;WordRight:-1;WordRight:-1;MarkToHome:-1;ExecuteScript:42;
Then simply set the cursor on any line containing the path and press ctrl-p, the file will open on the way under the cursor.
FIN
PS: I hope the number of users using macros in mcedit will suddenly increase.
PPS: I have hung the formatting of selected text for the combination ctrl-F using the indent utility (very convenient)
PPPS: I apologize for the large number of repetitions of the words “Next” and “After”