Bang My Head

Pesonal diary about web development, specifically rails, and emacs

Saturday, 18 April 2009

emacs orgmode : my code to reverse overview headings in a region



;; NOTE
;; use with caution. this is one of my first elsip programs.
;; the only testing its had is that it worked on
;; reversing the top-level headings in my org files.

;; Any comments on improving my coding appreciated.

;; usage: highlight region
;; M-x org-reverse-overview-headings-region


(defun org-record-all-subtree-beg-end-in-buffer ()
(interactive)
(let ((no-of-lines-tried-to-move-passed-buffer 0)
(sections-to-move '() ))

;; fold subtrees
(org-overview)
(goto-char (point-min))

;; loop through lines in region, with headlines folded
;; until buffer end
(while (>= 0 no-of-lines-tried-to-move-passed-buffer)
(when (org-on-heading-p)

(let ((subtree-start)
(subtree-end)
(subtree-gap-start)
(subtree-gap-end))

(beginning-of-line)
(setq subtree-start (point))
;; move to end of subtree
(outline-end-of-subtree)
(setq subtree-end (point))

;; remember beg and end of subtree
(push (cons subtree-start subtree-end) sections-to-move)))

;; move forward a line, note if you can't due to end of buffer
(setq no-of-lines-tried-to-move-passed-buffer (forward-line 1)))

;; return list of subtree start and end points
sections-to-move))








(defun org-reverse-overview-headings-region (beg end)
(interactive "r")
(narrow-to-region beg end)
(let ((sections-to-move (org-record-all-subtree-beg-end-in-buffer))
(source-buffer (current-buffer))
(new-buffer-output ""))

;; with-temp-buffer - creates a temporary buffer to do stuff in.
(setq new-buffer-output
(with-temp-buffer

(while sections-to-move
(let* ((section (car sections-to-move))
(section-beg (car section))
(section-end (cdr section)))

;; insert text from another buffer, between location start-point
;; to end-point, into the current buffer at point
(insert-buffer-substring source-buffer section-beg section-end)
(insert "\n")
(setq sections-to-move (cdr sections-to-move))))
;; return the temp buffer contents as a string to original buffer
(buffer-string)))

;; delete old contents of buffer
(kill-region (point-min) (point-max))
;; insert new from temp buffer
(insert new-buffer-output)
(org-overview)
(widen)))




Friday, 10 April 2009

SOLVED ERROR no such file to load -- mysql ( rails, x64, linux, fedora, gem )

tail -f log/development.log
> ...
> Status: 500 Internal Server Error
> no such file to load -- mysql
> ...

# check if mysql gem is installed
gem list

# its not. try installing
gem install mysql
> ...
> probably lack of necessary libraries and/or headers.
> ...

# problem installing. have I got the latest libraries
sudo yum search mysql | grep libs
> mysql-libs.i386 : The shared libraries required for MySQL clients
> mysql-libs.x86_64 : The shared libraries required for MySQL clients
sudo yum install mysql-libs
> Package mysql-libs-5.0.77-1.fc10.x86_64 already installed and latest version
> Nothing to do

sudo yum install mysql-devel

# maybe its a x86_64 architecture problem
export ARCHFLAGS="-arch x86_64";
sudo gem install mysql -- --with-mysql=`which mysql_config`
> ...
> checking for mysql_query() in -lmysqlclient... no
> ...

locate mysqlclient
> ...
> /usr/lib64/mysql/libmysqlclient.so
> ...

sudo gem install mysql -- --with-mysql-lib=/usr/lib64/mysql
> Successfully installed mysql-2.7
> 1 gem installed

Sunday, 5 April 2009

Grepping history and removing duplicate entries from history (without sort)

Here's some code to simplify the use of history and history grep.

The bash code shows
- how use aliases to shorten command names to reduce typing.
- how to wrap multiple commands in a function.
- how to call different commands based on the parameters passed.
- remove duplicate entries from the history, while keeping the order of the entries



hg - saves shell history to file , so history can be accessed from other shells before this one closes
- filters history commands i.e. remove preceeding and trailing white space from entries, remove calls to history
- removes duplicates from history ( optional see comment in hg function )
- runs history if no parameters
- greps history using parameters if passed
- how to edit or remove lines in a file, $HISTFILE, using sed

hgd - like hg, but doesn't remove duplicates from history file, however output is displayed without duplicates


h-rm-dups - a function to remove duplicate entries from the history file without loosing the entries order
- also remvoes leading and trailing white space from commands
- removes entries that called history
- is an alias for the function write-history-rm-dups-n-history-n-space


NOTE: see environment variable $HISTFILE for history file location

I added the following code to a file called ~/.aliases

Then called that file from ~/.bashrc with the following command: source ~/.aliases




# display history
# grep history if passed grep regex
# write new historys to file
# remove leading and trailing white space from history entries
# reload history into shell
hg () {
# write-history-rm-dups-n-history-n-space
write-history-rm-history-n-space
if [ "$#" -eq 0 ]; then
history;
else
history | grep $*;
fi
}

# dispaly history with duplicates removed but don't change history file
hgd () {
awk ' !x[$0]++' $HISTFILE | rm-trailing-white-space | rm-preceding-white-space | rm-history-calls-from-pipe | grep $*
}

# remove duplicate entries from history file
# write new historys to file
# remove leading and trailing white space from history entries
# reload history into shell
write-history-rm-dups-n-history-n-space () {
history -a
awk ' !x[$0]++' $HISTFILE | rm-trailing-white-space | rm-preceding-white-space | rm-history-calls | cat > ~/uniq-history-cmds-x.txt
mv ~/uniq-history-cmds-x.txt $HISTFILE
history -c
history -r
}

alias h-rm-dups=write-history-rm-dups-n-history-n-space

# write new historys to file
# remove leading and trailing white space from history entries
# reload history into shell
write-history-rm-history-n-space () {
history -a
cat $HISTFILE | rm-trailing-white-space | rm-preceding-white-space | cat > ~/uniq-history-cmds-x.txt
mv ~/uniq-history-cmds-x.txt $HISTFILE
history -c
history -r
}



rm-trailing-white-space () {
sed 's/ *$//g'
}

rm-preceding-white-space () {
sed 's/^ *//g'
}



rm-history-calls () {
# history commands are h, hg, hgu, and history
# remove lines that start with these commands followed by a space
# remove a line with just the history command or just the hg command.
sed '/^h .*/d' | sed '/^hg .*/d' | sed '/^hgu .*/d'| sed '/^history .*/d' | sed '/^history$/d' | sed '/^hg$/d'
}





Thursday, 5 March 2009

Emacs Screencast - Emacs HOME environment variable in windows

quicktime screencast: Emacs HOME environment variable

A rough screencast that:
  • Demonstrates how to set the HOME environment variable in windows
  • Shows how to get and set environment variables
  • Shows how to run an emacs function inside and emacs buffer
  • Shows how to change buffers
  • Shows the scratch buffer - a good place to test emacs scripting
NOTE: In the screencast, when I talk about some keyboard commands I forget to mention that the control is pressed a second time. This is because when I was doing it I never released it the first time. I realise this will cause confusion, I don't have time to redo it. However I've added the correct control sequence in the subtitles.

Wednesday, 4 March 2009

Emacs Screencast - Install emacs on windows using an installer.

This screencast shows where to grab an Emacs windows binary and how easy it can be to install it. Not very exciting content, but I thought I'd start at the beginining..... It does not show you how to use or configure emacs.

quicktime screencast: install emacs on windows using an installer

This is my first screencast. My voice is crackly and it was made without any preparation

I suggest you only watch it if you struggle using the links below, or want to check out how easy it is before doing it yourself.

Official Emacs Homepage
http://www.gnu.org/software/emacs/

Location for pre-compiled versions of Emacs
http://ftp.gnu.org/gnu/emacs/windows/

I use a patched version of Emacs called emacsW32.
The patch helps Emacs integrate smoother with windows.
The binary also includes the latest version of emacs.
http://ourcomments.org/Emacs/EmacsW32.html#unpatched


Today I learnt how much effort went into a good screencast.

Saturday, 28 February 2009

emacs rinari-console on windows

Has anyone managed to get rinari-console to run in emacs on a windows system.

I have managed to get rinari-console function to work by changing it.

To view this function in the source code

M-x find-function rinari-console

In rinari-console I replaced the line

(run-ruby command)

with

(set-buffer (apply 'make-comint "ruby" "irb" script '("--inf-ruby-mode" "-r" "irb/completion")))
(inf-ruby-mode)
(pop-to-buffer (setq inf-ruby-buffer (format "*%s*" "ruby")))


In the make-comint function
  • "ruby" - an arbitrary process name, also used to generate name of buffer ( *ruby*)
  • "irb" - is a bat file in my InstantRails installation, c:/rails/ruby/bin/irb.bat
  • script - is the location of my current projects ruby script/console, this is generated earlier in this function.
  • '("--inf-ruby-mode" "-r" "irb/completion") - options passed to irb script.
After making these changes, I could then call M-x rinari-console ( keybinding C-c ; c ) and it would open a buffer with irb loaded with the current projects environment.

NOTE: you might have to tell emacs where your ruby, rails, gem executables are.

; Add to .emacs
(setq exec-path (cons "C:/rails/ruby/bin" exec-path))

NOTE: if using InstantRails see FAQ: Q Can I use Instant Rails for all my Ruby needs?

Why it does not work

I think it fails due to windows ignoring the 'shebang'. The original code tries to call the script/console file directly. This file does not have an extension and therefore windows is unaware that it is an executable file, and what program to execute it in.

Calling irb.bat works because the file has a .bat extension which is mapped as an executable. You could also run a .rb or .rbw file, as long as you let windows know its an execuatble. You do this by adding your scripts extensions (i.e. .rb and .rbw ) to your system environment variable PATHTEXT. Then tell windows which program will execute files with this extension ( Explorer -> Folder Options -> File Types -> .rb -> ruby )