Skip to content

Instantly share code, notes, and snippets.

@pthalamy pthalamy/emacs.el
Created Mar 11, 2019

Embed
What would you like to do?
My emacs configuration
;; Default directory for lisp files
(add-to-list 'load-path "~/.emacs.d/lisp")
(add-to-list 'load-path "~/share/emacs/site-lisp/")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;; PACKAGES ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MELPA
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(when no-ssl
(warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)
;; use-package
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(require 'use-package)
(setq use-package-always-ensure t)
;;;;;;;; use-package EXAMPLE
;; (use-package color-moccur
;; :commands (isearch-moccur isearch-all)
;; :bind (("M-s O" . moccur)
;; :map isearch-mode-map
;; ("M-o" . isearch-moccur)
;; ("M-O" . isearch-moccur-all))
;; :init
;; (setq isearch-lazy-highlight t)
;; :config
;; (use-package moccur-edit))
(use-package yasnippet
:ensure t
;; :bind ("C-c y" . yas/trigger-key)
:config
;; This is where your snippets will lie.
(setq yas/root-directory '("~/.emacs.d/snippets"))
(mapc 'yas/load-directory yas/root-directory))
(use-package auto-complete
:ensure t
:config
;; (setq-default ac-sources '(ac-source-yasnippet ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
;; (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
;; (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
;; (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
;; (add-hook 'css-mode-hook 'ac-css-mode-setup)
;; (add-hook 'auto-complete-mode-hook 'ac-common-setup)
;; (add-to-list 'ac-modes 'objc-mode)
(global-auto-complete-mode t)
(auto-complete-mode 1))
(use-package auto-complete-c-headers)
(use-package emojify)
(use-package git-blamed)
(use-package gnuplot-mode)
(use-package helm
:ensure t
:bind
(("C-x f" . helm-recentf)
;; ("C-SPC" . helm-dabbrev)
("M-y" . helm-show-kill-ring)
("M-x" . helm-M-x)
("C-c h" . helm-command-prefix)
("C-x C-f" . helm-find-files)
("C-x b" . helm-buffers-list)
("M-y" . helm-show-kill-ring))
:bind
(:map helm-map
; rebind tab to run persistent action
("<tab>" . helm-execute-persistent-action)
; make TAB works in terminal; rebind tab to run persistent action
("C-i" . helm-execute-persistent-action)
; list actions using C-z
("C-z" . helm-select-action))
:config (progn (setq helm-buffers-fuzzy-matching t)
(helm-mode 1))
(setq helm-split-window-in-side-p t ; open helm buffer inside current window, not occupy whole other window
helm-move-to-line-cycle-in-source t ; move to end or beginning of source when reaching top or bottom of source.
helm-ff-search-library-in-sexp t ; search for library in `require' and `declare-function' sexp.
helm-scroll-amount 8 ; scroll 8 lines other window using M-<next>/M-<prior>
helm-ff-file-name-history-use-recentf t)
)
(use-package projectile
:ensure t
:bind-keymap ("C-c p" . projectile-command-map)
:bind
(:map projectile-command-map
("a" . projectile-find-other-file)
;; ("c" . projectile-compile-again)
)
:config
(setq projectile-enable-caching t)
(setq helm-projectile-fuzzy-match t)
(setq compilation-scroll-output 'first-error)
(setq compilation-skip-threshold 2)
(setq projectile-enable-caching t)
(projectile-global-mode))
(use-package helm-projectile
:ensure t
:bind (:map projectile-command-map
("f" . helm-projectile-find-file))
:init (helm-projectile-on))
;; (use-package minimap
;; ;; :bind
;; ;; ("C-x m" . minimap-mode)
;; :init
;; ;; (De)Activate Minimap Mode
;; (setq minimap-mode 0))
;; Show / Hide function body
(use-package hideshow
:bind
(("M-s M-s" . hs-toggle-hiding)
("M-s M-a" . hs-hide-all)
("M-s M-z" . hs-show-all))
:init
(setq hs-minor-mode t))
;; Tabbar change tab
(use-package tabbar
:bind
(([(super ctrl right)] . tabbar-forward-tab)
([(super ctrl left)] . tabbar-backward-tab)
([(super ctrl shift right)] . tabbar-forward-group)
([(super ctrl shift left)] . tabbar-backward-group))
:config
(;; Sort tabs by name
(defun tabbar-add-tab (tabset object &optional append_ignored)
(let ((tabs (tabbar-tabs tabset)))
(if (tabbar-get-tab object tabset)
tabs
(let ((tab (tabbar-make-tab object tabset)))
(tabbar-set-template tabset nil)
(set tabset (sort (cons tab tabs)
(lambda (a b) (string< (buffer-name (car a))
(buffer-name (car b)))))))))) ))
;; Doxymacs
(require 'doxymacs)
(add-hook 'c-mode-common-hook'doxymacs-mode)
(defun my-doxymacs-font-lock-hook ()
(if (or (eq major-mode 'c-mode) (eq major-mode 'c++-mode))
(doxymacs-font-lock)))
(add-hook 'font-lock-mode-hook 'my-doxymacs-font-lock-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; Configuration ;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;; ENVIRONMENT ;;;;;;;;;;;;;;;;;;;;;;
;; ;;Afficher les numéros de lignes dans la mode-line (barre du bas de fenêtre) :
(line-number-mode t)
(column-number-mode t)
;; Ne pas afficher le message d'accueil
(setq inhibit-startup-message t)
;; Pour une interface graphique un peu dépouillée
(menu-bar-mode -1)
(scroll-bar-mode -1)
(tool-bar-mode -1)
;; Save Emacs session when exiting Emacs
(desktop-save-mode)
;; ;; use only one desktop
(setq desktop-path '("~/.emacs.d/"))
(setq desktop-dirname "~/.emacs.d/")
;; (setq desktop-base-file-name "emacs-desktop")
;; ;; remove desktop after it's been read
;; (add-hook 'desktop-after-read-hook
;; '(lambda ()
;; ;; desktop-remove clears desktop-dirname
;; (setq desktop-dirname-tmp desktop-dirname)
;; (desktop-remove)
;; (setq desktop-dirname desktop-dirname-tmp)))
;; (defun saved-session ()
;; (file-exists-p (concat desktop-dirname "/" desktop-base-file-name)))
;; ;; use session-restore to restore the desktop manually
;; (defun session-restore ()
;; "Restore a saved emacs session."
;; (interactive)
;; (if (saved-session)
;; (desktop-read)
;; (message "No desktop found.")))
;; ;; use session-save to save the desktop manually
;; (defun session-save ()
;; "Save an emacs session."
;; (interactive)
;; (if (saved-session)
;; (if (y-or-n-p "Overwrite existing desktop? ")
;; (desktop-save-in-desktop-dir)
;; (message "Session not saved."))
;; (desktop-save-in-desktop-dir)))
;; ;; ask user whether to restore desktop at start-up
;; (add-hook 'after-init-hook
;; '(lambda ()
;; (if (saved-session)
;; (if (y-or-n-p "Restore desktop? ")
;; (session-restore)))))
;; All temp files in same dir
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
;; Make sure all backup files only live in one place
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
;; "y or n" instead of "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)
;; Trash can support
(setq delete-by-moving-to-trash t)
(global-auto-revert-mode 1)
;;;;;;;;;;;;;;;;;;;;; WINDOW ;;;;;;;;;;;;;;;;;;;;;;;
;; C-c left to undo, C-c right to redo window changes
(winner-mode 1)
;; Window manipulation
(global-set-key [(super +)] 'enlarge-window)
(global-set-key [(super -)] 'shrink-window)
;; Window enlarge-shrink
(global-set-key (kbd "s-}") 'enlarge-window-horizontally)
(global-set-key (kbd "s-{") 'shrink-window-horizontally)
;; Don't open new frame for every file opening
(setq ns-pop-up-frames 'nil)
;;;;;;;;;;;;;;;;;;;;; NAVIGATION ;;;;;;;;;;;;;;;;;;;
;; subword mode using meta
(global-subword-mode 1)
;;;;;;;;;;;;;;;;;;;;; EDITION ;;;;;;;;;;;;;;;;;;;;;;
;; Visionner la région (aka sélection) courante :
(transient-mark-mode t)
;; Définir des touches pour se déplacer rapidement :
;; Aller à la parenthèse ouvrante correspondante :
(global-set-key [M-shift-right] 'forward-sexp)
;; Aller à la parenthèse Fermante correspondante :
(global-set-key [M-shift-left] 'backward-sexp)
;; Disable right alt as meta
(setq ns-right-alternate-modifier nil)
;; Trailing whitespace is unnecessary
(add-hook 'before-save-hook (lambda () (delete-trailing-whitespace)))
(add-hook 'before-save-hook (lambda () (whitespace-cleanup)))
;; Linum mode
(global-linum-mode t)
;; Offset the number by two spaces to work around some weird fringe glitch
(setq linum-format " %d")
;; CUA-mode
(setq cua-mode 1)
;;;;;;;;;;;;;;;;;;;; CUSTOM SHORTCUTS ;;;;;;;;;;;;;;
;; Bind replace-string to C-x <F1>
(global-set-key (kbd "<f1>") 'replace-string)
(global-set-key (kbd "<f2>") 'grep-find)
;; Single key buffer kill
(global-set-key (kbd "s-k") 'kill-this-buffer)
;; Revert Buffers with s-u
(global-set-key (kbd "s-u") 'revert-buffer)
;; Unbind Pesky Sleep Button
(global-unset-key [(control z)])
(global-unset-key [(control x)(control z)])
;; Windows Style Undo
(global-set-key [(control z)] 'undo)
;;;;;;;;;;;;;;;;;;;; CUSTOM HIGHLIGHTING ;;;;;;;;;;;;;;;;;;
;; TODO/BUG/FIXME highlighting
(add-hook 'c-mode-common-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\<\\(FIXME\\|TODO\\|BUG\\|NOTE\\|WARNING\\|MAYBE\\)" 1 font-lock-warning-face t)))))
;; Associate .bb files to c-mode
(add-to-list 'auto-mode-alist '("\\.bb\\'" . c-mode))
(add-to-list 'auto-mode-alist '("\\.bbh\\'" . c-mode))
;; Always show matching parenthesis and brackets C-M-n C-M-p
(show-paren-mode 1)
;; Tramp ssh (faster)
(setq tramp-default-method "ssh")
;; coding style / tabs
(defun my-c-setup ()
(setq-default indent-tabs-mode nil)
(setq tab-width 4)
(setq c-default-style "k&r"
c-basic-offset 4)
;;; No namespace indent in c++
(c-set-offset 'innamespace [0])
(c-set-offset 'case-label '+))
(add-hook 'c++-mode-hook 'my-c-setup)
;; java coding style
(add-hook 'java-mode-hook (lambda ()
(setq c-basic-offset 4
tab-width 4
indent-tabs-mode t)))
(add-hook 'java-mode-hook
(lambda ()
"Treat Java 1.5 @-style annotations as comments."
(setq c-comment-start-regexp "(@|/(/|[*][*]?))")
(modify-syntax-entry ?@ "< b" java-mode-syntax-table)))
;; Generate Etags
(setq path-to-ctags "/usr/bin/etags") ;; <- your ctags path here
(defun create-tags (dir-name)
"Create tags file."
(interactive "DDirectory: ")
(eshell-command
(format "find %s -type f -name \"*.[ch]\" | etags -" dir-name)))
;;; Auto-Refresh
(defun er-refresh-etags (&optional extension)
"Run etags on all peer files in current dir and reload them silently."
(interactive)
(shell-command (format "etags *.%s" (or extension "el")))
(let ((tags-revert-without-query t)) ; don't query, revert silently
(visit-tags-table default-directory nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;; CUSTOM FUNCTIONS
;; Prints the decimal value of hex under cursor
(defun what-hexadecimal-value ()
"Prints the decimal value of a hexadecimal string under cursor.
Samples of valid input:
ffff
0xffff
#xffff
FFFF
0xFFFF
#xFFFF
Test cases
64*0xc8+#x12c 190*0x1f4+#x258
100 200 300 400 500 600"
(interactive )
(let (inputStr tempStr p1 p2 )
(save-excursion
(search-backward-regexp "[^0-9A-Fa-fx#]" nil t)
(forward-char)
(setq p1 (point) )
(search-forward-regexp "[^0-9A-Fa-fx#]" nil t)
(backward-char)
(setq p2 (point) ) )
(setq inputStr (buffer-substring-no-properties p1 p2) )
(let ((case-fold-search nil) )
(setq tempStr (replace-regexp-in-string "^0x" "" inputStr )) ; C, Perl, …
(setq tempStr (replace-regexp-in-string "^#x" "" tempStr )) ; elisp …
(setq tempStr (replace-regexp-in-string "^#" "" tempStr )) ; CSS …
)
(message "Hex %s is %d" tempStr (string-to-number tempStr 16 ) )
))
;; Edit config shortcut
(defun edit-config ()
(interactive)
"Edit Emacs Configuration File."
(find-file "~/.emacs.el"))
;; Reload configuration file
(defun reload-config ()
(interactive)
"Reload Emacs Configuration File."
(load-file "~/.emacs.el"))
;; Define key sequence for custom functions
(progn
;; define a prefix keymap
(define-prefix-command 'my-custom-map)
(define-key my-custom-map (kbd "r") 'reload-config)
(define-key my-custom-map (kbd "e") 'edit-config)
)
(global-set-key (kbd "C-c m") my-custom-map)
;;;;;;;;;;;;;;;;;;;;;;;;; COMPILATION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compilation Function
;; (defun projectile-compile-again (pfx)
;; """Run the same compile as the last time.
;; If there was no last time, or there is a prefix argument, this acts like
;; M-x projectile-compile-project.
;; """
;; (interactive "p")
;; (if (and (eq pfx 1)
;; compilation-last-buffer)
;; (progn
;; (set-buffer compilation-last-buffer)
;; (revert-buffer t t))
;; (call-interactively 'projectile-compile-project)))
;; (setq compilation-last-buffer nil)
;; Add limit to savehist
(setq history-length 100)
(put 'minibuffer-history 'history-length 50)
(put 'evil-ex-history 'history-length 50)
(put 'kill-ring 'history-length 25)
;; Replace selection when typing while text is selected
(delete-selection-mode 1)
;;;;;;;;;;;;;;;;;;;;
;; THEME AREA ;;
;;;;;;;;;;;;;;;;;;;;
(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.
'(custom-safe-themes
(quote
("c48551a5fb7b9fc019bf3f61ebf14cf7c9cdca79bcb2a4219195371c02268f11" default)))
'(package-selected-packages
(quote
(doxymacs yasnippet-snippets yasnippet-classic-snippets tabbar sublimity sublime-themes powerline mode-icons highlight-escape-sequences helm-projectile helm-mode-manager gnuplot-mode gnuplot git-blamed emojify diffview darkroom csv-mode auto-complete-c-headers ac-c-headers 2048-game)))
'(show-paren-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.
)
;; set theme
(use-package sublime-themes
:config
(load-theme 'spolsky))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.