Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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