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.
 
 
 
 
 
 

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