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.
 
 
 
 
 
 

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