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.
 
 
 
 
 
 

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