Backup of .emacs



(global-set-key "%" 'match-paren)
(defun match-paren (arg)
   "Go to the matching paren if on a paren; otherwise insert %."
     (interactive "p")
     (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
         ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
         (t (self-insert-command (or arg 1)))))

(defun my-linux-c-mode ()
   "C mode with adjusted defaults for use with the linux kernel."
   (c-set-style "linux"))

(add-hook 'c-mode-hook 'my-linux-c-mode)

(server-start)

(custom-set-variables
   ;; custom-set-variables was added by Custom.
   ;; If you edit it by hand, you could mess it up, so be careful.
   ;; Your init file should contain only one such instance.
   ;; If there is more than one, they won't work right.
   '(show-paren-mode t)
   '(transient-mark-mode t))
(custom-set-faces
   ;; custom-set-faces was added by Custom.
   ;; If you edit it by hand, you could mess it up, so be careful.
   ;; Your init file should contain only one such instance.
   ;; If there is more than one, they won't work right.
)

Update: 05/23/2013

Over the last six years I have made many more changes to the .emacs file. Latest update for backup!

;; ================
;; General settings
;; ================

;; Display the time
(setq display-time-day-and-date t
      display-time-24hr-format t)
(display-time)

;; Inhibit startup splash screen
(setq inhibit-startup-message t)

;; enable row and column numner mode
(setq line-number-mode t)
(setq column-number-mode t)

;; Disable automatic backups
(setq backup-inhibited t)

;; show trailing whitespace
(setq-default show-trailing-whitespace t)

;; enable global clipboard to copy, paste into emacs
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)

;; Disable tool-bar-mode and other custom changes!
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(comint-scroll-show-maximum-output nil)
 '(ediff-merge-split-window-function (quote split-window-vertically))
 '(ediff-split-window-function (quote split-window-vertically))
 '(tool-bar-mode nil)
 '(delete-selection-mode t))

;; Start the server for emacsclient -n to work
(server-start)

(set-background-color "black")
(set-foreground-color "DarkOliveGreen3")
(set-cursor-color "green yellow")

;; full screen mode using [F11]
(defun toggle-fullscreen (&optional f)
  (interactive)
  (let ((current-value (frame-parameter nil 'fullscreen)))
    (set-frame-parameter nil 'fullscreen
             (if (equal 'fullboth current-value)
                 (if (boundp 'old-fullscreen) old-fullscreen nil)
               (progn (setq old-fullscreen current-value)
                  'fullboth)))))
(global-set-key [f11] 'toggle-fullscreen)


;;
;; Map M-_ to delete trailing whitespace
;;
(define-key global-map (kbd "M-_") 'delete-trailing-whitespace)


;; Make new frames fullscreen by default. Note: this hook doesn't do
;; anything to the initial frame if it's in your .emacs, since that file is
;; read _after_ the initial frame is created.
;; NOTE: This is screwing up the emacsclient connections for new frames!

;; (add-hook 'after-make-frame-functions 'toggle-fullscreen)

;; Set c-x p as previous-multiframe-window
(global-set-key (kbd "C-x p") 'previous-multiframe-window)

;; Set a 10pt font
(set-face-attribute 'default nil :height 100)

;; Disable upcase-region
(put 'upcase-region 'disabled nil)

;; Kill process without query!
(add-hook 'comint-exec-hook
      (lambda ()
        (process-kill-without-query (get-buffer-process (current-buffer)))))

;; ====================
;; Programming settings
;; ====================




;; highlight text beyond 80 characters!  Love this one!!
(setq whitespace-style '(trailing lines-tail space-before-tab
                                  indentation space-after-tab)
      whitespace-line-column 80)
(global-whitespace-mode 1)


;; Paren mode
(show-paren-mode 1)

;; Use vim style parenthesis matching
(global-set-key "%" 'match-paren)
(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
    ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
    (t (self-insert-command (or arg 1)))))

;; Insert spaces instead of tabs
(setq-default indent-tabs-mode nil)

;; Use TABs on Makefile mode
(defun my-makefile-mode-hook()
  (setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-makefile-mode-hook)

;; Custom C\C++ style at work!
(c-add-style "my-style"
             '("bsd"
               (c-basic-offset . 2)
               (c-offsets-alist . (
                                   (topmost-intro . 0)
                                   (substatement . +)
                                   (substatement-open . 0)
                                   (case-label . +)
                                   (access-label . -)
                                   (inclass . +)
                                   (inline-open . 0)))))


;; Use my-style for C and C++ modes. Also enable hungry state to
;; eat out any trailing whitespace
(defun my-c++-mode-hook ()
  (c-set-style "my-style")
  (auto-fill mode)
  (c-toggle-hungry-state 1))

(defun my-c-mode-hook ()
  (c-set-style "my-style")

  (auto-fill mode)
  (c-toggle-hungry-state 1))


(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-hook   'my-c-mode-hook)

;; set gtags-mode by default for C and C++ modes
(add-hook 'c-mode-hook
          '(lambda ()
             (gtags-mode t)
             ))

(add-hook 'c++-mode-hook
          '(lambda ()
             (gtags-mode t)
             ))

;; Use C++-mode to edit *.h files (not C-mode)
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

;; Use my-style indentation for MDL files
(setq auto-mode-alist (cons '("\\.mdl\\'" . c-mode) auto-mode-alist))

[Emacs] Fun with rectangles

Lets have some fun with emacs rectangles. Lets assume you have the following text:

012345678901234567890123456789
      val1 = 10;
      val2 = 20;
      val3 = 30;

Now, you need to add "(int)" in front of all the numbers. You can do it one line at a time, or you could use rectangles in emacs. Move the cursor to the start of 10, set mark, make the selection until start of 30. Hint: To set the mark, just hit Ctrl+Space and take your cursor to where you need to stop. Something like this,

012345678901234567890123456789
      val1 = 10;
      val2 = 20;
      val3 = 30;

Ok, now lets insert the string using "string-insert-rectangle" and enter the string that we want to be inserted, in our case "(int)"

M-x string-insert-rectangle
String insert rectangle (default void ): (int)

012345678901234567890123456789
      val1 = (int)10;
      val2 = (int)20;
      val3 = (int)30;

Now, say you wanted to give a space between the (int) and the number. Mark the rectangle as show below:

012345678901234567890123456789
      val1 = (int)10;
      val2 = (int)20;
      val3 = (int)30;

We will use the "open-rectangle" command now.

M-x open-rectangle

012345678901234567890123456789
      val1 = (int) 10;
      val2 = (int) 20;
      val3 = (int) 30;

Cool, now let's delete the spaces in the front using "delete-rectangle". Make the rectangle and...

012345678901234567890123456789
      val1 = (int) 10;
      val2 = (int) 20;
      val3 = (int) 30;

M-x delete-rectangle

012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;

Now, just for fun, we are going to cut a rectangle and yank it somewhere else. Lets remove 1,2,3 from the variable names and put them in the front (bad idea, I know). Lets select the rectangle and call the "kill-rectangle" command

012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;

M-x kill-rectangle

 012345678901234567890123456789
|val = (int) 10;
 val = (int) 20;
 val = (int) 30;

Move the cursor (show above as | ) to where you want to yank the rectangle now (in our case, the beginning of the word) and run "yank-rectangle"

M-x yank-rectangle

012345678901234567890123456789
1val = (int) 10;
2val = (int) 20;
3val = (int) 30;

Now, of course there are key-binding available (its Emacs for heaven's sake, even if there is no key-binding you can make your own).

From the Emacs manual
C-x r k
Kill the text of the region-rectangle, saving its contents as the “last killed rectangle” (kill-rectangle).
C-x r d
Delete the text of the region-rectangle (delete-rectangle).
C-x r y
Yank the last killed rectangle with its upper left corner at point (yank-rectangle).
C-x r o
Insert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle to the right.
C-x r N
Insert line numbers along the left edge of the region-rectangle (rectangle-number-lines). This pushes the previous contents of the region-rectangle to the right.
C-x r c
Clear the region-rectangle by replacing all of its contents with spaces (clear-rectangle).
M-x delete-whitespace-rectangle
Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
C-x r t string
Replace rectangle contents with string on each line (string-rectangle).
M-x string-insert-rectangle string
Insert string on each line of the rectangle.
Enjoy!

Using Shift+Arrow keys in Emacs from GNU screen

While inside screen, when using the Shift+Left, Shift-Right, etc keys in Emacs (1) to mark and select, if you see the 2A 2B 2C and 2D characters instead you are probably still using the default TERM which is set to "screen".

Simple solution, use xterm-vt220 or xterm-color instead. You can either create an alias for emacs or change your .screenrc file.

$ alias em='export TERM=xterm-vt220 && emacs -nw'

OR in your ~/.screenrc add,

term xterm-vt220

----------------------------------------------------------------
(1) You need to use Emacs 23 or above.

[Bash] Sending stdout and stderr to single file

Remember: To send both the stdout and stderr messages from the console to a file do:

$ app > log.file 2>&1

If you want to send everything to a black-hole just replace log.file with /dev/null.

$ app > /dev/null 2>&1

If ever confused about the syntax, do man bash and search for REDIRECTION for more gyan

Update:
It is good idea to use tee, which lets you see the output as well as write it to a file.

$ app 2>&1 | tee file.log

Using [Flex] on OS X

If you are trying to use flex and you get the following error:

Undefined symbols for architecture x86_64:
  "_yywrap", referenced from:
      _yylex in cceJ8DuJ.o
      _input in cceJ8DuJ.o
ld: symbol(s) not found for architecture x86_64

You need to link with libfl.a. Apparently, OSX does not have libfl.a. Use libl.a instead.

$ gcc lex.yy.c -o flexout -ll

Or create a symlink

$ cd /usr/lib
$ ln -s libl.a libfl.a

Sample [Makefile]

Every time anyone asks me how to write a Makefile I need to write a sample Makefile from scratch for them. So, writing one here so that I can just give this link and explain.

# Sample Makefile
CC:= g++
LD:= g++

ROOT_DIR:= ${PWD}
SRC_DIR:= ${ROOT_DIR}/src
TARGET_DIR:= ${ROOT_DIR}/bin
INCLUDES_DIR:= ${ROOT_DIR}/include

SRC_FILES:= ${SRC_DIR}/test.cpp\
            ${SRC_DIR}/foo.cpp

INCLUDES:= -I. -I${INCLUDES_DIR}
OBJ_FILES:= $(patsubst %.cpp,%.o,$(SRC_FILES)

CFLAGS:= -c -Wall ${INCLUDES}
LDFLAGS:=

ifeq (${BUILD}, debug)
CFLAGS += -g
endif

TARGET:= ${TARGET_DIR}/testapp
all: ${TARGET}

${TARGET}: ${OBJ_FILES}
${LD} ${LDFLAGS} $^ -o $@

%.o: %.cpp
${CC} ${CFLAGS} $^ -o $@

clean:
rm -f ${TARGET} ${OBJ_FILES}

.PHONY: clean

[Makefile] Create object list automatically

From the list of source files you can automatically create the list of object files by  either using patsubst or the following handy trick

SRC_FILES:= test.cpp foo.cpp bar.cpp
OBJ_FILES:= $(SRC_FILES:%.cpp=%.o)

Using patsubst the same thing can be achieved.

OBJ_FILES:=$(patsubst %.cpp,%.o,$(SRC_FILES))



[Emacs] Adding gtags support to emacs on OSX

OS X does not have global (gtags) by default. I am a big fan of gtags on emacs and I can't function without it. Homebrew has a Mac port for global which is very easy to install.

brew install global

Once installed, you would need to find out the path to the gtags.el file for emacs. On my Mac it was installed into /usr/local/share/gtags.

Finally,  update the .emacs file with the following:

;; Enable gtags
(setq load-path (cons "/usr/local/share/gtags" load-path))
(autoload 'gtags-mode "gtags" "" t)

Voila, gtags is now working. Now I need to figure out how to enable it for Aquaemacs.

Update: No worries, Aquaemacs is able to read ~/.emacs file as well. The only thing that does not seem to work with Aquaemacs is emacsclient. Well, that is a different fire.

[Emacs] Delete marked selections and remove toolbar

In Emacs you can't delete a marked buffer directly using the [DEL] button on the keyboard. In order to do that enable the M-x delete-selection-mode

Also, if you don't like the emacs toolbar get rid of it using M-x tool-bar-mode

Set the following in your ~/.emacs file

(custom-set-variables
  '(tool-bar-mode nil)
  '(delete-selection-mode t))



[Emacs] Comment region shortcut

C-c C-c Comment a region

M-; Very handy. Comment and un-comment region

Esc-; Same as above