Skip to main content

coping emacs #1

After having written a lot of erlang code with emacs(uhhm ... no, erlide, the erlang eclipse plugin, is not yet a viable alternative...), I decided to start (back-)porting eclipse features to emacs. Yeahhhh! I am now among those 1337 |-|4><025 that have their own ".emacs" in ~.

Well one feature, that I can't live without, is moving the current line with Alt - up/down, this is really usefull together with anotherthe other feature: duplicating the current line(Ctrl-Alt-up/down).

Below is the elisp code that provides these features and binds them to the keys these are bound to in eclipse. Note: In contrast to the eclipse version of this feature, this implementation does not(yet) work with regions, but with lines only.

Ok, put this in your ~/.emacs

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Written by Sven Heyll in 2008
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun sh-wind-col (thunk)
(let ((col (- (point) (line-beginning-position))))
(apply thunk '())
(beginning-of-line)
(forward-char col)))

(defun sh-with-current-line (func)
(let ((cline (buffer-substring (line-beginning-position) (line-end-position))))
(apply func `(,cline))))

(defun sh-dup-line-down ()
(interactive)
(sh-wind-col
(lambda ()
(sh-with-current-line
(lambda (cline)
(end-of-line)
(insert ?\n)
(insert cline))))))

(defun sh-dup-line-up ()
(interactive)
(sh-wind-col
(lambda ()
(sh-with-current-line
(lambda (cline)
(beginning-of-line)
(insert cline)
(insert ?\n)
(forward-line -1))))))

(global-set-key [C-M-down] 'sh-dup-line-down)
(global-set-key [C-M-up] 'sh-dup-line-up)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun sh-move-line (dir)
(defun extract-a-line (dir)
(let ((line-start (line-beginning-position (+ 1 dir)))
(line-end (line-end-position (+ 1 dir))))
(setq res (delete-and-extract-region line-start line-end))
(delete-region line-start (+ 1 line-start))
res))
(if (not
(or
(and (< dir 0) (= (line-beginning-position) (point-min)))
(and (> dir 0) (= (line-end-position) (- (point-max) 1)))))
(let
((col (- (point) (line-beginning-position)))
(line (extract-a-line dir)))
(if (< dir 0)
(progn
(end-of-line)
(insert ?\n)
(insert line))
(progn
(beginning-of-line)
(insert line)
(insert ?\n)))
(beginning-of-line (truncate (+ 0.5 (* 0.5 dir))))
(forward-char col))))

(defun sh-line-down ()
(interactive)
(sh-move-line 1))

(defun sh-line-up ()
(interactive)
(sh-move-line -1))

(global-set-key [M-down] 'sh-line-down)
(global-set-key [M-up] 'sh-line-up)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Comments

Popular posts from this blog

Lazy Evaluation(there be dragons and basement cats)

Lazy Evaluation and "undefined" I am on the road to being a haskell programmer, and it still is a long way to go. Yesterday I had some nice guys from #haskell explain to me lazy evaluation. Take a look at this code: Prelude> let x = undefined in "hello world" "hello world" Prelude> Because of Haskells lazyness, x will not be evaluated because it is not used, hence undefined will not be evaluated and no exception will occur. The evaluation of "undefined" will result in a runtime exception: Prelude> undefined *** Exception: Prelude.undefined Prelude> Strictness Strictness means that the result of a function is undefined, if one of the arguments, the function is applied to, is undefined. Classical programming languages are strict. The following example in Java will demonstrate this. When the programm is run, it will throw a RuntimeException, although the variable "evilX" is never actually used, strictness requires that all argu

Learning Haskell, functional music

As you might have realized, I started to learn Haskell. One of the most fun things to do in any programming language is creating some kind of audible side effects with a program. Already back in the days when I started programming, I always played around with audio when toying around with a new language. I have found a wonderful set of lecture slides about haskell and multimedia programming, called school of expression. Inspired by the slides about functional music I implemented a little song. Ahh ... and yes it is intended to sound slightly strange . I used the synthesis toolkit to transform the music to real noise, simply by piping skini message to std-out. I used this command line to achieve the results audible in the table: sven@hhi1214a:~/Mukke$ ghc -o test1 test1.hs && ./test1 | stk-demo Plucked -n 16 -or -ip Sound samples: Plucked play Clarinet play Whistle(attention very crazy!) play As always the source... stueck = anfang :+: mitte :+: ende anfang = groovy :+: (Trans

Erlang mock - erlymock

NOTE THIS POST IS OUTDATED! The project has evolved and can be found here: ErlyMock Some features Easy to use Design based on easymock Works together with otp: can be used even if the clut is called from another process, by invoking mock:verify_after_last_call(Mock,optional: timeout) custom return functions predefined return functions for returning values, receiving message, throwing exceptions, etc.. erlymock automatically purges all modules that were mocked, after verify() Custom argument matchers: %% Orderchecking types: in_order, out_of_order, stub; %% Answering: {return, ...}|{error, ...}|{throw, ...}|{exit, ...}|{rec_msg, Pid}|{function, Fun(Args) -> RetVal} expect(Mock, Type, Module, Function, Arguments, Answer = {AT, _}) when AT==return;AT==error;AT==throw;AT==exit;AT==rec_msg;AT==function -> call(Mock, {expect, Type, Module, Function, length(Arguments), {Arguments, Answer}}). %% this version of expect is suited for useing custom argument matchers expect(Mock, Type,