3
\$\begingroup\$

The code below is the .emacs file, which is the initial configuration file for Emacs. I use Emacs version 24.5.1 on a computer running Ubuntu 14.04 as OS. I primarily use C/C++ for coding and LaTeX for writing documents. I love to use Emacs but I feel I could do much more with it and in a refined way.

;;(server-start)

(setq shell-file-name "bash")
(defalias 'yes-or-no-p 'y-or-n-p)
(setq x-select-enable-clipboard t)
(add-hook 'c-mode-hook #'electric-spacing-mode)
(add-hook 'c++-mode-hook #'electric-spacing-mode)

(add-to-list 'load-path "~/.emacs.d/elpa/highlight-current-line-20051013.1056/")
 (require 'highlight-current-line)
 (add-to-list 'load-path "/usr/share/emacs24/site-lisp/git/")
  (require 'git)
  (require 'git-blame)
(require 'linum)


(global-linum-mode 1)
(setq linum-format "%4d\u2502")
(defun linum-format-func (line)
  (let ((w (length (number-to-string (count-lines (point-min) (point-max))))))
     (propertize (format (format "%%%dd " w) line) 'face 'linum)))
(setq linum-format 'linum-format-func)


(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.
 '(ac-ispell-fuzzy-limit 4)
 '(ac-ispell-requires 4)
 '(ansi-color-names-vector
   ["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#e090d7" "#8cc4ff" "#eeeeec"])
 '(custom-safe-themes
   (quote
    ("1db14a77e979d2306b2714b0bd38e6f239209c0ac9542a434c533d1daa48ad9a" default)))
 '(ecb-layout-window-sizes nil)
 '(ecb-options-version "2.40")
 '(ecb-source-path (quote (("/" "/"))))
 '(highlight-current-line-globally t nil (highlight-current-line))
 '(inhibit-startup-screen t)
 '(initial-frame-alist (quote ((fullscreen . maximized)))))
(setq ecb-tip-of-the-day nil)

(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.
 '(highlight-current-line-face ((t (:background "gray4")))))

;; Ispell default dictionary
(setq ispell-dictionary "british")

(add-to-list 'load-path "~")
(autoload 'cuda-mode "cuda-mode.el")
(add-to-list 'auto-mode-alist '("\\.cu\\'" . cuda-mode))


(setq backup-directory-alist
          `((".*" . ,temporary-file-directory)))
    (setq auto-save-file-name-transforms
          `((".*" ,temporary-file-directory t)))

(message "Deleting old backup files...")
(let ((week (* 60 60 24 7))
      (current (float-time (current-time))))
  (dolist (file (directory-files temporary-file-directory t))
    (when (and (backup-file-name-p file)
               (> (- current (float-time (nth 5 (file-attributes file))))
                  week))
      (message "%s" file)
      (delete-file file))))
 (autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
 (add-to-list
  'auto-mode-alist
  '("\\.m$" . matlab-mode))
 (setq matlab-indent-function t)
 (setq matlab-shell-command "matlab")
;;*** 13.1 The (info "(emacs)Kill Ring")

;; auto-indent pasted code
(defadvice yank (after indent-region activate)
  (if (member major-mode
              '(emacs-lisp-mode scheme-mode lisp-mode c-mode c++-mode
                objc-mode latex-mode plain-tex-mode python-mode))
      (indent-region (region-beginning) (region-end) nil)))

(defadvice yank-pop (after indent-region activate)
  (if (member major-mode
              '(emacs-lisp-mode scheme-mode lisp-mode c-mode c++-mode
                objc-mode latex-mode plain-tex-mode python-mode))
      (indent-region (region-beginning) (region-end) nil)))



;;; Emacs is not a package manager, and here we load its package manager!
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(dolist (source '(("marmalade" . "http://marmalade-repo.org/packages/")
                  ("elpa" . "http://tromey.com/elpa/")
                  ;; TODO: Maybe, use this after emacs24 is released
                  ;; (development versions of packages)
                  ("melpa" . "http://melpa.milkbox.net/packages/")
                  ))
  (add-to-list 'package-archives source t))
(package-initialize)



;;; Required packages
;;; everytime emacs starts, it will automatically check if those packages are
;;; missing, it will install them automatically
(when (not package-archive-contents)
  (package-refresh-contents))
(defvar tmtxt/packages
  '(ggtags yasnippet auto-complete electric-spacing auto-complete-c-headers))
(dolist (p tmtxt/packages)
  (when (not (package-installed-p p))
    (package-install p)))


   (add-to-list 'load-path
                "/home/saurav/.emacs.d/elpa/yasnippet-20141223.303/")
   (require 'yasnippet)
(yas-global-mode 1)

;;; auto complete mod
;;; should be loaded after yasnippet so that they can work together
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
;;; set the trigger key so that it can work together with yasnippet on tab key,
;;; if the word exists in yasnippet, pressing tab will cause yasnippet to
;;; activate, otherwise, auto-complete will
(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")


;;; activate ecb
(require 'ecb)
;;(require 'ecb-autoloads)

;;Now, everything you need to run ECB is to type M-x ecb-activate RET and ECB is now ready for you to use. When you want to stop it, just execute the command ecb-deactivate.
(setq ecb-layout-name "right1")

(setq ecb-show-sources-in-directories-buffer 'always)

;;; activate and deactivate ecb
(global-set-key (kbd "C-x C-;") 'ecb-activate)
(global-set-key (kbd "C-x C-'") 'ecb-deactivate)
;;; show/hide ecb window
(global-set-key (kbd "C-;") 'ecb-show-ecb-windows)
(global-set-key (kbd "C-'") 'ecb-hide-ecb-windows)
;;; quick navigation between ecb windows
(global-set-key (kbd "C-)") 'ecb-goto-window-edit1)
(global-set-key (kbd "C-!") 'ecb-goto-window-directories)
(global-set-key (kbd "C-@") 'ecb-goto-window-sources)
(global-set-key (kbd "C-#") 'ecb-goto-window-methods)
(global-set-key (kbd "C-$") 'ecb-goto-window-compilation)



;;; replacement for built-in ecb-deactive, ecb-hide-ecb-windows and
;;; ecb-show-ecb-windows functions
;;; since they hide/deactive ecb but not restore the old windows for me
(defun tmtxt/ecb-deactivate ()
  "deactive ecb and then split emacs into 2 windows that contain 2 most recent buffers"
  (interactive)
  (ecb-deactivate)
  (split-window-right)
  (switch-to-next-buffer)
  (other-window 1))
(defun tmtxt/ecb-hide-ecb-windows ()
  "hide ecb and then split emacs into 2 windows that contain 2 most recent buffers"
  (interactive)
  (ecb-hide-ecb-windows)
  (split-window-right)
  (switch-to-next-buffer)
  (other-window 1))
(defun tmtxt/ecb-show-ecb-windows ()
  "show ecb windows and then delete all other windows except the current one"
  (interactive)
  (ecb-show-ecb-windows)
  (delete-other-windows))

(global-set-key (kbd "C-x C-'") 'tmtxt/ecb-deactivate)
(global-set-key (kbd "C-;") 'tmtxt/ecb-show-ecb-windows)
(global-set-key (kbd "C-'") 'tmtxt/ecb-hide-ecb-windows)
(electric-spacing-mode 1)

(require 'flymake)
;;Add this to Makefile
;;check-syntax:
;;     gcc -o nul -S ${CHK_SOURCES}

;;(add-hook 'find-file-hook 'flymake-find-file-hook)

;;(setq ecb-compile-window-height 12)
;; turn on c++-mode for files ending in ".sing" and ".lib"
(setq auto-mode-alist (cons '("\\.sing\\'" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.lib\\'" .  c++-mode) auto-mode-alist))
;; turn-on fontification for c++-mode
;;(add-hook 'c++-mode-hook
;;         (function (lambda () (font-lock-mode 1))))
;; turn on aut-new line and hungry-delete
;;(add-hook 'c++-mode-hook
;;          (function (lambda () (c-toggle-auto-hungry-state 1))))
;; a handy function for customization
(defun customize-face-at-point ()
  "Customize face which point is at."
  (interactive)
  (let ((face (get-text-property (point) 'face)))
    (if face
        (customize-face face)
      (message "No face defined at point"))))
(put 'upcase-region 'disabled nil)

(add-hook 'before-save-hook
          (lambda ()
            (when buffer-file-name
              (let ((dir (file-name-directory buffer-file-name)))
                (when (and (not (file-exists-p dir))
                           (y-or-n-p (format "Directory %s does not exist. Create it?" dir)))
                  (make-directory dir t))))))

(setq LaTeX-command-style '(("" "%(PDF)%(latex) -file-line-error %S%(PDFout)")))


(load-theme 'saurav t)
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
;;(set-frame-parameter nil 'fullscreen 'fullboth)

;; Completion words longer than 4 characters


(eval-after-load "auto-complete"
  '(progn
     (ac-ispell-setup)))

(add-hook 'git-commit-mode-hook 'ac-ispell-ac-setup)
(add-hook 'mail-mode-hook 'ac-ispell-ac-setup)


(require 'auto-complete-c-headers)
(add-to-list 'ac-sources 'ac-source-c-headers)
;;(ecb-activate)
(add-hook 'c-mode-hook 'ecb-activate)
(add-hook 'c++-mode-hook 'ecb-activate)
;;(global-hl-line-mode 1)
;;(set-face-background 'hl-line "#000000")
(set-face-foreground 'highlight nil)
(setq-default cursor-type 'hbar)

;;TEX customizations

;; TeX File Auto Formatting
;;(server-start)
(add-hook 'text-mode-hook 'turn-on-auto-fill)

(defun my-tex-mode-hook ()
  (setq-default TeX-master nil)
  (setq TeX-auto-save t)
  (setq TeX-parse-self t)
  (setq TeX-PDF-mode t)
  (setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                           ("marmalade" . "http://marmalade-repo.org/packages/")
                           ("melpa" . "http://melpa.milkbox.net/packages/")))
  )
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(setq reftex-plug-into-AUCTeX t)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)
(add-hook 'my-tex-mode-hook 'flyspell-mode-hook)
(add-hook 'TeX-mode-hook 'my-tex-mode-hook)
(add-hook 'LaTeX-mode-hook 'my-tex-mode-hook)
(dolist (hook '(LaTeX-mode-hook))
  (add-hook hook (lambda () (flyspell-mode 1))))
(dolist (hook '(TeX-mode-hook))
  (add-hook hook (lambda () (flyspell-mode 1))))
;; '(LaTeX-command "latex -synctex=1")

;;(setq TeX-view-program-list '(("Evince" "evince --page-index=%(outpage) %o")))
;;(setq TeX-view-program-selection '((output-pdf "Evince")))

;;doc-view customizations
(setq doc-view-continuous t)
(add-hook 'doc-view-mode-hook 'auto-revert-mode)
'(TeX-PDF-mode t)
'(TeX-source-correlate-method (quote synctex))
'(TeX-source-correlate-mode t)
'(TeX-source-correlate-start-server t)
;;'(TeX-view-program-list (quote (("Okular" "okular --unique %o#src:%n%b"))))
;;'(TeX-view-program-selection
;;  (quote
;;   ((output-pdf "Okular")
;;    ((output-dvi style-pstricks)
;;     "dvips and gv")
;;    (output-dvi "xdvi")
;;    (output-pdf "xpdf")
;;    (output-html "xdg-open"))))
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review. Could you add a bit of detail/context about what your code is doing, etc.? As it stands it is a lot of code to read with no context. \$\endgroup\$ – Phrancis Feb 20 '16 at 5:35
  • \$\begingroup\$ I think this is not the right way to post a question. Probably I should break into parts? I have my .emacs file and want to review if it's the right way or if it could be improved. \$\endgroup\$ – Saurav Feb 20 '16 at 5:36
  • 1
    \$\begingroup\$ It is fine to post everything in one question if you like, just add some context in to explain to reviewers in short what your emacs file is doing (especially if a section of it can be confusing) \$\endgroup\$ – Phrancis Feb 20 '16 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.