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.
 
 
 
 
 
 

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