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

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