Skip to content

Instantly share code, notes, and snippets.

@Stevearzh
Last active December 25, 2015 17:07
Show Gist options
  • Save Stevearzh/4509b3b516e9bbb1d017 to your computer and use it in GitHub Desktop.
Save Stevearzh/4509b3b516e9bbb1d017 to your computer and use it in GitHub Desktop.
;; ------------------------------------------------------------ ;;
;; MELPA
;; ------------------------------------------------------------ ;;
(require 'package)
(dolist (source '(
("melpa" . "http://melpa.org/packages/")
))
(add-to-list 'package-archives source 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)
;; ------------------------------------------------------------ ;;
;; Theme
;; ------------------------------------------------------------ ;;
(load-theme 'spacemacs-dark t)
;; ------------------------------------------------------------ ;;
;; eshell
;; ------------------------------------------------------------ ;;
(propertize "some-string" 'face `(:foreground "green"))
(defun curr-dir-git-branch-string (pwd)
"Returns current git branch as a string, or the empty string if
PWD is not in a git repo (or the git command is not found)."
(interactive)
(when (and (eshell-search-path "git")
(locate-dominating-file pwd ".git"))
(let ((git-output (shell-command-to-string (concat "cd " pwd " && git branch | grep '\\*' | sed -e 's/^\\* //'"))))
(concat "["
(if (> (length git-output) 0)
(substring git-output 0 -1)
"(no branch)")
"]")
)))
(setq eshell-history-size 1024)
(setq eshell-prompt-regexp "^[^#$]*[#$] ")
(load "em-hist") ; So the history vars are defined
(if (boundp 'eshell-save-history-on-exit)
(setq eshell-save-history-on-exit t)) ; Don't ask, just save
;(message "eshell-ask-to-save-history is %s" eshell-ask-to-save-history)
(if (boundp 'eshell-ask-to-save-history)
(setq eshell-ask-to-save-history 'always)) ; For older(?) version
;(message "eshell-ask-to-save-history is %s" eshell-ask-to-save-history)
(defun eshell/ef (fname-regexp &rest dir) (ef fname-regexp default-directory))
;;; ---- path manipulation
(defun pwd-repl-home (pwd)
(interactive)
(let* ((home (expand-file-name (getenv "HOME")))
(home-len (length home)))
(if (and
(>= (length pwd) home-len)
(equal home (substring pwd 0 home-len)))
(concat "~" (substring pwd home-len))
pwd)))
(defun curr-dir-git-branch-string (pwd)
"Returns current git branch as a string, or the empty string if
PWD is not in a git repo (or the git command is not found)."
(interactive)
(when (and (eshell-search-path "git")
(locate-dominating-file pwd ".git"))
(let ((git-output (shell-command-to-string (concat "cd " pwd " && git branch | grep '\\*' | sed -e 's/^\\* //'"))))
(propertize (concat "["
(if (> (length git-output) 0)
(substring git-output 0 -1)
"(no branch)")
"]") 'face `(:foreground "#4caf50"))
)))
(setq eshell-prompt-function
(lambda ()
(concat
(propertize ((lambda (p-lst)
(if (> (length p-lst) 3)
(concat
(mapconcat (lambda (elm) (if (zerop (length elm)) ""
(substring elm 0 1)))
(butlast p-lst 3)
"/")
"/"
(mapconcat (lambda (elm) elm)
(last p-lst 3)
"/"))
(mapconcat (lambda (elm) elm)
p-lst
"/")))
(split-string (pwd-repl-home (eshell/pwd)) "/")) 'face `(:foreground "#ffa634"))
(or (curr-dir-git-branch-string (eshell/pwd)))
(propertize " $ " 'face 'default))))
(setq eshell-highlight-prompt nil)
(eval-after-load 'eshell
'(require 'eshell-autojump nil t))
(setq eshell-last-dir-ring-size 500)
;; eshell path env settings
(defun eshell-mode-hook-func ()
(setq eshell-path-env (concat "/usr/local/bin:" eshell-path-env))
(setenv "PATH" (concat "/usr/local/bin:" (getenv "PATH")))
(define-key eshell-mode-map (kbd "M-s") 'other-window-or-split))
(add-hook 'eshell-mode-hook 'eshell-mode-hook-func)
;; ------------------------------------------------------------ ;;
;; ERC
;; ------------------------------------------------------------ ;;
;; Load authentication info from an external source. Put sensitive
;; passwords and the like in here.
(load "~/.emacs.d/erc/auth")
;; This is an example of how to make a new command. Type "/uptime" to
;; use it.
(defun erc-cmd-UPTIME (&rest ignore)
"Display the uptime of the system, as well as some load-related
stuff, to the current ERC buffer."
(let ((uname-output
(replace-regexp-in-string
", load average: " "] {Load average} ["
;; Collapse spaces, remove
(replace-regexp-in-string
" +" " "
;; Remove beginning and trailing whitespace
(replace-regexp-in-string
"^ +\\|[ \n]+$" ""
(shell-command-to-string "uptime"))))))
(erc-send-message
(concat "{Uptime} [" uname-output "]"))))
;; Auto connect when erc buffer create
;; (erc :server "irc.freenode.net" :port 8000 :nick "Stevearzh")
;; This causes ERC to connect to the Freenode network upon hitting
;; C-c f. Replace MYNICK with your IRC nick.
(global-set-key "\C-cf" (lambda () (interactive)
(erc :server "irc.freenode.net" :port 8000
:nick "Stevearzh")))
;; This causes ERC to connect to the IRC server on your own machine (if
;; you have one) upon hitting C-c e b. Replace MYNICK with your IRC
;; nick. Often, people like to run bitlbee (http://bitlbee.org/) as an
;; AIM/Jabber/MSN to IRC gateway, so that they can use ERC to chat with
;; people on those networks.
;; (global-set-key "\C-ceb" (lambda () (interactive)
;; (erc :server "localhost" :port 8000
;; :nick "Stevearzh")))
;; Identify automatically
(require 'erc-services)
(erc-services-mode 1)
(setq erc-prompt-for-nickserv-password nil)
(setq erc-nickserv-passwords
`((freenode (("Stevearzh" . ,freenode-nick-pass)
))))
;; Make C-c RET (or C-c C-RET) send messages instead of RET. This has
;; been commented out to avoid confusing new users.
;; (define-key erc-mode-map (kbd "RET") nil)
;; (define-key erc-mode-map (kbd "C-c RET") 'erc-send-current-line)
;; (define-key erc-mode-map (kbd "C-c C-RET") 'erc-send-current-line)
;;; Options
;; Join the #emacs and #erc channels whenever connecting to Freenode.
(setq erc-autojoin-channels-alist '((".*freenode.net" "#linuxba" "#archlinux-cn" "#c_lang_cn")))
;; Only auto-join channels after NickServ has installed our hostmask cloak
;; (erc-autojoin-mode 0)
;; (add-hook 'erc-server-NOTICE-functions 'ted-post-cloak-autojoin)
;; (defun ted-post-cloak-autojoin (proc parsed)
;; "Autojoin iff NickServ tells us to."
;; (with-current-buffer (process-buffer proc)
;; (when (and (string-equal "irc.freenode.net"
;; (erc-response.sender parsed))
;; (string-match ".*NickServ set your hostname to.*"
;; (erc-response.contents parsed)))
;; (erc-autojoin-channels erc-session-server (erc-current-nick))
;; nil)))
;; Rename server buffers to reflect the current network name instead
;; of SERVER:PORT. (e.g. "freenode" instead of "irc.freenode.net:6667"). This
;; is useful when using a bouncer like ZNC where you have multiple
;; connections to the same server.
(setq erc-rename-buffers t)
;; Interpret mIRC-style color commands in IRC chats
(setq erc-interpret-mirc-color t)
;; The following are commented out by default, but users of other
;; non-Emacs IRC clients might find them useful.
;; Kill buffers for channels after /part
;; (setq erc-kill-buffer-on-part t)
;; Kill buffers for private queries after quitting the server
;; (setq erc-kill-queries-on-quit t)
;; Kill buffers for server messages after quitting the server
;; (setq erc-kill-server-buffer-on-quit t)
;; ------------------------------------------------------------ ;;
;; Markdown Mode
;; ------------------------------------------------------------ ;;
(autoload 'markdown-mode "markdown-mode"
"Major mode for editing Markdown files" t)
(add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
;; ------------------------------------------------------------ ;;
;; Auto Complete
;; ------------------------------------------------------------ ;;
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
;; uses TAB as trigger key
(ac-set-trigger-key "TAB")
(setq ac-auto-start nil)
;; change colors
(set-face-background 'ac-candidate-face "lightgray")
(set-face-underline 'ac-candidate-face "darkgray")
(set-face-background 'ac-selection-face "steelblue")
;;change default sources
(setq-default ac-sources '(ac-source-words-in-all-buffer))
;; ------------------------------------------------------------ ;;
;; Highlight Indentation
;; ------------------------------------------------------------ ;;
(require 'highlight-indentation)
(set-face-background 'highlight-indentation-face "#7e819f")
(set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
;; ------------------------------------------------------------ ;;
;; Web Mode
;; ------------------------------------------------------------ ;;
;; Highlight current HTML element
(setq web-mode-enable-current-element-highlight t)
;; (setq web-mode-enable-current-column-highlight t)
(add-hook 'web-mode-hook 'highlight-indentation-mode)
(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.js\\'" . web-mode))
;; Using web-mode for editing plain HTML files
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
;; Associate an engine
(setq web-mode-engines-alist
'(("php" . "\\.phtml\\'")
("blade" . "\\.blade\\."))
)
;; Associate a content type
(add-to-list 'auto-mode-alist '("\\.api\\'" . web-mode))
(add-to-list 'auto-mode-alist '("/some/react/path/*\\.js[x]?\\'" . web-mode))
(setq web-mode-content-types-alist
'(("json" . "/some/path/*\\.api\\'")
("xml" . "/other/path/*\\.api\\'")
("jsx" . "/some/react/path/*\\.js[x]?\\'")))
;; Customisation
(defun my-web-mode-hook ()
"Hooks for Web mode."
;; Indentation
;; HTML offset indentation
(setq web-mode-html-offset 2)
(setq web-mode-markup-indent-offset 2)
;; CSS offset indentation
(setq web-mode-css-indent-offset 2)
;; Script offset indentation (for JavaScript, Java, PHP, etc.)
(setq web-mode-code-indent-offset 2)
;; Left padding
;; For <style> parts
(setq web-mode-style-padding 1)
;; For <script> parts
(setq web-mode-script-padding 1)
;; For multi-line blocks
(setq web-mode-block-padding 0)
;; Comments
(setq web-mode-comment-style 2)
;; Syntax Highlighting
;; (set-face-attribute 'web-mode-css-rule-face nil :foreground "Pink3")
;; Change the shortcut for element navigation
;; (define-key web-mode-map (kbd "C-n") (interactive) 'web-mode-tag-match)
;; Add a snippet
(setq web-mode-extra-snippets
'(("erb" . (("toto" . ("<% toto | %>\n\n<% end %>"))))
("php" . (("dowhile" . ("<?php do { ?>\n\n<?php } while (|); ?>"))
("debug" . ("<?php error_log(__LINE__); ?>"))))
))
;; Add auto-pair
(setq web-mode-extra-auto-pairs
'(("erb" . (("beg" "end")))
("php" . (("beg" "end")
("beg" "end")))
))
;; Enable / disable features
;; Auto-pairing
(setq web-mode-enable-auto-pairing t)
;; CSS colorization
(setq web-mode-enable-css-colorization t)
;; Set blocks background and default foreground
(setq web-mode-enable-block-face t)
;; Set parts background and default foreground
(setq web-mode-enable-part-face t)
;; Comment keywords
(setq web-mode-enable-comment-keywords t)
;; Heredoc (cf. PHP strings) fontification
(setq web-mode-enable-heredoc-fontification t)
)
(add-hook 'web-mode-hook 'my-web-mode-hook)
;; Context-aware auto-completion
(setq web-mode-ac-sources-alist
'(("css" . (ac-source-css-property))
("html" . (ac-source-words-in-buffer ac-source-abbrev))))
(setq web-mode-ac-sources-alist
'(("php" . (ac-source-yasnippet ac-source-php-auto-yasnippets))
("html" . (ac-source-emmet-html-aliases ac-source-emmet-html-snippets))
("css" . (ac-source-css-property ac-source-emmet-css-snippets))))
(add-hook 'web-mode-before-auto-complete-hooks
'(lambda ()
(let ((web-mode-cur-language
(web-mode-language-at-pos)))
(if (string= web-mode-cur-language "php")
(yas-activate-extra-mode 'php-mode)
(yas-deactivate-extra-mode 'php-mode))
(if (string= web-mode-cur-language "css")
(setq emmet-use-css-transform t)
(setq emmet-use-css-transform nil)))))
;; ------------------------------------------------------------ ;;
;; Sass Mode
;; ------------------------------------------------------------ ;;
(require 'sass-mode)
(require 'rainbow-mode)
(require 'highlight-indentation)
(add-hook 'sass-mode-hook 'rainbow-mode)
(add-hook 'sass-mode-hook 'highlight-indentation-mode)
;; ------------------------------------------------------------ ;;
;; JSX
;; ------------------------------------------------------------ ;;
;; JSX Syntax Highlighting using web-mode
(add-to-list 'auto-mode-alist '("\\.jsx$" . web-mode))
(defadvice web-mode-highlight-part (around tweak-jsx activate)
(if (equal web-mode-content-type "jsx")
(let ((web-mode-enable-part-face nil))
ad-do-it)
ad-do-it))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment