Skip to content

Latest commit

 

History

History
596 lines (533 loc) · 16.4 KB

stanhe.org

File metadata and controls

596 lines (533 loc) · 16.4 KB

Emacs Configurations

Base screen

some default settings

;; base config
;;(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
;;(global-linum-mode 1)
(fset 'yes-or-no-p 'y-or-n-p)
;;(setq-default mode-line-format nil)
(setq ring-bell-function 'ignore)
(setq inhibit-splash-screen -1)
(setq auto-save-default nil)
(setq make-backup-files nil)
(setq shell-command-switch "-ic")
(setq initial-scratch-message (concat ";;Happy hacking, " user-login-name "\n\n"))
(setq custom-file "~/.emacs.d/lisp/custom.el")
(load custom-file)
(setq-default abbrev-mode t)
(setq-default my-lisp-path "~/.emacs.d/lisp/")
(add-to-list 'exec-path "~/bin/")
(add-to-list 'load-path my-lisp-path)
(add-to-list 'default-frame-alist '(alpha . (91 . 90)))
;;(set-frame-parameter (selected-frame) 'alpha '(91 . 80))
(setq mac-command-modifier 'control)

(add-to-list 'org-file-apps '("\\.xlsx\\'" . "open %s"))
(add-to-list 'org-file-apps '("\\.docs\\'" . "open -a 'wpsoffice' %s"))
(add-to-list 'auto-mode-alist '("\\.lisp\\'" . lisp-interaction-mode))
(defun sth-open (path link)
  (let ((dir-path (file-name-directory path)))
    (cond
     ((or (string-match-p "AndroidStudioProjects" dir-path) (string-match-p "2.0" dir-path)) (shell-command (concat "open -a 'Android Studio' " dir-path)))
     ((string-match-p "WeChatProjects" dir-path) (shell-command (concat "open -a 'wechatwebdevtools' " dir-path)))
     )))
(add-to-list 'org-file-apps '("\\.git\\'" . sth-open))

Packages and elpa configurations

elpa

;init packages
(when (>= emacs-major-version 24)
  (setq package-archives '(("gnu" . "http://elpa.zilongshanren.com/gnu/")
			     ("melpa" . "http://elpa.zilongshanren.com/melpa/"))))

custom packages

(defvar stanhe/packages '(
			    ;; ========basic=======
			    use-package
			    neotree
			    avy
			    ace-window
			    company
			    company-tabnine
			    hungry-delete
			    counsel
			    evil
			    general
			    smartparens
			    which-key
			    window-numbering
			    popwin
			    counsel-projectile
			    sly
			    color-theme-sanityinc-tomorrow
			    exec-path-from-shell
			    ) "My default packages")

check and install

 (require 'cl-lib)

 (setq package-selected-packages stanhe/packages)

 (defun stanhe/packages-install-p ()
   (cl-loop for pkg in stanhe/packages
	  when (not (package-installed-p pkg)) do (cl-return nil)
	  finally (cl-return t)))

 (unless (stanhe/packages-install-p)
   (message "%s" "Refreshing package database...")
   (package-refresh-contents)
   (dolist (pkg stanhe/packages)
     (when (not (package-installed-p pkg))
	(package-install pkg))))

packages settings and keybindings

(when (memq window-system '(mac ns x))
  (exec-path-from-shell-initialize))

(require 'use-package)

(require 'my-abbrev)

(use-package evil
  :defer 1
  :init
  (general-evil-setup t)
  :config
  (evil-mode 1)
  (nvmap :prefix "SPC"
    "a" 'org-agenda
    "j" 'json-pretty-print
    "tn" 'fast-eshell-pop
    "tm" 'eshell-pop-toggle
    "ts" 'text-scale-adjust
    "q" 'quit-window
    "g" 'sth-open-fastgit
    "c" 'compile
    "o" 'sth-open-mcd
    "SPC" 'random-color-theme
    "r" 'sth-quickrun
    ;;"a" 'quickrun-with-arg
    "bk" 'kill-buffer
    "bs" 'ivy-switch-buffer
    "bb" 'back-to-previous-buffer
    "s" 'show-me-the-colors
    "m" 'counsel-load-theme
    "n" 'display-line-numbers-mode
    "e" 'my-eval-current-end
    "df" 'sth-defun
    "ff" 'counsel-find-file
    "fr" 'counsel-recentf
    "fb" 'open-file-browse
    "ft" 'sth-open-terminal
    "fv" 'open-file-vscode
    "x" 'counsel-M-x
    "<tab>" 'org-show-subtree
    "fm" 'my-config-file)
  (nvmap :prefix ","
    "," 'ace-jump-word-mode'
    "c" 'save-buffers-kill-terminal
    "q" 'delete-window
    "w" 'save-buffer
    "p" 'projectile-command-map
    "v" 'evil-visual-block
    "x1" 'delete-other-windows
    "xo" 'other-window
    "x0" 'delete-window
    "xq" 'delete-window
    "x2" 'split-window-below
    "x3" 'split-window-right
    "xf" 'sth-find-file
    "xm" 'counsel-M-x
    "xr" 'counsel-recentf
    "xb" 'ivy-switch-buffer
    "bb" 'back-to-previous-buffer
    "xB" 'list-buffers
    "xd" 'dired
    "xj" 'dired-jump
    "xp" 'image-dired
    "xs" 'save-buffer
    "xc" 'save-buffers-kill-terminal
    "xk" 'kill-buffer
    "xe" 'eval-last-sexp

    "eb" 'eval-buffer
    "aw" 'ace-swap-window
    "ag" 'counsel-ag
    "gf" 'counsel-git
    "gg" 'counsel-git-grep
    "go" 'counsel-git-log
    "gt" 'gradle-task
    "gr" 'gradle-run
    "oa" 'org-agenda

    "nf" 'neotree-find
    "nt" 'neotree-toggle
    )
  )

(use-package neotree
  :defer 3
  :config
  (setq neo-smart-open t)
  (nvmap :status '(normal emacs)
    :keymaps 'neotree-mode-map
    "s" 'neotree-hidden-file-toggle
    "g" 'neotree-refresh
    "d" 'neotree-delete-node
    "r" 'neotree-rename-node
    "R" 'neotree-change-root
    "c" 'neotree-create-node
    "C" 'neotree-copy-node
    "v" 'neotree-enter-vertical-split
    "h" 'neotree-enter-horizontal-split
    "a" 'neotree-enter-ace-window
    "SPC" 'neotree-quick-look
    "TAB" 'neotree-enter
    "RET" 'neotree-enter
    "q" 'neotree-hide
    ))

(use-package image-mode
  :defer 6
  :config
  (nvmap :status '(normal emacs)
    :keymaps '(image-mode-map)
    "n" 'image-next-file
    "p" 'image-previous-file
    "h" 'image-transform-fit-to-height
    "w" 'image-transform-fit-to-width
    "s" 'image-transform-reset
    "r" 'image-rotate
    ))

(use-package counsel
  :defer 3
  :init
  (setq ivy-use-virtual-buffers t
	  ivy-count-format "(%d/%d) "
	  ivy-height 20
	  ivy-extra-directories nil
	  enable-recursive-minibuffers t)
  :config
  (ivy-mode 1)
  (ivy-set-actions
   'counsel-find-file
   '(("m" delete-file "delete")
     ("f" (lambda (x) (shell-command (concat "open " x))) "open in finder.")
     ("t" (lambda (x) (shell-command (concat "open " x " -a Terminal.app"))) "open in terminal.")
     ("v" (lambda (x) (shell-command (concat "open " x " -a 'Visual Studio Code'"))) "open in vscode.")
     ))
  (global-set-key "\C-s" 'swiper)
  (global-set-key (kbd "C-c C-r") 'ivy-resume)
  (global-set-key (kbd "<f6>") 'ivy-resume)
  (global-set-key (kbd "M-x") 'counsel-M-x)
  (global-set-key (kbd "C-x C-f") 'counsel-find-file)
  (global-set-key (kbd "<f1> f") 'counsel-describe-function)
  (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
  (global-set-key (kbd "<f1> l") 'counsel-find-library)
  ;;(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
  (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
  (global-set-key (kbd "C-c g") 'counsel-git)
  (define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history))

(use-package company
  :defer 3
  :init
  (global-company-mode 1)
  (setq company-idle-delay 0.1
	  company-minimum-prefix-length 1)
  :hook(after-init-hook . global-company-mode)
  :config
  (nvmap
    :states '(insert)
    "C-n" 'company-select-next
    "C-p" 'company-select-previous))

(use-package company-tabnine
  :ensure t
  :defer 3
  :config
  (add-to-list 'company-backends #'company-tabnine))

(use-package hungry-delete
  :defer 3
  :config
  (global-hungry-delete-mode))

(use-package org
  :defer 3
  :init
  (setq
   org-src-fontify-natively t
   org-log-done 'time
   org-agenda-files '("~/.org/")
   org-todo-keywords '((sequence "TODO(t)" "|" "DONE(d)")
			 (sequence "REPORT(r)" "BUG(b)" "KNOWNCAUSE(k)" "|" "FIXED(f)")
			 (sequence "DELAY(l)" "|" "CANCELED(c)")
			 (sequence "|" "NOTE(n)" "NEXT(x)"))
   org-confirm-babel-evaluate nil
   org-agenda-include-diary t)
  :config
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((emacs-lisp . t)
     (python . t)
     (shell . t)
     (perl . t)
     (ruby . t)
     (sqlite . t)))
  (add-hook 'org-mode-hook
	      (lambda ()
		(defun sth-quickrun()
		  (interactive)
		  (org-ctrl-c-ctrl-c)))))


(use-package smartparens-config
  :defer 5
  :config
  (show-paren-mode)
  (smartparens-global-mode)
  (sp-local-pair '(emacs-lisp-mode lisp-interaction-mode) "'" nil :actions nil))

(use-package which-key
  :defer 1
  :config
  (which-key-mode 1))

(use-package window-numbering
  :defer 3
  :config
  (window-numbering-mode 1))

(use-package popwin
  :defer 3
  :config
  (popwin-mode 1))

(use-package dired-x
  :defer 3)
(use-package dired
  :defer 3
  :init
  (setq dired-recursive-deletes 'always
	  dired-recursive-copies 'always
	  dired-dwim-target t)
  :config
  (put 'dired-find-alternate-file 'disabled nil)
  (define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file))

(use-package ace-window
  :defer 2)

(use-package projectile
  :defer 3
  :init
  (setq projectile-completion-system 'ivy)
  :config
  (projectile-mode))

(use-package quickrun
  :defer 3
  :config
  (nvmap :status '(normal emacs)
    :keymaps 'quickrun--mode-map
    "q" 'quit-window
    "k" 'quickrun--kill-running-process))

(use-package avy
  :defer 1
  :config
  (global-set-key (kbd "C-;") 'avy-goto-word-or-subword-1))

(use-package sly
  :defer 3
  :config
  (setq inferior-lisp-program "/usr/local/bin/sbcl"))

Custom Functions

adb functions

(defun adb-home ()
  (interactive)
  (shell-command "adb shell input keyevent 3"))

(defun adb-back ()
  (interactive)
  (shell-command "adb shell input keyevent 4"))

(defun adb-cmd()
  (interactive)
  (let ((input (read-string "inupt cmd: ")))
    (shell-command (concat "adb shell " input ))))

(defun adb-input()
  (interactive)
  (let ((input (read-string "inupt string: ")))
    (shell-command (concat "adb shell input text " input ))))

(defun adb-sigle-del()
  (interactive)
  (shell-command "adb shell input keyevent 67"))

(defun adb-mult-del()
  (interactive)
  (dotimes (number 10)
    (shell-command "adb shell input keyevent 67")))

(defun adb-enter()
  (interactive)
  (shell-command "adb shell input keyevent 66"))

(defun adb-next()
  (interactive)
  (shell-command "adb shell input keyevent 61"))

(define-key-after
  global-map
  [menu-bar mymenu]
  (cons "STH-CMD" (make-sparse-keymap ""))
  'tools)

(define-key
  global-map
  [menu-bar mymenu  input]
  '("adb-input" . adb-input))

(define-key
  global-map
  [menu-bar mymenu back]
  '("adb-back" . adb-back))

(define-key
  global-map
  [menu-bar mymenu  home]
  '("adb-home" . adb-home))

(define-key
  global-map
  [menu-bar mymenu  cmd]
  '("adb-cmd" . adb-cmd))

my functions

 (defun my-config-file ()
   "my config file"
   (interactive)
   (find-file "~/.emacs.d/stanhe.org"))

 (defun back-to-previous-buffer ()
   (interactive)
   (switch-to-buffer nil))

 (define-advice show-paren-function (:around (fn) fix-show-paren-function)
   "Highlight enclosing parens."
   (cond ((looking-at-p "\\s(") (funcall fn))
	  (t (save-excursion
	       (ignore-errors (backward-up-list))
	       (funcall fn)))))

 (defun show-me-the-colors ()
   "random color theme"
   (interactive)
   (cl-loop do
	  (random-color-theme)
	  (unless (sit-for 3)
	    (keyboard-quit))))

 (defun random-color-theme ()
   "Random color theme."
   (interactive)
   (unless (featurep 'counsel) (require 'counsel))
   (let* ((available-themes (mapcar 'symbol-name (custom-available-themes)))
	   (theme (seq-random-elt available-themes)))
     (counsel-load-theme-action theme)
     (message "Color theme [%s] loaded." theme)))

 (defun init-my-load-path()
   "add lisp/ load path"
   (dolist (path (directory-files my-lisp-path t "[^.]"))
     (when (file-directory-p path)
	(add-to-list 'load-path path))))

 (defun choose-theme-by-time ()
   "choose the theme by time now."
   (interactive)
   (let ((now (string-to-number (format-time-string "%H"))))
     (cond ((< now 6) (load-theme 'sanityinc-tomorrow-blue 1))
	    ((< now 22) (load-theme 'sanityinc-tomorrow-day 1))
	    (t (load-theme 'sanityinc-tomorrow-night 1)))))

 (defun eshell/clear ()
   "clear buffer of eshell."
   (let ((eshell-buffer-maximum-lines 0)) (eshell-truncate-buffer)))

 (defun my-eval-current-end()
   "goto current line tail ,and eval it"
   (interactive)
   (save-excursion
     (end-of-line)
     (call-interactively 'eval-last-sexp)))

 (defmacro _loading(seconds msg &optional action)
    "my custom loading."
    `(dotimes-with-progress-reporter
	  (k (* ,seconds 100) (when ,action ,action))
	  ,msg
      (sit-for 0.01)))

 (defun open-file-browse()
    "open file in browse"
    (interactive)
    (shell-command (concat "open " (expand-file-name default-directory))))

 (defun open-file-vscode()
    "open file in vscode"
    (interactive)
    (shell-command (concat "open " (buffer-file-name)  " -a 'Visual Studio Code'")))

 (defun sth-find-file()
   "my find file"
   (interactive)
   (counsel-find-file "~/"))

 (defun sth-quickrun()
   "my custom quickrun,need define each time!"
   (interactive)
   (message "please use your custom sth-quickrun!"))

 (defun sth-open-mcd ()
   "open my cd ~/.mcd_path"
   (interactive)
   (setq my-cd "~/.mcd_path")
   ;; read file
   (defun read-lines (file)
     (with-temp-buffer
	(insert-file-contents file)
	(split-string (buffer-string) "\n" t)))
   (let* ((lines (read-lines my-cd))(num 0) (item nil) (paths (list)))
     (while (< num (length lines))
	(setq item (nth num lines))
	(if (eq (string-match "#" item) 0)
	    nil
	  (progn
	    (setq item (replace-regexp-in-string ".+:" "" item))
	    (setq paths (cons item paths))))
	(setq num (1+ num)))
     (ivy-read "open path of ~/.mcd_path in finder: "
		(reverse paths)
		:action '(1 ("f" (lambda (x) (shell-command (concat "open " x))) "open in finder")
			    ("t" (lambda (x) (shell-command (concat "open " x " -a Terminal.app"))) "open in terminal"))
		)))

 (defun sth-open-terminal ()
   "open current directory in terminal"
   (interactive)
   (let ((path (expand-file-name default-directory)))
     (when (string-equal "/" path)
	  (setq path "~"))
     (shell-command (concat "open " path " -a Terminal.app"))))

 (defun 500w ()
   "random for 500W"
   (interactive)
   (let ((i nil))
     (while (< (length i) 6)
	(setq random_i (1+ (random 33)))
	(add-to-list 'i random_i))
     (setq blue_ball (1+ (random 16)))
     (setq red_ball (mapconcat 'number-to-string (reverse (sort i '>)) " "))
     (message "===> 500W: %s | %d" red_ball blue_ball)))

 (defun sth-org-babel-highlight-result ()
   "Highlight the result of the current source block.
   Adapt from `org-babel-remove-result'."
   (interactive)
   (let ((location (org-babel-where-is-src-block-result nil nil)))
     (when location
	(save-excursion
	  (goto-char location)
	  (when (looking-at (concat org-babel-result-regexp ".*$"))
	    (pulse-momentary-highlight-region
	     (1+ (match-end 0))
	     (progn (forward-line 1) (org-babel-result-end))))))))

 (add-hook 'org-babel-after-execute-hook
	    (defun sth-org-babel-highlight-result-maybe ()
	      (when (eq this-command 'org-ctrl-c-ctrl-c)
		(sth-org-babel-highlight-result))))

 (defun sth-open-fastgit(is_replace)
   (interactive
    (list (if current-prefix-arg "no_replace" "replace")))
   (defun sth-open-fast-shell-open(arg)(shell-command (concat "open " arg)))
   (when mark-active
     (let ((region (funcall region-extract-function nil)))
	(deactivate-mark)
	(if (string-equal is_replace "replace")
	    (cond
	     ((string-match "github.com" region)(sth-open-fast-shell-open (replace-regexp-in-string "github.com" "hub.fastgit.org" region)))
	     ((string-match "hub.fastgit.org" region)(sth-open-fast-shell-open (replace-regexp-in-string "hub.fastgit.org" "github.com" region)))
	     (t (sth-open-fast-shell-open  region)))
	  (sth-open-fast-shell-open region)))))

cover with better keybindings

(global-set-key (kbd "C-h") 'delete-backward-char)
(global-set-key (kbd "C-SPC") 'delete-window)
(global-set-key (kbd "M-/") 'hippie-expand)
(global-set-key (kbd "<C-return>") (lambda ()
				       (interactive)
				       (progn
					 (end-of-line)
					 (newline-and-indent))))