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.
 
 
 
 
 
 

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