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.
 
 
 
 
 
 

2038 line
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!
  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. (defcmd (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. (setenv "FZF_DEFAULT_COMMAND" defcmd))
  655. (set-variable 'fzf/window-height 45)
  656. (set-variable 'fzf/args "-x --color bw --print-query --inline-info --cycle")
  657. (defun my-fzf-or-find-file ()
  658. "Call fzf if usable or call find-file."
  659. (declare (interactive-only t))
  660. (interactive)
  661. (if (and (executable-find "fzf")
  662. (fboundp 'fzf)
  663. (not (file-remote-p default-directory)))
  664. (fzf)
  665. (call-interactively 'find-file)))
  666. (define-key ctl-x-map (kbd "f") 'my-fzf-or-find-file)
  667. ;; recently
  668. (when (safe-require-or-eval 'recently)
  669. (recently-mode 1))
  670. (when (safe-require-or-eval 'editorconfig)
  671. (set-variable 'editorconfig-get-properties-function
  672. 'editorconfig-core-get-properties-hash)
  673. (editorconfig-mode 1)
  674. (set-variable 'editorconfig-mode-lighter " EC")
  675. (when (fboundp 'ws-butler-mode)
  676. (set-variable 'editorconfig-trim-whitespaces-mode
  677. 'ws-butler-mode))
  678. (with-eval-after-load 'org-src
  679. ;; [*.org\[\*Org Src*\[ c \]*\]]
  680. (add-hook 'org-src-mode-hook
  681. 'editorconfig-mode-apply t)))
  682. (when (fboundp 'editorconfig-custom-majormode)
  683. (add-hook 'editorconfig-after-apply-functions
  684. 'editorconfig-custom-majormode))
  685. ;; Add readonly=true to set read-only-mode
  686. (add-hook 'editorconfig-after-apply-functions
  687. (lambda (props)
  688. (let ((r (gethash 'readonly props)))
  689. (when (and (string= r "true")
  690. (not buffer-read-only))
  691. (read-only-mode 1)))))
  692. (add-hook 'editorconfig-hack-properties-functions
  693. '(lambda (props)
  694. (when (derived-mode-p 'makefile-mode)
  695. (puthash 'indent_style "tab" props))))
  696. ;; (when (fboundp 'editorconfig-charset-extras)
  697. ;; (add-hook 'editorconfig-custom-hooks
  698. ;; 'editorconfig-charset-extras))
  699. (setq revert-without-query '(".+"))
  700. ;; save cursor position
  701. (when (safe-require-or-eval 'saveplace)
  702. (setq-default save-place t)
  703. (setq save-place-file (concat user-emacs-directory
  704. "places")))
  705. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  706. (setq make-backup-files t)
  707. (setq vc-make-backup-files t)
  708. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  709. (setq backup-directory-alist
  710. (cons (cons "." (expand-file-name (concat user-emacs-directory
  711. "backup")))
  712. backup-directory-alist))
  713. (setq version-control 't)
  714. (setq delete-old-versions t)
  715. (setq kept-new-versions 20)
  716. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  717. "auto-save/")))
  718. ;; (setq delete-auto-save-files t)
  719. (setq auto-save-visited-interval 8)
  720. (auto-save-visited-mode 1)
  721. (add-to-list 'completion-ignored-extensions ".bak")
  722. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  723. (setq delete-by-moving-to-trash t)
  724. ;; trash-directory "~/.emacs.d/trash")
  725. (add-hook 'after-save-hook
  726. 'executable-make-buffer-file-executable-if-script-p)
  727. (set-variable 'bookmark-default-file
  728. (expand-file-name (concat user-emacs-directory
  729. "bmk")))
  730. (set-variable 'bookmark-save-flag
  731. 1)
  732. (with-eval-after-load 'recentf
  733. (defvar recentf-exclude)
  734. (defvar bookmark-default-file)
  735. (add-to-list 'recentf-exclude
  736. (regexp-quote bookmark-default-file)))
  737. (when (safe-require-or-eval 'smart-revert)
  738. (smart-revert-on))
  739. ;; autosave
  740. ;; auto-save-visited-mode can be used instead?
  741. ;; (when (safe-require-or-eval 'autosave)
  742. ;; (autosave-set 8))
  743. ;; bookmarks
  744. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  745. ;; vc
  746. (set-variable 'vc-handled-backends '(RCS))
  747. (set-variable 'vc-rcs-register-switches "-l")
  748. (set-variable 'vc-rcs-checkin-switches "-l")
  749. (set-variable 'vc-command-messages t)
  750. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  751. ;; share clipboard with x
  752. ;; this page describes this in details, but only these sexps seem to be needed
  753. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  754. (and nil
  755. (not window-system)
  756. (not (eq window-system 'mac))
  757. (getenv "DISPLAY")
  758. (not (equal (getenv "DISPLAY") ""))
  759. (executable-find "xclip")
  760. ;; (< emacs-major-version 24)
  761. '(safe-require-or-eval 'xclip)
  762. nil
  763. (turn-on-xclip))
  764. (and (eq system-type 'darwin)
  765. (safe-require-or-eval 'pasteboard)
  766. (turn-on-pasteboard))
  767. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  768. ;; some modes and hooks
  769. ;; Include some extra modes
  770. (require 'generic-x)
  771. (when (autoload-eval-lazily 'wgrep)
  772. (set-variable 'wgrep-auto-save-buffer t)
  773. (with-eval-after-load 'grep
  774. (defvar grep-mode-map)
  775. (define-key grep-mode-map
  776. "e"
  777. 'wgrep-change-to-wgrep-mode)))
  778. (with-eval-after-load 'remember
  779. (defvar remember-mode-map (make-sparse-keymap))
  780. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  781. (with-eval-after-load 'magit-files
  782. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  783. ;; existing keybindings.
  784. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  785. ;; to disable thie mode here, but do anyway.
  786. ;; See also https://github.com/magit/magit/issues/3517
  787. (global-magit-file-mode -1))
  788. (with-eval-after-load 'magit-section
  789. (set-face-background 'magit-section-highlight
  790. nil))
  791. (with-eval-after-load 'magit-diff
  792. (set-face-background 'magit-diff-added-highlight
  793. nil)
  794. (set-face-background 'magit-diff-removed-highlight
  795. nil)
  796. (set-face-background 'magit-diff-context-highlight
  797. nil)
  798. )
  799. (when (boundp 'git-rebase-filename-regexp)
  800. (add-to-list 'auto-mode-alist
  801. `(,git-rebase-filename-regexp . text-mode)))
  802. (when (safe-require-or-eval 'aggressive-indent)
  803. (defvar aggressive-indent-excluded-modes)
  804. (setq aggressive-indent-excluded-modes
  805. `(diff-mode
  806. toml-mode
  807. conf-mode
  808. dockerfile-mode
  809. groovy-mode
  810. scala-mode
  811. ,@aggressive-indent-excluded-modes))
  812. (global-aggressive-indent-mode 1))
  813. (when (autoload-eval-lazily 'ggtags)
  814. (add-hook 'c-mode-common-hook
  815. (lambda ()
  816. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  817. (ggtags-mode 1))))
  818. (add-hook 'python-mode-hook
  819. (lambda ()
  820. (ggtags-mode 1))))
  821. (when (autoload-eval-lazily 'imenu-list)
  822. ;; (set-variable 'imenu-list-auto-resize t)
  823. (set-variable 'imenu-list-focus-after-activation t)
  824. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  825. (add-hook 'emacs-lisp-mode-hook
  826. (lambda ()
  827. (setq imenu-generic-expression
  828. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  829. ,@imenu-generic-expression))))
  830. (with-eval-after-load 'compile
  831. (defvar compilation-filter-start)
  832. (defvar compilation-error-regexp-alist)
  833. (require 'ansi-color)
  834. (add-hook 'compilation-filter-hook
  835. (lambda ()
  836. (let ((inhibit-read-only t))
  837. (ansi-color-apply-on-region compilation-filter-start
  838. (point)))))
  839. (add-to-list 'compilation-error-regexp-alist
  840. ;; ansible-lint
  841. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  842. ;; Workaround to avoid ensime error
  843. (defvar ensime-mode-key-prefix nil)
  844. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  845. (when (safe-require-or-eval 'company)
  846. (global-company-mode)
  847. (set-variable 'company-idle-delay 0.5)
  848. (set-variable 'company-minimum-prefix-length 2)
  849. (set-variable 'company-selection-wrap-around t))
  850. ;; https://github.com/lunaryorn/flycheck
  851. (when (safe-require-or-eval 'flycheck)
  852. (call-after-init 'global-flycheck-mode))
  853. (when (autoload-eval-lazily 'ilookup)
  854. (define-key ctl-x-map "d" 'ilookup-open-word))
  855. (set-variable 'ac-ignore-case nil)
  856. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  857. (define-key ctl-x-map "t" 'term-run-shell-command))
  858. (add-to-list 'safe-local-variable-values
  859. '(encoding utf-8))
  860. (setq enable-local-variables :safe)
  861. ;; Detect file type from shebang and set major-mode.
  862. (add-to-list 'interpreter-mode-alist
  863. '("python3" . python-mode))
  864. (add-to-list 'interpreter-mode-alist
  865. '("python2" . python-mode))
  866. (with-eval-after-load 'python
  867. (defvar python-mode-map (make-sparse-keymap))
  868. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  869. (when (autoload-eval-lazily 'pipenv)
  870. (add-hook 'python-mode-hook
  871. (lambda ()
  872. (pipenv-mode 1)
  873. (pipenv-projectile-after-switch-default)))
  874. )
  875. (set-variable 'flycheck-python-pycompile-executable "python3")
  876. (set-variable 'python-indent-guess-indent-offset nil)
  877. (with-eval-after-load 'blacken
  878. (when (require 'with-venv nil t)
  879. (with-venv-advice-add 'blacken-buffer)))
  880. ;; http://fukuyama.co/foreign-regexp
  881. '(and (safe-require-or-eval 'foreign-regexp)
  882. (progn
  883. (setq foreign-regexp/regexp-type 'perl)
  884. '(setq reb-re-syntax 'foreign-regexp)
  885. ))
  886. (autoload-eval-lazily 'sql '(sql-mode)
  887. (require 'sql-indent nil t))
  888. (add-to-list 'auto-mode-alist
  889. '("\\.hql\\'" . sql-mode))
  890. (when (autoload-eval-lazily 'git-command)
  891. (define-key ctl-x-map "g" 'git-command))
  892. (when (autoload-eval-lazily 'gited)
  893. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  894. (when (safe-require-or-eval 'git-commit)
  895. (global-git-commit-mode 1))
  896. (with-eval-after-load 'git-commit
  897. (add-hook 'git-commit-setup-hook
  898. 'turn-off-auto-fill t))
  899. (autoload-eval-lazily 'sl)
  900. (with-eval-after-load 'rst
  901. (defvar rst-mode-map)
  902. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  903. (with-eval-after-load 'jdee
  904. (add-hook 'jdee-mode-hook
  905. (lambda ()
  906. (make-local-variable 'global-mode-string)
  907. (add-to-list 'global-mode-string
  908. mode-line-position))))
  909. ;; Cannot enable error thrown. Why???
  910. ;; https://github.com/m0smith/malabar-mode#Installation
  911. ;; (when (autoload-eval-lazily 'malabar-mode)
  912. ;; (add-to-list 'load-path
  913. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  914. ;; (safe-require-or-eval 'cedet-devel-load)
  915. ;; (call-after-init 'activate-malabar-mode))
  916. (with-eval-after-load 'make-mode
  917. (defvar makefile-mode-map (make-sparse-keymap))
  918. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  919. ;; this functions is set in write-file-functions, i cannot find any
  920. ;; good way to remove this.
  921. (fset 'makefile-warn-suspicious-lines 'ignore))
  922. (with-eval-after-load 'verilog-mode
  923. (defvar verilog-mode-map (make-sparse-keymap))
  924. (define-key verilog-mode-map ";" 'self-insert-command))
  925. (setq diff-switches "-u")
  926. (with-eval-after-load 'diff-mode
  927. ;; (when (and (eq major-mode
  928. ;; 'diff-mode)
  929. ;; (not buffer-file-name))
  930. ;; ;; do not pass when major-mode is derived mode of diff-mode
  931. ;; (view-mode 1))
  932. (set-face-attribute 'diff-header nil
  933. :foreground nil
  934. :background nil
  935. :weight 'bold)
  936. (set-face-attribute 'diff-file-header nil
  937. :foreground nil
  938. :background nil
  939. :weight 'bold)
  940. (set-face-foreground 'diff-index "blue")
  941. (set-face-attribute 'diff-hunk-header nil
  942. :foreground "cyan"
  943. :weight 'normal)
  944. (set-face-attribute 'diff-context nil
  945. ;; :foreground "white"
  946. :foreground nil
  947. :weight 'normal)
  948. (set-face-foreground 'diff-removed "red")
  949. (set-face-foreground 'diff-added "green")
  950. (set-face-background 'diff-removed nil)
  951. (set-face-background 'diff-added nil)
  952. (set-face-attribute 'diff-changed nil
  953. :foreground "magenta"
  954. :weight 'normal)
  955. (set-face-attribute 'diff-refine-changed nil
  956. :foreground nil
  957. :background nil
  958. :weight 'bold
  959. :inverse-video t)
  960. ;; Annoying !
  961. ;;(diff-auto-refine-mode)
  962. )
  963. ;; (ffap-bindings)
  964. (set-variable 'browse-url-browser-function
  965. 'eww-browse-url)
  966. (set-variable 'sh-here-document-word "__EOC__")
  967. (when (autoload-eval-lazily 'adoc-mode
  968. nil
  969. (defvar adoc-mode-map (make-sparse-keymap))
  970. (define-key adoc-mode-map (kbd "C-m") 'newline))
  971. (setq auto-mode-alist
  972. `(("\\.adoc\\'" . adoc-mode)
  973. ("\\.asciidoc\\'" . adoc-mode)
  974. ,@auto-mode-alist)))
  975. (with-eval-after-load 'markup-faces
  976. ;; Is this too match ?
  977. (set-face-foreground 'markup-meta-face
  978. "color-245")
  979. (set-face-foreground 'markup-meta-hide-face
  980. "color-245")
  981. )
  982. ;; TODO: check if this is required
  983. (when (autoload-eval-lazily 'groovy-mode nil
  984. (defvar groovy-mode-map (make-sparse-keymap))
  985. (define-key groovy-mode-map "(" 'self-insert-command)
  986. (define-key groovy-mode-map ")" 'self-insert-command)
  987. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  988. )
  989. (add-to-list 'auto-mode-alist
  990. '("build\\.gradle\\'" . groovy-mode)))
  991. (add-to-list 'auto-mode-alist
  992. '("\\.gawk\\'" . awk-mode))
  993. (with-eval-after-load 'yaml-mode
  994. (defvar yaml-mode-map (make-sparse-keymap))
  995. (define-key yaml-mode-map (kbd "C-m") 'newline))
  996. (with-eval-after-load 'html-mode
  997. (defvar html-mode-map (make-sparse-keymap))
  998. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  999. (with-eval-after-load 'text-mode
  1000. (define-key text-mode-map (kbd "C-m") 'newline))
  1001. (autoload-eval-lazily 'info nil
  1002. (defvar Info-additional-directory-list)
  1003. (dolist (dir (directory-files (concat user-emacs-directory
  1004. "info")
  1005. t
  1006. "^[^.].*"))
  1007. (when (file-directory-p dir)
  1008. (add-to-list 'Info-additional-directory-list
  1009. dir)))
  1010. (let ((dir (expand-file-name "~/.brew/share/info")))
  1011. (when (file-directory-p dir)
  1012. (add-to-list 'Info-additional-directory-list
  1013. dir))))
  1014. (with-eval-after-load 'apropos
  1015. (defvar apropos-mode-map (make-sparse-keymap))
  1016. (define-key apropos-mode-map "n" 'next-line)
  1017. (define-key apropos-mode-map "p" 'previous-line))
  1018. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1019. ;; (define-key isearch-mode-map
  1020. ;; (kbd "C-j") 'isearch-other-control-char)
  1021. ;; (define-key isearch-mode-map
  1022. ;; (kbd "C-k") 'isearch-other-control-char)
  1023. ;; (define-key isearch-mode-map
  1024. ;; (kbd "C-h") 'isearch-other-control-char)
  1025. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1026. (define-key isearch-mode-map (kbd "M-r")
  1027. 'isearch-query-replace-regexp)
  1028. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1029. (setq lazy-highlight-cleanup nil)
  1030. ;; face for isearch highlighing
  1031. (set-face-attribute 'lazy-highlight
  1032. nil
  1033. :foreground `unspecified
  1034. :background `unspecified
  1035. :underline t
  1036. ;; :weight `bold
  1037. )
  1038. (add-hook 'outline-mode-hook
  1039. (lambda ()
  1040. (when (string-match "\\.md\\'" buffer-file-name)
  1041. (set (make-local-variable 'outline-regexp) "#+ "))))
  1042. (add-hook 'outline-mode-hook
  1043. 'outline-show-all)
  1044. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1045. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1046. (when (autoload-eval-lazily 'markdown-mode
  1047. '(markdown-mode gfm-mode)
  1048. (defvar gfm-mode-map (make-sparse-keymap))
  1049. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1050. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1051. (set-variable 'markdown-command (or (executable-find "markdown")
  1052. (executable-find "markdown.pl")
  1053. ""))
  1054. (add-hook 'markdown-mode-hook
  1055. (lambda ()
  1056. (outline-minor-mode 1)
  1057. (flyspell-mode)
  1058. (set (make-local-variable 'comment-start) ";")))
  1059. )
  1060. ;; c-mode
  1061. ;; http://www.emacswiki.org/emacs/IndentingC
  1062. ;; http://en.wikipedia.org/wiki/Indent_style
  1063. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1064. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1065. (with-eval-after-load 'cc-vars
  1066. (defvar c-default-style nil)
  1067. (add-to-list 'c-default-style
  1068. '(c-mode . "k&r"))
  1069. (add-to-list 'c-default-style
  1070. '(c++-mode . "k&r")))
  1071. (autoload-eval-lazily 'js2-mode nil
  1072. ;; currently do not use js2-mode
  1073. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1074. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1075. ;; (defvar js2-mode-map (make-sparse-keymap))
  1076. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1077. ;; (interactive)
  1078. ;; (js2-enter-key)
  1079. ;; (indent-for-tab-command)))
  1080. ;; (add-hook (kill-local-variable 'before-save-hook)
  1081. ;; 'js2-before-save)
  1082. ;; (add-hook 'before-save-hook
  1083. ;; 'my-indent-buffer
  1084. ;; nil
  1085. ;; t)
  1086. )
  1087. (add-to-list 'interpreter-mode-alist
  1088. '("node" . js-mode))
  1089. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1090. (with-eval-after-load 'uniquify
  1091. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1092. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1093. (setq uniquify-min-dir-content 1))
  1094. (with-eval-after-load 'view
  1095. (defvar view-mode-map (make-sparse-keymap))
  1096. (define-key view-mode-map "j" 'scroll-up-line)
  1097. (define-key view-mode-map "k" 'scroll-down-line)
  1098. (define-key view-mode-map "v" 'toggle-read-only)
  1099. (define-key view-mode-map "q" 'bury-buffer)
  1100. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1101. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1102. ;; (define-key view-mode-map
  1103. ;; "n" 'nonincremental-repeat-search-forward)
  1104. ;; (define-key view-mode-map
  1105. ;; "N" 'nonincremental-repeat-search-backward)
  1106. ;; N conflicts with git-walktree
  1107. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1108. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1109. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1110. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1111. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1112. (global-set-key "\M-r" 'view-mode)
  1113. ;; (setq view-read-only t)
  1114. (with-eval-after-load 'term
  1115. (defvar term-raw-map (make-sparse-keymap))
  1116. (define-key term-raw-map (kbd "C-x")
  1117. (lookup-key (current-global-map)
  1118. (kbd "C-x"))))
  1119. (add-hook 'term-mode-hook
  1120. (lambda ()
  1121. ;; Stop current line highlighting
  1122. (set (make-local-variable (defvar hl-line-range-function))
  1123. (lambda () '(0 . 0)))
  1124. (set (make-local-variable 'scroll-margin)
  1125. 0)
  1126. (set-variable 'term-buffer-maximum-size 20480)
  1127. ))
  1128. (add-hook 'Man-mode-hook
  1129. (lambda ()
  1130. (view-mode 1)
  1131. (setq truncate-lines nil)))
  1132. (set-variable 'Man-notify-method (if window-system
  1133. 'newframe
  1134. 'aggressive))
  1135. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1136. "woman_cache.el")))
  1137. ;; not work because man.el will be loaded when man called
  1138. (defalias 'man 'woman)
  1139. (add-to-list 'auto-mode-alist
  1140. '("tox\\.ini\\'" . conf-unix-mode))
  1141. (when (autoload-eval-lazily 'toml-mode)
  1142. (add-to-list 'auto-mode-alist
  1143. '("/tox\\.ini\\'" . toml-mode))
  1144. (add-to-list 'auto-mode-alist
  1145. '("/Pipfile\\'" . toml-mode))
  1146. (add-to-list 'auto-mode-alist
  1147. '("/poetry\\.lock\\'" . toml-mode))
  1148. )
  1149. (when (autoload-eval-lazily 'json-mode)
  1150. (add-to-list 'auto-mode-alist
  1151. '("/Pipfile\\.lock\\'" . json-mode)))
  1152. (add-hook 'go-mode-hook
  1153. (lambda()
  1154. (defvar go-mode-map)
  1155. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1156. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1157. (when (autoload-eval-lazily 'k8s-mode)
  1158. (add-to-list 'auto-mode-alist
  1159. '("\\.k8s\\'" . k8s-mode)))
  1160. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1161. ;; buffers
  1162. (defvar bs-configurations)
  1163. (when (autoload-eval-lazily 'bs '(bs-show)
  1164. (add-to-list 'bs-configurations
  1165. '("specials" "^\\*" nil ".*" nil nil))
  1166. (add-to-list 'bs-configurations
  1167. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1168. (defvar bs-mode-map)
  1169. (defvar bs-current-configuration)
  1170. (define-key bs-mode-map (kbd "t")
  1171. ;; TODO: fix toggle feature
  1172. (lambda ()
  1173. (interactive)
  1174. (if (string= "specials"
  1175. bs-current-configuration)
  1176. (bs-set-configuration "files")
  1177. (bs-set-configuration "specials"))
  1178. (bs-refresh)
  1179. (bs-message-without-log "%s"
  1180. (bs--current-config-message))))
  1181. ;; (setq bs-configurations (list
  1182. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1183. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1184. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1185. )
  1186. (defalias 'list-buffers 'bs-show)
  1187. (set-variable 'bs-default-configuration "files-and-specials")
  1188. (set-variable 'bs-default-sort-name "by nothing")
  1189. (add-hook 'bs-mode-hook
  1190. (lambda ()
  1191. (set (make-local-variable 'scroll-margin) 0))))
  1192. ;;(iswitchb-mode 1)
  1193. (icomplete-mode)
  1194. (defun iswitchb-buffer-display-other-window ()
  1195. "Do iswitchb in other window."
  1196. (interactive)
  1197. (let ((iswitchb-default-method 'display))
  1198. (call-interactively 'iswitchb-buffer)))
  1199. ;; buffer killing
  1200. ;; (defun my-delete-window-killing-buffer () nil)
  1201. (defun my-query-kill-current-buffer ()
  1202. "Interactively kill current buffer."
  1203. (interactive)
  1204. (if (y-or-n-p (concat "kill current buffer? :"))
  1205. (kill-buffer (current-buffer))))
  1206. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1207. (substitute-key-definition 'kill-buffer
  1208. 'my-query-kill-current-buffer
  1209. global-map)
  1210. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1211. ;; recentf-mode
  1212. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1213. "recentf")))
  1214. (set-variable 'recentf-max-menu-items 20)
  1215. (set-variable 'recentf-max-saved-items 30)
  1216. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1217. (set-variable 'recentf-auto-cleanup 3)
  1218. ;; (safe-require-or-eval 'sync-recentf)
  1219. ;; (when (safe-require-or-eval 'recentf)
  1220. ;; (add-to-list 'recentf-exclude
  1221. ;; (regexp-quote recentf-save-file))
  1222. ;; (add-to-list 'recentf-exclude
  1223. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1224. ;; (add-to-list 'recentf-exclude
  1225. ;; "/sync-recentf-marker\\'")
  1226. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1227. ;; (remove-hook 'find-file-hook
  1228. ;; 'recentf-track-opened-file)
  1229. ;; (defun my-recentf-load-track-save-list ()
  1230. ;; "Load current recentf list from file, track current visiting file, then save
  1231. ;; the list."
  1232. ;; (recentf-load-list)
  1233. ;; (recentf-track-opened-file)
  1234. ;; (recentf-save-list))
  1235. ;; (add-hook 'find-file-hook
  1236. ;; 'my-recentf-load-track-save-list)
  1237. ;; (add-hook 'kill-emacs-hook
  1238. ;; 'recentf-load-list)
  1239. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1240. ;; ;; (add-hook 'find-file-hook
  1241. ;; ;; (lambda ()
  1242. ;; ;; (recentf-add-file default-directory)))
  1243. ;; (when (autoload-eval-lazily 'recentf-show)
  1244. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1245. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1246. ;; ;; 'recentf-load-list)
  1247. ;; )
  1248. ;; (recentf-mode 1)
  1249. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1250. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1251. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1252. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1253. ;; (add-hook 'recentf-dialog-mode-hook
  1254. ;; (lambda ()
  1255. ;; ;; (recentf-save-list)
  1256. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1257. ;; ;; 'my-recentf-cd-and-find-file)
  1258. ;; (cd "~/"))))
  1259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1260. ;; dired
  1261. (defun my-file-head (filename &optional n)
  1262. "Return list of first N lines of file FILENAME."
  1263. ;; TODO: Fix for janapese text
  1264. ;; TODO: Fix for short text
  1265. (let ((num (or n 10))
  1266. (size 100)
  1267. (beg 0)
  1268. (end 0)
  1269. (result '())
  1270. (read -1))
  1271. (with-temp-buffer
  1272. (erase-buffer)
  1273. (while (or (<= (count-lines (point-min)
  1274. (point-max))
  1275. num)
  1276. (eq read 0))
  1277. (setq end (+ beg size))
  1278. (setq read (nth 1 (insert-file-contents-literally filename
  1279. nil
  1280. beg
  1281. end)))
  1282. (goto-char (point-max))
  1283. (setq beg (+ beg size)))
  1284. (goto-char (point-min))
  1285. (while (< (length result) num)
  1286. (let ((start (point)))
  1287. (forward-line 1)
  1288. (setq result
  1289. `(,@result ,(buffer-substring-no-properties start
  1290. (point))))))
  1291. result
  1292. ;; (buffer-substring-no-properties (point-min)
  1293. ;; (progn
  1294. ;; (forward-line num)
  1295. ;; (point)))
  1296. )))
  1297. ;; (apply 'concat (my-file-head "./shrc" 10)
  1298. (defun my-dired-echo-file-head (arg)
  1299. "Echo head of current file.
  1300. ARG is num to show, or defaults to 7."
  1301. (interactive "P")
  1302. (let ((f (dired-get-filename)))
  1303. (message "%s"
  1304. (apply 'concat
  1305. (my-file-head f
  1306. 7)))))
  1307. (defun my-dired-diff ()
  1308. "Show diff of marked file and file of current line."
  1309. (interactive)
  1310. (let ((files (dired-get-marked-files nil nil nil t)))
  1311. (if (eq (car files)
  1312. t)
  1313. (diff (cadr files) (dired-get-filename))
  1314. (message "One file must be marked!"))))
  1315. (defun dired-get-file-info ()
  1316. "Print information of current line file."
  1317. (interactive)
  1318. (let ((f (shell-quote-argument (dired-get-filename t))))
  1319. (if (file-directory-p f)
  1320. (progn
  1321. (message "Calculating disk usage...")
  1322. (shell-command (concat "du -hsD "
  1323. f)))
  1324. (shell-command (concat "file "
  1325. f)))))
  1326. (defun my-dired-scroll-up ()
  1327. "Scroll up."
  1328. (interactive)
  1329. (my-dired-previous-line (- (window-height) 1)))
  1330. (defun my-dired-scroll-down ()
  1331. "Scroll down."
  1332. (interactive)
  1333. (my-dired-next-line (- (window-height) 1)))
  1334. ;; (defun my-dired-forward-line (arg)
  1335. ;; ""
  1336. ;; (interactive "p"))
  1337. (defun my-dired-previous-line (arg)
  1338. "Move ARG lines up."
  1339. (interactive "p")
  1340. (if (> arg 0)
  1341. (progn
  1342. (if (eq (line-number-at-pos)
  1343. 1)
  1344. (goto-char (point-max))
  1345. (forward-line -1))
  1346. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1347. (dired-get-subdir))
  1348. (- arg 1)
  1349. arg)))
  1350. (dired-move-to-filename)))
  1351. (defun my-dired-next-line (arg)
  1352. "Move ARG lines down."
  1353. (interactive "p")
  1354. (if (> arg 0)
  1355. (progn
  1356. (if (eq (point)
  1357. (point-max))
  1358. (goto-char (point-min))
  1359. (forward-line 1))
  1360. (my-dired-next-line (if (or (dired-get-filename nil t)
  1361. (dired-get-subdir))
  1362. (- arg 1)
  1363. arg)))
  1364. (dired-move-to-filename)))
  1365. (defun my-tramp-remote-find-file (f)
  1366. "Open F."
  1367. (interactive (list (read-file-name "My Find File Tramp: "
  1368. "/scp:"
  1369. nil ;; "/scp:"
  1370. (confirm-nonexistent-file-or-buffer))))
  1371. (find-file f))
  1372. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1373. (if (eq window-system 'mac)
  1374. (setq dired-listing-switches "-lhF")
  1375. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1376. )
  1377. (setq dired-listing-switches "-lhF")
  1378. ;; when using dired-find-alternate-file
  1379. ;; reuse current dired buffer for the file to open
  1380. ;; (put 'dired-find-alternate-file 'disabled nil)
  1381. (set-variable 'dired-ls-F-marks-symlinks t)
  1382. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1383. (set-variable 'ls-lisp-dirs-first t)
  1384. (set-variable 'ls-lisp-use-localized-time-format t)
  1385. (set-variable 'ls-lisp-format-time-list
  1386. '("%Y-%m-%d %H:%M"
  1387. "%Y-%m-%d "))
  1388. (set-variable 'dired-dwim-target t)
  1389. (set-variable 'dired-isearch-filenames t)
  1390. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1391. (set-variable 'dired-hide-details-hide-information-lines nil)
  1392. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1393. (set-variable 'dired-recursive-deletes 'always)
  1394. ;; (add-hook 'dired-after-readin-hook
  1395. ;; 'my-replace-nasi-none)
  1396. (with-eval-after-load 'dired
  1397. (safe-require-or-eval 'ls-lisp)
  1398. (defvar dired-mode-map (make-sparse-keymap))
  1399. ;; dired-do-chgrp sometimes cause system hung
  1400. (define-key dired-mode-map "G" 'ignore)
  1401. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1402. (define-key dired-mode-map "i" 'dired-get-file-info)
  1403. ;; (define-key dired-mode-map "f" 'find-file)
  1404. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1405. (define-key dired-mode-map "z" 'fzf)
  1406. (define-key dired-mode-map "!" 'shell-command)
  1407. (define-key dired-mode-map "&" 'async-shell-command)
  1408. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1409. (define-key dired-mode-map "=" 'my-dired-diff)
  1410. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1411. (define-key dired-mode-map "b" 'gtkbm)
  1412. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1413. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1414. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1415. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1416. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1417. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1418. (substitute-key-definition 'dired-next-line
  1419. 'my-dired-next-line
  1420. dired-mode-map)
  1421. (substitute-key-definition 'dired-previous-line
  1422. 'my-dired-previous-line
  1423. dired-mode-map)
  1424. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1425. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1426. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1427. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1428. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1429. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1430. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1431. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1432. (add-hook 'dired-mode-hook
  1433. (lambda ()
  1434. (when (fboundp 'dired-hide-details-mode)
  1435. (dired-hide-details-mode t)
  1436. (local-set-key "l" 'dired-hide-details-mode))
  1437. (let ((file "._Icon\015"))
  1438. (when nil
  1439. '(file-readable-p file)
  1440. (delete-file file)))))
  1441. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1442. (add-to-list 'pack-program-alist
  1443. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1444. (set-variable 'pack-silence
  1445. t)
  1446. (with-eval-after-load 'dired
  1447. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1448. (when (autoload-eval-lazily 'dired-list-all-mode)
  1449. (setq dired-listing-switches "-lhF")
  1450. (with-eval-after-load 'dired
  1451. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1452. (when (autoload-eval-lazily 'dired-filter)
  1453. (add-hook 'dired-mode-hook
  1454. 'dired-filter-mode))
  1455. (set-variable 'dired-filter-stack nil)
  1456. ;; Currently disabled in favor of dired-from-git-ls-files
  1457. ;; (define-key ctl-x-map "f" 'find-dired)
  1458. ;; It works!
  1459. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1460. (defun my-dired-git-ls-files (args)
  1461. "Dired from git ls-files."
  1462. (interactive "sgit ls-files args: ")
  1463. (pop-to-buffer-same-window
  1464. (dired-noselect `(,default-directory
  1465. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1466. "\0" t))
  1467. ""))
  1468. )
  1469. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1470. (with-eval-after-load 'dired
  1471. (defvar dired-mode-map (make-sparse-keymap))
  1472. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1473. ;; (define-minor-mode my-dired-glob-filter)
  1474. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1475. ;; misc funcs
  1476. (when (fboundp 'browse-url-default-macosx-browser)
  1477. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1478. (defalias 'qcalc 'quick-calc)
  1479. (defun memo (&optional dir)
  1480. "Open memo.txt in DIR."
  1481. (interactive)
  1482. (pop-to-buffer (find-file-noselect (concat (if dir
  1483. (file-name-as-directory dir)
  1484. "")
  1485. "memo.txt"))))
  1486. (set (defvar my-rgrep-alist nil
  1487. "Alist of rgrep command.
  1488. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1489. condition to choose COMMAND when evaluated.")
  1490. `(
  1491. ;; ripgrep
  1492. ("rg"
  1493. (executable-find "rg")
  1494. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1495. ;; git grep
  1496. ("gitgrep"
  1497. (eq 0
  1498. (shell-command "git rev-parse --git-dir"))
  1499. "git --no-pager -c color.grep=always grep -nH -e ")
  1500. ;; sift
  1501. ("sift"
  1502. (executable-find "sift")
  1503. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1504. ;; the silver searcher
  1505. ("ag"
  1506. (executable-find "ag")
  1507. "ag --nogroup --nopager --filename ")
  1508. ;; ack
  1509. ("ack"
  1510. (executable-find "ack")
  1511. "ack --nogroup --nopager --with-filename ")
  1512. ;; gnu global
  1513. ("global"
  1514. (and (require 'ggtags nil t)
  1515. (executable-find "global")
  1516. (ggtags-current-project-root))
  1517. "global --result grep ")
  1518. ;; grep
  1519. ("grep"
  1520. t
  1521. ,(concat "find . "
  1522. "-path '*/.git' -prune -o "
  1523. "-path '*/.svn' -prune -o "
  1524. "-type f -print0 | "
  1525. "xargs -0 grep -nH -e "))
  1526. )
  1527. )
  1528. (defvar my-rgrep-default nil
  1529. "Default command name for my-rgrep.")
  1530. (defun my-rgrep-grep-command (&optional name alist)
  1531. "Return recursive grep command for current directory or nil.
  1532. If NAME is given, use that without testing.
  1533. Commands are searched from ALIST."
  1534. (if alist
  1535. (if name
  1536. ;; if name is given search that from alist and return the command
  1537. (nth 2 (assoc name
  1538. alist))
  1539. ;; if name is not given try test in 1th elem
  1540. (let ((car (car alist))
  1541. (cdr (cdr alist)))
  1542. (if (eval (nth 1 car))
  1543. ;; if the condition is true return the command
  1544. (nth 2 car)
  1545. ;; try next one
  1546. (and cdr
  1547. (my-rgrep-grep-command name cdr)))))
  1548. ;; if alist is not given set default value
  1549. (my-rgrep-grep-command name my-rgrep-alist)))
  1550. (defun my-rgrep (command-args)
  1551. "My recursive grep. Run COMMAND-ARGS.
  1552. If prefix argument is given, use current symbol as default search target
  1553. and search from projectile root (if projectile is available)."
  1554. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1555. nil)))
  1556. (if cmd
  1557. (list (read-shell-command "grep command: "
  1558. (concat cmd
  1559. (if current-prefix-arg
  1560. (thing-at-point 'symbol t)
  1561. ""))
  1562. 'grep-find-history))
  1563. (error "My-Rgrep: Command for rgrep not found")
  1564. )))
  1565. (if (and current-prefix-arg
  1566. (safe-require-or-eval 'projectile)
  1567. (projectile-project-p))
  1568. (projectile-with-default-dir (projectile-project-root)
  1569. (compilation-start command-args
  1570. 'grep-mode))
  1571. (compilation-start command-args
  1572. 'grep-mode)))
  1573. (defun my-rgrep-thing-at-point-projectile-root ()
  1574. "My recursive grep to find thing at point from project root."
  1575. (interactive)
  1576. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1577. nil))
  1578. (command-args
  1579. (if cmd
  1580. (concat cmd
  1581. (or (thing-at-point 'symbol t)
  1582. (error "No symbol at point")))
  1583. (error "My-Rgrep: Command for rgrep not found"))))
  1584. (if (safe-require-or-eval 'projectile)
  1585. (projectile-with-default-dir (or (projectile-project-root)
  1586. default-directory)
  1587. (compilation-start command-args
  1588. 'grep-mode))
  1589. (compilation-start command-args
  1590. 'grep-mode))))
  1591. (defmacro define-my-rgrep (name)
  1592. "Define rgrep for NAME."
  1593. `(defun ,(intern (concat "my-rgrep-"
  1594. name)) ()
  1595. ,(format "My recursive grep by %s."
  1596. name)
  1597. (interactive)
  1598. (let ((my-rgrep-default ,name))
  1599. (if (called-interactively-p 'any)
  1600. (call-interactively 'my-rgrep)
  1601. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1602. )
  1603. (define-my-rgrep "ack")
  1604. (define-my-rgrep "ag")
  1605. (define-my-rgrep "rg")
  1606. (define-my-rgrep "sift")
  1607. (define-my-rgrep "gitgrep")
  1608. (define-my-rgrep "grep")
  1609. (define-my-rgrep "global")
  1610. (define-key ctl-x-map "s" 'my-rgrep)
  1611. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1612. (defun my-occur (regexp &optional region)
  1613. "My occur command to search REGEXP."
  1614. (interactive (list (read-string "List lines matching regexp: "
  1615. (thing-at-point 'symbol t))))
  1616. (occur regexp nil region))
  1617. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1618. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1619. (defalias 'make 'compile)
  1620. (define-key ctl-x-map "c" 'compile)
  1621. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1622. ;; editorconfig-auto-apply
  1623. (define-minor-mode editorconfig-auto-apply-mode
  1624. "When saving .editorconfig file update buffer configs."
  1625. :global t ;; TODO: global nil and instruct to hook to editorconfig-conf-mode-hook
  1626. :lighter ""
  1627. (if editorconfig-auto-apply-mode
  1628. (add-hook 'after-save-hook
  1629. 'editorconfig-auto-apply-mode--run)
  1630. (remove-hook 'after-save-hook
  1631. 'editorconfig-auto-apply-mode--run)))
  1632. (defun editorconfig-auto-apply-mode--run ()
  1633. "When saving .editorconfig file walk all buffers and update configs."
  1634. (when (eq major-mode
  1635. 'editorconfig-conf-mode)
  1636. (let ((dir (file-name-directory buffer-file-name)))
  1637. (cl-dolist (buf (buffer-list))
  1638. (when (and (buffer-file-name buf)
  1639. (file-in-directory-p (buffer-file-name buf)
  1640. dir))
  1641. (with-current-buffer buf
  1642. (editorconfig-mode-apply)))))))
  1643. (editorconfig-auto-apply-mode 1)
  1644. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1645. (define-key ctl-x-map "T" 'git-worktree)
  1646. (define-key ctl-x-map "W" 'git-walktree)
  1647. ;;;;;;;;;;;;;;;;
  1648. ;; flychcek-black
  1649. (require 'flycheck)
  1650. (flycheck-define-checker python-black-check
  1651. "A Python style checker."
  1652. :command ("python3" "-m" "black"
  1653. "--check"
  1654. (config-file "--config" flycheck-black)
  1655. source)
  1656. :error-parser flycheck-parse-black-check
  1657. ;; :error-patterns
  1658. ;; (
  1659. ;; (error line-start "error: cannot format " (file-name) ": " (message) ": " line ":" column ": " (one-or-more any) line-end)
  1660. ;; (error line-start (message) " " (file-name) line-end)
  1661. ;; )
  1662. :enabled (lambda ()
  1663. (or (not (flycheck-python-needs-module-p 'python-black-check))
  1664. (flycheck-python-find-module 'python-black-check "black")))
  1665. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "black"))
  1666. :modes python-mode)
  1667. ;; (flycheck-define-checker python-black-diff
  1668. ;; "A Python style checker."
  1669. ;; :command ("python3"
  1670. ;; "-m" "black"
  1671. ;; (config-file "--config" flycheck-black)
  1672. ;; "--diff" source)
  1673. ;; :error-parser my-flycheck-parse-unified-diff
  1674. ;; :enabled (lambda ()
  1675. ;; (or (not (flycheck-python-needs-module-p 'python-black))
  1676. ;; (flycheck-python-find-module 'python-black "black")))
  1677. ;; :verify (lambda (_) (flycheck-python-verify-module 'python-black "black"))
  1678. ;; :modes python-mode)
  1679. (flycheck-def-config-file-var flycheck-black python-black-check "pyproject.toml"
  1680. :safe #'stringp)
  1681. (add-to-list 'flycheck-checkers
  1682. 'python-black-check)
  1683. (defun flycheck-parse-black-check (output checker buffer)
  1684. "Flycheck parser to check if reformat is required."
  1685. (let ((result nil))
  1686. (with-temp-buffer
  1687. (insert output)
  1688. (save-match-data
  1689. (goto-char (point-min))
  1690. (when (re-search-forward "^would reformat .*$" nil t)
  1691. (add-to-list 'result (flycheck-error-new-at
  1692. (point-min)
  1693. nil
  1694. 'error
  1695. ;;(format "Black: %s" (match-string 0))
  1696. "Black: would reformat"
  1697. :buffer buffer
  1698. :checker checker)))
  1699. (goto-char (point-min))
  1700. (when (re-search-forward "^error: .*$" nil t)
  1701. (add-to-list 'result (flycheck-error-new-at
  1702. (point-min)
  1703. nil
  1704. 'error
  1705. ;; Fix not to include absolute file path
  1706. (format "Black: %s" (match-string 0))
  1707. :buffer buffer
  1708. :checker checker)))))
  1709. result))
  1710. (defun my-flycheck-parse-unified-diff (output checker buffer)
  1711. "Flycheck parser to parse diff output."
  1712. (let ((source-line 0)
  1713. (result ())
  1714. (hunk "HUNK"))
  1715. (with-temp-buffer
  1716. (insert output)
  1717. (goto-char (point-min))
  1718. (while (not (eq (point) (point-max)))
  1719. ;; FIXME: Do not stop when no result
  1720. (while (not (re-search-forward "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*$" (point-at-eol) t))
  1721. (forward-line 1)
  1722. (goto-char (point-at-bol)))
  1723. (setq source-line
  1724. (string-to-number (match-string 1)))
  1725. ;; TODO: Add filename support
  1726. (setq hunk
  1727. (match-string 0))
  1728. ;;(while (not (rearch-forward "^\\(-\\|\\+\\)")))
  1729. )
  1730. (add-to-list 'result
  1731. (flycheck-error-new-at
  1732. 0
  1733. nil
  1734. 'error
  1735. "MESSAGE"
  1736. :buffer buffer
  1737. :checker checker
  1738. :group hunk)))
  1739. result))
  1740. (set-variable 'flycheck-python-mypy-ini
  1741. ".mypy.ini")
  1742. ;; Local Variables:
  1743. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1744. ;; flycheck-checker: emacs-lisp
  1745. ;; End:
  1746. ;;; emancs.el ends here