Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (63 sloc) 3.93 KB

gedit_better_errors

These instructions allow clicking on a better_errors link in a Google Chrome browser to open in gedit in Ubuntu.

I was watching a railscast and found a new gem called Better Errors. The link to the railscast is here http://railscasts.com/episodes/402-better-errors-railspanel. Anyway, when I was reading the comments, I noticed that someone had posted a comment to get the links from the Better Errors page to work with Vim in linux. Well I use gedit to edit source files in Ubuntu, so I decided to change the code posted on Ivan Tkalin's blog here http://www.tkalin.com/blog_posts/using-console-vim-as-vim-protocol-handler-in-ubuntu and convert it for gedit. I hope this can help someone having the same issues I had.

Create a gedit launcher script.

In Ivan's blog he creates a small vim launcher script, which will handle a cvim://... uri. I have changed the code below to handle run-gedit://... uri's. (I do not use gedit as to not cause any conflicts.) I also like the idea of having the script written in Ruby, since the gem we are trying to write this script for is in ruby as well (so it still requires Ruby to be installed), but it can be rewritten to whatever scripting language you choose. All it does is, retrieve the file name and line parameter from the URI string and launch gedit directly without the gnome terminal being involved. I feel this is a cleaner approach.

Create the file /usr/local/bin/run-gedit file with the following content:

#!/usr/bin/env ruby
#
# This file was created to execute gedit from the better_errors gem.
#
# This file is here to facilitate clicking on a link from the better errors page and running it in gedit.
#

require 'uri'
require 'cgi'

full_path = ARGV[0]

if full_path
  uri = URI::parse(full_path)  
  
  if uri.query
    params = CGI::parse(uri.query)
    line = params["line"][0]
    file = params["url"][0]
  end
  
  gedit_params = "+#{line} " + "#{file}"    
end

`gedit #{gedit_params}`

Don't forget to give this file execute permissions, as well as, give yourself permissions to run without sudo. I usually just type the following in the terminal because I prefer GUI tools to the command line. (I come from a Windows world, but have seen the light.)

gksu nautilus

Create .desktop file for the launcher we just created above.

As you can tell from above, it does not really matter what script we use. The .desktop file is what handles the call from google to asscociate the uri to execute the script.

Create the file /usr/share/applications/run-gedit.desktop with the following content:

[Desktop Entry]
Encoding=UTF-8
Name=run-gedit(uri)
Comment=Edit text files in a using gedit given a uri
Exec=/usr/local/bin/run-gedit %U
Terminal=false
Type=Application
Icon=accessories-text-editor
Categories=Application;Utility;TextEditor;
MimeType=text/plain;x-scheme-handler/run-gedit;
StartupNotify=true
StartupWMClass=RUN-GEDIT

Refresh the mime types database.

In the file above, line MimeType=text/plain;x-scheme-handler/run-gedit; registers run-gedit:// scheme handler, but to make it work we need to update the mime types database cache. Execute the following command from the terminal:

sudo update-desktop-database

Add the following code to your rails application.

In your Gemfile add:


group :development do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'meta_request'
end

Create the file [application]/config/initializers/better_errors.rb with the following content:


BetterErrors.editor = "run-gedit://open?url=%{file}&line=%{line}"

Run a test.

Now everything should work. To test that it works, Open your application and cause an error. Click on the link and it should work.