You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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