You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2008 lines
66 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. ;; TODO: Configure pony-tpl-mode
  116. pony-mode
  117. gited
  118. highlight-indentation
  119. diminish
  120. editorconfig
  121. editorconfig-custom-majormode
  122. git-command
  123. prompt-text
  124. ;; 10sr repository
  125. ;; 10sr-extras
  126. terminal-title
  127. recentf-show
  128. dired-list-all-mode
  129. pack
  130. set-modeline-color
  131. read-only-only-mode
  132. smart-revert
  133. autosave
  134. ;;window-organizer
  135. ilookup
  136. pasteboard
  137. awk-preview
  138. recently
  139. ))
  140. (when (safe-require-or-eval 'package)
  141. (setq package-archives
  142. `(,@package-archives
  143. ("melpa" . "https://melpa.org/packages/")
  144. ;; Somehow fails to download via https
  145. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  146. (package-initialize)
  147. (defun my-auto-install-package ()
  148. "Install packages semi-automatically."
  149. (interactive)
  150. (package-refresh-contents)
  151. (mapc (lambda (pkg)
  152. (or (package-installed-p pkg)
  153. (package-install pkg)))
  154. 10sr-package-list))
  155. )
  156. ;; (lazy-load-eval 'sudoku)
  157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  158. ;; my-idle-hook
  159. (defvar my-idle-hook nil
  160. "Hook run when idle for several secs.")
  161. (defvar my-idle-hook-sec 5
  162. "Second to run `my-idle-hook'.")
  163. (run-with-idle-timer my-idle-hook-sec
  164. t
  165. (lambda ()
  166. (run-hooks 'my-idle-hook)))
  167. ;; (add-hook 'my-idle-hook
  168. ;; (lambda ()
  169. ;; (message "idle hook message")))
  170. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  171. ;; start and quit
  172. (setq inhibit-startup-message t)
  173. (setq confirm-kill-emacs 'y-or-n-p)
  174. (setq gc-cons-threshold (* 1024 1024 16))
  175. (setq garbage-collection-messages nil)
  176. (when window-system
  177. (add-to-list 'default-frame-alist '(cursor-type . box))
  178. (add-to-list 'default-frame-alist '(background-color . "white"))
  179. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  180. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  181. ;; does not work?
  182. )
  183. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  184. (menu-bar-mode 1)
  185. (define-key ctl-x-map "m" 'menu-bar-open)
  186. (and (fboundp 'tool-bar-mode)
  187. (tool-bar-mode 0))
  188. (and (fboundp 'set-scroll-bar-mode)
  189. (set-scroll-bar-mode nil))
  190. (call-after-init
  191. (lambda ()
  192. (message "%s %s" invocation-name emacs-version)
  193. (message "Invocation directory: %s" default-directory)
  194. (message "%s was taken to initialize emacs." (emacs-init-time))
  195. (view-echo-area-messages)
  196. ;; (view-emacs-news)
  197. ))
  198. (cd ".") ; when using windows use / instead of \ in `default-directory'
  199. ;; locale
  200. (set-language-environment "Japanese")
  201. (set-default-coding-systems 'utf-8-unix)
  202. (prefer-coding-system 'utf-8-unix)
  203. (setq system-time-locale "C")
  204. ;; my prefix map
  205. (defvar my-prefix-map nil
  206. "My prefix map.")
  207. (define-prefix-command 'my-prefix-map)
  208. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  209. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  210. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  211. ;; (comint-show-maximum-output)
  212. ;; kill scratch
  213. (call-after-init (lambda ()
  214. (let ((buf (get-buffer "*scratch*")))
  215. (when buf
  216. (kill-buffer buf)))))
  217. ;; modifier keys
  218. ;; (setq mac-option-modifier 'control)
  219. ;; display
  220. (setq visible-bell t)
  221. (setq ring-bell-function 'ignore)
  222. (mouse-avoidance-mode 'banish)
  223. (setq echo-keystrokes 0.1)
  224. (defun reload-init-file ()
  225. "Reload Emacs init file."
  226. (interactive)
  227. (when (and user-init-file
  228. (file-readable-p user-init-file))
  229. (load-file user-init-file)))
  230. (safe-require-or-eval 'session)
  231. ;; server
  232. (set-variable 'server-name (concat "server"
  233. (number-to-string (emacs-pid))))
  234. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  235. ;; In Darwin environment, init fails with message like 'Service name too long'
  236. ;; when server-use-tcp is nil
  237. (when (or (eq system-type
  238. 'cygwin)
  239. (eq system-type
  240. 'darwin))
  241. (set-variable 'server-use-tcp t))
  242. ;; MSYS2 fix
  243. (when (eq system-type
  244. 'windows-nt)
  245. (setq shell-file-name
  246. (executable-find "bash"))
  247. '(setq function-key-map
  248. `(,@function-key-map ([pause] . [?\C-c])
  249. ))
  250. (define-key key-translation-map
  251. (kbd "<pause>")
  252. (kbd "C-c"))
  253. '(keyboard-translate [pause]
  254. (kbd "C-c")p)
  255. ;; TODO: move to other place later
  256. (when (not window-system)
  257. (setq interprogram-paste-function nil)
  258. (setq interprogram-cut-function nil)))
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260. ;; global keys
  261. (global-set-key (kbd "<up>") 'scroll-down-line)
  262. (global-set-key (kbd "<down>") 'scroll-up-line)
  263. (global-set-key (kbd "<left>") 'scroll-down)
  264. (global-set-key (kbd "<right>") 'scroll-up)
  265. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  266. (global-set-key (kbd "C-\\") help-map)
  267. (define-key ctl-x-map (kbd "DEL") help-map)
  268. (define-key ctl-x-map (kbd "C-h") help-map)
  269. (define-key help-map "a" 'apropos)
  270. ;; disable annoying keys
  271. (global-set-key [prior] 'ignore)
  272. (global-set-key (kbd "<next>") 'ignore)
  273. (global-set-key [menu] 'ignore)
  274. (global-set-key [down-mouse-1] 'ignore)
  275. (global-set-key [down-mouse-2] 'ignore)
  276. (global-set-key [down-mouse-3] 'ignore)
  277. (global-set-key [mouse-1] 'ignore)
  278. (global-set-key [mouse-2] 'ignore)
  279. (global-set-key [mouse-3] 'ignore)
  280. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  281. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  282. (when (safe-require-or-eval 'which-key)
  283. (which-key-mode))
  284. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  285. ;; editor
  286. (setq kill-whole-line t)
  287. (setq scroll-conservatively 35
  288. scroll-margin 2)
  289. (setq-default major-mode 'text-mode)
  290. (setq next-line-add-newlines nil)
  291. (setq kill-read-only-ok t)
  292. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  293. ;; (setq-default line-spacing 0.2)
  294. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  295. ;; (setq-default tab-width 4)
  296. (setq-default indent-tabs-mode nil)
  297. (setq-default indent-line-function 'indent-to-left-margin)
  298. ;; (setq-default indent-line-function nil)
  299. (setq-default truncate-lines nil)
  300. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  301. (delete-selection-mode 1)
  302. (cua-mode 0)
  303. (setq line-move-visual nil)
  304. (setq create-lockfiles nil)
  305. (add-hook 'before-save-hook
  306. 'time-stamp)
  307. ;; Add Time-stamp: <> to insert timestamp there
  308. (set-variable 'time-stamp-format
  309. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  310. ;; key bindings
  311. ;; moving around
  312. ;;(keyboard-translate ?\M-j ?\C-j)
  313. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  314. (define-key esc-map "p" 'backward-paragraph)
  315. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  316. (define-key esc-map "n" 'forward-paragraph)
  317. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  318. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  319. (global-set-key (kbd "C-<left>") 'scroll-down)
  320. (global-set-key (kbd "C-<right>") 'scroll-up)
  321. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  322. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  323. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  324. ;; C-h and DEL
  325. (global-set-key (kbd "C-h") (kbd "DEL"))
  326. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  327. (global-set-key (kbd "C-m") 'newline-and-indent)
  328. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  329. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  330. (define-key esc-map "u" 'undo)
  331. (define-key esc-map "i" (kbd "ESC TAB"))
  332. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  333. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  334. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  335. (require 'page-ext nil t)
  336. (when (safe-require-or-eval 'page-break-lines)
  337. (global-page-break-lines-mode 1))
  338. (when (safe-require-or-eval 'git-gutter)
  339. (custom-set-variables
  340. '(git-gutter:lighter " Gttr"))
  341. (custom-set-variables
  342. '(git-gutter:update-interval 2))
  343. (custom-set-variables
  344. '(git-gutter:unchanged-sign " "))
  345. (when (>= (display-color-cells)
  346. 256)
  347. (let ((c "color-233"))
  348. (set-face-background 'git-gutter:modified c)
  349. (set-face-background 'git-gutter:added c)
  350. (set-face-background 'git-gutter:deleted c)
  351. (set-face-background 'git-gutter:unchanged c)))
  352. (global-git-gutter-mode 1)
  353. )
  354. ;; (when (safe-require-or-eval 'fancy-narrow)
  355. ;; (fancy-narrow-mode 1))
  356. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  357. ;; title and mode-line
  358. (when (safe-require-or-eval 'terminal-title)
  359. ;; if TERM is not screen use default value
  360. (if (getenv "TMUX")
  361. ;; if use tmux locally just basename of current dir
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. (if (and (let ((tty-type (frame-parameter nil
  366. 'tty-type)))
  367. (and tty-type
  368. (equal (car (split-string tty-type
  369. "-"))
  370. "screen")))
  371. (not (getenv "SSH_CONNECTION")))
  372. (set-variable 'terminal-title-format
  373. '((file-name-nondirectory (directory-file-name
  374. default-directory))))
  375. ;; seems that TMUX is used locally and ssh to remote host
  376. (set-variable 'terminal-title-format
  377. `("em:"
  378. ,user-login-name
  379. "@"
  380. ,(car (split-string (system-name)
  381. "\\."))
  382. ":"
  383. default-directory))
  384. )
  385. )
  386. (terminal-title-mode))
  387. (setq eol-mnemonic-dos "\\r\\n")
  388. (setq eol-mnemonic-mac "\\r")
  389. (setq eol-mnemonic-unix "")
  390. (which-function-mode 1)
  391. (line-number-mode 0)
  392. (column-number-mode 0)
  393. (size-indication-mode 0)
  394. (setq mode-line-position
  395. '(:eval (format "L%%l/%d%s:C%%c"
  396. (count-lines (point-max)
  397. (point-min))
  398. (if (buffer-narrowed-p)
  399. "[N]"
  400. "")
  401. )))
  402. (when (safe-require-or-eval 'diminish)
  403. ;; FIXME: Eval after enabling mode
  404. (diminish 'recently-mode)
  405. (diminish 'editorconfig-mode)
  406. (diminish 'auto-highlight-symbol-mode)
  407. (diminish 'global-whitespace-mode)
  408. (diminish 'which-key-mode)
  409. (diminish 'page-break-lines-mode)
  410. (diminish 'highlight-indentation-mode))
  411. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  412. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  413. ;; minibuffer
  414. (setq insert-default-directory t)
  415. (setq completion-ignore-case t
  416. read-file-name-completion-ignore-case t
  417. read-buffer-completion-ignore-case t)
  418. (setq resize-mini-windows t)
  419. (temp-buffer-resize-mode 1)
  420. (savehist-mode 1)
  421. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  422. (set-variable 'help-at-pt-display-when-idle t)
  423. (fset 'yes-or-no-p 'y-or-n-p)
  424. ;; complete symbol when `eval'
  425. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  426. (define-key minibuffer-local-map (kbd "C-u")
  427. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  428. ;; I dont know these bindings are good
  429. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  430. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  431. (when (safe-require-or-eval 'minibuffer-line)
  432. (set-face-underline 'minibuffer-line nil)
  433. (set-variable 'minibuffer-line-refresh-interval
  434. 25)
  435. ;; Set idle timer
  436. (defvar my-minibuffer-line--idle-timer nil)
  437. (defvar minibuffer-line-mode)
  438. (add-hook 'minibuffer-line-mode-hook
  439. (lambda ()
  440. (when my-minibuffer-line--idle-timer
  441. (cancel-timer my-minibuffer-line--idle-timer)
  442. (setq my-minibuffer-line--idle-timer nil))
  443. (when minibuffer-line-mode
  444. (setq my-minibuffer-line--idle-timer
  445. (run-with-idle-timer 0.5
  446. t
  447. 'minibuffer-line--update)))))
  448. (set-variable 'minibuffer-line-format
  449. `(,(concat user-login-name
  450. "@"
  451. (car (split-string (system-name)
  452. "\\."))
  453. ":")
  454. (:eval (abbreviate-file-name (or buffer-file-name
  455. default-directory)))
  456. (:eval (and (fboundp 'git-ps1-mode-get-current)
  457. (git-ps1-mode-get-current " [GIT:%s]")))
  458. " "
  459. (:eval (format-time-string display-time-format))))
  460. (minibuffer-line-mode 1)
  461. )
  462. (when (safe-require-or-eval 'prompt-text)
  463. (set-variable 'prompt-text-format
  464. `(,(concat ""
  465. user-login-name
  466. "@"
  467. (car (split-string (system-name)
  468. "\\."))
  469. ":")
  470. (:eval (abbreviate-file-name (or buffer-file-name
  471. default-directory)))
  472. (:eval (and (fboundp 'git-ps1-mode-get-current)
  473. (git-ps1-mode-get-current " [GIT:%s]")))
  474. " "
  475. (:eval (format-time-string display-time-format))
  476. "\n"
  477. (:eval (symbol-name this-command))
  478. ": "))
  479. (prompt-text-mode 1))
  480. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  481. ;; letters, font-lock mode and fonts
  482. (setq text-quoting-style 'grave)
  483. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  484. ;; (set-window-margins (selected-window) 1 1)
  485. (unless window-system
  486. (setq frame-background-mode 'dark))
  487. (and (or (eq system-type 'Darwin)
  488. (eq system-type 'darwin))
  489. (fboundp 'mac-set-input-method-parameter)
  490. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  491. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  492. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  493. (boundp 'input-method-inactivate-hook))
  494. (add-hook 'input-method-activate-hook
  495. (lambda () (set-cursor-color "red")))
  496. (add-hook 'input-method-inactivate-hook
  497. (lambda () (set-cursor-color "black"))))
  498. (when (safe-require-or-eval 'paren)
  499. (show-paren-mode 1)
  500. (setq show-paren-delay 0.5
  501. show-paren-style 'parenthesis) ; mixed is hard to read
  502. ;; (set-face-background 'show-paren-match
  503. ;; "black")
  504. ;; ;; (face-foreground 'default))
  505. ;; (set-face-foreground 'show-paren-match
  506. ;; "white")
  507. ;; (set-face-inverse-video-p 'show-paren-match
  508. ;; t)
  509. )
  510. (transient-mark-mode 1)
  511. (global-font-lock-mode 1)
  512. (setq font-lock-global-modes
  513. '(not
  514. help-mode
  515. eshell-mode
  516. ;;term-mode
  517. Man-mode
  518. magit-diff-mode
  519. magit-revision-mode))
  520. ;; (standard-display-ascii ?\n "$\n")
  521. ;; (defvar my-eol-face
  522. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  523. ;; )
  524. ;; (defvar my-tab-face
  525. ;; '(("\t" . '(0 highlight t nil))))
  526. (defvar my-jspace-face
  527. '(("\u3000" . '(0 highlight t nil))))
  528. (add-hook 'font-lock-mode-hook
  529. (lambda ()
  530. ;; (font-lock-add-keywords nil my-eol-face)
  531. (font-lock-add-keywords nil my-jspace-face)
  532. ))
  533. (when (safe-require-or-eval 'whitespace)
  534. (add-to-list 'whitespace-display-mappings
  535. ;; We need t since last one takes precedence
  536. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  537. ;; (add-to-list 'whitespace-display-mappings
  538. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  539. (setq whitespace-style '(face
  540. trailing ; trailing blanks
  541. ;; tabs
  542. ;; spaces
  543. ;; lines
  544. lines-tail ; lines over 80
  545. newline ; newlines
  546. empty ; empty lines at beg or end of buffer
  547. ;; big-indent
  548. ;; space-mark
  549. tab-mark
  550. newline-mark ; use display table for newline
  551. ))
  552. ;; (setq whitespace-newline 'font-lock-comment-face)
  553. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  554. (defun my-whitesspace-mode-reload ()
  555. "Reload whitespace-mode config."
  556. (interactive)
  557. (when whitespace-mode
  558. (whitespace-mode 0)
  559. (whitespace-mode 1)))
  560. (set-variable 'whitespace-line-column nil)
  561. (global-whitespace-mode t)
  562. (add-hook 'dired-mode-hook
  563. (lambda ()
  564. (set (make-local-variable 'whitespace-style) nil)))
  565. (if (>= (display-color-cells)
  566. 256)
  567. (set-face-foreground 'whitespace-newline "color-109")
  568. ;; (progn
  569. ;; (set-face-bold-p 'whitespace-newline
  570. ;; t))
  571. ))
  572. (and nil
  573. '(safe-require-or-eval 'fill-column-indicator)
  574. (setq fill-column-indicator))
  575. ;; highlight current line
  576. ;; http://wiki.riywo.com/index.php?Meadow
  577. (face-spec-set 'hl-line
  578. '((((min-colors 256)
  579. (background dark))
  580. (:background "color-234"))
  581. (((min-colors 256)
  582. (background light))
  583. (:background "color-234"))
  584. (t
  585. (:underline "black"))))
  586. (set-variable 'hl-line-global-modes
  587. '(not
  588. term-mode))
  589. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  590. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  591. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  592. ;;(safe-require-or-eval 'set-modeline-color)
  593. ;; (let ((fg (face-foreground 'default))
  594. ;; (bg (face-background 'default)))
  595. ;; (set-face-background 'mode-line-inactive
  596. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  597. ;; (set-face-foreground 'mode-line-inactive
  598. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  599. ;; (set-face-underline 'mode-line-inactive
  600. ;; t)
  601. ;; (set-face-underline 'vertical-border
  602. ;; nil)
  603. ;; (when (safe-require-or-eval 'end-mark)
  604. ;; (global-end-mark-mode))
  605. ;; M-x highlight-* to highlight things
  606. (global-hi-lock-mode 1)
  607. (unless (fboundp 'highlight-region-text)
  608. (defun highlight-region-text (beg end)
  609. "Highlight text between BEG and END."
  610. (interactive "r")
  611. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  612. end)))
  613. (setq deactivate-mark t)))
  614. (when (safe-require-or-eval 'auto-highlight-symbol)
  615. (set-variable 'ahs-idle-interval 0.6)
  616. (global-auto-highlight-symbol-mode 1))
  617. (when (safe-require-or-eval 'highlight-indentation)
  618. (set-face-background 'highlight-indentation-face "color-236")
  619. (dolist (hook
  620. '(
  621. prog-mode-hook
  622. ))
  623. (add-hook hook
  624. 'highlight-indentation-mode)))
  625. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  626. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  627. ;; file handling
  628. (when (and (autoload-eval-lazily 'fzf)
  629. (executable-find "fzf"))
  630. ;; Too slow!
  631. ;; (set-variable 'fzf/executable "sk")
  632. ;; (set-variable 'fzf/args "--color bw --print-query")
  633. ;; Modified from hardcoded default to include directories, hidden files, and root directory
  634. (let ((defcmd (concat "set -o pipefail; "
  635. "command find -L . "
  636. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  637. "-o -print 2> /dev/null "
  638. "| "
  639. "sed -e 's|^\\./||'")))
  640. (setenv "FZF_DEFAULT_COMMAND" defcmd))
  641. ;; (define-key ctl-x-map (kbd "C-f") 'fzf)
  642. )
  643. (when (safe-require-or-eval 'recently)
  644. (recently-mode 1))
  645. (when (safe-require-or-eval 'editorconfig)
  646. (set-variable 'editorconfig-get-properties-function
  647. 'editorconfig-core-get-properties-hash)
  648. (editorconfig-mode 1)
  649. (set-variable 'editorconfig-mode-lighter " EC")
  650. (when (fboundp 'ws-butler-mode)
  651. (set-variable 'editorconfig-trim-whitespaces-mode
  652. 'ws-butler-mode))
  653. (with-eval-after-load 'org-src
  654. ;; [*.org\[\*Org Src*\[ c \]*\]]
  655. (add-hook 'org-src-mode-hook
  656. 'editorconfig-mode-apply t)))
  657. (when (fboundp 'editorconfig-custom-majormode)
  658. (add-hook 'editorconfig-after-apply-functions
  659. 'editorconfig-custom-majormode))
  660. ;; Add readonly=true to set read-only-mode
  661. (add-hook 'editorconfig-after-apply-functions
  662. (lambda (props)
  663. (let ((r (gethash 'readonly props)))
  664. (when (and (string= r "true")
  665. (not buffer-read-only))
  666. (read-only-mode 1)))))
  667. (add-hook 'editorconfig-hack-properties-functions
  668. '(lambda (props)
  669. (when (derived-mode-p 'makefile-mode)
  670. (puthash 'indent_style "tab" props))))
  671. ;; (when (fboundp 'editorconfig-charset-extras)
  672. ;; (add-hook 'editorconfig-custom-hooks
  673. ;; 'editorconfig-charset-extras))
  674. (setq revert-without-query '(".+"))
  675. ;; save cursor position
  676. (when (safe-require-or-eval 'saveplace)
  677. (setq-default save-place t)
  678. (setq save-place-file (concat user-emacs-directory
  679. "places")))
  680. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  681. (setq make-backup-files t)
  682. (setq vc-make-backup-files t)
  683. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  684. (setq backup-directory-alist
  685. (cons (cons "." (expand-file-name (concat user-emacs-directory
  686. "backup")))
  687. backup-directory-alist))
  688. (setq version-control 't)
  689. (setq delete-old-versions t)
  690. (setq kept-new-versions 20)
  691. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  692. "auto-save/")))
  693. ;; (setq delete-auto-save-files t)
  694. (setq auto-save-visited-interval 8)
  695. (auto-save-visited-mode 1)
  696. (add-to-list 'completion-ignored-extensions ".bak")
  697. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  698. (setq delete-by-moving-to-trash t)
  699. ;; trash-directory "~/.emacs.d/trash")
  700. (add-hook 'after-save-hook
  701. 'executable-make-buffer-file-executable-if-script-p)
  702. (set-variable 'bookmark-default-file
  703. (expand-file-name (concat user-emacs-directory
  704. "bmk")))
  705. (set-variable 'bookmark-save-flag
  706. 1)
  707. (with-eval-after-load 'recentf
  708. (defvar recentf-exclude)
  709. (defvar bookmark-default-file)
  710. (add-to-list 'recentf-exclude
  711. (regexp-quote bookmark-default-file)))
  712. (when (safe-require-or-eval 'smart-revert)
  713. (smart-revert-on))
  714. ;; autosave
  715. ;; auto-save-visited-mode can be used instead?
  716. ;; (when (safe-require-or-eval 'autosave)
  717. ;; (autosave-set 8))
  718. ;; bookmarks
  719. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  720. ;; vc
  721. (set-variable 'vc-handled-backends '(RCS))
  722. (set-variable 'vc-rcs-register-switches "-l")
  723. (set-variable 'vc-rcs-checkin-switches "-l")
  724. (set-variable 'vc-command-messages t)
  725. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  726. ;; share clipboard with x
  727. ;; this page describes this in details, but only these sexps seem to be needed
  728. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  729. (and nil
  730. (not window-system)
  731. (not (eq window-system 'mac))
  732. (getenv "DISPLAY")
  733. (not (equal (getenv "DISPLAY") ""))
  734. (executable-find "xclip")
  735. ;; (< emacs-major-version 24)
  736. '(safe-require-or-eval 'xclip)
  737. nil
  738. (turn-on-xclip))
  739. (and (eq system-type 'darwin)
  740. (safe-require-or-eval 'pasteboard)
  741. (turn-on-pasteboard))
  742. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  743. ;; some modes and hooks
  744. ;; Include some extra modes
  745. (require 'generic-x)
  746. (when (autoload-eval-lazily 'wgrep)
  747. (set-variable 'wgrep-auto-save-buffer t)
  748. (with-eval-after-load 'grep
  749. (defvar grep-mode-map)
  750. (define-key grep-mode-map
  751. "e"
  752. 'wgrep-change-to-wgrep-mode)))
  753. (with-eval-after-load 'remember
  754. (defvar remember-mode-map (make-sparse-keymap))
  755. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  756. (with-eval-after-load 'magit-files
  757. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  758. ;; existing keybindings.
  759. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  760. ;; to disable thie mode here, but do anyway.
  761. ;; See also https://github.com/magit/magit/issues/3517
  762. (global-magit-file-mode -1))
  763. (with-eval-after-load 'magit-section
  764. (set-face-background 'magit-section-highlight
  765. nil))
  766. (with-eval-after-load 'magit-diff
  767. (set-face-background 'magit-diff-added-highlight
  768. nil)
  769. (set-face-background 'magit-diff-removed-highlight
  770. nil)
  771. (set-face-background 'magit-diff-context-highlight
  772. nil)
  773. )
  774. (when (boundp 'git-rebase-filename-regexp)
  775. (add-to-list 'auto-mode-alist
  776. `(,git-rebase-filename-regexp . text-mode)))
  777. (when (safe-require-or-eval 'aggressive-indent)
  778. (defvar aggressive-indent-excluded-modes)
  779. (setq aggressive-indent-excluded-modes
  780. `(diff-mode
  781. toml-mode
  782. conf-mode
  783. dockerfile-mode
  784. groovy-mode
  785. scala-mode
  786. ,@aggressive-indent-excluded-modes))
  787. (global-aggressive-indent-mode 1))
  788. (when (autoload-eval-lazily 'ggtags)
  789. (add-hook 'c-mode-common-hook
  790. (lambda ()
  791. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  792. (ggtags-mode 1))))
  793. (add-hook 'python-mode-hook
  794. (lambda ()
  795. (ggtags-mode 1))))
  796. (when (autoload-eval-lazily 'imenu-list)
  797. ;; (set-variable 'imenu-list-auto-resize t)
  798. (set-variable 'imenu-list-focus-after-activation t)
  799. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  800. (add-hook 'emacs-lisp-mode-hook
  801. (lambda ()
  802. (setq imenu-generic-expression
  803. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  804. ,@imenu-generic-expression))))
  805. (with-eval-after-load 'compile
  806. (defvar compilation-filter-start)
  807. (defvar compilation-error-regexp-alist)
  808. (require 'ansi-color)
  809. (add-hook 'compilation-filter-hook
  810. (lambda ()
  811. (let ((inhibit-read-only t))
  812. (ansi-color-apply-on-region compilation-filter-start
  813. (point)))))
  814. (add-to-list 'compilation-error-regexp-alist
  815. ;; ansible-lint
  816. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  817. ;; Workaround to avoid ensime error
  818. (defvar ensime-mode-key-prefix nil)
  819. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  820. (when (safe-require-or-eval 'company)
  821. (global-company-mode)
  822. (set-variable 'company-idle-delay 0.5)
  823. (set-variable 'company-minimum-prefix-length 2)
  824. (set-variable 'company-selection-wrap-around t))
  825. ;; https://github.com/lunaryorn/flycheck
  826. (when (safe-require-or-eval 'flycheck)
  827. (call-after-init 'global-flycheck-mode))
  828. (when (autoload-eval-lazily 'ilookup)
  829. (define-key ctl-x-map "d" 'ilookup-open-word))
  830. (set-variable 'ac-ignore-case nil)
  831. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  832. (define-key ctl-x-map "t" 'term-run-shell-command))
  833. (add-to-list 'safe-local-variable-values
  834. '(encoding utf-8))
  835. (setq enable-local-variables :safe)
  836. ;; Detect file type from shebang and set major-mode.
  837. (add-to-list 'interpreter-mode-alist
  838. '("python3" . python-mode))
  839. (add-to-list 'interpreter-mode-alist
  840. '("python2" . python-mode))
  841. (with-eval-after-load 'python
  842. (defvar python-mode-map (make-sparse-keymap))
  843. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  844. (when (autoload-eval-lazily 'pipenv)
  845. (add-hook 'python-mode-hook
  846. (lambda ()
  847. (pipenv-mode 1)
  848. (pipenv-projectile-after-switch-default)))
  849. )
  850. (set-variable 'flycheck-python-pycompile-executable "python3")
  851. (set-variable 'python-indent-guess-indent-offset nil)
  852. (with-eval-after-load 'blacken
  853. (when (require 'with-venv nil t)
  854. (with-venv-advice-add 'blacken-buffer)))
  855. ;; http://fukuyama.co/foreign-regexp
  856. '(and (safe-require-or-eval 'foreign-regexp)
  857. (progn
  858. (setq foreign-regexp/regexp-type 'perl)
  859. '(setq reb-re-syntax 'foreign-regexp)
  860. ))
  861. (autoload-eval-lazily 'sql '(sql-mode)
  862. (require 'sql-indent nil t))
  863. (add-to-list 'auto-mode-alist
  864. '("\\.hql\\'" . sql-mode))
  865. (when (autoload-eval-lazily 'git-command)
  866. (define-key ctl-x-map "g" 'git-command))
  867. (when (autoload-eval-lazily 'gited)
  868. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  869. (when (safe-require-or-eval 'git-commit)
  870. (global-git-commit-mode 1))
  871. (with-eval-after-load 'git-commit
  872. (add-hook 'git-commit-setup-hook
  873. 'turn-off-auto-fill t))
  874. (autoload-eval-lazily 'sl)
  875. (with-eval-after-load 'rst
  876. (defvar rst-mode-map)
  877. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  878. (with-eval-after-load 'jdee
  879. (add-hook 'jdee-mode-hook
  880. (lambda ()
  881. (make-local-variable 'global-mode-string)
  882. (add-to-list 'global-mode-string
  883. mode-line-position))))
  884. ;; Cannot enable error thrown. Why???
  885. ;; https://github.com/m0smith/malabar-mode#Installation
  886. ;; (when (autoload-eval-lazily 'malabar-mode)
  887. ;; (add-to-list 'load-path
  888. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  889. ;; (safe-require-or-eval 'cedet-devel-load)
  890. ;; (call-after-init 'activate-malabar-mode))
  891. (with-eval-after-load 'make-mode
  892. (defvar makefile-mode-map (make-sparse-keymap))
  893. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  894. ;; this functions is set in write-file-functions, i cannot find any
  895. ;; good way to remove this.
  896. (fset 'makefile-warn-suspicious-lines 'ignore))
  897. (with-eval-after-load 'verilog-mode
  898. (defvar verilog-mode-map (make-sparse-keymap))
  899. (define-key verilog-mode-map ";" 'self-insert-command))
  900. (setq diff-switches "-u")
  901. (with-eval-after-load 'diff-mode
  902. ;; (when (and (eq major-mode
  903. ;; 'diff-mode)
  904. ;; (not buffer-file-name))
  905. ;; ;; do not pass when major-mode is derived mode of diff-mode
  906. ;; (view-mode 1))
  907. (set-face-attribute 'diff-header nil
  908. :foreground nil
  909. :background nil
  910. :weight 'bold)
  911. (set-face-attribute 'diff-file-header nil
  912. :foreground nil
  913. :background nil
  914. :weight 'bold)
  915. (set-face-foreground 'diff-index "blue")
  916. (set-face-attribute 'diff-hunk-header nil
  917. :foreground "cyan"
  918. :weight 'normal)
  919. (set-face-attribute 'diff-context nil
  920. ;; :foreground "white"
  921. :foreground nil
  922. :weight 'normal)
  923. (set-face-foreground 'diff-removed "red")
  924. (set-face-foreground 'diff-added "green")
  925. (set-face-background 'diff-removed nil)
  926. (set-face-background 'diff-added nil)
  927. (set-face-attribute 'diff-changed nil
  928. :foreground "magenta"
  929. :weight 'normal)
  930. (set-face-attribute 'diff-refine-changed nil
  931. :foreground nil
  932. :background nil
  933. :weight 'bold
  934. :inverse-video t)
  935. ;; Annoying !
  936. ;;(diff-auto-refine-mode)
  937. )
  938. ;; (ffap-bindings)
  939. (set-variable 'browse-url-browser-function
  940. 'eww-browse-url)
  941. (set-variable 'sh-here-document-word "__EOC__")
  942. (when (autoload-eval-lazily 'adoc-mode
  943. nil
  944. (defvar adoc-mode-map (make-sparse-keymap))
  945. (define-key adoc-mode-map (kbd "C-m") 'newline))
  946. (setq auto-mode-alist
  947. `(("\\.adoc\\'" . adoc-mode)
  948. ("\\.asciidoc\\'" . adoc-mode)
  949. ,@auto-mode-alist)))
  950. (with-eval-after-load 'markup-faces
  951. ;; Is this too match ?
  952. (set-face-foreground 'markup-meta-face
  953. "color-245")
  954. (set-face-foreground 'markup-meta-hide-face
  955. "color-245")
  956. )
  957. ;; TODO: check if this is required
  958. (when (autoload-eval-lazily 'groovy-mode nil
  959. (defvar groovy-mode-map (make-sparse-keymap))
  960. (define-key groovy-mode-map "(" 'self-insert-command)
  961. (define-key groovy-mode-map ")" 'self-insert-command)
  962. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  963. )
  964. (add-to-list 'auto-mode-alist
  965. '("build\\.gradle\\'" . groovy-mode)))
  966. (add-to-list 'auto-mode-alist
  967. '("\\.gawk\\'" . awk-mode))
  968. (with-eval-after-load 'yaml-mode
  969. (defvar yaml-mode-map (make-sparse-keymap))
  970. (define-key yaml-mode-map (kbd "C-m") 'newline))
  971. (with-eval-after-load 'html-mode
  972. (defvar html-mode-map (make-sparse-keymap))
  973. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  974. (with-eval-after-load 'text-mode
  975. (define-key text-mode-map (kbd "C-m") 'newline))
  976. (autoload-eval-lazily 'info nil
  977. (defvar Info-additional-directory-list)
  978. (dolist (dir (directory-files (concat user-emacs-directory
  979. "info")
  980. t
  981. "^[^.].*"))
  982. (when (file-directory-p dir)
  983. (add-to-list 'Info-additional-directory-list
  984. dir)))
  985. (let ((dir (expand-file-name "~/.brew/share/info")))
  986. (when (file-directory-p dir)
  987. (add-to-list 'Info-additional-directory-list
  988. dir))))
  989. (with-eval-after-load 'apropos
  990. (defvar apropos-mode-map (make-sparse-keymap))
  991. (define-key apropos-mode-map "n" 'next-line)
  992. (define-key apropos-mode-map "p" 'previous-line))
  993. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  994. ;; (define-key isearch-mode-map
  995. ;; (kbd "C-j") 'isearch-other-control-char)
  996. ;; (define-key isearch-mode-map
  997. ;; (kbd "C-k") 'isearch-other-control-char)
  998. ;; (define-key isearch-mode-map
  999. ;; (kbd "C-h") 'isearch-other-control-char)
  1000. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1001. (define-key isearch-mode-map (kbd "M-r")
  1002. 'isearch-query-replace-regexp)
  1003. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1004. (setq lazy-highlight-cleanup nil)
  1005. ;; face for isearch highlighing
  1006. (set-face-attribute 'lazy-highlight
  1007. nil
  1008. :foreground `unspecified
  1009. :background `unspecified
  1010. :underline t
  1011. ;; :weight `bold
  1012. )
  1013. (add-hook 'outline-mode-hook
  1014. (lambda ()
  1015. (when (string-match "\\.md\\'" buffer-file-name)
  1016. (set (make-local-variable 'outline-regexp) "#+ "))))
  1017. (add-hook 'outline-mode-hook
  1018. 'outline-show-all)
  1019. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1020. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1021. (when (autoload-eval-lazily 'markdown-mode
  1022. '(markdown-mode gfm-mode)
  1023. (defvar gfm-mode-map (make-sparse-keymap))
  1024. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1025. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1026. (set-variable 'markdown-command (or (executable-find "markdown")
  1027. (executable-find "markdown.pl")
  1028. ""))
  1029. (add-hook 'markdown-mode-hook
  1030. (lambda ()
  1031. (outline-minor-mode 1)
  1032. (flyspell-mode)
  1033. (set (make-local-variable 'comment-start) ";")))
  1034. )
  1035. ;; c-mode
  1036. ;; http://www.emacswiki.org/emacs/IndentingC
  1037. ;; http://en.wikipedia.org/wiki/Indent_style
  1038. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1039. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1040. (with-eval-after-load 'cc-vars
  1041. (defvar c-default-style nil)
  1042. (add-to-list 'c-default-style
  1043. '(c-mode . "k&r"))
  1044. (add-to-list 'c-default-style
  1045. '(c++-mode . "k&r")))
  1046. (autoload-eval-lazily 'js2-mode nil
  1047. ;; currently do not use js2-mode
  1048. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1049. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1050. ;; (defvar js2-mode-map (make-sparse-keymap))
  1051. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1052. ;; (interactive)
  1053. ;; (js2-enter-key)
  1054. ;; (indent-for-tab-command)))
  1055. ;; (add-hook (kill-local-variable 'before-save-hook)
  1056. ;; 'js2-before-save)
  1057. ;; (add-hook 'before-save-hook
  1058. ;; 'my-indent-buffer
  1059. ;; nil
  1060. ;; t)
  1061. )
  1062. (add-to-list 'interpreter-mode-alist
  1063. '("node" . js-mode))
  1064. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1065. (with-eval-after-load 'uniquify
  1066. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1067. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1068. (setq uniquify-min-dir-content 1))
  1069. (with-eval-after-load 'view
  1070. (defvar view-mode-map (make-sparse-keymap))
  1071. (define-key view-mode-map "j" 'scroll-up-line)
  1072. (define-key view-mode-map "k" 'scroll-down-line)
  1073. (define-key view-mode-map "v" 'toggle-read-only)
  1074. (define-key view-mode-map "q" 'bury-buffer)
  1075. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1076. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1077. ;; (define-key view-mode-map
  1078. ;; "n" 'nonincremental-repeat-search-forward)
  1079. ;; (define-key view-mode-map
  1080. ;; "N" 'nonincremental-repeat-search-backward)
  1081. ;; N conflicts with git-walktree
  1082. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1083. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1084. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1085. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1086. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1087. (global-set-key "\M-r" 'view-mode)
  1088. ;; (setq view-read-only t)
  1089. (with-eval-after-load 'term
  1090. (defvar term-raw-map (make-sparse-keymap))
  1091. (define-key term-raw-map (kbd "C-x")
  1092. (lookup-key (current-global-map)
  1093. (kbd "C-x"))))
  1094. (add-hook 'term-mode-hook
  1095. (lambda ()
  1096. ;; Stop current line highlighting
  1097. (set (make-local-variable (defvar hl-line-range-function))
  1098. (lambda () '(0 . 0)))
  1099. (set (make-local-variable 'scroll-margin)
  1100. 0)
  1101. (set-variable 'term-buffer-maximum-size 20480)
  1102. ))
  1103. (add-hook 'Man-mode-hook
  1104. (lambda ()
  1105. (view-mode 1)
  1106. (setq truncate-lines nil)))
  1107. (set-variable 'Man-notify-method (if window-system
  1108. 'newframe
  1109. 'aggressive))
  1110. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1111. "woman_cache.el")))
  1112. ;; not work because man.el will be loaded when man called
  1113. (defalias 'man 'woman)
  1114. (add-to-list 'auto-mode-alist
  1115. '("tox\\.ini\\'" . conf-unix-mode))
  1116. (when (autoload-eval-lazily 'toml-mode)
  1117. (add-to-list 'auto-mode-alist
  1118. '("/tox\\.ini\\'" . toml-mode))
  1119. (add-to-list 'auto-mode-alist
  1120. '("/Pipfile\\'" . toml-mode))
  1121. (add-to-list 'auto-mode-alist
  1122. '("/poetry\\.lock\\'" . toml-mode))
  1123. )
  1124. (when (autoload-eval-lazily 'json-mode)
  1125. (add-to-list 'auto-mode-alist
  1126. '("/Pipfile\\.lock\\'" . json-mode)))
  1127. (add-hook 'go-mode-hook
  1128. (lambda()
  1129. (defvar go-mode-map)
  1130. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1131. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1132. (when (autoload-eval-lazily 'k8s-mode)
  1133. (add-to-list 'auto-mode-alist
  1134. '("\\.k8s\\'" . k8s-mode)))
  1135. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1136. ;; buffers
  1137. (defvar bs-configurations)
  1138. (when (autoload-eval-lazily 'bs '(bs-show)
  1139. (add-to-list 'bs-configurations
  1140. '("specials" "^\\*" nil ".*" nil nil))
  1141. (add-to-list 'bs-configurations
  1142. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1143. (defvar bs-mode-map)
  1144. (defvar bs-current-configuration)
  1145. (define-key bs-mode-map (kbd "t")
  1146. ;; TODO: fix toggle feature
  1147. (lambda ()
  1148. (interactive)
  1149. (if (string= "specials"
  1150. bs-current-configuration)
  1151. (bs-set-configuration "files")
  1152. (bs-set-configuration "specials"))
  1153. (bs-refresh)
  1154. (bs-message-without-log "%s"
  1155. (bs--current-config-message))))
  1156. ;; (setq bs-configurations (list
  1157. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1158. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1159. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1160. )
  1161. (defalias 'list-buffers 'bs-show)
  1162. (set-variable 'bs-default-configuration "files-and-specials")
  1163. (set-variable 'bs-default-sort-name "by nothing")
  1164. (add-hook 'bs-mode-hook
  1165. (lambda ()
  1166. (set (make-local-variable 'scroll-margin) 0))))
  1167. ;;(iswitchb-mode 1)
  1168. (icomplete-mode)
  1169. (defun iswitchb-buffer-display-other-window ()
  1170. "Do iswitchb in other window."
  1171. (interactive)
  1172. (let ((iswitchb-default-method 'display))
  1173. (call-interactively 'iswitchb-buffer)))
  1174. ;; buffer killing
  1175. ;; (defun my-delete-window-killing-buffer () nil)
  1176. (defun my-query-kill-current-buffer ()
  1177. "Interactively kill current buffer."
  1178. (interactive)
  1179. (if (y-or-n-p (concat "kill current buffer? :"))
  1180. (kill-buffer (current-buffer))))
  1181. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1182. (substitute-key-definition 'kill-buffer
  1183. 'my-query-kill-current-buffer
  1184. global-map)
  1185. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1186. ;; recentf-mode
  1187. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1188. "recentf")))
  1189. (set-variable 'recentf-max-menu-items 20)
  1190. (set-variable 'recentf-max-saved-items 30)
  1191. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1192. (set-variable 'recentf-auto-cleanup 3)
  1193. ;; (safe-require-or-eval 'sync-recentf)
  1194. ;; (when (safe-require-or-eval 'recentf)
  1195. ;; (add-to-list 'recentf-exclude
  1196. ;; (regexp-quote recentf-save-file))
  1197. ;; (add-to-list 'recentf-exclude
  1198. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1199. ;; (add-to-list 'recentf-exclude
  1200. ;; "/sync-recentf-marker\\'")
  1201. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1202. ;; (remove-hook 'find-file-hook
  1203. ;; 'recentf-track-opened-file)
  1204. ;; (defun my-recentf-load-track-save-list ()
  1205. ;; "Load current recentf list from file, track current visiting file, then save
  1206. ;; the list."
  1207. ;; (recentf-load-list)
  1208. ;; (recentf-track-opened-file)
  1209. ;; (recentf-save-list))
  1210. ;; (add-hook 'find-file-hook
  1211. ;; 'my-recentf-load-track-save-list)
  1212. ;; (add-hook 'kill-emacs-hook
  1213. ;; 'recentf-load-list)
  1214. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1215. ;; ;; (add-hook 'find-file-hook
  1216. ;; ;; (lambda ()
  1217. ;; ;; (recentf-add-file default-directory)))
  1218. ;; (when (autoload-eval-lazily 'recentf-show)
  1219. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1220. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1221. ;; ;; 'recentf-load-list)
  1222. ;; )
  1223. ;; (recentf-mode 1)
  1224. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1225. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1226. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1227. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1228. ;; (add-hook 'recentf-dialog-mode-hook
  1229. ;; (lambda ()
  1230. ;; ;; (recentf-save-list)
  1231. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1232. ;; ;; 'my-recentf-cd-and-find-file)
  1233. ;; (cd "~/"))))
  1234. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1235. ;; dired
  1236. (defun my-file-head (filename &optional n)
  1237. "Return list of first N lines of file FILENAME."
  1238. ;; TODO: Fix for janapese text
  1239. ;; TODO: Fix for short text
  1240. (let ((num (or n 10))
  1241. (size 100)
  1242. (beg 0)
  1243. (end 0)
  1244. (result '())
  1245. (read -1))
  1246. (with-temp-buffer
  1247. (erase-buffer)
  1248. (while (or (<= (count-lines (point-min)
  1249. (point-max))
  1250. num)
  1251. (eq read 0))
  1252. (setq end (+ beg size))
  1253. (setq read (nth 1 (insert-file-contents-literally filename
  1254. nil
  1255. beg
  1256. end)))
  1257. (goto-char (point-max))
  1258. (setq beg (+ beg size)))
  1259. (goto-char (point-min))
  1260. (while (< (length result) num)
  1261. (let ((start (point)))
  1262. (forward-line 1)
  1263. (setq result
  1264. `(,@result ,(buffer-substring-no-properties start
  1265. (point))))))
  1266. result
  1267. ;; (buffer-substring-no-properties (point-min)
  1268. ;; (progn
  1269. ;; (forward-line num)
  1270. ;; (point)))
  1271. )))
  1272. ;; (apply 'concat (my-file-head "./shrc" 10)
  1273. (defun my-dired-echo-file-head (arg)
  1274. "Echo head of current file.
  1275. ARG is num to show, or defaults to 7."
  1276. (interactive "P")
  1277. (let ((f (dired-get-filename)))
  1278. (message "%s"
  1279. (apply 'concat
  1280. (my-file-head f
  1281. 7)))))
  1282. (defun my-dired-diff ()
  1283. "Show diff of marked file and file of current line."
  1284. (interactive)
  1285. (let ((files (dired-get-marked-files nil nil nil t)))
  1286. (if (eq (car files)
  1287. t)
  1288. (diff (cadr files) (dired-get-filename))
  1289. (message "One file must be marked!"))))
  1290. (defun dired-get-file-info ()
  1291. "Print information of current line file."
  1292. (interactive)
  1293. (let ((f (shell-quote-argument (dired-get-filename t))))
  1294. (if (file-directory-p f)
  1295. (progn
  1296. (message "Calculating disk usage...")
  1297. (shell-command (concat "du -hsD "
  1298. f)))
  1299. (shell-command (concat "file "
  1300. f)))))
  1301. (defun my-dired-scroll-up ()
  1302. "Scroll up."
  1303. (interactive)
  1304. (my-dired-previous-line (- (window-height) 1)))
  1305. (defun my-dired-scroll-down ()
  1306. "Scroll down."
  1307. (interactive)
  1308. (my-dired-next-line (- (window-height) 1)))
  1309. ;; (defun my-dired-forward-line (arg)
  1310. ;; ""
  1311. ;; (interactive "p"))
  1312. (defun my-dired-previous-line (arg)
  1313. "Move ARG lines up."
  1314. (interactive "p")
  1315. (if (> arg 0)
  1316. (progn
  1317. (if (eq (line-number-at-pos)
  1318. 1)
  1319. (goto-char (point-max))
  1320. (forward-line -1))
  1321. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1322. (dired-get-subdir))
  1323. (- arg 1)
  1324. arg)))
  1325. (dired-move-to-filename)))
  1326. (defun my-dired-next-line (arg)
  1327. "Move ARG lines down."
  1328. (interactive "p")
  1329. (if (> arg 0)
  1330. (progn
  1331. (if (eq (point)
  1332. (point-max))
  1333. (goto-char (point-min))
  1334. (forward-line 1))
  1335. (my-dired-next-line (if (or (dired-get-filename nil t)
  1336. (dired-get-subdir))
  1337. (- arg 1)
  1338. arg)))
  1339. (dired-move-to-filename)))
  1340. (defun my-tramp-remote-find-file (f)
  1341. "Open F."
  1342. (interactive (list (read-file-name "My Find File Tramp: "
  1343. "/scp:"
  1344. nil ;; "/scp:"
  1345. (confirm-nonexistent-file-or-buffer))))
  1346. (find-file f))
  1347. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1348. (if (eq window-system 'mac)
  1349. (setq dired-listing-switches "-lhF")
  1350. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1351. )
  1352. (setq dired-listing-switches "-lhF")
  1353. ;; when using dired-find-alternate-file
  1354. ;; reuse current dired buffer for the file to open
  1355. ;; (put 'dired-find-alternate-file 'disabled nil)
  1356. (set-variable 'dired-ls-F-marks-symlinks t)
  1357. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1358. (set-variable 'ls-lisp-dirs-first t)
  1359. (set-variable 'ls-lisp-use-localized-time-format t)
  1360. (set-variable 'ls-lisp-format-time-list
  1361. '("%Y-%m-%d %H:%M"
  1362. "%Y-%m-%d "))
  1363. (set-variable 'dired-dwim-target t)
  1364. (set-variable 'dired-isearch-filenames t)
  1365. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1366. (set-variable 'dired-hide-details-hide-information-lines nil)
  1367. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1368. (set-variable 'dired-recursive-deletes 'always)
  1369. ;; (add-hook 'dired-after-readin-hook
  1370. ;; 'my-replace-nasi-none)
  1371. (with-eval-after-load 'dired
  1372. (safe-require-or-eval 'ls-lisp)
  1373. (defvar dired-mode-map (make-sparse-keymap))
  1374. ;; dired-do-chgrp sometimes cause system hung
  1375. (define-key dired-mode-map "G" 'ignore)
  1376. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1377. (define-key dired-mode-map "i" 'dired-get-file-info)
  1378. (define-key dired-mode-map "f" 'find-file)
  1379. (define-key dired-mode-map "!" 'shell-command)
  1380. (define-key dired-mode-map "&" 'async-shell-command)
  1381. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1382. (define-key dired-mode-map "=" 'my-dired-diff)
  1383. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1384. (define-key dired-mode-map "b" 'gtkbm)
  1385. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1386. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1387. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1388. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1389. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1390. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1391. (substitute-key-definition 'dired-next-line
  1392. 'my-dired-next-line
  1393. dired-mode-map)
  1394. (substitute-key-definition 'dired-previous-line
  1395. 'my-dired-previous-line
  1396. dired-mode-map)
  1397. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1398. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1399. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1400. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1401. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1402. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1403. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1404. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1405. (add-hook 'dired-mode-hook
  1406. (lambda ()
  1407. (when (fboundp 'dired-hide-details-mode)
  1408. (dired-hide-details-mode t)
  1409. (local-set-key "l" 'dired-hide-details-mode))
  1410. (let ((file "._Icon\015"))
  1411. (when nil
  1412. '(file-readable-p file)
  1413. (delete-file file)))))
  1414. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1415. (add-to-list 'pack-program-alist
  1416. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1417. (set-variable 'pack-silence
  1418. t)
  1419. (with-eval-after-load 'dired
  1420. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1421. (when (autoload-eval-lazily 'dired-list-all-mode)
  1422. (setq dired-listing-switches "-lhF")
  1423. (with-eval-after-load 'dired
  1424. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1425. (when (autoload-eval-lazily 'dired-filter)
  1426. (add-hook 'dired-mode-hook
  1427. 'dired-filter-mode))
  1428. (set-variable 'dired-filter-stack nil)
  1429. ;; Currently disabled in favor of dired-from-git-ls-files
  1430. ;; (define-key ctl-x-map "f" 'find-dired)
  1431. ;; It works!
  1432. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1433. (defun my-dired-git-ls-files (args)
  1434. "Dired from git ls-files."
  1435. (interactive "sgit ls-files args: ")
  1436. (pop-to-buffer-same-window
  1437. (dired-noselect `(,default-directory
  1438. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1439. "\0" t))
  1440. ""))
  1441. )
  1442. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1443. (with-eval-after-load 'dired
  1444. (defvar dired-mode-map (make-sparse-keymap))
  1445. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1446. ;; (define-minor-mode my-dired-glob-filter)
  1447. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1448. ;; misc funcs
  1449. (when (fboundp 'browse-url-default-macosx-browser)
  1450. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1451. (defalias 'qcalc 'quick-calc)
  1452. (defun memo (&optional dir)
  1453. "Open memo.txt in DIR."
  1454. (interactive)
  1455. (pop-to-buffer (find-file-noselect (concat (if dir
  1456. (file-name-as-directory dir)
  1457. "")
  1458. "memo.txt"))))
  1459. (set (defvar my-rgrep-alist nil
  1460. "Alist of rgrep command.
  1461. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1462. condition to choose COMMAND when evaluated.")
  1463. `(
  1464. ;; ripgrep
  1465. ("rg"
  1466. (executable-find "rg")
  1467. "rg --hidden --no-heading --smart-case ")
  1468. ;; git grep
  1469. ("gitgrep"
  1470. (eq 0
  1471. (shell-command "git rev-parse --git-dir"))
  1472. "git --no-pager -c color.grep=always grep -nH -e ")
  1473. ;; sift
  1474. ("sift"
  1475. (executable-find "sift")
  1476. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1477. ;; the silver searcher
  1478. ("ag"
  1479. (executable-find "ag")
  1480. "ag --nogroup --nopager --filename ")
  1481. ;; ack
  1482. ("ack"
  1483. (executable-find "ack")
  1484. "ack --nogroup --nopager --with-filename ")
  1485. ;; gnu global
  1486. ("global"
  1487. (and (require 'ggtags nil t)
  1488. (executable-find "global")
  1489. (ggtags-current-project-root))
  1490. "global --result grep ")
  1491. ;; grep
  1492. ("grep"
  1493. t
  1494. ,(concat "find . "
  1495. "-path '*/.git' -prune -o "
  1496. "-path '*/.svn' -prune -o "
  1497. "-type f -print0 | "
  1498. "xargs -0 grep -nH -e "))
  1499. )
  1500. )
  1501. (defvar my-rgrep-default nil
  1502. "Default command name for my-rgrep.")
  1503. (defun my-rgrep-grep-command (&optional name alist)
  1504. "Return recursive grep command for current directory or nil.
  1505. If NAME is given, use that without testing.
  1506. Commands are searched from ALIST."
  1507. (if alist
  1508. (if name
  1509. ;; if name is given search that from alist and return the command
  1510. (nth 2 (assoc name
  1511. alist))
  1512. ;; if name is not given try test in 1th elem
  1513. (let ((car (car alist))
  1514. (cdr (cdr alist)))
  1515. (if (eval (nth 1 car))
  1516. ;; if the condition is true return the command
  1517. (nth 2 car)
  1518. ;; try next one
  1519. (and cdr
  1520. (my-rgrep-grep-command name cdr)))))
  1521. ;; if alist is not given set default value
  1522. (my-rgrep-grep-command name my-rgrep-alist)))
  1523. (defun my-rgrep (command-args)
  1524. "My recursive grep. Run COMMAND-ARGS.
  1525. If prefix argument is given, use current symbol as default search target
  1526. and search from projectile root (if projectile is available)."
  1527. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1528. nil)))
  1529. (if cmd
  1530. (list (read-shell-command "grep command: "
  1531. (concat cmd
  1532. (if current-prefix-arg
  1533. (thing-at-point 'symbol t)
  1534. ""))
  1535. 'grep-find-history))
  1536. (error "My-Rgrep: Command for rgrep not found")
  1537. )))
  1538. (if (and current-prefix-arg
  1539. (safe-require-or-eval 'projectile)
  1540. (projectile-project-p))
  1541. (projectile-with-default-dir (projectile-project-root)
  1542. (compilation-start command-args
  1543. 'grep-mode))
  1544. (compilation-start command-args
  1545. 'grep-mode)))
  1546. (defun my-rgrep-thing-at-point-projectile-root ()
  1547. "My recursive grep to find thing at point from project root."
  1548. (interactive)
  1549. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1550. nil))
  1551. (command-args
  1552. (if cmd
  1553. (concat cmd
  1554. (or (thing-at-point 'symbol t)
  1555. (error "No symbol at point")))
  1556. (error "My-Rgrep: Command for rgrep not found"))))
  1557. (if (safe-require-or-eval 'projectile)
  1558. (projectile-with-default-dir (or (projectile-project-root)
  1559. default-directory)
  1560. (compilation-start command-args
  1561. 'grep-mode))
  1562. (compilation-start command-args
  1563. 'grep-mode))))
  1564. (defmacro define-my-rgrep (name)
  1565. "Define rgrep for NAME."
  1566. `(defun ,(intern (concat "my-rgrep-"
  1567. name)) ()
  1568. ,(format "My recursive grep by %s."
  1569. name)
  1570. (interactive)
  1571. (let ((my-rgrep-default ,name))
  1572. (if (called-interactively-p 'any)
  1573. (call-interactively 'my-rgrep)
  1574. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1575. )
  1576. (define-my-rgrep "ack")
  1577. (define-my-rgrep "ag")
  1578. (define-my-rgrep "rg")
  1579. (define-my-rgrep "sift")
  1580. (define-my-rgrep "gitgrep")
  1581. (define-my-rgrep "grep")
  1582. (define-my-rgrep "global")
  1583. (define-key ctl-x-map "s" 'my-rgrep)
  1584. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1585. (defun my-occur (regexp &optional region)
  1586. "My occur command to search REGEXP."
  1587. (interactive (list (read-string "List lines matching regexp: "
  1588. (thing-at-point 'symbol t))))
  1589. (occur regexp nil region))
  1590. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1591. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1592. (defalias 'make 'compile)
  1593. (define-key ctl-x-map "c" 'compile)
  1594. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1595. ;; editorconfig-auto-apply
  1596. (define-minor-mode editorconfig-auto-apply-mode
  1597. "When saving .editorconfig file update buffer configs."
  1598. :global t ;; TODO: global nil and instruct to hook to editorconfig-conf-mode-hook
  1599. :lighter ""
  1600. (if editorconfig-auto-apply-mode
  1601. (add-hook 'after-save-hook
  1602. 'editorconfig-auto-apply-mode--run)
  1603. (remove-hook 'after-save-hook
  1604. 'editorconfig-auto-apply-mode--run)))
  1605. (defun editorconfig-auto-apply-mode--run ()
  1606. "When saving .editorconfig file walk all buffers and update configs."
  1607. (when (eq major-mode
  1608. 'editorconfig-conf-mode)
  1609. (let ((dir (file-name-directory buffer-file-name)))
  1610. (cl-dolist (buf (buffer-list))
  1611. (when (and (buffer-file-name buf)
  1612. (file-in-directory-p (buffer-file-name buf)
  1613. dir))
  1614. (with-current-buffer buf
  1615. (editorconfig-mode-apply)))))))
  1616. (editorconfig-auto-apply-mode 1)
  1617. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1618. (define-key ctl-x-map "T" 'git-worktree)
  1619. (define-key ctl-x-map "W" 'git-walktree)
  1620. ;;;;;;;;;;;;;;;;
  1621. ;; flychcek-black
  1622. (require 'flycheck)
  1623. (flycheck-define-checker python-black-check
  1624. "A Python style checker."
  1625. :command ("python3" "-m" "black"
  1626. "--check"
  1627. (config-file "--config" flycheck-black)
  1628. source)
  1629. :error-parser flycheck-parse-black-check
  1630. ;; :error-patterns
  1631. ;; (
  1632. ;; (error line-start "error: cannot format " (file-name) ": " (message) ": " line ":" column ": " (one-or-more any) line-end)
  1633. ;; (error line-start (message) " " (file-name) line-end)
  1634. ;; )
  1635. :enabled (lambda ()
  1636. (or (not (flycheck-python-needs-module-p 'python-black-check))
  1637. (flycheck-python-find-module 'python-black-check "black")))
  1638. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "black"))
  1639. :modes python-mode)
  1640. ;; (flycheck-define-checker python-black-diff
  1641. ;; "A Python style checker."
  1642. ;; :command ("python3"
  1643. ;; "-m" "black"
  1644. ;; (config-file "--config" flycheck-black)
  1645. ;; "--diff" source)
  1646. ;; :error-parser my-flycheck-parse-unified-diff
  1647. ;; :enabled (lambda ()
  1648. ;; (or (not (flycheck-python-needs-module-p 'python-black))
  1649. ;; (flycheck-python-find-module 'python-black "black")))
  1650. ;; :verify (lambda (_) (flycheck-python-verify-module 'python-black "black"))
  1651. ;; :modes python-mode)
  1652. (flycheck-def-config-file-var flycheck-black python-black-check "pyproject.toml"
  1653. :safe #'stringp)
  1654. (add-to-list 'flycheck-checkers
  1655. 'python-black-check)
  1656. (defun flycheck-parse-black-check (output checker buffer)
  1657. "Flycheck parser to check if reformat is required."
  1658. (let ((result nil))
  1659. (with-temp-buffer
  1660. (insert output)
  1661. (save-match-data
  1662. (goto-char (point-min))
  1663. (when (re-search-forward "^would reformat .*$" nil t)
  1664. (add-to-list 'result (flycheck-error-new-at
  1665. (point-min)
  1666. nil
  1667. 'error
  1668. ;;(format "Black: %s" (match-string 0))
  1669. "Black: would reformat"
  1670. :buffer buffer
  1671. :checker checker)))
  1672. (goto-char (point-min))
  1673. (when (re-search-forward "^error: .*$" nil t)
  1674. (add-to-list 'result (flycheck-error-new-at
  1675. (point-min)
  1676. nil
  1677. 'error
  1678. ;; Fix not to include absolute file path
  1679. (format "Black: %s" (match-string 0))
  1680. :buffer buffer
  1681. :checker checker)))))
  1682. result))
  1683. (defun my-flycheck-parse-unified-diff (output checker buffer)
  1684. "Flycheck parser to parse diff output."
  1685. (let ((source-line 0)
  1686. (result ())
  1687. (hunk "HUNK"))
  1688. (with-temp-buffer
  1689. (insert output)
  1690. (goto-char (point-min))
  1691. (while (not (eq (point) (point-max)))
  1692. ;; FIXME: Do not stop when no result
  1693. (while (not (re-search-forward "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*$" (point-at-eol) t))
  1694. (forward-line 1)
  1695. (goto-char (point-at-bol)))
  1696. (setq source-line
  1697. (string-to-number (match-string 1)))
  1698. ;; TODO: Add filename support
  1699. (setq hunk
  1700. (match-string 0))
  1701. ;;(while (not (rearch-forward "^\\(-\\|\\+\\)")))
  1702. )
  1703. (add-to-list 'result
  1704. (flycheck-error-new-at
  1705. 0
  1706. nil
  1707. 'error
  1708. "MESSAGE"
  1709. :buffer buffer
  1710. :checker checker
  1711. :group hunk)))
  1712. result))
  1713. (set-variable 'flycheck-python-mypy-ini
  1714. ".mypy.ini")
  1715. ;; Local Variables:
  1716. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1717. ;; flycheck-checker: emacs-lisp
  1718. ;; End:
  1719. ;;; emancs.el ends here