Saturday, November 7, 2009

HOWTO: dos2unix in emacs

Overview
I switch between Linux, OSX, and Windows quiet a bit. One problem with that can be the dreaded EOL issue.

For example, if you add #!/usr/bin/env python and have a DOS/Windows formatted file, running it in UNIX right from a shell may not work.

For my development, I prefer to standardize on UNIX format. For the languages I use, I know it will not cause any issues regardless of where they are run.

Requirements
If you are using linux, OSX, or Solaris you will edit your ~/.emacs file. If you are running Windows, you will be editing your %HOME%/_emacs

For this howto, emacs-22 is being used under Ubuntu Linux 9.10. I doubt the OS or version of emacs will make a difference.

Force Unix EOL
To avoid having to deal with EOL on your own code, add the following to your .emacs file.
; Ensure we stay in UNIX mode for EOL char
(setq inhibit-eol-conversion t)
Now emacs will only use UNIX EOL Plus, you will see ^M in your file for each line with a DOS/Windows EOL.

dos2unix Code
I found this code and have been using it. Why re-invent the wheel.
;;; Convert DOS file format to Unix
;; look at
;; M-x comint-strip-ctrl-m
;;   Command: Strip trailing `^M' characters from the current output group.
;; from: elf@ee.ryerson.ca (Luis Fernandes)
;; 22 May 1997
;;; Usage: M-x dos2unix
;;;
(defun dos2unix ()
"Convert this entire buffer from MS-DOS text file format to UNIX."
(interactive)
(save-excursion
 (goto-char (point-min))
 (replace-regexp "\r$" "" nil)
 (goto-char (1- (point-max)))
 (if (looking-at "\C-z")
     (delete-char 1))))
To use, press ESC-x and type dos2unix After pressing return, you will see the magic happen. I suggest making a backup before doing any global changes to anything. All ^M will disappear.

Final Thoughts
Adding this lisp code will help you standardize on UNIX EOL. You will find the full .emacs file here.

HOWTO: setup emacs to run scripts within UI

Overview
Lately, I have gone back to my old friend... emacs. Some would agree that a better and more flexible editor has never been created. I used emacs for many years for C/C++, perl, HTML, and shell programming. Over time, projects and jobs drove me to full IDEs like Netbeans and Visual Studio.

I recently purchased a DELL Mini 10v netbook running (of course) Ubuntu. With the tiny (but beautiful and clear) screen, big IDEs are just not practical. Even with all panels minimized there is still only a tiny amount of space.

For my last trip, I needed to develop some code to hack a pile data I was emailed right before I got on the plane... stat! Thank ___ for having a plane friendly computer like a netbook.

I have become dependent on all the work heavy IDEs automatically do. Do I really know who to code or does the IDE guide me to a completed program? Plus, data hacking is better for scripting languages like python or perl. On taxi I thought it over and made my call. Welcome back to my tool belt emacs.

Once we hit cruising altitude, I booted up Ubuntu 9.04, fired up emacs and started coding. There was one problem. I have been so used to simply pressing F6 to run my program. I wanted to do the same thing in emacs. Why be limited?

The following tells you howto setup emacs, for this type of behavior, using just a small amount of lisp code. The only real dependency is having the languages you are using in the path. If python or perl doesn't run from a command prompt, this will not work very well.

Requirements
If you are using linux, OSX, or Solaris you will edit your ~/.emacs file. If you are running Windows, you will be editing your %HOME%/_emacs

For this howto, emacs-22 is being used under Ubuntu Linux 9.10. I doubt the OS or version of emacs will make a difference.

Launch Interpreter using F5
Simply open your .emacs or _emacs file and add the following
;; ============================================================================
;; Runs Scripting Languages Program using compile key bindings
;;
;; if you have another lanugage, simply create a ____-run defun and add an if
;; statement to script-run()
;; ============================================================================

(defun python-run ()
"Use compile to run python programs"
(interactive)
(message "Running python for buffer %s." (buffer-name))
(compile (concat "python " (buffer-name)))
)
(defun perl-run ()
"Use compile to run perl programs"
(interactive)
(message "Running perl for buffer %s." (buffer-name))
(compile (concat "perl -w " (buffer-name)))
)
(defun script-run ()
"Use to run a script"
(interactive)
(if (equal (file-name-extension(buffer-name)) "py")
(python-run))
(if (equal (file-name-extension(buffer-name)) "pl")
(perl-run))
)
(global-set-key [f5] 'script-run)

If the file is python (py) then python will be run in a separate buffer. If the file is perl (pl) then perl will be run in a separate buffer. To trigger this, all that is needed is to press F5 while in a buffer that is either a python or perl file. That is it.

Final Thoughts
This approach can easily be expanded for other languages like ruby and such. I hope this helps simplify programming using emacs. Find my full .emacs file here.

Personally, I am glad I dusted off emacs. Humm... what about VI.

POLL: To me, a netbook is.... (Results)

  • 20% say "to small for me"
  • 20% say "only good for travel"
  • 20% say "perfect if it runs Windows"
  • 40% say "perfect if it runs Ubuntu"

Monday, September 14, 2009