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.
 
 
 
 
 
 

2345 lines
77 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. (when (fboundp 'web-mode)
  927. (add-to-list 'auto-mode-alist
  928. '("\\.html\\.j2\\'" . web-mode))
  929. )
  930. (when (autoload-eval-lazily 'wgrep)
  931. (set-variable 'wgrep-auto-save-buffer t)
  932. (with-eval-after-load 'grep
  933. (defvar grep-mode-map)
  934. (define-key grep-mode-map
  935. "e"
  936. 'wgrep-change-to-wgrep-mode)))
  937. (when (fboundp 'grep-context-mode)
  938. (add-hook 'compilation-mode-hook #'grep-context-mode))
  939. (with-eval-after-load 'remember
  940. (defvar remember-mode-map)
  941. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  942. (set-variable 'remember-notes-initial-major-mode
  943. 'change-log-mode)
  944. (with-eval-after-load 'magit-files
  945. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  946. ;; existing keybindings.
  947. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  948. ;; to disable thie mode here, but do anyway.
  949. ;; See also https://github.com/magit/magit/issues/3517
  950. (global-magit-file-mode -1))
  951. (with-eval-after-load 'magit-section
  952. (set-face-background 'magit-section-highlight
  953. nil))
  954. ;; Sane colors
  955. (with-eval-after-load 'magit-diff
  956. (set-face-background 'magit-diff-context nil)
  957. (set-face-background 'magit-diff-context-highlight nil)
  958. (set-face-foreground 'magit-diff-hunk-heading nil)
  959. (set-face-background 'magit-diff-hunk-heading nil)
  960. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  961. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  962. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  963. (set-face-foreground 'magit-diff-added "green")
  964. (set-face-background 'magit-diff-added nil)
  965. (set-face-foreground 'magit-diff-added-highlight "green")
  966. (set-face-background 'magit-diff-added-highlight nil)
  967. (set-face-foreground 'magit-diff-removed "red")
  968. (set-face-background 'magit-diff-removed nil)
  969. (set-face-foreground 'magit-diff-removed-highlight "red")
  970. (set-face-background 'magit-diff-removed-highlight nil)
  971. (set-face-background 'magit-diff-lines-boundary "blue")
  972. )
  973. (when (boundp 'git-rebase-filename-regexp)
  974. (add-to-list 'auto-mode-alist
  975. `(,git-rebase-filename-regexp . text-mode)))
  976. (when (safe-require-or-eval 'aggressive-indent)
  977. (defvar aggressive-indent-excluded-modes)
  978. (setq aggressive-indent-excluded-modes
  979. `(diff-mode
  980. toml-mode
  981. conf-mode
  982. dockerfile-mode
  983. groovy-mode
  984. scala-mode
  985. ,@aggressive-indent-excluded-modes))
  986. (global-aggressive-indent-mode 1))
  987. (when (fboundp 'ggtags-mode)
  988. ;; TODO: Do not use for emacs-lisp-mode
  989. (add-hook 'prog-mode-hook
  990. 'ggtags-mode))
  991. (when (autoload-eval-lazily 'imenu-list)
  992. ;; (set-variable 'imenu-list-auto-resize t)
  993. (set-variable 'imenu-list-focus-after-activation t)
  994. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  995. (add-hook 'emacs-lisp-mode-hook
  996. (lambda ()
  997. (setq imenu-generic-expression
  998. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  999. ,@imenu-generic-expression))))
  1000. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1001. (with-eval-after-load 'compile
  1002. (defvar compilation-filter-start)
  1003. (defvar compilation-error-regexp-alist)
  1004. (require 'ansi-color)
  1005. (add-hook 'compilation-filter-hook
  1006. (lambda ()
  1007. (let ((inhibit-read-only t))
  1008. (ansi-color-apply-on-region compilation-filter-start
  1009. (point)))))
  1010. (add-to-list 'compilation-error-regexp-alist
  1011. ;; ansible-lint
  1012. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1013. (add-to-list 'compilation-error-regexp-alist
  1014. ;; pydocstyle
  1015. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1016. )
  1017. ;; Workaround to avoid ensime error
  1018. (defvar ensime-mode-key-prefix nil)
  1019. (when (safe-require-or-eval 'company)
  1020. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1021. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1022. (global-company-mode)
  1023. (set-variable 'company-idle-delay nil)
  1024. (set-variable 'company-minimum-prefix-length 2)
  1025. (set-variable 'company-selection-wrap-around t)
  1026. (defvar company-mode-map)
  1027. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1028. ;; (with-eval-after-load 'python
  1029. ;; (defvar python-indent-trigger-commands)
  1030. ;; ;; TODO: This disables completion in puthon?
  1031. ;; (add-to-list 'python-indent-trigger-commands
  1032. ;; 'company-indent-or-complete-common))
  1033. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1034. (defvar company-active-map)
  1035. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1036. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1037. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1038. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1039. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1040. (defvar company-mode)
  1041. (defvar company-candidates)
  1042. (defvar company-candidates-length)
  1043. ;; (popup-tip "Hello, World!")
  1044. (defun my-company-lighter-current-length ()
  1045. "Get current candidate length."
  1046. (interactive)
  1047. (let ((l nil)
  1048. (inhibit-message t))
  1049. (when (and company-mode
  1050. (not (minibufferp))
  1051. ;; Do nothing when already in company completion
  1052. (not company-candidates))
  1053. ;; FIXME: Somehow it cannto catch errors from ggtags
  1054. (ignore-errors
  1055. ;; (company-auto-begin)
  1056. (company-manual-begin)
  1057. (setq l company-candidates-length)
  1058. (company-cancel)))
  1059. (if l
  1060. (format "[%d]" l)
  1061. "")))
  1062. (defvar company-lighter)
  1063. (set-variable 'company-lighter-base "Cmp")
  1064. ;; (add-to-list 'company-lighter
  1065. ;; '(:eval (my-company-lighter-current-length))
  1066. ;; t)
  1067. ;; This breaks japanese text input
  1068. ;; (set-variable 'my-company-length-popup-tip-timer
  1069. ;; (run-with-idle-timer 0.2 t
  1070. ;; 'my-company-length-popup-tip))
  1071. ;; (current-active-maps)
  1072. ;; (lookup-key)
  1073. '(mapcar (lambda (map)
  1074. (lookup-key map (kbd "C-i")))
  1075. (current-active-maps))
  1076. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1077. (set-face-attribute 'company-tooltip nil
  1078. :foreground "black" :background "lightgrey")
  1079. (set-face-attribute 'company-tooltip-common nil
  1080. :foreground "black" :background "lightgrey")
  1081. (set-face-attribute 'company-tooltip-common-selection nil
  1082. :foreground "white" :background "steelblue")
  1083. (set-face-attribute 'company-tooltip-selection nil
  1084. :foreground "black" :background "steelblue")
  1085. (set-face-attribute 'company-preview-common nil
  1086. :background nil :foreground "lightgrey" :underline t)
  1087. (set-face-attribute 'company-scrollbar-fg nil
  1088. :background "orange")
  1089. (set-face-attribute 'company-scrollbar-bg nil
  1090. :background "gray40")
  1091. )
  1092. ;; https://github.com/lunaryorn/flycheck
  1093. (when (safe-require-or-eval 'flycheck)
  1094. (call-after-init (global-flycheck-mode)))
  1095. (with-eval-after-load 'flycheck
  1096. (when (fboundp 'flycheck-black-check-setup)
  1097. (flycheck-black-check-setup)))
  1098. (when (autoload-eval-lazily 'ilookup)
  1099. (define-key ctl-x-map "d" 'ilookup-open-word))
  1100. (set-variable 'ac-ignore-case nil)
  1101. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1102. (define-key ctl-x-map "t" 'term-run-shell-command))
  1103. (add-to-list 'safe-local-variable-values
  1104. '(encoding utf-8))
  1105. (setq enable-local-variables :safe)
  1106. ;; Detect file type from shebang and set major-mode.
  1107. (add-to-list 'interpreter-mode-alist
  1108. '("python3" . python-mode))
  1109. (add-to-list 'interpreter-mode-alist
  1110. '("python2" . python-mode))
  1111. (with-eval-after-load 'python
  1112. (defvar python-mode-map (make-sparse-keymap))
  1113. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1114. ;; I want to use this, but this breaks normal self-insert-command
  1115. ;; (set-variable 'py-indent-list-style
  1116. ;; 'one-level-to-beginning-of-statement)
  1117. (set-variable 'pydoc-command
  1118. "python3 -m pydoc")
  1119. (with-eval-after-load 'pydoc
  1120. (when (require 'with-venv nil t)
  1121. (with-venv-advice-add 'pydoc)))
  1122. (when (autoload-eval-lazily 'pipenv)
  1123. ;; (declare-function pipenv-projectile-after-switch-default "pipenv")
  1124. ;; (add-hook 'python-mode-hook
  1125. ;; (lambda ()
  1126. ;; (pipenv-mode 1)
  1127. ;; (pipenv-projectile-after-switch-default)))
  1128. )
  1129. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1130. (set-variable 'flycheck-python-pylint-executable "python3")
  1131. (set-variable 'flycheck-python-pycompile-executable "python3")
  1132. (set-variable 'python-indent-guess-indent-offset nil)
  1133. (with-eval-after-load 'blacken
  1134. (when (require 'with-venv nil t)
  1135. (with-venv-advice-add 'blacken-buffer)))
  1136. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1137. (defun my-set-venv-flycheck-executable-find ()
  1138. "Set flycheck executabie find."
  1139. (when (require 'with-venv nil t)
  1140. (set-variable 'flycheck-executable-find
  1141. '(lambda (e)
  1142. (with-venv
  1143. (executable-find e)))
  1144. t)))
  1145. ;; TODO: This sucks when venv dir is not found
  1146. ;; (add-hook 'python-mode-hook
  1147. ;; 'my-set-venv-flycheck-executable-find)
  1148. ;; Run multiple chekcers
  1149. ;; https://github.com/flycheck/flycheck/issues/186
  1150. ;; http://fukuyama.co/foreign-regexp
  1151. '(and (safe-require-or-eval 'foreign-regexp)
  1152. (progn
  1153. (setq foreign-regexp/regexp-type 'perl)
  1154. '(setq reb-re-syntax 'foreign-regexp)
  1155. ))
  1156. (autoload-eval-lazily 'sql '(sql-mode)
  1157. (require 'sql-indent nil t))
  1158. (add-to-list 'auto-mode-alist
  1159. '("\\.hql\\'" . sql-mode))
  1160. (when (autoload-eval-lazily 'git-command)
  1161. (define-key ctl-x-map "g" 'git-command))
  1162. ;; This keybind cause unexpected call really many many times
  1163. ;; (when (autoload-eval-lazily 'gited)
  1164. ;; (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1165. (defalias 'gited 'gited-list)
  1166. (when (safe-require-or-eval 'git-commit)
  1167. (global-git-commit-mode 1))
  1168. (with-eval-after-load 'git-commit
  1169. (add-hook 'git-commit-setup-hook
  1170. 'turn-off-auto-fill t))
  1171. (autoload-eval-lazily 'sl)
  1172. (with-eval-after-load 'rst
  1173. (defvar rst-mode-map)
  1174. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1175. (with-eval-after-load 'jdee
  1176. (add-hook 'jdee-mode-hook
  1177. (lambda ()
  1178. (make-local-variable 'global-mode-string)
  1179. (add-to-list 'global-mode-string
  1180. mode-line-position))))
  1181. ;; Cannot enable error thrown. Why???
  1182. ;; https://github.com/m0smith/malabar-mode#Installation
  1183. ;; (when (autoload-eval-lazily 'malabar-mode)
  1184. ;; (add-to-list 'load-path
  1185. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1186. ;; (safe-require-or-eval 'cedet-devel-load)
  1187. ;; (call-after-init (activate-malabar-mode)))
  1188. (with-eval-after-load 'make-mode
  1189. (defvar makefile-mode-map (make-sparse-keymap))
  1190. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1191. ;; this functions is set in write-file-functions, i cannot find any
  1192. ;; good way to remove this.
  1193. (fset 'makefile-warn-suspicious-lines 'ignore))
  1194. (with-eval-after-load 'verilog-mode
  1195. (defvar verilog-mode-map (make-sparse-keymap))
  1196. (define-key verilog-mode-map ";" 'self-insert-command))
  1197. (setq diff-switches "-u")
  1198. (with-eval-after-load 'diff-mode
  1199. ;; (when (and (eq major-mode
  1200. ;; 'diff-mode)
  1201. ;; (not buffer-file-name))
  1202. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1203. ;; (view-mode 1))
  1204. (set-face-attribute 'diff-header nil
  1205. :foreground nil
  1206. :background nil
  1207. :weight 'bold)
  1208. (set-face-attribute 'diff-file-header nil
  1209. :foreground nil
  1210. :background nil
  1211. :weight 'bold)
  1212. (set-face-foreground 'diff-index "blue")
  1213. (set-face-attribute 'diff-hunk-header nil
  1214. :foreground "cyan"
  1215. :weight 'normal)
  1216. (set-face-attribute 'diff-context nil
  1217. ;; :foreground "white"
  1218. :foreground nil
  1219. :weight 'normal)
  1220. (set-face-foreground 'diff-removed "red")
  1221. (set-face-foreground 'diff-added "green")
  1222. (set-face-background 'diff-removed nil)
  1223. (set-face-background 'diff-added nil)
  1224. (set-face-attribute 'diff-changed nil
  1225. :foreground "magenta"
  1226. :weight 'normal)
  1227. (set-face-attribute 'diff-refine-changed nil
  1228. :foreground nil
  1229. :background nil
  1230. :weight 'bold
  1231. :inverse-video t)
  1232. ;; Annoying !
  1233. ;;(diff-auto-refine-mode)
  1234. )
  1235. ;; (ffap-bindings)
  1236. (set-variable 'browse-url-browser-function
  1237. 'eww-browse-url)
  1238. (set-variable 'sh-here-document-word "__EOC__")
  1239. (when (autoload-eval-lazily 'adoc-mode
  1240. nil
  1241. (defvar adoc-mode-map (make-sparse-keymap))
  1242. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1243. (setq auto-mode-alist
  1244. `(("\\.adoc\\'" . adoc-mode)
  1245. ("\\.asciidoc\\'" . adoc-mode)
  1246. ,@auto-mode-alist)))
  1247. (with-eval-after-load 'markup-faces
  1248. ;; Is this too match ?
  1249. (set-face-foreground 'markup-meta-face
  1250. "color-245")
  1251. (set-face-foreground 'markup-meta-hide-face
  1252. "color-245")
  1253. )
  1254. ;; TODO: check if this is required
  1255. (when (autoload-eval-lazily 'groovy-mode nil
  1256. (defvar groovy-mode-map (make-sparse-keymap))
  1257. (define-key groovy-mode-map "(" 'self-insert-command)
  1258. (define-key groovy-mode-map ")" 'self-insert-command)
  1259. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1260. )
  1261. (add-to-list 'auto-mode-alist
  1262. '("build\\.gradle\\'" . groovy-mode)))
  1263. (add-to-list 'auto-mode-alist
  1264. '("\\.gawk\\'" . awk-mode))
  1265. (with-eval-after-load 'yaml-mode
  1266. (defvar yaml-mode-map (make-sparse-keymap))
  1267. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1268. (with-eval-after-load 'html-mode
  1269. (defvar html-mode-map (make-sparse-keymap))
  1270. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1271. (with-eval-after-load 'text-mode
  1272. (define-key text-mode-map (kbd "C-m") 'newline))
  1273. (autoload-eval-lazily 'info nil
  1274. (defvar Info-additional-directory-list)
  1275. (dolist (dir (directory-files (concat user-emacs-directory
  1276. "info")
  1277. t
  1278. "^[^.].*"))
  1279. (when (file-directory-p dir)
  1280. (add-to-list 'Info-additional-directory-list
  1281. dir)))
  1282. (let ((dir (expand-file-name "~/.brew/share/info")))
  1283. (when (file-directory-p dir)
  1284. (add-to-list 'Info-additional-directory-list
  1285. dir))))
  1286. (with-eval-after-load 'apropos
  1287. (defvar apropos-mode-map (make-sparse-keymap))
  1288. (define-key apropos-mode-map "n" 'next-line)
  1289. (define-key apropos-mode-map "p" 'previous-line))
  1290. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1291. ;; (define-key isearch-mode-map
  1292. ;; (kbd "C-j") 'isearch-other-control-char)
  1293. ;; (define-key isearch-mode-map
  1294. ;; (kbd "C-k") 'isearch-other-control-char)
  1295. ;; (define-key isearch-mode-map
  1296. ;; (kbd "C-h") 'isearch-other-control-char)
  1297. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1298. (define-key isearch-mode-map (kbd "M-r")
  1299. 'isearch-query-replace-regexp)
  1300. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1301. (setq lazy-highlight-cleanup nil)
  1302. ;; face for isearch highlighing
  1303. (set-face-attribute 'lazy-highlight
  1304. nil
  1305. :foreground `unspecified
  1306. :background `unspecified
  1307. :underline t
  1308. ;; :weight `bold
  1309. )
  1310. (add-hook 'outline-mode-hook
  1311. (lambda ()
  1312. (when (string-match "\\.md\\'" buffer-file-name)
  1313. (set (make-local-variable 'outline-regexp) "#+ "))))
  1314. (add-hook 'outline-mode-hook
  1315. 'outline-show-all)
  1316. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1317. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1318. (when (autoload-eval-lazily 'markdown-mode
  1319. '(markdown-mode gfm-mode)
  1320. (defvar gfm-mode-map (make-sparse-keymap))
  1321. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1322. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1323. )
  1324. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1325. (set-variable 'markdown-command (or (executable-find "markdown")
  1326. (executable-find "markdown.pl")
  1327. ""))
  1328. (add-hook 'markdown-mode-hook
  1329. (lambda ()
  1330. (outline-minor-mode 1)
  1331. (flyspell-mode)
  1332. (set (make-local-variable 'comment-start) ";")))
  1333. )
  1334. ;; c-mode
  1335. ;; http://www.emacswiki.org/emacs/IndentingC
  1336. ;; http://en.wikipedia.org/wiki/Indent_style
  1337. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1338. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1339. (with-eval-after-load 'cc-vars
  1340. (defvar c-default-style nil)
  1341. (add-to-list 'c-default-style
  1342. '(c-mode . "k&r"))
  1343. (add-to-list 'c-default-style
  1344. '(c++-mode . "k&r")))
  1345. (autoload-eval-lazily 'js2-mode nil
  1346. ;; currently do not use js2-mode
  1347. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1348. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1349. ;; (defvar js2-mode-map (make-sparse-keymap))
  1350. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1351. ;; (interactive)
  1352. ;; (js2-enter-key)
  1353. ;; (indent-for-tab-command)))
  1354. ;; (add-hook (kill-local-variable 'before-save-hook)
  1355. ;; 'js2-before-save)
  1356. ;; (add-hook 'before-save-hook
  1357. ;; 'my-indent-buffer
  1358. ;; nil
  1359. ;; t)
  1360. )
  1361. (add-to-list 'interpreter-mode-alist
  1362. '("node" . js-mode))
  1363. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1364. (with-eval-after-load 'uniquify
  1365. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1366. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1367. (setq uniquify-min-dir-content 1))
  1368. (with-eval-after-load 'view
  1369. (defvar view-mode-map (make-sparse-keymap))
  1370. (define-key view-mode-map "j" 'scroll-up-line)
  1371. (define-key view-mode-map "k" 'scroll-down-line)
  1372. (define-key view-mode-map "v" 'toggle-read-only)
  1373. (define-key view-mode-map "q" 'bury-buffer)
  1374. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1375. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1376. ;; (define-key view-mode-map
  1377. ;; "n" 'nonincremental-repeat-search-forward)
  1378. ;; (define-key view-mode-map
  1379. ;; "N" 'nonincremental-repeat-search-backward)
  1380. ;; N conflicts with git-walktree
  1381. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1382. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1383. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1384. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1385. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1386. (global-set-key "\M-r" 'view-mode)
  1387. ;; (setq view-read-only t)
  1388. (with-eval-after-load 'term
  1389. (defvar term-raw-map (make-sparse-keymap))
  1390. (define-key term-raw-map (kbd "C-x")
  1391. (lookup-key (current-global-map)
  1392. (kbd "C-x"))))
  1393. (add-hook 'term-mode-hook
  1394. (lambda ()
  1395. ;; Stop current line highlighting
  1396. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1397. (set-variable 'scroll-margin 0 t)
  1398. ))
  1399. (set-variable 'term-buffer-maximum-size 20480)
  1400. (set-variable 'term-suppress-hard-newline t)
  1401. (add-hook 'Man-mode-hook
  1402. (lambda ()
  1403. (view-mode 1)
  1404. (setq truncate-lines nil)))
  1405. (set-variable 'Man-notify-method (if window-system
  1406. 'newframe
  1407. 'aggressive))
  1408. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1409. "woman_cache.el")))
  1410. ;; not work because man.el will be loaded when man called
  1411. (defalias 'man 'woman)
  1412. (add-to-list 'auto-mode-alist
  1413. '("tox\\.ini\\'" . conf-unix-mode))
  1414. (when (autoload-eval-lazily 'toml-mode)
  1415. (add-to-list 'auto-mode-alist
  1416. '("/tox\\.ini\\'" . toml-mode))
  1417. (add-to-list 'auto-mode-alist
  1418. '("/Pipfile\\'" . toml-mode))
  1419. (add-to-list 'auto-mode-alist
  1420. '("/poetry\\.lock\\'" . toml-mode))
  1421. )
  1422. (when (autoload-eval-lazily 'json-mode)
  1423. (add-to-list 'auto-mode-alist
  1424. '("/Pipfile\\.lock\\'" . json-mode)))
  1425. (add-hook 'go-mode-hook
  1426. (lambda()
  1427. (defvar go-mode-map)
  1428. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1429. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1430. (when (autoload-eval-lazily 'k8s-mode)
  1431. (add-to-list 'auto-mode-alist
  1432. '("\\.k8s\\'" . k8s-mode)))
  1433. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1434. ;; buffers
  1435. (defvar bs-configurations)
  1436. (declare-function bs-set-configuration "bs")
  1437. (declare-function bs-refresh "bs")
  1438. (declare-function bs-message-without-log "bs")
  1439. (declare-function bs--current-config-message "bs")
  1440. (when (autoload-eval-lazily 'bs '(bs-show)
  1441. (add-to-list 'bs-configurations
  1442. '("specials" "^\\*" nil ".*" nil nil))
  1443. (add-to-list 'bs-configurations
  1444. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1445. (defvar bs-mode-map)
  1446. (defvar bs-current-configuration)
  1447. (define-key bs-mode-map (kbd "t")
  1448. ;; TODO: fix toggle feature
  1449. (lambda ()
  1450. (interactive)
  1451. (if (string= "specials"
  1452. bs-current-configuration)
  1453. (bs-set-configuration "files")
  1454. (bs-set-configuration "specials"))
  1455. (bs-refresh)
  1456. (bs-message-without-log "%s"
  1457. (bs--current-config-message))))
  1458. ;; (setq bs-configurations (list
  1459. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1460. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1461. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1462. )
  1463. (defalias 'list-buffers 'bs-show)
  1464. (set-variable 'bs-default-configuration "files-and-specials")
  1465. (set-variable 'bs-default-sort-name "by nothing")
  1466. (add-hook 'bs-mode-hook
  1467. (lambda ()
  1468. (set (make-local-variable 'scroll-margin) 0))))
  1469. ;;(iswitchb-mode 1)
  1470. (icomplete-mode)
  1471. (defun iswitchb-buffer-display-other-window ()
  1472. "Do iswitchb in other window."
  1473. (interactive)
  1474. (let ((iswitchb-default-method 'display))
  1475. (call-interactively 'iswitchb-buffer)))
  1476. ;; buffer killing
  1477. ;; (defun my-delete-window-killing-buffer () nil)
  1478. (defun my-query-kill-current-buffer ()
  1479. "Interactively kill current buffer."
  1480. (interactive)
  1481. (if (y-or-n-p (concat "kill current buffer? :"))
  1482. (kill-buffer (current-buffer))))
  1483. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1484. (substitute-key-definition 'kill-buffer
  1485. 'my-query-kill-current-buffer
  1486. global-map)
  1487. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1488. ;; dired
  1489. (defun my-file-head (filename &optional n)
  1490. "Return list of first N lines of file FILENAME."
  1491. ;; TODO: Fix for janapese text
  1492. ;; TODO: Fix for short text
  1493. (let ((num (or n 10))
  1494. (size 100)
  1495. (beg 0)
  1496. (end 0)
  1497. (result '())
  1498. (read -1))
  1499. (with-temp-buffer
  1500. (erase-buffer)
  1501. (while (or (<= (count-lines (point-min)
  1502. (point-max))
  1503. num)
  1504. (eq read 0))
  1505. (setq end (+ beg size))
  1506. (setq read (nth 1 (insert-file-contents-literally filename
  1507. nil
  1508. beg
  1509. end)))
  1510. (goto-char (point-max))
  1511. (setq beg (+ beg size)))
  1512. (goto-char (point-min))
  1513. (while (< (length result) num)
  1514. (let ((start (point)))
  1515. (forward-line 1)
  1516. (setq result
  1517. `(,@result ,(buffer-substring-no-properties start
  1518. (point))))))
  1519. result
  1520. ;; (buffer-substring-no-properties (point-min)
  1521. ;; (progn
  1522. ;; (forward-line num)
  1523. ;; (point)))
  1524. )))
  1525. ;; (apply 'concat (my-file-head "./shrc" 10)
  1526. (defun my-dired-echo-file-head (arg)
  1527. "Echo head of current file.
  1528. ARG is num to show, or defaults to 7."
  1529. (interactive "P")
  1530. (let ((f (dired-get-filename)))
  1531. (message "%s"
  1532. (apply 'concat
  1533. (my-file-head f
  1534. 7)))))
  1535. (defun my-dired-diff ()
  1536. "Show diff of marked file and file of current line."
  1537. (interactive)
  1538. (let ((files (dired-get-marked-files nil nil nil t)))
  1539. (if (eq (car files)
  1540. t)
  1541. (diff (cadr files) (dired-get-filename))
  1542. (message "One file must be marked!"))))
  1543. (defun dired-get-file-info ()
  1544. "Print information of current line file."
  1545. (interactive)
  1546. (let ((f (shell-quote-argument (dired-get-filename t))))
  1547. (if (file-directory-p f)
  1548. (progn
  1549. (message "Calculating disk usage...")
  1550. (shell-command (concat "du -hsD "
  1551. f)))
  1552. (shell-command (concat "file "
  1553. f)))))
  1554. (defun my-dired-scroll-up ()
  1555. "Scroll up."
  1556. (interactive)
  1557. (my-dired-previous-line (- (window-height) 1)))
  1558. (defun my-dired-scroll-down ()
  1559. "Scroll down."
  1560. (interactive)
  1561. (my-dired-next-line (- (window-height) 1)))
  1562. ;; (defun my-dired-forward-line (arg)
  1563. ;; ""
  1564. ;; (interactive "p"))
  1565. (defun my-dired-previous-line (arg)
  1566. "Move ARG lines up."
  1567. (interactive "p")
  1568. (if (> arg 0)
  1569. (progn
  1570. (if (eq (line-number-at-pos)
  1571. 1)
  1572. (goto-char (point-max))
  1573. (forward-line -1))
  1574. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1575. (dired-get-subdir))
  1576. (- arg 1)
  1577. arg)))
  1578. (dired-move-to-filename)))
  1579. (defun my-dired-next-line (arg)
  1580. "Move ARG lines down."
  1581. (interactive "p")
  1582. (if (> arg 0)
  1583. (progn
  1584. (if (eq (point)
  1585. (point-max))
  1586. (goto-char (point-min))
  1587. (forward-line 1))
  1588. (my-dired-next-line (if (or (dired-get-filename nil t)
  1589. (dired-get-subdir))
  1590. (- arg 1)
  1591. arg)))
  1592. (dired-move-to-filename)))
  1593. (defun my-tramp-remote-find-file (f)
  1594. "Open F."
  1595. (interactive (list (read-file-name "My Find File Tramp: "
  1596. "/scp:"
  1597. nil ;; "/scp:"
  1598. (confirm-nonexistent-file-or-buffer))))
  1599. (find-file f))
  1600. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1601. (if (eq window-system 'mac)
  1602. (setq dired-listing-switches "-lhF")
  1603. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1604. )
  1605. (setq dired-listing-switches "-lhF")
  1606. ;; when using dired-find-alternate-file
  1607. ;; reuse current dired buffer for the file to open
  1608. ;; (put 'dired-find-alternate-file 'disabled nil)
  1609. (set-variable 'dired-ls-F-marks-symlinks t)
  1610. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1611. (set-variable 'ls-lisp-dirs-first t)
  1612. (set-variable 'ls-lisp-use-localized-time-format t)
  1613. (set-variable 'ls-lisp-format-time-list
  1614. '("%Y-%m-%d %H:%M"
  1615. "%Y-%m-%d "))
  1616. (set-variable 'dired-dwim-target t)
  1617. (set-variable 'dired-isearch-filenames t)
  1618. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1619. (set-variable 'dired-hide-details-hide-information-lines nil)
  1620. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1621. (set-variable 'dired-recursive-deletes 'always)
  1622. ;; (add-hook 'dired-after-readin-hook
  1623. ;; 'my-replace-nasi-none)
  1624. (with-eval-after-load 'dired
  1625. (safe-require-or-eval 'ls-lisp)
  1626. (defvar dired-mode-map (make-sparse-keymap))
  1627. ;; dired-do-chgrp sometimes cause system hung
  1628. (define-key dired-mode-map "G" 'ignore)
  1629. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1630. (define-key dired-mode-map "i" 'dired-get-file-info)
  1631. ;; (define-key dired-mode-map "f" 'find-file)
  1632. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1633. (define-key dired-mode-map "z" 'fzf)
  1634. (define-key dired-mode-map "!" 'shell-command)
  1635. (define-key dired-mode-map "&" 'async-shell-command)
  1636. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1637. (define-key dired-mode-map "=" 'my-dired-diff)
  1638. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1639. (define-key dired-mode-map "b" 'gtkbm)
  1640. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1641. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1642. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1643. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1644. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1645. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1646. (substitute-key-definition 'dired-next-line
  1647. 'my-dired-next-line
  1648. dired-mode-map)
  1649. (substitute-key-definition 'dired-previous-line
  1650. 'my-dired-previous-line
  1651. dired-mode-map)
  1652. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1653. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1654. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1655. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1656. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1657. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1658. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1659. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1660. (add-hook 'dired-mode-hook
  1661. (lambda ()
  1662. (when (fboundp 'dired-hide-details-mode)
  1663. (dired-hide-details-mode t)
  1664. (local-set-key "l" 'dired-hide-details-mode))
  1665. (let ((file "._Icon\015"))
  1666. (when nil
  1667. '(file-readable-p file)
  1668. (delete-file file)))))
  1669. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1670. (defvar pack-program-alist)
  1671. (add-to-list 'pack-program-alist
  1672. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1673. (set-variable 'pack-silence
  1674. t)
  1675. (with-eval-after-load 'dired
  1676. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1677. (when (autoload-eval-lazily 'dired-list-all-mode)
  1678. (setq dired-listing-switches "-lhF")
  1679. (with-eval-after-load 'dired
  1680. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1681. (when (autoload-eval-lazily 'dired-filter)
  1682. (add-hook 'dired-mode-hook
  1683. 'dired-filter-mode))
  1684. (set-variable 'dired-filter-stack nil)
  1685. ;; Currently disabled in favor of dired-from-git-ls-files
  1686. ;; (define-key ctl-x-map "f" 'find-dired)
  1687. (defvar my-dired-git-ls-files-history
  1688. "History for `my-dired-git-ls-files'." nil)
  1689. (defun my-dired-git-ls-files (arg)
  1690. "Dired from git ls-files."
  1691. (interactive (list
  1692. (read-shell-command "git ls-files: "
  1693. "git ls-files -z ")))
  1694. (pop-to-buffer-same-window
  1695. (dired-noselect `(,default-directory
  1696. ,@(split-string (shell-command-to-string arg)
  1697. "\0" t))
  1698. ""))
  1699. )
  1700. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1701. (with-eval-after-load 'dired
  1702. (defvar dired-mode-map (make-sparse-keymap))
  1703. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1704. ;; (define-minor-mode my-dired-glob-filter)
  1705. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1706. ;; misc funcs
  1707. (define-key ctl-x-map "T" 'git-worktree)
  1708. (define-key ctl-x-map "W" 'git-walktree)
  1709. (when (fboundp 'browse-url-default-macosx-browser)
  1710. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1711. (defalias 'qcalc 'quick-calc)
  1712. (defun memo (&optional dir)
  1713. "Open memo.txt in DIR."
  1714. (interactive)
  1715. (pop-to-buffer (find-file-noselect (concat (if dir
  1716. (file-name-as-directory dir)
  1717. "")
  1718. "memo.txt"))))
  1719. ;; TODO: remember-projectile
  1720. (set (defvar my-privnotes-path nil
  1721. "My privnotes repository path.")
  1722. (expand-file-name "~/my/privnotes"))
  1723. (defun my-privnotes-readme (dir)
  1724. "Open my privnotes DIR."
  1725. (interactive (list
  1726. (read-file-name "Privnotes: "
  1727. (expand-file-name (format-time-string "%Y%m%d_")
  1728. my-privnotes-path))))
  1729. (let ((path (expand-file-name "README.md" dir)))
  1730. (with-current-buffer (find-file path)
  1731. (unless (file-exists-p path)
  1732. (insert (file-name-base dir)
  1733. "\n"
  1734. "=======\n"
  1735. "\n\n")))))
  1736. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1737. (set (defvar my-rgrep-alist nil
  1738. "Alist of rgrep command.
  1739. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1740. condition to choose COMMAND when evaluated.")
  1741. `(
  1742. ;; ripgrep
  1743. ("rg"
  1744. (executable-find "rg")
  1745. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1746. ;; git grep
  1747. ("gitgrep"
  1748. (eq 0
  1749. (shell-command "git rev-parse --git-dir"))
  1750. "git --no-pager -c color.grep=always grep -nH -e ")
  1751. ;; sift
  1752. ("sift"
  1753. (executable-find "sift")
  1754. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1755. ;; the silver searcher
  1756. ("ag"
  1757. (executable-find "ag")
  1758. "ag --nogroup --nopager --filename ")
  1759. ;; ack
  1760. ("ack"
  1761. (executable-find "ack")
  1762. "ack --nogroup --nopager --with-filename ")
  1763. ;; gnu global
  1764. ("global"
  1765. (and (require 'ggtags nil t)
  1766. (executable-find "global")
  1767. (ggtags-current-project-root))
  1768. "global --result grep ")
  1769. ;; grep
  1770. ("grep"
  1771. t
  1772. ,(concat "find . "
  1773. "-path '*/.git' -prune -o "
  1774. "-path '*/.svn' -prune -o "
  1775. "-type f -print0 | "
  1776. "xargs -0 grep -nH -e "))
  1777. )
  1778. )
  1779. (defvar my-rgrep-default nil
  1780. "Default command name for my-rgrep.")
  1781. (defun my-rgrep-grep-command (&optional name alist)
  1782. "Return recursive grep command for current directory or nil.
  1783. If NAME is given, use that without testing.
  1784. Commands are searched from ALIST."
  1785. (if alist
  1786. (if name
  1787. ;; if name is given search that from alist and return the command
  1788. (nth 2 (assoc name
  1789. alist))
  1790. ;; if name is not given try test in 1th elem
  1791. (let ((car (car alist))
  1792. (cdr (cdr alist)))
  1793. (if (eval (nth 1 car))
  1794. ;; if the condition is true return the command
  1795. (nth 2 car)
  1796. ;; try next one
  1797. (and cdr
  1798. (my-rgrep-grep-command name cdr)))))
  1799. ;; if alist is not given set default value
  1800. (my-rgrep-grep-command name my-rgrep-alist)))
  1801. (defun my-rgrep (command-args)
  1802. "My recursive grep. Run COMMAND-ARGS.
  1803. If prefix argument is given, use current symbol as default search target
  1804. and search from projectile root (if projectile is available)."
  1805. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1806. nil)))
  1807. (if cmd
  1808. (list (read-shell-command "grep command: "
  1809. (concat cmd
  1810. (if current-prefix-arg
  1811. (thing-at-point 'symbol t)
  1812. ""))
  1813. 'grep-find-history))
  1814. (error "My-Rgrep: Command for rgrep not found")
  1815. )))
  1816. (if (and current-prefix-arg
  1817. (safe-require-or-eval 'projectile)
  1818. (projectile-project-p))
  1819. (projectile-with-default-dir (projectile-project-root)
  1820. (compilation-start command-args
  1821. 'grep-mode))
  1822. (compilation-start command-args
  1823. 'grep-mode)))
  1824. (defun my-rgrep-thing-at-point-projectile-root ()
  1825. "My recursive grep to find thing at point from project root."
  1826. (interactive)
  1827. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1828. nil))
  1829. (command-args
  1830. (if cmd
  1831. (concat cmd
  1832. (or (thing-at-point 'symbol t)
  1833. (error "No symbol at point")))
  1834. (error "My-Rgrep: Command for rgrep not found"))))
  1835. (if (safe-require-or-eval 'projectile)
  1836. (projectile-with-default-dir (or (projectile-project-root)
  1837. default-directory)
  1838. (compilation-start command-args
  1839. 'grep-mode))
  1840. (compilation-start command-args
  1841. 'grep-mode))))
  1842. (defmacro define-my-rgrep (name)
  1843. "Define rgrep for NAME."
  1844. `(defun ,(intern (concat "my-rgrep-"
  1845. name)) ()
  1846. ,(format "My recursive grep by %s."
  1847. name)
  1848. (interactive)
  1849. (let ((my-rgrep-default ,name))
  1850. (if (called-interactively-p 'any)
  1851. (call-interactively 'my-rgrep)
  1852. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1853. )
  1854. (define-my-rgrep "ack")
  1855. (define-my-rgrep "ag")
  1856. (define-my-rgrep "rg")
  1857. (define-my-rgrep "sift")
  1858. (define-my-rgrep "gitgrep")
  1859. (define-my-rgrep "grep")
  1860. (define-my-rgrep "global")
  1861. (define-key ctl-x-map "s" 'my-rgrep)
  1862. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1863. (defun my-occur (regexp &optional region)
  1864. "My occur command to search REGEXP."
  1865. (interactive (list (read-string "List lines matching regexp: "
  1866. (thing-at-point 'symbol t))))
  1867. (occur regexp nil region))
  1868. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1869. (when (fboundp 'swoop)
  1870. (define-key ctl-x-map (kbd "C-o") 'swoop)
  1871. )
  1872. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1873. (defalias 'make 'compile)
  1874. (define-key ctl-x-map "c" 'compile)
  1875. (defun my-pushbullet-note (text &optional title)
  1876. "Push TEXT."
  1877. (interactive "sText to Push: ")
  1878. (pb/push-item '("") text "note" (or title "")))
  1879. ;;;;;;;;;;;;;;;;;;;;;
  1880. ;; git-bug
  1881. (defconst git-bug-ls-regexp
  1882. (eval-when-compile
  1883. (rx bol
  1884. (submatch (one-or-more alphanumeric)) ; id
  1885. ;; (one-or-more any)
  1886. (one-or-more space)
  1887. (submatch (or "open" "close")) ; status
  1888. (one-or-more space)
  1889. (submatch (maximal-match (zero-or-more print))) ; title
  1890. "\t"
  1891. (submatch (one-or-more alphanumeric)) ; user
  1892. (one-or-more space)
  1893. "C:"
  1894. (submatch (one-or-more digit)) ; Comment num
  1895. (one-or-more space)
  1896. "L:"
  1897. (submatch (one-or-more digit)) ; Label num
  1898. eol
  1899. ))
  1900. "Regexp to parse line of output of git-bug ls.
  1901. Used by `git-bug-ls'.")
  1902. (defun git-bug-bugs ()
  1903. "Get list of git-bug bugs."
  1904. (with-temp-buffer
  1905. (git-bug--call-process "bug" "ls")
  1906. (goto-char (point-min))
  1907. (let ((bugs nil))
  1908. (while (not (eq (point) (point-max)))
  1909. (save-match-data
  1910. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  1911. (setq bugs `(,@bugs
  1912. ,(list
  1913. :id (match-string 1)
  1914. :status (match-string 2)
  1915. :title (string-trim (match-string 3))
  1916. :user (match-string 4)
  1917. :comment-num (match-string 5)
  1918. :label-num (match-string 6)
  1919. )))))
  1920. (forward-line 1)
  1921. (goto-char (point-at-bol)))
  1922. bugs)))
  1923. (defun git-bug-ls-noselect (&optional directory)
  1924. "Open git bug list buffer.
  1925. If optional arg DIRECTORY is given change current directory to there before
  1926. initializing."
  1927. (setq directory (expand-file-name (or directory
  1928. default-directory)))
  1929. (cl-assert (file-directory-p directory))
  1930. (let* ((root (git-bug--get-repository-root directory))
  1931. (name (file-name-nondirectory root))
  1932. (bname (format "*GitBug<%s>*" name)))
  1933. (with-current-buffer (get-buffer-create bname)
  1934. (cd root)
  1935. (git-bug-ls--set-tabulated-list-mode-variables)
  1936. (git-bug-ls-mode)
  1937. (current-buffer))))
  1938. (defun git-bug--get-repository-root (dir)
  1939. "Resolve repository root of DIR.
  1940. If DIR is not inside of any git repository, signal an error."
  1941. (cl-assert (file-directory-p dir))
  1942. (with-temp-buffer
  1943. (cd dir)
  1944. (git-bug--call-process "rev-parse" "--show-toplevel")
  1945. (goto-char (point-min))
  1946. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1947. (defun git-bug--call-process (&rest args)
  1948. "Start git process synchronously with ARGS.
  1949. Raise error when git process ends with non-zero status.
  1950. Any output will be written to current buffer."
  1951. (let ((status (apply 'call-process
  1952. "git"
  1953. nil
  1954. t
  1955. nil
  1956. args)))
  1957. (cl-assert (eq status 0)
  1958. nil
  1959. (buffer-substring-no-properties (point-min) (point-max)))))
  1960. ;;;;;;;;;;;;;;;;;;;
  1961. ;; peek-file-mode
  1962. (defun peek-file (file)
  1963. "Peek FILE."
  1964. (interactive)
  1965. (message "%s" file))
  1966. ;;;;;;;;;;;;;;;;;;;;
  1967. ;; remember-projectile
  1968. ;; TODO: Add global-minor-mode
  1969. (defvar remember-data-file)
  1970. (defun my-set-remember-data-file-buffer-local ()
  1971. "Set `remember-data-file'."
  1972. (when (require 'projectile nil t)
  1973. (setq-local remember-data-file
  1974. (expand-file-name ".remember.notes"
  1975. (projectile-project-root)))))
  1976. (add-hook 'after-change-major-mode-hook
  1977. 'my-set-remember-data-file-buffer-local)
  1978. ;;;;;;;;;;;;;;;;;;;;;;;
  1979. ;; flycheck-pydocstyle
  1980. (require 'flycheck nil t)
  1981. ;; TODO: Use `source' and config file
  1982. (flycheck-define-checker python-pydocstyle
  1983. "Docstring style checker."
  1984. :command ("python" "-m" "pydocstyle"
  1985. source-inplace)
  1986. :error-patterns
  1987. (
  1988. (error line-start (file-name) ":" line " " (one-or-more not-newline) "\n" (one-or-more blank) (message) line-end)
  1989. )
  1990. :enabled (lambda ()
  1991. (or (not (flycheck-python-needs-module-p 'python-pydocstyle))
  1992. (flycheck-python-find-module 'python-black-check "pydocstyle")))
  1993. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "pydocstyle"))
  1994. :modes python-mode)
  1995. (defun flycheck-pydocstyle-setup ()
  1996. "Setup Flycheck pydocstyle."
  1997. (interactive)
  1998. (add-to-list 'flycheck-checkers
  1999. 'python-pydocstyle))
  2000. (flycheck-pydocstyle-setup)
  2001. ;; Local Variables:
  2002. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2003. ;; flycheck-checker: emacs-lisp
  2004. ;; End:
  2005. ;;; emancs.el ends here