Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2574 linhas
84 KiB

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