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.
 
 
 
 
 
 

2016 lines
66 KiB

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