.emacs

Posted on February 2, 2015 by Sanchayan Maity
Tags: emacs

I started using Emacs a while back and can’t believe how I worked without it in gedit. One can customise Emacs anyway one likes. Here is my .emacs file which enables gtags mode for C files, indentation for C source files, named sessions saving/naming, automatic brace pairings, maximise on opening emacs and changing frames with shift and arrow keys. Do note that nothing in the below .emacs file is my work, it has all been collected from different places on the internet.

Most helpful has been:

http://scottfrazersblog.blogspot.in/2009/12/emacs-named-desktop-sessions.html

(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.
'(ansi-color-names-vector
["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#e090d7" "#8cc4ff" "#eeeeec"])
'(custom-enabled-themes (quote (deeper-blue)))
'(inhibit-startup-screen 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.
)

(when (>= emacs-major-version 24)
  (require 'package)
  (package-initialize)
  (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
  )

(add-hook 'c-mode-hook 'ggtags-mode)

(desktop-save-mode 1)

(require 'cl)

(setq-default c-basic-offset 4 c-default-style "linux")
(setq-default tab-width 4 indent-tabs-mode t)

(require 'autopair)
(autopair-global-mode)

(custom-set-variables
 '(initial-frame-alist (quote ((fullscreen . maximized)))))

(global-auto-revert-mode t)

(windmove-default-keybindings)

(setq windmove-wrap-around t)

(add-to-list 'load-path "~/.emacs.d/lisp/")

(require 'sr-speedbar)

(defun c-lineup-arglist-tabs-only (ignored)
 "Line up argument lists by tabs, not spaces"
 (let* ((anchor (c-langelem-pos c-syntactic-element))
   (column (c-langelem-2nd-pos c-syntactic-element))
   (offset (- (1+ column) anchor))
   (steps (floor offset c-basic-offset)))
  (* (max steps 1)
     c-basic-offset)))

(add-hook 'c-mode-common-hook
          (lambda ()
          ;; Add kernel style
          (c-add-style
           "linux-tabs-only"
           '("linux" (c-offsets-alist
                      (arglist-cont-nonempty
                       c-lineup-gcc-asm-reg
                       c-lineup-arglist-tabs-only))))))

(add-hook 'c-mode-hook
          (lambda ()
            (let ((filename (buffer-file-name)))
            ;; Enable kernel mode for the appropriate files
            (when (and filename
                       (string-match (expand-file-name "~/src/linux-trees")
                                      filename))
             (setq indent-tabs-mode t)
             (c-set-style "linux-tabs-only")))))

(require 'desktop)

(defvar my-desktop-session-dir
 (concat (getenv "HOME") "/.emacs.d/desktop-sessions/")
 "*Directory to save desktop sessions in")

(defvar my-desktop-session-name-hist nil
 "Desktop session name history")

(defun my-desktop-save (&optional name)
 "Save desktop by name."
 (interactive)
 (unless name
   (setq name (my-desktop-get-session-name "Save session" t)))
 (when name
   (make-directory (concat my-desktop-session-dir name) t)
   (desktop-save (concat my-desktop-session-dir name) t)))

(defun my-desktop-save-and-clear ()
  "Save and clear desktop."
  (interactive)
  (call-interactively 'my-desktop-save)
  (desktop-clear)
  (setq desktop-dirname nil))

(defun my-desktop-read (&optional name)
  "Read desktop by name."
  (interactive)
  (unless name
    (setq name (my-desktop-get-session-name "Load session")))
  (when name
    (desktop-clear)
    (desktop-read (concat my-desktop-session-dir name))))

(defun my-desktop-change (&optional name)
  "Change desktops by name."
  (interactive)
  (let ((name (my-desktop-get-current-name)))
    (when name
      (my-desktop-save name))
    (call-interactively 'my-desktop-read)))

(defun my-desktop-name ()
  "Return the current desktop name."
  (interactive)
  (let ((name (my-desktop-get-current-name)))
    (if name
        (message (concat "Desktop name: " name))
      (message "No named desktop loaded"))))

(defun my-desktop-get-current-name ()
  "Get the current desktop name."
  (when desktop-dirname
    (let ((dirname (substring desktop-dirname 0 -1)))
      (when (string= (file-name-directory dirname) my-desktop-session-dir)
        (file-name-nondirectory dirname)))))

(defun my-desktop-get-session-name (prompt &optional use-default)
  "Get a session name."
  (let* ((default (and use-default (my-desktop-get-current-name)))
         (full-prompt (concat prompt (if default
                                         (concat " (default " default "): ")
                                       ": "))))
  (completing-read full-prompt (and (file-exists-p my-desktop-session-dir)
                                    (directory-files my-desktop-session-dir))
                   nil nil nil my-desktop-session-name-hist default)))

(defun my-desktop-kill-emacs-hook ()
  "Save desktop before killing emacs."
  (when (file-exists-p (concat my-desktop-session-dir "last-session"))
    (setq desktop-file-modtime
          (nth 5 (file-attributes (desktop-full-file-name (concat my-desktop-session-dir "last-session"))))))
  (my-desktop-save "last-session"))

(add-hook 'kill-emacs-hook 'my-desktop-kill-emacs-hook)