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.
 
 
 
 
 
 

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