Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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