25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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