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.
 
 
 
 
 
 

1958 regels
64 KiB

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