Posts filed under ‘emacs’

Emacs and Oracle

After learning so much about Emacs to get my Rails setup working the way I wanted it, I though it was about time I figured out how to use SQLPLUS in Emacs too. Turns out it was really easy, as sql-mode is built right in, no .emacs changes required or extra files to download.

To connect to SQLPLUS in an emacs buffer, fire up emacs and type ALT-x sql-oracle. Emacs will prompt you for a username and password and a database to connect to. The database name will need to be in your local tnsnames.ora – in other words, if you cannot sqlplus username/password@database, emacs will not be able to connect either.

You can now enter commands into SQLPLUS just like normal, only inside Emacs. The thing that always frustrated me about SQLPLUS is that there is no command history recall, so I always found myself writing a query in an editor, and copying and pasting into SQLPLUS. Not anymore … thanks to Emacs.

When you enter sql-oracle mode and login, Emacs splits your window in two. You can then edit your SQL in the original window and send the query to SQLPLUS in the other buffer in (at least) one of two ways:

  • Send the entire buffer by typing ALT-x sql-send-buffer
  • Select a region to send by typing ALT-x sql-send-region, or if you buffer is in sql-mode, type CTRL-C CTRL-r

You can of course bind a key sequence to each of these commands to save on the typing if you use them a lot!

If you have an SQLPLUS buffer open for a while, it could get very large. To truncate it, use the command ALT-x comint-truncate-buffer, or add the following to your .emacs:


(add-hook 'sql-interactive-mode-hook
              (function (lambda ()
                          (setq comint-output-filter-functions 'comint-truncate-buffer
                                comint-buffer-maximum-size 5000
                                comint-scroll-show-maximum-output t
                                comint-input-ring-size 500))))

Which will keep your buffer under 5000 lines.

Advertisements

July 10, 2007 at 12:54 pm 4 comments

Emacs and Rails

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In the last article, I described how to get Ruby syntax highlighting, electric mode and some useful techniques to run code you are editing. This time I am going to describe how to install a full blown Rails development environment, that includes code snippets, and much more. Note that the Emacs-rails package requires Emacs 22 or greater.

What you need

If you have been following along with my setup, copy snippet.el and find-recursive.el into your include directory (or somewhere on your emails load path). Extract the Emacs-rails tar file into the include directory too. That should create a new directory, emacs-rails.

Now for the necessary changes to the _emacs file:


; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

One other thing to note – I needed to perform these includes in the _emacs AFTER my code to setup ECB. Emacs-rails seems to include a different version of the Speedbar, which breaks ECB.

What can emacs-rails do?

I have found the documentation to be a bit sketchy, but there is a good summary of the various snippets and some of the other commands available.

In my setup, rails-minor-mode started automatically when I browsed into one of my Rails projects using ECB. If for some reason it doesn’t start, you can start it with ALT-x rails-minor-mode.

Since I am using ECB, I don’t envisage using the commands to jump to models and controllers much but I will try them out and see how it goes!

The snippets are very useful. If you are running emacs with toolbars enabled (the default) a new snippets menu will appear when in Rails mode, which you can use to insert them. Better is to learn the short codes for the snippets you use often. For example, typing:


flash

and hitting the tab key will result in the following appearing:


flash[:notice] = 'Successfully'

Each of the placeholder fields are highlighted, so you can hit TAB to get into each of them and just start typing to replace them – its easier to play with than to explain!

There are a host of other menu options available when in Rails mode that allow you to view the log files and start the webserver among other things. I have only just scratched the surface myself and am still learning, so I cannot say much more at the moment!

Update – A few months later

After using Ruby mode and Rails mode for a few months, something I could just not get used to was Electric mode. I can live with the automatically inserted ‘end’ keys words when I open a new method def or an if statement, but single character completions like a closing quote or curly bracket just frustrated me. You still have to type ctrl-f to skip forward a character to get past that auto-inserted quote, or use your arrow keys – its easier to just type the quote!

Different people will prefer different things, so to customise just how much electric you want Emacs to provide you can edit the ruby-electric.el file. Look for a section like the following:

(defun ruby-electric-setup-keymap()
  (define-key ruby-mode-map " " 'ruby-electric-space)
;;  (define-key ruby-mode-map "{" 'ruby-electric-curlies)
;;  (define-key ruby-mode-map "(" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "|" 'ruby-electric-bar))
  )

Each line provides electric mode for each of the different characters – I commented out all the lines starting with ‘;;’ (as ; denotes a commented line), which means that now I only get electric ‘end’ keywords added and nothing else.

Summary

In this series of five articles, I have explained why I decided to try Emacs, how to install it on windows and the Mac, setup ECB, some Ruby specific tips and finally this article to get the Rails development environment working. Hopefully you found them useful! At this point I am still a relative Emacs newbie – I don’t know much beyond what is in these posts, but I am learning something new almost every day. For the sake of completeness, the contents of my .emacs is below.


; sets emacs to prompt on close so you cannot close by accident!!
;(setq confirm-kill-emacs 'yes-or-no-p)
(global-font-lock-mode 1)

; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "C-m" 'newline-and-indent)
	    (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

; These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)

; This installs ecb - it activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

(setq ecb-gzip-setup (quote cons))
(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
(setq ecb-source-path (quote ("c:/rails")))

; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

July 3, 2007 at 4:56 pm 6 comments

Emacs Ruby Foo

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In my last post I described how I configured emacs to have a Textmate style code browser pane. Since then I have been doing some Rails hacking and have used it a lot and found it works well. In this post I am going to describe some Ruby specific customisations.

Let there be Syntax Highlighting

For most languages, Emacs will syntax highlight your source code right out of the box. It does this by switching into a language specific mode when you open files with certain extensions. Ruby Mode is not present any of my Emacs installs out of the box, but adding support is trivial.

ruby-mode.el

In the Ruby source distribution, under the misc directory, you will find several files. The one called ruby-mode.el is the one that adds Ruby syntax highlighting support to Emacs.

Download it, place it somewhere in your Emacs load path. On windows I created a directory called C:\emacs-22.1\includes and copied ruby-mode.el into it.

(Edit – While you’re there, download inf-ruby.el and put it in the same directory – its needed later when setting up Rails mode)

First ensure the ‘includes directory’ is in the emacs load path by defining it in your _emacs file (at or near the top as it needs to be declared before you use anything in it):


; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

Now add the following lines to your _emacs:


; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

Now when you open a file with an rb extension, ruby-mode will automatically activate. Also, if you open any rhtml files, html-mode will activate which is useful in Rails apps.

Adding Electric

Before saying anything more about ruby-mode, we may as well install ruby-electric mode. I am still in two minds if I like this feature or not, but I am living with it for now. Basically electric-mode defines ‘electric keys’. For example, if you type a quote, the closing quote will automatically be inserted. Same for {, [, and (. Also typing ‘def ‘ will result in the corresponding end being inserted too. Its easier if you play with it a little to see what I mean!

To install electric-mode, download the ruby-electric.el file from the Ruby misc directory into your include directory, and add the following to your _emacs:


(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
            (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

Now, we have Ruby Syntax highlighting, electric keys, and auto-indenting, but we can do more …

Messed up you indentation?

If you let you code get into a bit of a twist, you can fix the indenting by press the TAB key while on the line – give it a try, moving over each line and pressing TAB on each to correct the indents!

Comment a block

If you want to comment out a large section of code, you can select it and the type ALT-x ruby-encomment-region and it will place a ‘#’ symbol at the beginning of each line. ALT-x ruby-decomment-region does the opposite.

Compile and Run Code in Emacs

There are many ways to run code in Emacs. The easiest out-of-the-box way, is to type ALT-x compile. Emacs will prompt for a compile command, which by default is ‘make -k’. Replace this with:

ruby -w my_file_name.rb

The emacs window will split in two and the results of running the script will be in the new window.

If you are writing a text document that includes code snippets, and you want to ensure they are correct, you can select the region and type ALT-| to run it. Again, you need to enter the ruby command to run the code.

Smarter Compile?

All that typing ruby -w filename will start to annoy you after a while – the solution is mode-compile which has some brains built in. It can tell when you are editing a Ruby file (or many other types of file) and run it with the correct compiler/interpreter automatically. Download mode-compile.el and put it in your includes directory. As usual, add the following to your _emacs:


; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
   "Command to compile current buffer file based on the major mode" t)
(global-set-key "C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
 "Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "C-ck" 'mode-compile-kill)

Now you can compile/run code by typing CTRL-c c (and if your code enters a nasty infinite loop, you can kill it with CTRL-c k). The output will again appear in a new split window. If there are any compile errors in the output, you can move the cursor over them and hit return and emacs will jump to the offending line in your source file – pretty neat eh?

That is enough for this post – next time I will add Rails snippets and possibly irb support into our Emacs install.

June 21, 2007 at 6:16 pm 11 comments

The Emacs Code Browser

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

This is the third post in a series where I have been talking about why I decided to learn emacs and getting started with it on Windows or OS X. In this post, I will explain some of the customisations I made to fulfil my Rails development wishlist.

File Browsing

Textmate boasts an impressive feature, which is a file browsing pane, giving you easy access to all the files in your project. Luckily Emacs has something similar, called the Emacs Code Browser (ECB), but you have to figure out how to install it, which is not difficult, but it takes some time.

Downloads

Unfortunately, ECB relies on the presence of three other Emacs plugins so the first thing is to gather up all the files you need.

  • Semantic is used to help build text parsing applications in Emacs. Don’t worry about it beyond installing. I grabbed semantic-1.4.4 from here.
  • Eieio is also required. Again, I am not sure what its for beyond being required. I got eieio-0.17 here.
  • Speedbar is a code browser, that is also used by ECB. I downloaded speedbar-0.14beta4 here.
  • Finally, get ECB itself

Installing

Now we have the 4 downloads, we need to put them somewhere. On Windows, I created a directory called plugins inside my emacs directory, giving me C:\emacs-22.1\plugins

On the Mac, I created a directory called .emacs_includes/plugins in my home directory.

I am pretty sure Emacs gurus would tell me these files should go somewhere else, but this works for me and the location is not really important. Extract each of the 4 downloads into the plugins directory.

Installing Emacs plugins generally involves putting some files in a known location, and telling Emacs about them using the .emacs file. Open your .emacs file (or _emacs see the last post for more information) and add the following (this is a Windows example, edit the paths for OS X):

 
;Allows syntax highlighting to work, among other things
(global-font-lock-mode 1)
;These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)
; This installs ecb - it is activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

Loading ECB

Restart Emacs and start ECB by typing ALT-x ecb-activate

The Emacs window will change, adding a new section with 4 windows on the left side. In this default mode, the top window shows directories, next files, then history and finally methods in the current file.

To close ECB enter ALT-x ecb-deactivate

I changed this default layout to show only a combined directory and file listing and opened file history by adding the following lines to my .emacs:


(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))

The final thing you need to do, is add a list of directories containing the files you wish to browse, for example:


(setq ecb-source-path (quote ("d:/myRailsProject" "d:/useful scripts")))

There is a lot to configure in ECB if you wish. All the details are available in the packaged manual, which you can view by typing ALT-x ecb-show-help.

Some commands I have learned so far include:

  • Jump to the directory window CTRL-. gd
  • Jump to the history window CTRL-. gh
  • Jump to the last window you were in CTRL-. gl
  • Jump to the first editor window CTRL-. g1

The directory browser can be controlled without using the mouse too – just use the arrow keys and enter – give it a go!

As you can see, setting up ECB is not difficult, and its well worth it in my opinion. In the next article, I will add some Ruby customisations to our Emacs installation.

June 13, 2007 at 4:33 pm 15 comments

Getting started

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In the last post in this series, I talked about why I decided to learn Emacs. In this post, I am going to explain how to get Emacs up and running and go over some essential stuff.

Installing

If you are using a Mac, I recommend getting Aquamacs Emacs. Its basically Emacs, but with a nice Mac look and feel. Installing is simple, just download the universal binary, open and drag the Aquamac Emacs icon into the applications folder.

On windows its almost as simple. Download the zip file (I grabbed emacs-22.1 from here), and extract it into any folder on your machine, I choose C:\, which placed Emacs in C:\emacs-22.1

.emacs?

Emacs is highly customisable, and all the settings relevant to your setup are stored in a file called .emacs or _emacs. Emacs searches for this file in your home directory, which is fine on OS X and Linux, but what about windows where you don’t really have a home directory?

The trick is to create an environment variable called HOME that contains the location of a directory you wish to use. A sensible place to store your _emacs is in the Application Data folder, normally located at C:\documents and settings\*username*\Application Data

To create the environment variable:

  1. Right Click on My Computer and select properties
  2. Click on the Advanced tab
  3. Click the environment variable button at the bottom
  4. Click the new button under the User variables for username pane
  5. Enter HOME as the variable name, and the location of the directory that will contain your _emacs file as the value

Playtime

Now its time to fire up emacs. On the Mac, run the Aquamac Emacs application you have just installed, on Windows double click on C:\emacs-22.1\bin\emacs.exe

The first thing you will notice is that Emacs behaves just like most other text editors – you can type stuff, open, close and save files using the menus etc. However, to start on the road to becoming a power user, you have to start learning the keyboard commands of which there are many!

The best way to learn is to work your way through the built-in tutorial. While reading it, you will quickly learn that most Emacs commands are accessed by holding the CRTL or ALT key and issuing some number of key presses. To open the tutorial, press and hold CTRL and type ‘h’. Release all the keys and type ‘t’ – the tutorial will then open, and it can teach you much more than I can!

In my next post, I will describe how I customised Emacs to help with Ruby and Rails development.

June 13, 2007 at 12:02 pm Leave a comment

Choosing an Editor

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

Over the last few months, I have started playing with Ruby and of course Rails, and I quickly became frustrated with the number files I had to keep opening and closing to manage all my models, views and controllers to name just a few. Until recently, I used any editor that was close to hand – Vim or Emacs in a terminal, Textpad on Windows and Textedit or emacs on the mac – needless to say I could not call myself a power user in any of them.

As many bloggers have said in the past, its worth learning one editor well, which is easy to say until another holy editor war breaks out leaving vim and emacs in opposite corners hurling insults at one another.

First of all, I decided to try out Textmate on my macbook. Its certainly a nice product, and I loved the code browsing pane for developing my Rails apps. I played with it for an evening, but always had that nagging feeling that the lack of portability would bite me sooner rather than later. I have no doubt that Textmate is a fantastic editor, but if I am going to invest my time in learning an editor really well, I need to be able to use it on Windows, in a terminal window and on a Mac. For now Textmate is confined to the Mac, so I ruled it out.

The emacs zealots create a lot of fuss about how great it is and to be honest, I like the way a total newbie can fire it up and actually get something typed immediately, unlike with Vim. I set about my mission, which was to see if emacs would satisfy my wish-list:

  • Provide a code browser pane like in Textmate
  • Ruby (and other languages) syntax highlighting
  • Automatic code indenting and automatic closing braces, quotes, if statements would be nice
  • Compile/Run code inside the editor
  • Do all of this on Windows, OS X and inside a decent terminal

Other things that would be nice nice to have:

  • Rails code snippets like in Textmate
  • Spell check as you type
  • Customisable color schemes to suit tired eyes!

Needless to say, emacs did not disappoint. After quite a bit reading and fiddling, I got it doing everything I wanted, moving me a small way up that power user ladder. In the next few articles, I will describe exactly what I did to get things working.

June 12, 2007 at 4:22 pm 5 comments


Feeds