Robert Basic's blog

Posts tagged 'plugin'

Import custom Python modules in Vim plugins

by Robert Basic on June 16, 2016.

This took me a while to figure out so I’m writing it down for future self and anyone else who needs it.

I started writing a new Vim plugin that will use the Python interface as most of the work will be done there, mostly to keep my sanity.

Having a plugin layout such as:

.
├── lib
│   └── mypymodule
│       └── ham.py
└── plugin
    └── my-vim-plugin.vim

I want to be able to do a

from mypymodule import ham

from within the my-vim-plugin.vim file.

For that to happen the <sfile> command line special comes to rescue. It is the file name of the sourced file in Vim, that is, the file name of the Vim plugin. Using the :p and :h file name modifiers it gives us the full path to the plugin directory of our plugin.

" This will give something like
" /home/robert/projects/my-vim-plugin/plugin
let g:plugin_path = expand('<sfile>:p:h')

And here comes the kicker: the <sfile> needs to be expanded outside of our Vim function where it is used, otherwise the <sfile> points to the path of the file that called the Vim function.

In code, getting the path to the sourced file from within the Vim function would be incorrect:

" ./plugin/my-vim-plugin.vim
function! MyVimPlugin()
python << endpython

import vim

vim.command("let a:plugin_path = expand('<sfile>:p:h')
plugin_path = vim.eval("a:plugin_path")
print plugin_path

endpython
endfunction

because it would end up printing the current working directory from where the MyVimPlugin function is called.

The correct way to do is to get the path to the sourced file outside of the Vim function:

" ./plugin/my-vim-plugin.vim
let g:plugin_path = expand('<sfile>:p:h')

function! MyVimPlugin()
python << endpython

import vim

vim.command("let a:plugin_path = expand('<sfile>:p:h')
plugin_path = vim.eval("a:plugin_path")
print plugin_path

endpython
endfunction

This way the path is set at the time the plugin is sourced and not at the time when function is called.

Finally, to be able to import the mypymodule from the lib, we need to point to the lib directory and add it to the system paths. Complete example:

" ./plugin/my-vim-plugin.vim
let g:plugin_path = expand('<sfile>:p:h')

function! MyVimPlugin()
python << endpython

import os
import sys
import vim

" Get the Vim variable to Python
plugin_path = vim.eval("g:plugin_path")
" Get the absolute path to the lib directory
python_module_path = os.path.abspath('%s/../lib' % (plugin_path))
" Append it to the system paths
sys.path.append(python_module_path)

" And import!
from mypymodule import ham

endpython
endfunction

By the way, here’s the documentation for the Vim Python module.

Happy hackin’!

Tags: plugin, python, vim.
Categories: Development, Programming.

Vim Hugo helper

by Robert Basic on March 25, 2016.

I think I just wrote my first Vim plugin. OK, it’s more a bunch of Vim functions slapped together than an actual plugin, but gotta start somewhere, right?

Last week I converted this blog to a static web site and I’m using Hugo as the static website engine. Writing posts is a lot easier now, plus it’s written in Go, which I started learning a few weeks ago.

Vim Hugo helper is a plugin, collection of Vim functions that help me while writing posts. It will hopefully speed up a few recurring tasks such as drafting and undrafting posts, inserting code highlight blocks, etc. I guess I’ll be adding more to it in the future, but for now it has a total of 5 functions.

Front matter reorder

Hugo posts have a front matter which is basically meta data for the content. Hugo will create the front matter out of an archetype which is a template of sorts for the front matter. One thing I don’t like is that when a new post is created, the meta data is sorted alphabetically so the HugoHelperFrontMatterReorder function reorders it in the way that I do like.

Drafting/undrafting posts

The HugoHelperDraft and HugoHelperUndraft functions simply draft and undraft posts by setting the draft meta data in the front matter to either true or false.

Code highlight blocks

The HugoHelperHighlight(‘language’) function inserts the highlight shortcode that comes built-in with Hugo and sets the language of the highlight block.

Setting the date of the post

Finally, the HugoHelperDateIsNow function sets the date meta data of the post to the current date and time.

It’s not much, definitely has room for improvements, but I used it even when writing this very post, so I guess this helper is helpful.

Tags: hugo, plugin, vim.
Categories: Development, Programming.

Loading custom module plugins

by Robert Basic on July 20, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

OK, here’s a quicky one from the office :P

I was trying to load a Front Controller plugin which resides in app/modules/my_module/controllers/plugins/ and not in the “usual” lib/My_App/Plugin/. I want this plugin to be called in every request and I want the plugin file to be under it’s “parent” module.

Here’s what I did: added the path to the plugin and it’s namespace to the Zend_Application_Module_Autoloader as a new resource type and then just register the plugin in the front controller in an other _init method.

Code is better, here’s some:

<?php
class News_Bootstrap extends Zend_Application_Module_Bootstrap
{
    /**
     * Autoloader for the "news" module
     *
     * @return Zend_Application_Module_Autoloader
     */
    public function _initNewsAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(
                                array(
                                    'namespace' => 'News',
                                    'basePath' => APPLICATION_PATH . '/modules/news'
                                )
                            );

        // adding model resources to the autoloader
        $moduleLoader->addResourceTypes(
                array(
                    'plugins' => array(
                        'path' => 'controllers/plugins',
                        'namespace' => 'Controller_Plugin'
                    )
                )
            );

        return $moduleLoader;
    }

    public function _initPlugins()
    {
        $this->bootstrap('frontcontroller');
        $fc = $this->getResource('frontcontroller');

        $fc->registerPlugin(new News_Controller_Plugin_Scheduler());
    }
}

If anyone knows a better way for doing this, please do share it with me.

Now back to work. Cheerio.

Tags: framework, loading, php, plugin, zend.
Categories: Development, Programming.

Toggler

by Robert Basic on February 04, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

At the office we’re starting out on a new project, which will require (thanks to our designer) a lot off divs and images and whatnot to slide up and down. All these elements will of course have it’s own ID’s and classes, so writing one function to slide/toggle them all is impossible, plus the design of these elements is so weird that the built-in animation effects are of no use. So I hacked together my first! jQuery plugin which will hopefully help us with this task.

As I was more inspired to write code, than to come up with names, I called this little fella toggler, a jQuery plugin for togglering elements around…

What it does is actually calling .animate() on the height the top of the element that is to be togglered.

Setting it up is easy: include the jquery-toggler.js script, call the toggler() function on any clickable element and set the rel attribute of that element to match the ID of the element which is to be togglered (clearly, if you look in the source of the example, you’ll understand that better than my jibberish).

The default height when the element is closed (togglered up) is 0px, when the element is open (togglered down) is 200px and the default speed of this magical animation is set to 1000 (1 second). You can of course change these by passing them to the toggler({speed:500}) function.

toggler is available at GitHub: http://github.com/robertbasic/toggler

Example is here: http://robertbasic.com/toggler/

toggler yourself out.

Edit Februray 6th: Apparently I completely misunderstood the designer what kind of effect he wants, thus now I changed the code. The new code is pushed to github and the example is updated.

Tags: jquery, plugin, toggler.
Categories: Development, Programming.

TickTweet WordPress plug-in

by Robert Basic on November 21, 2008.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Few weeks ago @imjustcreative mentioned on Twitter that he would like a WordPress plug-in that would scroll (tick) tweets where “soultweet” is mentioned. As I wanted to do a plug-in for some time, but never had any good ideas, I told him that I’ll take up the job. So I started to work on this in my free time.

Before I even started looking at anything, I decided that I want this plug-in to be fast, to work with smallest possible data to save bandwidth and to keep the number of calls towards Twitter low.

First I looked into the Twitter Search API documentation, to see how data can be retrieved from Twitter — in Atom or in JSON.

The first idea...

As a JSON document is smaller than an XML document, I decided to retrieve data in JSON. Of course, once retrieved it would be cached locally in a file for some time (5 minutes is my default).

I also wanted to avoid the possibility of the page waiting to retrieve the data from Twitter, so I figured that it would be the best to call it up with Ajax. That way, when the plug-in is called up, it sends an Ajax request to himself, the page continues loading normally and in the background runs the Ajax request.

The draft was there, I looked at the WordPress writing a plug-in page and in a week or so the first version of the plug-in was ready to go out.

I tested it locally on my Windows machine (a basic WAMP setup) and on my Ubuntu machine (a basic LAMP setup), on this server and on another one which has a ton of security limitations (server of my College). I was glad to see that it worked like a charm on all 4 servers. I put up a TickTweet page, and let it out in the wild through Twitter.

The retweet madness started immediately. @imjustcreative, @jonimueller and @bishop1073 downloaded it right away. Soon as they enabled the plug-in, the short and exciting life of TickTweet started to end. Errors, bugs… Joni’s server is running on PHP 4, and I had a few PHP 5 only functions. My bad. On Graham’s and Bishop’s server who knows what went wrong. Graham helped me a lot tracing down the bugs, a few of them were found and squashed, but that was not enough. So I decided to pull back TickTweet, rethink it and possibly rewrite it.

The second idea...

OK, this JSON — Ajax thingy won’t work. Back to the paper. I started looking at the WordPress core to see what functions and/or classes are available in it for this kind of task… Didn’t took me long to find the fetch_rss() function. Man I was happy to find that! It’s using the MagpieRSS and the Snoopy classes to retrieve the data. I figured, those are included in WP’s core, they’re gonna do the job just fine. So I’ve rewritten it.

Testing again. The College’s server was dropped out right away, no way around that security. On others it worked fine. I tested for a couple of days just to make sure. When I thought it was OK, I’ve let it go once again. I contacted Joni, Graham and Bishop to tell them that the new rewritten version is out. On Joni’s site it worked. Awesome. On Bishop’s site worked. Kinda. On Graham’s site didn’t work. He tried it on another site. Worked. Cool. Finally it works. I was happy.

But not for long. The next day I saw that on my site it’s ticking some ol’ tweets. What?! Then started the bug hunting again. I looked at each line of code, var_dumped every variable. No luck. Somehow, all of a sudden, my server is not getting the data from Twitter. No changes on the server configuration, no change in the code, but it just won’t work.

The third idea...

The third idea is to leave this “plug-in” as—is, and to stop working on it. It just doesn’t pay off. Sure, I could trace down where it hangs on my server, going backwards through the code, but it’s just not worth it. Those who are interested in this plug-in, you can find it at the TickTweet page, use it, rewrite it, change it, trash it.

Cheers!

Tags: about, blog, php, plugin, site, wordpress.
Categories: Blablabla, Development, Programming, Software.