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.
 
 
 
 
 
 

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