Skip to content

Instantly share code, notes, and snippets.

@egor7
Last active June 13, 2020 20:04
Show Gist options
  • Select an option

  • Save egor7/e5ac4a9c11469394bbad53e419686096 to your computer and use it in GitHub Desktop.

Select an option

Save egor7/e5ac4a9c11469394bbad53e419686096 to your computer and use it in GitHub Desktop.
_emacs 2020-06
; -*-Emacs-Lisp-*-
;
; .bashrc
; # no ncurces glitches
; [[ $- == *i* ]] && stty sane
; export TERM=linux
;
; C-x RET r - revert buffer with coding system
;
; M-x occur - show minibuffer with searched results
; then type C-c C-f to activate folowing mode
;
; C-x C-n / C-u 1 C-x C-n - on and off the goal column
;
; C-x n n / C-x n w - turn on and off region narrowing
;
; M-x hs-minor-mode - turns hide/show mode for code folding
;
; C-u 16 C-x $ / C-x $ - code folding via indent level (selective display)
;
; C-u NUM is equivalent to M-NUM
;
; M-!, M-& - different output for exec commands
;
; C-x C-v - revert-buffer trick
;
(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.
'(abbrev-mode t)
'(auto-save-default nil)
'(auto-save-mode nil)
'(backward-delete-char-untabify-method nil)
'(blink-cursor-mode nil)
'(c-offsets-alist nil)
'(column-number-mode t)
'(compilation-read-command nil)
'(compilation-scroll-output t)
'(confirm-kill-emacs (quote yes-or-no-p))
'(cursor-in-non-selected-windows t)
'(cursor-type (quote box))
'(delete-selection-mode t)
'(global-hi-lock-mode 1)
'(gud-gdb-command-name "gdb --annotate=1")
'(inhibit-startup-screen t)
'(large-file-warning-threshold nil)
'(make-backup-files nil)
'(mark-even-if-inactive t)
'(menu-bar-mode nil)
'(org-agenda-files (quote ("~/.gtd")))
'(require-final-newline nil)
'(safe-local-variable-values
(quote
((buffer-file-coding-system . utf-8-unix)
(nil
(buffer-file-coding-system . utf-8-unix))
(eval setq my-compile-command
(concat "goimports -w . & go fmt & go build" ""))
(eval setq my-compile-command
(concat "goimports -w . & go fmt & go build & go test" ""))
(eval setq my-compile-command
(concat "goimports -w . go fmt & go build & go test" ""))
(eval setq my-compile-command
(concat "go fmt & go build & go test" ""))
(eval setq my-compile-command
(concat "go fmt & go build" ""))
(eval setq my-compile-command
(concat "go fmt & go test" ""))
(eval setq my-compile-command
(concat "go fmt & go run " buffer-file-name))
(eval setq my-compile-command
(concat "go fmt; go run " buffer-file-name))
(eval setq my-compile-command
(concat "go run " buffer-file-name))
(compile-command concat "go run " buffer-file-name))))
'(scroll-bar-mode nil)
'(scroll-conservatively 50)
'(scroll-margin 5)
'(scroll-preserve-screen-position (quote t))
'(show-paren-mode t)
;!; '(tool-bar-mode nil)
'(transient-mark-mode nil)
'(truncate-lines t)
)
;; uniqueue buffer names
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
;; disavle svn, verion control
(setq vc-handled-backends nil)
;; undo
(setq undo-limit 500000)
(setq undo-strong-limit 500000)
(setq undo-outer-limit 50000000)
;; conf-mode
(add-to-list 'auto-mode-alist '("\\.inc$" . conf-mode))
;; hexl-mode
(add-to-list 'auto-mode-alist '("\\.raw$" . hexl-mode))
;; tramp
(require 'tramp)
(setq tramp-default-method "ssh")
;; eshell
(setenv "EMACS" "true")
;; Don't make me type out 'yes' and 'no'
(fset 'yes-or-no-p 'y-or-n-p)
;; Color the code
(require 'font-lock)
(global-font-lock-mode t)
;; Reverting
(global-auto-revert-mode t)
;; disable selection highlighting
(setq transient-mark-mode nil)
;; kill whole line
(setq kill-whole-line t)
;; ****************************************************************************
;; Key bindings
;; ****************************************************************************
;; Tab, see http://superuser.com/questions/268029
(defvar just-tab-keymap (make-sparse-keymap) "Keymap for just-tab-mode")
(define-minor-mode just-tab-mode
"Just want the TAB key to be a TAB"
:global t :lighter " TAB" :init-value 0 :keymap just-tab-keymap
(define-key just-tab-keymap (kbd "TAB") 'indent-for-tab-command))
(global-set-key (kbd "TAB") 'self-insert-command);
; (setq-default indent-tabs-mode nil)
; (setq-default c-electric-flag nil)
;; Undo/Redo
(global-set-key [(ctrl z)] 'undo)
;; C-tab switchs to a next window
(global-set-key [(control tab)] 'other-window)
;; C-up
(global-set-key "\M-n" '(lambda () (interactive) (scroll-up 1)))
(global-set-key "\M-p" '(lambda () (interactive) (scroll-down 1)))
(global-set-key [(control down)] '(lambda () (interactive) (scroll-up 1)))
(global-set-key [(control up)] '(lambda () (interactive) (scroll-down 1)))
(global-set-key [(meta down)] '(lambda () (interactive) (scroll-up 1)))
(global-set-key [(meta up)] '(lambda () (interactive) (scroll-down 1)))
;; Insert time
(require 'time-stamp)
;(setq time-stamp-format "%02d.%02m.%:y")
(setq time-stamp-format "%:y.%02m.%02d")
(defun insert-date ()
(interactive)
(insert (time-stamp-string)))
(defun insert-auth ()
(interactive)
(insert "eshalashnikov"))
(global-set-key "\M-D" 'insert-date )
(global-set-key "\M-A" 'insert-auth)
;; [F9] Run test
(defun test-from-build ()
"save and call compile as make all"
(interactive)
(save-buffer)
(cd "test")
(setenv "EMACS-TEST" "true")
(compile "./test.sh")
(cd "..")
(message "test executed!"))
(defun test-from-test ()
"save and call test"
(interactive)
(save-buffer)
(setenv "EMACS-TEST" "true")
(compile "./test.sh")
(message "test executed!"))
(global-set-key [f9] 'test-from-build)
(global-set-key [(shift f9)] 'test-from-test)
;; http://www.moxleystratton.com/article/dot-emacs
;; Fix the worse part about emacs: indentation craziness
;; 1. When I hit the TAB key, I always want a TAB character inserted
;; 2. Don't automatically indent the line I am editing.
;; 3. When I hit the BACKSPACE key to the right of a TAB character, I want the
;; TAB character deleted-- not replaced with tabwidth-1 spaces.
(defun newline-and-indent-relative ()
"Insert a newline, then indent relative to the previous line."
(interactive "*") (newline) (indent-relative))
;! groovy (defun indent-according-to-mode () ())
(defalias 'newline-and-indent 'newline-and-indent-relative)
; tabs
(eval-after-load "cc-mode"
'(define-key c-mode-map (kbd "TAB") 'self-insert-command))
(eval-after-load "c++-mode"
'(define-key c++-mode-map (kbd "TAB") 'self-insert-command))
(global-set-key (kbd "DEL") 'backward-delete-char)
(setq c-backspace-function 'backward-delete-char)
;; java mode
(defun my-java-mode-hook ()
"Set up java-mode"
;; tabs
(setq indent-tabs-mode nil)
)
(add-hook 'java-mode-hook 'my-java-mode-hook)
;; python mode
(defun my-python-mode-hook ()
"Set up python-mode"
;; tabs
(setq indent-tabs-mode nil)
(setq py-smart-indentation nil)
(setq post-self-insert-hook 'blink-paren-post-self-insert-function)
(setf (cdr python-mode-map) nil)
)
(add-hook 'python-mode-hook 'my-python-mode-hook)
;; lex/yacc mode
(add-to-list 'auto-mode-alist '("\\.l$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.y$" . c++-mode))
;; cc-mode
(require 'cc-mode)
;; Do not check for old-style (K&R) function declarations;
;; this speeds up indenting a lot.
(setq c-recognize-knr-p nil)
;; Switch fromm *.<impl> to *.<head> and vice versa
(defun switch-cc-to-h ()
(interactive)
(when (string-match "^\\(.*\\)\\.\\([^.]*\\)$" buffer-file-name)
(let ((name (match-string 1 buffer-file-name))
(suffix (match-string 2 buffer-file-name)))
(cond ((string-match suffix "c\\|cpp\\|cxx")
(cond ((file-exists-p (concat name ".h"))
(find-file (concat name ".h"))
)
))
((string-match suffix "h\\|hxx")
(cond ((file-exists-p (concat name ".c"))
(find-file (concat name ".c"))
)
((file-exists-p (concat name ".cpp"))
(find-file (concat name ".cpp"))
)
((file-exists-p (concat name ".cxx"))
(find-file (concat name ".cxx"))
)
))))))
(global-set-key [f5] 'switch-cc-to-h)
;; "I always compile my .emacs, saves me about two seconds
;; startuptime. But that only helps if the .emacs.elc is newer
;; than the .emacs. So compile .emacs if it's not."
(defun autocompile nil
"compile itself if ~/.emacs"
(interactive)
(require 'bytecomp)
(if (string= (buffer-file-name) (expand-file-name (concat
default-directory ".emacs")))
(byte-compile-file (buffer-file-name))))
(add-hook 'after-save-hook 'autocompile)
;; Pretty diff mode
(defun newframe ()
(interactive)
(make-frame)
(other-frame 1)
)
( global-set-key "\M-D" 'insert-date )
;(add-hook 'ediff-before-setup-hook 'newframe)
;(add-hook 'ediff-quit-hook 'delete-frame)
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
;(setq ediff-split-window-function 'split-window-vertically)
(setq ediff-split-window-function 'split-window-horizontally)
(defun my-diff (switch)
(cond ((getenv "EMACS-TEST")
(message "=== called from emacs"))
)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff-files file1 file2)))
(add-to-list 'command-switch-alist '("-diff" . my-diff))
(setq-default ediff-forward-word-function 'forward-char)
(put 'scroll-left 'disabled nil)
(defadvice eshell (around eshell-around (name) activate)
"Create a shell buffer named NAME."
(interactive "sName: ")
(setq name (concat "<" name ">"))
ad-do-it
(rename-buffer name)
)
(put 'set-goal-column 'disabled nil)
;; elisp
(setq initial-scratch-message "; M-x lisp-interaction-mode\n; C-j, C-M-x to evaluate\n\n")
;; display-time-format
;; ;; annoing tmux updates
;; (setq display-time-string-forms
;; '((propertize (concat " " 24-hours ":" minutes " "))))
;; (display-time-mode 1)
(put 'narrow-to-region 'disabled nil)
;;
;; Prevent Emacs from extending file when
;; pressing down arrow at end of buffer.
;;
;; not working via (setq-default next-line-add-newlines nil)
(setq require-final-newline nil)
(setq mode-require-final-newline nil)
;; copy current dir
;; from dired mode
;; to external file ~/.emacs.d/cwd
(require 'dired)
;; dired
(setq dired-recursive-copies 'always)
(setq dired-recursive-deletes 'always)
(setq dired-dwim-target t)
(setq dired-listing-switches "-Aalhp")
(setq grep-find-command "find . -type f -name \"*.sql\" -exec grep -nHi -e \"Y\" \"{}\" \";\"")
;(add-hook 'dired-mode-hook 'dired-hide-details-mode)
(require 'ls-lisp)
(setq ls-lisp-use-insert-directory-program nil)
(setq ls-lisp-dirs-first t)
(setq ls-lisp-ignore-case t)
;;(setq ls-lisp-verbosity '(links uid gid))
;!(define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file) ; was dired-advertised-find-file
;!(define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory
(define-key dired-mode-map "c" 'dired-export-cwd)
(defun dired-export-cwd ()
(interactive)
(with-temp-file "~/\.emacs\.d/cwd"
(insert default-directory)))
;; http://stackoverflow.com/questions/19907939/how-can-one-quickly-browse-through-lots-of-files-in-emacs
;; little modification to dired-mode that let's you browse through lots of files
(add-hook 'dired-mode-hook
(lambda()
;(define-key dired-mode-map (kbd "C-o") 'dired-view-current) ; was dired-display-file
(define-key dired-mode-map (kbd "C-n") 'dired-view-next) ; was dired-next-line
(define-key dired-mode-map (kbd "C-p") 'dired-view-previous))) ; was dired-previous-line
(defun dired-view-next ()
"Move down one line and view the current file in another window."
(interactive)
(dired-next-line 1)
(dired-view-current))
(defun dired-view-previous ()
"Move up one line and view the current file in another window."
(interactive)
(dired-previous-line 1)
(dired-view-current))
(defun dired-view-current ()
"View the current file in another window (possibly newly created)."
(interactive)
(if (not (window-parent))
(split-window)) ; create a new window if necessary
(let ((file (dired-get-file-for-visit))
(dbuffer (current-buffer)))
(other-window 1) ; switch to the other window
(unless (equal dbuffer (current-buffer)) ; don't kill the dired buffer
(if (or view-mode (equal major-mode 'dired-mode)) ; only if in view- or dired-mode
(kill-buffer))) ; ... kill it
(let ((filebuffer (get-file-buffer file)))
(if filebuffer ; does a buffer already look at the file
(switch-to-buffer filebuffer) ; simply switch
(view-file file)) ; ... view it
(other-window -1)))) ; give the attention back to the dired buffer
;;
;; === compile-command
;;
;; URL
;; http://www.emacswiki.org/emacs/CompileCommand
;;
;; [F8] Run build
(defvar orig-compile-command "")
(defvar my-compile-command "")
(defun user-save-and-build ()
"save and call compile as make all"
(interactive)
(save-buffer)
(if (string= "" my-compile-command)
(compile "./build.sh" )
(progn
(setq orig-compile-command compile-command)
(setq compile-command my-compile-command)
(call-interactively 'compile)
(setq compile-command orig-compile-command)))
(message "build executed!"))
(global-set-key [f8] 'user-save-and-build)
;; [F6] Run export
(defun user-save-and-export ()
"save and call export"
(interactive)
(save-buffer)
(compile "export.sh" )
(message "export done!"))
(global-set-key [f6] 'user-save-and-export)
(defun my-sql-mode-hook ()
;; (setq outline-regexp "PACK\\|\\(\t\\)*PROCEDUR\\|\\(\t\\)*FUNCTION")
;; (defvar outline-heading-end-regexp)
;; (setq outline-heading-end-regexp "IS\n\\|RETURN\n\\|)\n\\|;\n")
;; (outline-minor-mode)
(setq indent-tabs-mode nil)
(setq tab-width 4)
)
(add-hook 'sql-mode-hook 'my-sql-mode-hook)
;; match parens
(defun goto-match-paren (arg)
"Goto matching paren."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
(global-set-key [(ctrl %)] 'goto-match-paren)
(defun toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd (not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
(define-key ctl-x-4-map "t" 'toggle-window-split)
;; windows
; (autoload 'visual-basic-mode "visual-basic-mode" "Visual Basic mode." t)
; (setq auto-mode-alist (append '(("\\.\\(frm\\|bas\\|vbs\\|cls\\)$" .
; visual-basic-mode)) auto-mode-alist))
; (autoload 'display-time-mode "display-time-mode")
(setq display-time-24hr-format 1)
(display-time-mode 1)
;(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(add-to-list 'load-path "~/.emacs.d/lisp")
;;(require 'solarized)
;(load-theme 'solarized-light)
;(load-theme 'solarized-dark)
; php
(autoload 'php-mode "php-mode" "Major mode for editing php code." t)
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))
; eldoc
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
(add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)
(add-to-list 'same-window-buffer-names "*Help*")
(add-to-list 'same-window-buffer-names "*Apropos*")
(add-to-list 'same-window-buffer-names "*Summary*")
(add-to-list 'same-window-buffer-names "*Buffer List*")
; sql
(add-to-list 'same-window-buffer-names "*SQL*")
(require 'sql)
(defalias 'sql-get-login 'ignore)
; auto-custom
(put 'dired-find-alternate-file 'disabled nil)
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
(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-face-attribute 'region nil :background "#F88")
(defun insert-file-name ()
"Insert the full path file name into the current buffer."
(interactive)
(insert (buffer-file-name (window-buffer (minibuffer-selected-window)))))
;; frame size
(add-to-list 'default-frame-alist '(left . 0)) ; 250
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 53))
(add-to-list 'default-frame-alist '(width . 177)) ;; 1440x900, 177 for left=0, 146
;; oracle sql mode
;;
;; code bellow much better ;; (add-hook 'sql-mode-hook 'sql-highlight-oracle-keywords)
(defun highlight-plsql ()
(when (and (stringp buffer-file-name)
(string-match "\\.\\(psql\\|sql\\|spc\\|bdy\\|prc\\|pck\\|plsql\\|tpl\\)\\'" buffer-file-name))
(sql-set-product 'oracle)
))
(defun highlight-mssql ()
(when (and (stringp buffer-file-name)
(string-match "\\.tsql\\'" buffer-file-name))
(sql-set-product 'ms)
))
(add-hook 'find-file-hook 'highlight-plsql)
(add-hook 'find-file-hook 'highlight-mssql)
(setq auto-mode-alist (append '(("\\.\\(psql\\|sql\\|spc\\|bdy\\|prc\\|pck\\|plsql\\|tpl\\|tsql\\)\\'" . sql-mode))
auto-mode-alist))
(setq electric-indent-mode nil) ; CTRL-J
(electric-indent-mode -1)
;; frame title
(setq frame-title-format
(list ;; (format "%s %%S: %%j " (system-name))
'(buffer-file-name "%f" (dired-directory dired-directory "%b"))))
;; RC shell
;; (modify-coding-system-alist 'file "\\.rc\\'" 'utf-8)
;; ALL !!!
;; (modify-coding-system-alist 'file "\\.*\\'" 'windows-1251)
;;(modify-coding-system-alist 'file "\\.py\\'" 'utf-8)
;; batch mode
(add-to-list 'auto-mode-alist '("\\.sh\\'" . bat-mode))
;; full screen
(global-set-key [M-return] 'toggle-frame-fullscreen)
;(autoload 'tcl-mode "tcl-mode" "Major mode for editing tcl-scripts." t)
(setq auto-mode-alist (append '(("\\.\\(tcl\\|wsh\\)$" . tcl-mode)) auto-mode-alist))
(defvar my-compile-command "")
(put 'my-compile-command 'safe-local-variable (lambda (x) t))
(defun select-all ()
"select and save all"
(interactive)
(save-excursion
(push-mark (point))
(push-mark (point-max) nil t)
(goto-char (point-min))
(kill-ring-save (region-beginning) (region-end)))
(message "all buffer content has been selected!"))
(global-set-key [f5] 'select-all)
(define-abbrev sql-mode-abbrev-table "sss" "" 'sql-sss)
(define-skeleton sql-sss
"Shorthand for SELECT" nil
"SELECT *" \n
> " FROM " _)
(setq skeleton-end-hook nil)
;; basic mode
(autoload 'visual-basic-mode "visual-basic-mode" "Visual Basic mode." t)
(push '("\\.\\(?:frm\\|\\(?:ba\\|cl\\|vb\\)s\\)\\'" . visual-basic-mode) auto-mode-alist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment