Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

2736 rindas
89 KiB

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