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

2402 lines
79 KiB

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