Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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