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

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