Re: Emacs tips and tricks
Posted: Tue Oct 10, 2017 6:31 am
^ Cool stuff. I always use writeroom-mode, never tried olivetti-mode.
Code: Select all
;; Parts of the code are taken from https://github.com/domtronn/all-the-icons.el/wiki/Mode-Line
;; Requires all-the-icons packages and fonts.
;; franksinistra -- 2017
(provide 'pretty-modeline)
(defun mode-line-fill-right (face reserve)
"Leave reserve space to the right of the mode-line)
(unless reserve
(setq reserve 20))
(when (and window-system (eq 'right (get-scroll-bar-mode)))
(setq reserve (- reserve 3)))
(propertize " "
'display `((space :align-to (- (+ right right-fringe right-margin) ,reserve)))))
(defun mode-line-fill-center (face reserve)
"Return empty space to the center of remaining space."
(unless reserve
(setq reserve 20))
(when (and window-system (eq 'right (get-scroll-bar-mode)))
(setq reserve (- reserve 3)))
(propertize " "
'display `((space :align-to (- (+ center (.5 . right-margin)) ,reserve
(.5 . left-margin))))))
;; Set length and padding
(defconst RIGHT_PADDING 1)
(defun reserve-left/middle ()
(/ (length (format-mode-line mode-line-align-middle)) 2))
(defun reserve-middle/right ()
(+ RIGHT_PADDING (length (format-mode-line mode-line-align-right))))
;; Mode-line functions, requires all-the-icons package and fonts
(defun custom-modeline-mode-icon ()
"Display major-mode associated with buffer"
(let ((icon (all-the-icons-icon-for-buffer)))
(unless (symbolp icon)
(propertize icon 'help-echo (format "Major-mode: ` %s'" major-mode)
'display '(raise 0.0)
'face `(:height 1.2 :family ,(all-the-icons-icon-family-for-buffer))))))
;;;; VC icons for modeline
(defun -custom-modeline-github-vc ()
(let ((branch (mapconcat 'concat (cdr (split-string vc-mode "[:-]")) "-")))
(concat
(propertize (format " %s" (all-the-icons-alltheicon "git")) 'face `(:height 1.2) 'display '(raise -0.1))
" · "
(propertize (format "%s" (all-the-icons-octicon "git-branch"))
'face `(:height 1.3 :family ,(all-the-icons-octicon-family))
'display '(raise -0.1))
(propertize (format " %s" branch) 'face `(:height 0.9)))))
(defun -custom-modeline-svn-vc ()
(let ((revision (cadr (split-string vc-mode "-"))))
(concat
(propertize (format " %s" (all-the-icons-faicon "cloud")) 'face `(:height 1.2) 'display '(raise -0.1))
(propertize (format " · %s" revision) 'face `(:height 0.9)))))
(defun custom-modeline-icon-vc ()
(when vc-mode
(cond
((string-match "Git[:-]" vc-mode) (-custom-modeline-github-vc))
((string-match "SVN-" vc-mode) (-custom-modeline-svn-vc))
(t (format "%s" vc-mode)))))
;;;; Flycheck icons for modeline
(defun custom-modeline-flycheck-status ()
(let* ((text (pcase flycheck-last-status-change
(`finished (if flycheck-current-errors
(let ((count (let-alist (flycheck-count-errors flycheck-current-errors)
(+ (or .warning 0) (or .error 0)))))
(format "✖ %s Issue%s" count (unless (eq 1 count) "s")))
"✔ No Issues"))
(`running "⟲ Running")
(`no-checker "⚠ No Checker")
(`not-checked "✖ Disabled")
(`errored "⚠ Error")
(`interrupted "⛔ Interrupted")
(`suspicious ""))))
(propertize text
'help-echo "Show Flycheck Errors"
'mouse-face '(:box 1)
'local-map (make-mode-line-mouse-map
'mouse-1 (lambda () (interactive) (flycheck-list-errors))))))
;; Set-up mode-line formats based on location
(setq mode-line-align-left
'(""
"%2 "
(:eval (custom-modeline-mode-icon))
"%2 "
(:propertize "%b" face mode-line-buffer-id)
"%2 "
(:eval (when (bound-and-true-p flycheck-mode)
(custom-modeline-flycheck-status)))
" "))
(setq mode-line-align-middle
'(""
(:eval (custom-modeline-icon-vc))
"%3 "
(:eval
(when (eql (buffer-modified-p) t)
;; propertize adds metadata to text, so you can add colours and formatting, amongst other things
(propertize (all-the-icons-faicon "chain-broken")
'face `(:family ,(all-the-icons-faicon-family)
:height 1.2))))
" "
(:eval
(when (eql buffer-read-only t)
(propertize (all-the-icons-faicon "lock")
'face `(:family ,(all-the-icons-faicon-family)
:height 1.2))))
""))
(setq mode-line-align-right
'(""
mode-line-misc-info
"%2 "
(:eval (format "%%l/%d : %%c " (line-number-at-pos (point-max))))))
(setq-default mode-line-format
"Set-up mode-line"
(list
mode-line-align-left
'(:eval (mode-line-fill-center 'mode-line
(reserve-left/middle)))
mode-line-align-middle
'(:eval
(mode-line-fill-right 'mode-line
(reserve-middle/right)))
mode-line-align-right))
Code: Select all
;;; The mode-line
(line-number-mode)
(column-number-mode)
;; Show buffer position percentage starting from top
(validate-setq mode-line-percent-position '(-3 "%o"))
;; Increase mode-line size with a border (box) of the same colour and
;; reduce font size by tweaking height
(set-face-attribute 'mode-line nil
:inverse-video nil
:height 0.9
:box '(:line-width 6 :color "#373b41" :style nil))
(set-face-attribute 'mode-line-inactive nil
:inverse-video nil
:height 0.9
:box '(:line-width 8 :color "#282a2e" :style nil))
(defvar mu-eyebrowse-mode-line
'(:propertize
(:eval
(when (bound-and-true-p eyebrowse-mode)
(let* ((num (eyebrowse--get 'current-slot))
(tag (when num
(nth 2 (assoc num (eyebrowse--get 'window-configs)))))
(str (concat
" "
(if (and tag (< 0 (length tag))) tag
(when num (int-to-string num)))
" ")))
str))))
"Mode line format for Eyebrowse.")
(put 'mu-eyebrowse-mode-line 'risky-local-variable t)
(defvar mu-projectile-mode-line
'(:propertize
(:eval (when (ignore-errors (projectile-project-root))
(concat " " (projectile-project-name))))
face font-lock-constant-face)
"Mode line format for Projectile.")
(put 'mu-projectile-mode-line 'risky-local-variable t)
(defvar mu-vc-mode-line
'(" " (:propertize
;; Strip the backend name from the VC status information
(:eval (let ((backend (symbol-name (vc-backend (buffer-file-name)))))
(substring vc-mode (+ (length backend) 2))))
face font-lock-keyword-face))
"Mode line format for VC Mode.")
(put 'mu-vc-mode-line 'risky-local-variable t)
(setq-default mode-line-format
'("%e" mode-line-front-space
mu-eyebrowse-mode-line ; Current workspace
"· "
;; Standard info about the current buffer
mode-line-mule-info
mode-line-client
mode-line-modified
mode-line-remote
mode-line-frame-identification
mode-line-buffer-identification " " mode-line-position
;; Some specific information about the current buffer:
mu-projectile-mode-line ; Project information
(vc-mode mu-vc-mode-line) ; VC information
(multiple-cursors-mode mc/mode-line) ; Number of cursors
;; And the modes
" " mode-line-modes mode-line-end-spaces))
Code: Select all
(defun mu-counsel-search-project (initial-input &optional use-current-dir)
"Search using `counsel-rg' from the project root for INITIAL-INPUT.
If there is no project root, or if the prefix argument
USE-CURRENT-DIR is set, then search from the current directory
instead."
(interactive (list (thing-at-point 'symbol)
current-prefix-arg))
(let ((current-prefix-arg)
(dir (if use-current-dir
default-directory
(condition-case err
(projectile-project-root)
(error default-directory)))))
(funcall 'counsel-rg initial-input dir)))
(bind-key* "M-?" #'mu-counsel-search-project)
Code: Select all
(defun mu-sql-switch-to-sqli ()
"Switch to SQLi buffer."
(interactive)
(unless (and sql-buffer
(buffer-live-p (get-buffer sql-buffer)))
(sql-set-sqli-buffer))
(pop-to-buffer sql-buffer))
Code: Select all
(defun mu--start-with-p (symbol)
"Check if there is a SYMBOL at (point)."
(interactive)
(equal symbol (buffer-substring-no-properties (point) (+ 1 (point)))))
(defun mu--live-delete-and-extract-sexp ()
"Delete the sexp and return it."
(interactive)
(let* ((begin (point)))
(forward-sexp)
(let* ((result (buffer-substring-no-properties begin (point))))
(delete-region begin (point))
result)))
(defun mu--wrap-with-symbols (opening closing)
"Wrap current coll within OPENING and CLOSING."
(interactive)
(insert opening
(substring (mu--live-delete-and-extract-sexp) 1 -1)
closing))
(defun mu-live-cycle-clj-coll ()
"Convert the coll at (point) from (x) -> {x} -> [x] -> (x)."
(interactive)
(let* ((original-point (point)))
(while (and (> (point) 1)
(not (mu--start-with-p "("))
(not (mu--start-with-p "{"))
(not (mu--start-with-p "[")))
(backward-char))
(cond
((mu--start-with-p "(")
(mu--wrap-with-symbols "{" "}"))
((mu--start-with-p "{")
(mu--wrap-with-symbols "[" "]"))
((mu--start-with-p "[")
(mu--wrap-with-symbols "(" ")"))
((equal 1 (point))
(message "Beginning of file reached, this was probably a mistake.")))
(goto-char original-point)))