Skip to content

Instantly share code, notes, and snippets.

@pthalamy
Last active March 12, 2019 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pthalamy/a72eecb8cec16f1ba6ef85fbb0c56f95 to your computer and use it in GitHub Desktop.
Save pthalamy/a72eecb8cec16f1ba6ef85fbb0c56f95 to your computer and use it in GitHub Desktop.
;; Default directory for lisp files
(add-to-list 'load-path "~/.emacs.d/lisp")
(add-to-list 'load-path "~/share/emacs/site-lisp/")
;; Melpa
(require 'package) ;; You might already have this line
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/") t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
;; (package-initialize) ;; You might already have this line
;; Correspondance des parenthèses :
;; Avec ceci, positionnez le curseur sur une parenthèse ouvrante ou
;; une parenthèse fermante, Emacs met en couleur la paire de
;; parenthèses.
(show-paren-mode 1)
;; Afficher les numéros de lignes dans la mode-line (barre du bas de
;; fenêtre) :
(line-number-mode t)
(column-number-mode t)
;; All temp files in same dir
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
;; Ne pas afficher le message d'accueil
(setq inhibit-startup-message t)
;; Visionner la région (aka sélection) courante :
(transient-mark-mode t)
;; Correction orthographique :
(ispell-change-dictionary "francais")
;; Souligner les mots incorrects en mode LaTeX
(add-hook 'latex-mode-hook 'flyspell-mode)
;; Se limiter à des lignes de 80 caractères dans les modes textes (y
;; compris le mode LaTeX) :
;; cf. http://www-verimag.imag.fr/~moy/emacs/#autofill
;; (add-hook 'text-mode-hook 'turn-on-auto-fill)
;; Changer le comportement de la selection de fichiers (C-x C-f)
;; (ido-mode 1)
;; Pour une interface graphique un peu dépouillée
(menu-bar-mode -1)
(scroll-bar-mode -1)
(tool-bar-mode -1)
;; 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)
;; Ouvrir les .txt en org-mode
(add-to-list 'auto-mode-alist '("\\.txt\\'" . org-mode))
;; Save Emacs session when exiting Emacs
(desktop-save-mode 1)
;; Disable right alt as meta
(setq ns-right-alternate-modifier nil)
;; Bind replace-string to C-x <F1>
(global-set-key (kbd "<f1>") 'replace-string)
(global-set-key (kbd "<f2>") 'grep-find)
;; yasnippet
(require 'yasnippet)
(setq yas/trigger-key (kbd "C-c <kp-multiply>"))
;; This is where your snippets will lie.
(setq yas/root-directory '("~/.emacs.d/snippets"))
(mapc 'yas/load-directory yas/root-directory)
(yas-global-mode 1)
;; auto-complete
(require 'auto-complete-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)
(global-auto-complete-mode t)
(add-to-list 'ac-modes 'objc-mode)
(auto-complete-mode 1)
;; TODO/BUG/FIXME highlighting
(add-hook 'c-mode-common-hook
(lambda ()
(font-lock-add-keywords nil
'(("\\<\\(FIXME\\|TODO\\|BUG\\|NOTE\\|WARNING\\|TOCHECK\\)" 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)
;; Change window using M-arrow
;; (windmove-default-keybindings 'meta)
;; (global-set-key (kbd "C-c <left>") 'windmove-left)
;; (global-set-key (kbd "C-c <right>") 'windmove-right)
;; (global-set-key (kbd "C-c <up>") 'windmove-up)
;; (global-set-key (kbd "C-c <down>") 'windmove-down)
;; C-c left to undo, C-c right to redo window changes
(winner-mode 1)
;; Tabbar change tab
(tabbar-mode 1)
(global-set-key [(super right)] 'tabbar-forward-tab)
(global-set-key [(super left)] 'tabbar-backward-tab)
(global-set-key [(super shift right)] 'tabbar-forward-group)
(global-set-key [(super shift left)] 'tabbar-backward-group)
;; 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))))))))))
;; Get default PATH
(if (not (getenv "TERM_PROGRAM"))
(let ((path (shell-command-to-string
"$SHELL -cl \"printf %s \\\"\\\$PATH\\\"\"")))
(setenv "PATH" path)))
;; "y or n" instead of "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)
;; Don't truncate lines
(setq truncate-lines t)
;; Make sure all backup files only live in one place
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
;; Trailing whitespace is unnecessary
;; (add-hook 'before-save-hook (lambda () (delete-trailing-whitespace)))
;; (add-hook 'before-save-hook (lambda () (whitespace-cleanup)))
;; Trash can support
(setq delete-by-moving-to-trash t)
;; Window manipulation
(global-set-key [(super +)] 'enlarge-window)
(global-set-key [(super -)] 'shrink-window)
;; shell colors again
(require 'ansi-color)
;; 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)
;; Cleanup lists
;; (add-to-list 'c-cleanup-list 'brace-else-brace)
;; (add-to-list 'c-cleanup-list 'brace-elseif-brace)
;; (add-to-list 'c-cleanup-list 'brace-catch-brace)
;; (add-to-list 'c-cleanup-list 'empty-defun-btace)
;; (add-to-list 'c-cleanup-list 'defun-close-semi)
(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)))
;; Extended Helm Config
(require 'helm)
(require 'helm-config)
;; The default "C-x c" is quite close to "C-x C-c", which quits Emacs.
;; Changed to "C-c h". Note: We must set "C-c h" globally, because we
;; cannot change `helm-command-prefix-key' once `helm-config' is loaded.
(global-set-key (kbd "C-c h") 'helm-command-prefix)
(global-unset-key (kbd "C-x c"))
(global-unset-key (kbd "C-x C-f"))
(global-set-key (kbd "C-x C-f") 'helm-find-files)
(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action) ; rebind tab to run persistent action
(define-key helm-map (kbd "C-i") 'helm-execute-persistent-action) ; make TAB works in terminal
(define-key helm-map (kbd "C-z") 'helm-select-action) ; list actions using C-z
(when (executable-find "curl")
(setq helm-net-prefer-curl t))
(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)
(global-set-key (kbd "M-y") 'helm-show-kill-ring)
(helm-mode 1)
;; 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 ) )
))
;; I want an easy command for opening new shells:
(defun new-shell (name)
"Opens a new shell buffer with the given name in
asterisks (*name*) in the current directory and changes the
prompt to 'name>'."
(interactive "sName: ")
(pop-to-buffer (concat "*" name "*"))
(unless (eq major-mode 'shell-mode)
(shell (current-buffer))
(sleep-for 0 200)
(delete-region (point-min) (point-max))
;; (comint-simple-send (get-buffer-process (current-buffer))
;; (concat "export PS1=\"" name ">\"")
;; )
))
(global-set-key (kbd "C-c s") 'new-shell)
;; comint install
(progn (add-hook 'comint-preoutput-filter-functions 'xterm-color-filter)
(setq comint-output-filter-functions (remove 'ansi-color-process-output comint-output-filter-functions)))
;; comint uninstall
(progn (remove-hook 'comint-preoutput-filter-functions 'xterm-color-filter)
(add-to-list 'comint-output-filter-functions 'ansi-color-process-output))
;; You can also use it with eshell (and thus get color output from system ls):
(require 'eshell)
(add-hook 'eshell-mode-hook
(lambda ()
(setq xterm-color-preserve-properties t)))
(add-to-list 'eshell-preoutput-filter-functions 'xterm-color-filter)
(setq eshell-output-filter-functions (remove 'eshell-handle-ansi-color eshell-output-filter-functions))
;; Don't forget to setenv TERM xterm-256color
;; Show / Hide function body
(require 'hideshow)
(setq hs-minor-mode t)
(global-set-key (kbd "M-s M-s") 'hs-toggle-hiding)
(global-set-key (kbd "M-s M-a") 'hs-hide-all)
(global-set-key (kbd "M-s M-z") 'hs-show-all)
;; Don't open new frame for every file openning
(setq ns-pop-up-frames 'nil)
;; BATTERY MODE !
;; (display-battery-mode 1)
;; (display-time-mode 1)
(global-auto-revert-mode 1)
;; subword mode using meta
(global-subword-mode 1)
;; (global-set-key (kbd "M-left") 'subword-backward)
;; (global-set-key (kbd "M-right") 'subword-forward)
;; (global-set-key (kbd "M-DEL") 'subword-backward-kill)
;; Linum mode
(global-linum-mode t)
;; Offset the number by two spaces to work around some weird fringe glitch
(setq linum-format " %d")
;; Window enlarge-shrink
(global-set-key (kbd "s-}") 'enlarge-window-horizontally)
(global-set-key (kbd "s-{") 'shrink-window-horizontally)
;; Projectile
(projectile-global-mode 1)
(setq projectile-enable-caching t)
;; (setq helm-projectile-fuzzy-match nil)
(require 'helm-projectile)
(helm-projectile-on)
(global-unset-key (kbd "C-c p c"))
(global-set-key (kbd "C-c p c") 'projectile-compile-again)
(global-set-key (kbd "C-c p a") 'projectile-find-other-file)
(setq compilation-scroll-output 'first-error)
;; (setq compilation-scroll-output t)
(setq compilation-skip-threshold 2)
;; ;; Guess style
;; (add-to-list 'load-path "~/.emacs.d/lisp/guess-style/")
;; (autoload 'guess-style-set-variable "guess-style" nil t)
;; (autoload 'guess-style-guess-variable "guess-style")
;; (autoload 'guess-style-guess-all "guess-style" nil t)
;; (add-hook 'c-mode-common-hook 'guess-style-guess-all)
;; Activate Minimap Mode
(setq minimap-mode 0)
(global-unset-key (kbd "C-x m"))
(global-set-key (kbd "C-x m") 'minimap-mode)
;; Powerline
(require 'powerline)
(powerline-default-theme)
;; 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:
;; 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"))
;; 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)
;; Single key buffer kill
(global-set-key (kbd "s-k") 'kill-this-buffer)
;; CUA-mode
(setq cua-mode 1)
;; 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)
;; Close HTML tags on tab
(setq sgml-quick-keys 'indent)
;; Revert Buffers with s-u
(global-set-key (kbd "s-u") 'revert-buffer)
;; Disable Page-Up / Page-down on this computer
(global-unset-key (kbd "<next>") )
(global-set-key (kbd "C-c <next>") 'end-of-buffer)
(global-unset-key (kbd "<prior>") )
(global-set-key (kbd "C-c <prior>") 'beginning-of-buffer)
;; Helm M-X
(global-unset-key (kbd "M-x") )
(global-set-key (kbd "M-x") 'helm-M-x)
;; Spolsky Theme
(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-enabled-themes '(spolsky))
'(custom-safe-themes
'("c48551a5fb7b9fc019bf3f61ebf14cf7c9cdca79bcb2a4219195371c02268f11" default))
'(package-selected-packages
'(projectile-git-autofetch gnuplot gnuplot-mode yasnippet-classic-snippets yasnippet-snippets xterm-color tabbar sublime-themes seq minimap markdown-mode+ let-alist helm-projectile helm-directory git-blamed dash auto-yasnippet airline-themes ac-helm)))
(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.
'(default ((t (:background nil)))))
;; 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)
;; 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)
;; bury *scratch* buffer instead of kill it
(defadvice kill-buffer (around kill-buffer-around-advice activate)
(let ((buffer-to-kill (ad-get-arg 0)))
(if (equal buffer-to-kill "*scratch*")
(bury-buffer)
ad-do-it)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment