📜 ⬆️ ⬇️

While Vim is writing the code for you ...



I will not grovel for a long time on the topic of how great and powerful Vim is - this has long and reliably been known. Some even argue that "Virtually any string of Latin letters is a syntactically correct command for vi."

Sometimes a programmer is so subtly aware of the dark side of power that Vim starts writing code instead of him, leaving time for meditation and other useful things. One of these useful things could be writing another super useful plugin. The following lines of this humble treatise tell exactly about this.
')


Characters



Actually, there are only two of them - Vim and Python. But the interaction of these components leads to a thousand changes and ten thousand transformations. And once, when I was once again visited by the desire to transfer part of my daily activity to Vim, I took up writing a plug-in for reading posts from the main page of reddit. The idea is very simple - to pull out the list of posts in json format and display them in a formatted format in the editor's buffer. And, of course, this idea was partly educational in nature.

Vimscript, Python, Ruby are available as options for writing a plugin. I don’t have the opportunity to compare their strengths and weaknesses; I’ll just say that Python was chosen simply because I am well acquainted with it.

Plugin: Start



The simplest plugin for Vim is a file with the extension * .vim - but it’s better not to do that) Create a directory with the plugin, which we place in .vim / bundle / (use vundle). Inside we will get the plugin directory with source codes and add the line Bundle 'vim-reddit' . After these gestures, the plugin will be launched when Vim starts.

The basis of the plugin will still be the code written in Vimscript ( reddit.vim ). It will contain some settings, highlighting displayed posts, as well as a wrapper for calling Python code. In brief, I will talk about the main points and “rules of good tone” (although I sometimes ignore them myself).

Because in the code we will use Python, you need to check that Vim is built with support for this language:

if !has('python') echo "Error: Required vim compiled with +python" finish endif 


Then you need to check whether the plugin has been downloaded again. If not, we openly declare to the world our presence:

 if exists('g:vim_reddit_module') finish endif let g:vim_reddit_module = 1 


Python module


Then the main action begins. To begin with, I’ll reveal the terrible secret of running Python code from Vimscript:

 function! Reddit() python << EOF # some function main() EOF endfunction 


Inside the block, you can relax and call a couple of Python libraries. Let's create a module reddit.py ( __init__.py is meant), which will do all the work of loading json. But just to connect it does not work, because python cannot find this module. To get out of this tragic situation, you can add the path to the module in sys.path :

 import sys, vim sys.path.append(vim.eval("expand('<sfile>:p:h')")) 


I guess you already guessed that the eval method calculates the Vim command and returns the result) After that you can safely import the module:

 from reddit import main main() 


Now we will smoothly move on to the disgrace that occurs in reddit.main () . Again, I’ll skip the uninteresting details of json downloads and go straight through the features. And of the features here, perhaps, only work with the buffer. The fact is that the current buffer, in which we will add the loaded posts, is a simple array of strings. Therefore, cleaning looks like this:

 del vim.current.buffer[:] 


and adding a string like this:

 vim.current.buffer[0] = "Reddit front page " vim.current.buffer.append(20*"=" + " ") 


There is nothing more specific in the module. I will note only my keen desire to make the plugin flexible and customizable. This led to the appearance of the configuration file default.json , which, in addition to the json source, also contains instructions on which data to choose from it (by simple mapping). Therefore, in theory, it is possible to adapt this plugin to read any news feed in json format with a similar structure (i.e., a set of posts with certain attributes).

Little about the little things



To make reading more enjoyable, I added some useful details:

 setlocal nomodifiable setlocal buftype=nofile call s:reddit_syntax() 


As a result, we have an uneditable buffer. The reddit_syntax function paints content in all colors of the rainbow. She was spied on right here , with the exception of one group, which I added exclusively for myself:

 syntax match title /^.\{-1,} / highlight title gui=bold guifg=yellowgreen 


The group is called title and is described by the corresponding regular, and then it is highlighted with bold font.

Conclusion



The plugin is simple and low-functional as (to find an appropriate comparison) , but nevertheless it achieves its main goals - I (and, I hope, some of the readers) saw the light of the changes and finishing of Vim to fit my modest needs. Well, besides, now I can read reddit from Vim)

The plugin code is in my repository , it is extremely easy to install using vundle.

PS: It turns out that morning posts are much more difficult)

Source: https://habr.com/ru/post/187290/


All Articles