Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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