segunda-feira, 15 de junho de 2015

Package-require: require a library, eventually loading the package from the Emacs package archives

package-require is an Emacs Lisp utility function that will install a package if not installed from the archives, and then load the corresponding library. If the library name differs from the package name, it can be supplied.

(defun package-require (package-name &optional library-name)
  "Install the package named PACKAGE-NAME from the repositories
  if it doesn't exist locally and then load the library given by
  LIBRARY-NAME, or PACKAGE-NAME if it is homonimous.  Both names
  are strings, not symbols."

  (let ((package (intern package-name)))
    (unless (package-installed-p package)
      (package-install package)))
  (load-library (or library-name package-name)))

To use it, write in your emacs file, for instance:

(package-require "bpe")

What if the package name differs from the library to load, like in AucTeX? Just write the library name after the package. The following example loads "tex-site", installing "auctex" first if it does not exist.

(package-require "auctex" "tex-site")

A shell script to redo the home .cabal directory

Haskell user installation: automation with an interface

Note: A new version was posted at the 16 Jun 2015.  Please use the download link below, as it has been updated.

Haskell is a powerful language that you will love to hate and hate yourself to death by loving it. Haskell is expressive and, in my experience, will teach you more and more about computation as you use it. Like python, opens a new way to think of computing architecture and algorithms.
One of the most interesting tools of Haskell is The Haskell Cabal. Cabal is a system for building and packaging Haskell libraries and programs. There is a tool, cabal-install, which retrieves programs and libraries from source repositories, builds them on the local machine, and installs them by default in the user's home directory. It also allows sandboxes, directories whose library setup differs from the user or from the system libraries, used mainly for development.

Shell to the rescue

Since cabal is not a package manager, after a while you will find that it is more fruitful to just wipe the whole user installed packages and start over. It usually meant for me remembering all the packages I use and install them by hand. I have made before a shell script to help me automate that task. It worked very well, but I failed always to remember all the command line arguments I had to pass to install certain sections.
The following script is a bash script that will wipe out the ~/.cabal and ~/.ghc directories (with the exception of the configuration files) and restart the installation. Running this shell script allows an user to fire it and forget until it is built. It is nothing sophisticated, nor it needs to be. It just does the job.

Download

Link for download: haskell.

Requirements

It uses the Bourne Again Shell (bash), and has some code specific to bash. You should have it installed on your machine (all Linux distributions have bash, and most use it by default).
You should also have a working installation of Haskell in your computer, with the Cabal development libraries and the cabal command line tool.

Usage

Just follow the menus. You can wipe the whole cabal installation (.cabal and .ghc). This script always preserve the configuration files.
As for the main menu:

  • Clean wipes the cabal installation.
  • Install wipes the previous installations and installs only cabal.
  • All reinstalls all package blocks, unattended. Fire it up and go on a date. It'll be ready when it is ready.
  • All other items install some package blocks.

What if I want different packages?

Edit any of the functions beginning by install_, adapting them to your needs. If you want a new block, add it to the main menu, and create a corresponding do_ function, taking the existing ones as example.

Licence

Public domain.
If you wish to suggest improvements, feel free to add them to the comment box, and I'll open a git repository in github.

quinta-feira, 19 de março de 2015

How to modify an ember component from a controller.

The Problem

How do I change state in an ember component from an ember controller? For instance, calling a method or setting a variable?

The solution

The simple way is to share a property between the controller and the component. That is the gist of two-way binding, after all! What we may do is to use the binding facilities in Ember.

At the controller:

App.MyRouteController = Ember.Controller.extend({
    myComponent1Var: false;
    myComponent2Var: false;

    (...)
});

We have defined two property for two different instances of MyOwnComponent. At the component we will now pass the property at the controller as a binding for a component property

At the route template:

{{my-own-component varBinding="controller.myComponent1Var}}

(...)

{{my-own-component varBinding="controller.myComponent2Var}}

At this point, two instances of MyOwnComponent share completely different variables. Now, each of the components shares it's own property with the controller, and the property is called var (from varBinding). The component knows nothing about the controller and the controller knows nothing about the component. They just share state.

But what if I want to call a method at the component from the controller? Just watch the common property at the component. Add this to the component:

App.MyOwnComponent = Ember.Component.extend({
    (...)

    varChanged: function() {
        if (this.get('var')) {
            this.method();
        }
    }.observes('var');

    (...)
});

At the varChanged method, the method named method is called whenever the property var changes into a truish value.

The two-way binding allows one to communicate from a controller to a component ª(and vice-versa) without any of these objects having to ke aware of the other.