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.
 
 
 
 
 
 

2344 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. editorconfig
  132. editorconfig-custom-majormode
  133. git-command
  134. prompt-text
  135. ;; 10sr repository
  136. ;; 10sr-extras
  137. terminal-title
  138. dired-list-all-mode
  139. pack
  140. set-modeline-color
  141. read-only-only-mode
  142. smart-revert
  143. autosave
  144. ;;window-organizer
  145. ilookup
  146. pasteboard
  147. awk-preview
  148. recently
  149. ))
  150. (when (safe-require-or-eval 'package)
  151. (setq package-archives
  152. `(,@package-archives
  153. ("melpa" . "https://melpa.org/packages/")
  154. ;; Somehow fails to download via https
  155. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  156. (package-initialize)
  157. (defun my-auto-install-package ()
  158. "Install packages semi-automatically."
  159. (interactive)
  160. (package-refresh-contents)
  161. (mapc (lambda (pkg)
  162. (or (package-installed-p pkg)
  163. (package-install pkg)))
  164. 10sr-package-list))
  165. )
  166. ;; (lazy-load-eval 'sudoku)
  167. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  168. ;; my-idle-hook
  169. (defvar my-idle-hook nil
  170. "Hook run when idle for several secs.")
  171. (defvar my-idle-hook-sec 5
  172. "Second to run `my-idle-hook'.")
  173. (run-with-idle-timer my-idle-hook-sec
  174. t
  175. (lambda ()
  176. (run-hooks 'my-idle-hook)))
  177. ;; (add-hook 'my-idle-hook
  178. ;; (lambda ()
  179. ;; (message "idle hook message")))
  180. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  181. ;; start and quit
  182. (setq inhibit-startup-message t)
  183. (setq initial-buffer-choice 'messages-buffer)
  184. (setq confirm-kill-emacs 'y-or-n-p)
  185. (setq gc-cons-threshold (* 1024 1024 16))
  186. (setq garbage-collection-messages nil)
  187. (when window-system
  188. (add-to-list 'default-frame-alist '(cursor-type . box))
  189. (add-to-list 'default-frame-alist '(background-color . "white"))
  190. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  191. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  192. ;; does not work?
  193. )
  194. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  195. (menu-bar-mode 1)
  196. (define-key ctl-x-map "m" 'menu-bar-open)
  197. (and (fboundp 'tool-bar-mode)
  198. (tool-bar-mode 0))
  199. (and (fboundp 'set-scroll-bar-mode)
  200. (set-scroll-bar-mode nil))
  201. (call-after-init
  202. (message "%s %s" invocation-name emacs-version)
  203. (message "Invocation directory: %s" default-directory)
  204. (message "%s was taken to initialize emacs." (emacs-init-time))
  205. ;; (view-echo-area-messages)
  206. ;; (view-emacs-news)
  207. )
  208. (with-current-buffer "*Messages*"
  209. (emacs-lock-mode 'kill))
  210. (cd ".") ; when using windows use / instead of \ in `default-directory'
  211. ;; locale
  212. (set-language-environment "Japanese")
  213. (set-default-coding-systems 'utf-8-unix)
  214. (prefer-coding-system 'utf-8-unix)
  215. (setq system-time-locale "C")
  216. ;; my prefix map
  217. (defvar my-prefix-map nil
  218. "My prefix map.")
  219. (define-prefix-command 'my-prefix-map)
  220. (global-set-key (kbd "C-^") 'my-prefix-map)
  221. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  222. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  223. ;; (comint-show-maximum-output)
  224. ;; kill scratch
  225. (call-after-init
  226. (let ((buf (get-buffer "*scratch*")))
  227. (when buf
  228. (kill-buffer buf))))
  229. ;; modifier keys
  230. ;; (setq mac-option-modifier 'control)
  231. ;; display
  232. (setq visible-bell t)
  233. (setq ring-bell-function 'ignore)
  234. (mouse-avoidance-mode 'banish)
  235. (setq echo-keystrokes 0.1)
  236. (defun reload-init-file ()
  237. "Reload Emacs init file."
  238. (interactive)
  239. (when (and user-init-file
  240. (file-readable-p user-init-file))
  241. (load-file user-init-file)))
  242. (safe-require-or-eval 'session)
  243. ;; server
  244. (set-variable 'server-name (concat "server"
  245. (number-to-string (emacs-pid))))
  246. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  247. ;; In Darwin environment, init fails with message like 'Service name too long'
  248. ;; when server-use-tcp is nil
  249. (when (or (eq system-type
  250. 'cygwin)
  251. (eq system-type
  252. 'darwin))
  253. (set-variable 'server-use-tcp t))
  254. ;; MSYS2 fix
  255. (when (eq system-type
  256. 'windows-nt)
  257. (setq shell-file-name
  258. (executable-find "bash"))
  259. '(setq function-key-map
  260. `(,@function-key-map ([pause] . [?\C-c])
  261. ))
  262. (define-key key-translation-map
  263. (kbd "<pause>")
  264. (kbd "C-c"))
  265. '(keyboard-translate [pause]
  266. (kbd "C-c")p)
  267. ;; TODO: move to other place later
  268. (when (not window-system)
  269. (setq interprogram-paste-function nil)
  270. (setq interprogram-cut-function nil)))
  271. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  272. ;; global keys
  273. (global-set-key (kbd "<up>") 'scroll-down-line)
  274. (global-set-key (kbd "<down>") 'scroll-up-line)
  275. (global-set-key (kbd "<left>") 'scroll-down)
  276. (global-set-key (kbd "<right>") 'scroll-up)
  277. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  278. (global-set-key (kbd "C-\\") help-map)
  279. (define-key ctl-x-map (kbd "DEL") help-map)
  280. (define-key ctl-x-map (kbd "C-h") help-map)
  281. (define-key help-map "a" 'apropos)
  282. ;; disable annoying keys
  283. (global-set-key [prior] 'ignore)
  284. (global-set-key (kbd "<next>") 'ignore)
  285. (global-set-key [menu] 'ignore)
  286. (global-set-key [down-mouse-1] 'ignore)
  287. (global-set-key [down-mouse-2] 'ignore)
  288. (global-set-key [down-mouse-3] 'ignore)
  289. (global-set-key [mouse-1] 'ignore)
  290. (global-set-key [mouse-2] 'ignore)
  291. (global-set-key [mouse-3] 'ignore)
  292. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  293. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  294. ;; Interactively evaluate Emacs Lisp expressions
  295. (define-key ctl-x-map "i" 'ielm)
  296. (when (safe-require-or-eval 'which-key)
  297. (which-key-mode))
  298. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  299. ;; editor
  300. ;; Basically it should not set globally (instead use something like file local
  301. ;; variables or editorconfig), but for most cases I just need this...
  302. (defun my-set-require-final-newline ()
  303. "Set `require-final-newline'."
  304. (set (make-local-variable 'require-final-newline)
  305. mode-require-final-newline))
  306. (add-hook 'prog-mode-hook
  307. 'my-set-require-final-newline)
  308. (add-hook 'text-mode-hook
  309. 'my-set-require-final-newline)
  310. (add-hook 'conf-mode-hook
  311. 'my-set-require-final-newline)
  312. ;; Used from term-cursor
  313. ;; hbar is too hard to find...
  314. (defun my-cursor-type-change (&rest args)
  315. "ARGS are discarded."
  316. ;; TODO: Support wdired and wgrep
  317. (if buffer-read-only
  318. (setq cursor-type 'hbar)
  319. (setq cursor-type 'box)))
  320. ;; (add-hook 'switch-buffer-functions
  321. ;; 'my-cursor-type-change)
  322. ;; (add-hook 'read-only-mode-hook
  323. ;; 'my-cursor-type-change)
  324. ;; (when (fboundp 'global-term-cursor-mode)
  325. ;; (global-term-cursor-mode 1))
  326. ;; ;; (term-cursor--eval)
  327. (setq kill-whole-line t)
  328. (setq scroll-conservatively 35
  329. scroll-margin 2)
  330. (setq-default major-mode 'text-mode)
  331. (setq next-line-add-newlines nil)
  332. (setq kill-read-only-ok t)
  333. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  334. ;; (setq-default line-spacing 0.2)
  335. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  336. ;; (setq-default tab-width 4)
  337. (setq-default indent-tabs-mode nil)
  338. (setq-default indent-line-function 'indent-to-left-margin)
  339. ;; (setq-default indent-line-function nil)
  340. (setq-default truncate-lines nil)
  341. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  342. (delete-selection-mode 1)
  343. (cua-mode 0)
  344. (setq line-move-visual nil)
  345. (setq create-lockfiles nil)
  346. (add-hook 'before-save-hook
  347. 'time-stamp)
  348. ;; Add Time-stamp: <> to insert timestamp there
  349. (set-variable 'time-stamp-format
  350. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  351. ;; key bindings
  352. ;; moving around
  353. ;;(keyboard-translate ?\M-j ?\C-j)
  354. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  355. (define-key esc-map "p" 'backward-paragraph)
  356. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  357. (define-key esc-map "n" 'forward-paragraph)
  358. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  359. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  360. (global-set-key (kbd "C-<left>") 'scroll-down)
  361. (global-set-key (kbd "C-<right>") 'scroll-up)
  362. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  363. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  364. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  365. ;; C-h and DEL
  366. (global-set-key (kbd "C-h") (kbd "DEL"))
  367. ;; (normal-erase-is-backspace-mode 1)
  368. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  369. (global-set-key (kbd "C-m") 'newline-and-indent)
  370. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  371. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  372. (define-key esc-map "u" 'undo)
  373. (define-key esc-map "i" (kbd "ESC TAB"))
  374. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  375. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  376. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  377. (require 'page-ext nil t)
  378. (when (safe-require-or-eval 'page-break-lines)
  379. (global-page-break-lines-mode 1))
  380. (when (safe-require-or-eval 'git-gutter)
  381. (declare-function global-git-gutter-mode "git-gutter")
  382. (custom-set-variables
  383. '(git-gutter:lighter " Gttr"))
  384. (custom-set-variables
  385. '(git-gutter:update-interval 2))
  386. (custom-set-variables
  387. '(git-gutter:unchanged-sign " "))
  388. (when (>= (display-color-cells)
  389. 256)
  390. (let ((c "color-233"))
  391. (set-face-background 'git-gutter:modified c)
  392. (set-face-background 'git-gutter:added c)
  393. (set-face-background 'git-gutter:deleted c)
  394. (set-face-background 'git-gutter:unchanged c)))
  395. (global-git-gutter-mode 1)
  396. )
  397. ;; (when (safe-require-or-eval 'fancy-narrow)
  398. ;; (fancy-narrow-mode 1))
  399. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  400. ;; title and mode-line
  401. (when (safe-require-or-eval 'terminal-title)
  402. ;; if TERM is not screen use default value
  403. (if (getenv "TMUX")
  404. ;; if use tmux locally just basename of current dir
  405. (set-variable 'terminal-title-format
  406. '((file-name-nondirectory (directory-file-name
  407. default-directory))))
  408. (if (and (let ((tty-type (frame-parameter nil
  409. 'tty-type)))
  410. (and tty-type
  411. (equal (car (split-string tty-type
  412. "-"))
  413. "screen")))
  414. (not (getenv "SSH_CONNECTION")))
  415. (set-variable 'terminal-title-format
  416. '((file-name-nondirectory (directory-file-name
  417. default-directory))))
  418. ;; seems that TMUX is used locally and ssh to remote host
  419. (set-variable 'terminal-title-format
  420. `("em:"
  421. ,user-login-name
  422. "@"
  423. ,(car (split-string (system-name)
  424. "\\."))
  425. ":"
  426. default-directory))
  427. )
  428. )
  429. (terminal-title-mode))
  430. (setq eol-mnemonic-dos "\\r\\n")
  431. (setq eol-mnemonic-mac "\\r")
  432. (setq eol-mnemonic-unix "")
  433. (which-function-mode 1)
  434. (line-number-mode 0)
  435. (column-number-mode 0)
  436. (size-indication-mode 0)
  437. (setq mode-line-position
  438. '(:eval (format ":%%l:%%c /%d%s"
  439. (count-lines (point-max)
  440. (point-min))
  441. (if (buffer-narrowed-p)
  442. "[N]"
  443. "")
  444. )))
  445. (when (safe-require-or-eval 'diminish)
  446. ;; FIXME: Eval after enabling mode
  447. (call-after-init
  448. (diminish 'recently-mode)
  449. (diminish 'editorconfig-mode)
  450. (diminish 'auto-highlight-symbol-mode)
  451. (diminish 'global-whitespace-mode)
  452. (diminish 'which-key-mode)
  453. (diminish 'page-break-lines-mode)
  454. (diminish 'highlight-indentation-mode)))
  455. (setq mode-line-front-space "")
  456. ;; (setq mode-line-end-spaces "")
  457. ;; Set current frame name to empty string
  458. (setq-default mode-line-format
  459. (let* ((l mode-line-format)
  460. (l (cl-substitute " " " "
  461. l
  462. :test 'equal))
  463. (l (cl-substitute " " " "
  464. l
  465. :test 'equal))
  466. )
  467. l))
  468. (set-frame-parameter nil 'name "")
  469. ;; See color-name-rgb-alist for available color names
  470. ;; http://www.raebear.net/computers/emacs-colors/
  471. ;; https://www.emacswiki.org/emacs/ListColors
  472. ;; (list-colors-display is not a complete list)
  473. (defconst my-mode-line-background-default
  474. (face-background 'mode-line)
  475. "Default color of mode-line at init.")
  476. (defun my-mode-line-color-update (&rest args)
  477. "ARGS are discarded"
  478. (let ((ro "skyblue")
  479. (rw my-mode-line-background-default))
  480. (if (or (not buffer-read-only)
  481. (and (eq major-mode 'wdired-mode)))
  482. (set-face-background 'mode-line
  483. rw)
  484. (set-face-background 'mode-line
  485. ro))))
  486. (add-hook 'switch-buffer-functions
  487. 'my-mode-line-color-update)
  488. (add-hook 'read-only-mode-hook
  489. 'my-mode-line-color-update)
  490. (add-hook 'wdired-mode-hook
  491. 'my-mode-line-color-update)
  492. (advice-add 'wdired-change-to-dired-mode
  493. :after
  494. 'my-mode-line-color-update)
  495. (advice-add 'wgrep-change-to-wgrep-mode
  496. :after
  497. 'my-mode-line-color-update)
  498. (advice-add 'wgrep-to-original-mode
  499. :after
  500. 'my-mode-line-color-update)
  501. (set-face-background 'header-line
  502. my-mode-line-background-default)
  503. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  504. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  505. ;; minibuffer
  506. (setq insert-default-directory t)
  507. (setq completion-ignore-case t
  508. read-file-name-completion-ignore-case t
  509. read-buffer-completion-ignore-case t)
  510. (setq resize-mini-windows t)
  511. (temp-buffer-resize-mode 1)
  512. (savehist-mode 1)
  513. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  514. (set-variable 'help-at-pt-display-when-idle t)
  515. (fset 'yes-or-no-p 'y-or-n-p)
  516. ;; complete symbol when `eval'
  517. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  518. (define-key minibuffer-local-map (kbd "C-u")
  519. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  520. ;; I dont know these bindings are good
  521. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  522. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  523. (when (safe-require-or-eval 'minibuffer-line)
  524. (set-face-underline 'minibuffer-line nil)
  525. (set-variable 'minibuffer-line-refresh-interval
  526. 25)
  527. ;; Set idle timer
  528. (defvar my-minibuffer-line--idle-timer nil)
  529. (defvar minibuffer-line-mode)
  530. (add-hook 'minibuffer-line-mode-hook
  531. (lambda ()
  532. (when my-minibuffer-line--idle-timer
  533. (cancel-timer my-minibuffer-line--idle-timer)
  534. (setq my-minibuffer-line--idle-timer nil))
  535. (when minibuffer-line-mode
  536. (setq my-minibuffer-line--idle-timer
  537. (run-with-idle-timer 0.5
  538. t
  539. 'minibuffer-line--update)))))
  540. (set-variable 'minibuffer-line-format
  541. `(,(concat user-login-name
  542. "@"
  543. (car (split-string (system-name)
  544. "\\."))
  545. ":")
  546. (:eval (abbreviate-file-name (or buffer-file-name
  547. default-directory)))
  548. (:eval (and (fboundp 'git-ps1-mode-get-current)
  549. (git-ps1-mode-get-current " [GIT:%s]")))
  550. " "
  551. (:eval (format-time-string display-time-format))))
  552. (minibuffer-line-mode 1)
  553. )
  554. (when (safe-require-or-eval 'prompt-text)
  555. (set-variable 'prompt-text-format
  556. `(,(concat ""
  557. user-login-name
  558. "@"
  559. (car (split-string (system-name)
  560. "\\."))
  561. ":")
  562. (:eval (abbreviate-file-name (or buffer-file-name
  563. default-directory)))
  564. (:eval (and (fboundp 'git-ps1-mode-get-current)
  565. (git-ps1-mode-get-current " [GIT:%s]")))
  566. " "
  567. (:eval (format-time-string display-time-format))
  568. "\n"
  569. (:eval (symbol-name this-command))
  570. ": "))
  571. (prompt-text-mode 1))
  572. (autoload-eval-lazily 'helm nil
  573. (defvar helm-map)
  574. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  575. (setq-default header-line-format
  576. '(:eval (let ((f (or (buffer-file-name)
  577. default-directory)))
  578. (when f
  579. (abbreviate-file-name f)))))
  580. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  581. ;; letters, font-lock mode and fonts
  582. (setq text-quoting-style 'grave)
  583. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  584. ;; (set-window-margins (selected-window) 1 1)
  585. (unless window-system
  586. (setq frame-background-mode 'dark))
  587. (and (or (eq system-type 'Darwin)
  588. (eq system-type 'darwin))
  589. (fboundp 'mac-set-input-method-parameter)
  590. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  591. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  592. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  593. (boundp 'input-method-inactivate-hook))
  594. (add-hook 'input-method-activate-hook
  595. (lambda () (set-cursor-color "red")))
  596. (add-hook 'input-method-inactivate-hook
  597. (lambda () (set-cursor-color "black"))))
  598. (when (safe-require-or-eval 'paren)
  599. (show-paren-mode 1)
  600. (setq show-paren-delay 0.5
  601. show-paren-style 'parenthesis) ; mixed is hard to read
  602. ;; (set-face-background 'show-paren-match
  603. ;; "black")
  604. ;; ;; (face-foreground 'default))
  605. ;; (set-face-foreground 'show-paren-match
  606. ;; "white")
  607. ;; (set-face-inverse-video-p 'show-paren-match
  608. ;; t)
  609. )
  610. (transient-mark-mode 1)
  611. (global-font-lock-mode 1)
  612. (setq font-lock-global-modes
  613. '(not
  614. help-mode
  615. eshell-mode
  616. ;;term-mode
  617. Man-mode
  618. magit-diff-mode
  619. magit-revision-mode))
  620. ;; (standard-display-ascii ?\n "$\n")
  621. ;; (defvar my-eol-face
  622. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  623. ;; )
  624. ;; (defvar my-tab-face
  625. ;; '(("\t" . '(0 highlight t nil))))
  626. (defvar my-jspace-face
  627. '(("\u3000" . '(0 highlight t nil))))
  628. (add-hook 'font-lock-mode-hook
  629. (lambda ()
  630. ;; (font-lock-add-keywords nil my-eol-face)
  631. (font-lock-add-keywords nil my-jspace-face)
  632. ))
  633. (when (safe-require-or-eval 'whitespace)
  634. (add-to-list 'whitespace-display-mappings
  635. ;; We need t since last one takes precedence
  636. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  637. ;; (add-to-list 'whitespace-display-mappings
  638. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  639. (setq whitespace-style '(face
  640. trailing ; trailing blanks
  641. ;; tabs
  642. ;; spaces
  643. ;; lines
  644. lines-tail ; lines over 80
  645. newline ; newlines
  646. ;; empty ; empty lines at beg or end of buffer
  647. ;; big-indent
  648. ;; space-mark
  649. tab-mark
  650. newline-mark ; use display table for newline
  651. ))
  652. ;; (setq whitespace-newline 'font-lock-comment-face)
  653. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  654. (defun my-whitesspace-mode-reload ()
  655. "Reload whitespace-mode config."
  656. (interactive)
  657. (when whitespace-mode
  658. (whitespace-mode 0)
  659. (whitespace-mode 1)))
  660. (set-variable 'whitespace-line-column nil)
  661. (global-whitespace-mode t)
  662. (add-hook 'dired-mode-hook
  663. (lambda ()
  664. (set (make-local-variable 'whitespace-style) nil)))
  665. (if (>= (display-color-cells)
  666. 256)
  667. (set-face-foreground 'whitespace-newline "color-109")
  668. ;; (progn
  669. ;; (set-face-bold-p 'whitespace-newline
  670. ;; t))
  671. ))
  672. (and nil
  673. '(safe-require-or-eval 'fill-column-indicator)
  674. (setq fill-column-indicator))
  675. (defun my-gen-hl-line-color-dark ()
  676. "Generate color for current line in black background."
  677. (let* ((candidates (mapcar 'number-to-string (number-sequence 1 6)))
  678. (limit (length candidates))
  679. (r 0) (g 0) (b 0))
  680. (while (and (<= (abs (- r g)) 1)
  681. (<= (abs (- g b)) 1)
  682. (<= (abs (- b r)) 1))
  683. (setq r (random limit))
  684. (setq g (random limit))
  685. (setq b (random limit)))
  686. (format "#%s%s%s"
  687. (nth r candidates)
  688. (nth g candidates)
  689. (nth b candidates)
  690. )))
  691. ;; (my-gen-hl-line-color-dark)
  692. ;; highlight current line
  693. ;; http://wiki.riywo.com/index.php?Meadow
  694. (face-spec-set 'hl-line
  695. `((((min-colors 256)
  696. (background dark))
  697. ;; Rotate midnightblue
  698. (:background ,(my-gen-hl-line-color-dark)))
  699. (((min-colors 256)
  700. (background light))
  701. ;; TODO: What is should be?
  702. (:background "color-234"))
  703. (t
  704. (:underline "black"))))
  705. (set-variable 'hl-line-global-modes
  706. '(not
  707. term-mode))
  708. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  709. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  710. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  711. ;;(safe-require-or-eval 'set-modeline-color)
  712. ;; (let ((fg (face-foreground 'default))
  713. ;; (bg (face-background 'default)))
  714. ;; (set-face-background 'mode-line-inactive
  715. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  716. ;; (set-face-foreground 'mode-line-inactive
  717. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  718. ;; (set-face-underline 'mode-line-inactive
  719. ;; t)
  720. ;; (set-face-underline 'vertical-border
  721. ;; nil)
  722. ;; (when (safe-require-or-eval 'end-mark)
  723. ;; (global-end-mark-mode))
  724. ;; M-x highlight-* to highlight things
  725. (global-hi-lock-mode 1)
  726. (unless (fboundp 'highlight-region-text)
  727. (defun highlight-region-text (beg end)
  728. "Highlight text between BEG and END."
  729. (interactive "r")
  730. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  731. end)))
  732. (setq deactivate-mark t)))
  733. (when (safe-require-or-eval 'auto-highlight-symbol)
  734. (set-variable 'ahs-idle-interval 0.6)
  735. (global-auto-highlight-symbol-mode 1))
  736. (when (safe-require-or-eval 'highlight-indentation)
  737. (set-face-background 'highlight-indentation-face "color-236")
  738. (dolist (hook
  739. '(
  740. prog-mode-hook
  741. text-mode-hook
  742. ))
  743. (add-hook hook
  744. 'highlight-indentation-mode)))
  745. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  746. (when (fboundp 'fic-mode)
  747. (add-hook 'prog-mode-hook
  748. 'fic-mode))
  749. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  750. ;; file handling
  751. (auto-insert-mode 1)
  752. ;; fzf
  753. ;; Too slow in term buffer!
  754. ;; (set-variable 'fzf/executable "sk")
  755. ;; (set-variable 'fzf/args "--color bw --print-query")
  756. ;; Modified from hardcoded default to include:
  757. ;; - directories
  758. ;; - hidden files
  759. ;; - root directory (.)
  760. ;; - parent directory (..)
  761. ;; ripgrep cannot list directories...
  762. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  763. (let* ((find (if (executable-find "bfs")
  764. ;; Breadth-first find https://github.com/tavianator/bfs
  765. "bfs"
  766. ;; Use gfind if available?
  767. "find"))
  768. (findcmd (concat "set -eu; set -o pipefail; "
  769. "echo .; "
  770. "echo ..; "
  771. "command " find " -L . "
  772. "-mindepth 1 "
  773. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  774. "-o -print "
  775. "2> /dev/null "
  776. "| "
  777. "cut -b3-"))
  778. (fdcmd (concat "set -eu; set -o pipefail; "
  779. "echo .; "
  780. "echo ..; "
  781. "command fd "
  782. "--follow --hidden --no-ignore "
  783. "--color always "
  784. "2>/dev/null")))
  785. (if (executable-find "fd")
  786. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  787. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  788. (set-variable 'fzf/window-height 45)
  789. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  790. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  791. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  792. (defun my-fzf-or-find-file ()
  793. "Call fzf if usable or call find-file."
  794. (declare (interactive-only t))
  795. (interactive)
  796. (if (and (executable-find "fzf")
  797. (fboundp 'fzf)
  798. (not (file-remote-p default-directory)))
  799. (fzf)
  800. (call-interactively 'find-file)))
  801. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  802. (defun my-fzf-all-lines ()
  803. "Fzf all lines."
  804. (interactive)
  805. (let ((process-environment (cl-copy-list process-environment)))
  806. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  807. (fzf)))
  808. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  809. ;; recently
  810. (when (safe-require-or-eval 'recently)
  811. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  812. (set-variable 'recently-max 1000)
  813. (recently-mode 1))
  814. (when (safe-require-or-eval 'editorconfig)
  815. (set-variable 'editorconfig-get-properties-function
  816. 'editorconfig-core-get-properties-hash)
  817. (editorconfig-mode 1)
  818. (set-variable 'editorconfig-mode-lighter "")
  819. (when (fboundp 'ws-butler-mode)
  820. (set-variable 'editorconfig-trim-whitespaces-mode
  821. 'ws-butler-mode))
  822. (with-eval-after-load 'org-src
  823. ;; [*.org\[\*Org Src*\[ c \]*\]]
  824. (add-hook 'org-src-mode-hook
  825. 'editorconfig-mode-apply t)))
  826. (when (fboundp 'editorconfig-custom-majormode)
  827. (add-hook 'editorconfig-after-apply-functions
  828. 'editorconfig-custom-majormode))
  829. ;; Add readonly=true to set read-only-mode
  830. (add-hook 'editorconfig-after-apply-functions
  831. (lambda (props)
  832. (let ((r (gethash 'readonly props)))
  833. (when (and (string= r "true")
  834. (not buffer-read-only))
  835. (read-only-mode 1)))))
  836. (add-hook 'editorconfig-hack-properties-functions
  837. '(lambda (props)
  838. (when (derived-mode-p 'makefile-mode)
  839. (puthash 'indent_style "tab" props))))
  840. (when (fboundp 'editorconfig-auto-apply-enable)
  841. (add-hook 'editorconfig-conf-mode-hook
  842. 'editorconfig-auto-apply-enable))
  843. ;; (when (fboundp 'editorconfig-charset-extras)
  844. ;; (add-hook 'editorconfig-custom-hooks
  845. ;; 'editorconfig-charset-extras))
  846. (setq revert-without-query '(".+"))
  847. ;; save cursor position
  848. (when (safe-require-or-eval 'saveplace)
  849. (setq-default save-place t)
  850. (setq save-place-file (concat user-emacs-directory
  851. "places")))
  852. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  853. (setq make-backup-files t)
  854. (setq vc-make-backup-files t)
  855. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  856. (setq backup-directory-alist
  857. (cons (cons "." (expand-file-name (concat user-emacs-directory
  858. "backup")))
  859. backup-directory-alist))
  860. (setq version-control 't)
  861. (setq delete-old-versions t)
  862. (setq kept-new-versions 20)
  863. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  864. "auto-save/")))
  865. ;; (setq delete-auto-save-files t)
  866. (setq auto-save-visited-interval 8)
  867. (auto-save-visited-mode 1)
  868. ;; (add-to-list 'auto-save-file-name-transforms
  869. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  870. ;; (setq auto-save-interval 3)
  871. ;; (auto-save-mode 1)
  872. (add-to-list 'completion-ignored-extensions ".bak")
  873. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  874. (setq delete-by-moving-to-trash t)
  875. ;; trash-directory "~/.emacs.d/trash")
  876. (add-hook 'after-save-hook
  877. 'executable-make-buffer-file-executable-if-script-p)
  878. (set-variable 'bookmark-default-file
  879. (expand-file-name (concat user-emacs-directory
  880. "bmk")))
  881. (set-variable 'bookmark-save-flag
  882. 1)
  883. (with-eval-after-load 'recentf
  884. (defvar recentf-exclude)
  885. (defvar bookmark-default-file)
  886. (add-to-list 'recentf-exclude
  887. (regexp-quote bookmark-default-file)))
  888. (when (safe-require-or-eval 'smart-revert)
  889. (smart-revert-on))
  890. ;; autosave
  891. ;; auto-save-visited-mode can be used instead?
  892. ;; (when (safe-require-or-eval 'autosave)
  893. ;; (autosave-set 8))
  894. ;; bookmarks
  895. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  896. ;; vc
  897. (set-variable 'vc-handled-backends '(RCS))
  898. (set-variable 'vc-rcs-register-switches "-l")
  899. (set-variable 'vc-rcs-checkin-switches "-l")
  900. (set-variable 'vc-command-messages t)
  901. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  902. ;; share clipboard with x
  903. ;; this page describes this in details, but only these sexps seem to be needed
  904. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  905. (and nil
  906. (not window-system)
  907. (not (eq window-system 'mac))
  908. (getenv "DISPLAY")
  909. (not (equal (getenv "DISPLAY") ""))
  910. (executable-find "xclip")
  911. ;; (< emacs-major-version 24)
  912. '(safe-require-or-eval 'xclip)
  913. nil
  914. (turn-on-xclip))
  915. (and (eq system-type 'darwin)
  916. (safe-require-or-eval 'pasteboard)
  917. (turn-on-pasteboard))
  918. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  919. ;; some modes and hooks
  920. ;; Include some extra modes
  921. (require 'generic-x)
  922. (when (fboundp 'web-mode)
  923. (add-to-list 'auto-mode-alist
  924. '("\\.html\\.j2\\'" . web-mode))
  925. )
  926. (when (autoload-eval-lazily 'wgrep)
  927. (set-variable 'wgrep-auto-save-buffer t)
  928. (with-eval-after-load 'grep
  929. (defvar grep-mode-map)
  930. (define-key grep-mode-map
  931. "e"
  932. 'wgrep-change-to-wgrep-mode)))
  933. (when (fboundp 'grep-context-mode)
  934. (add-hook 'compilation-mode-hook #'grep-context-mode))
  935. (with-eval-after-load 'remember
  936. (defvar remember-mode-map)
  937. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  938. (set-variable 'remember-notes-initial-major-mode
  939. 'change-log-mode)
  940. (with-eval-after-load 'magit-files
  941. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  942. ;; existing keybindings.
  943. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  944. ;; to disable thie mode here, but do anyway.
  945. ;; See also https://github.com/magit/magit/issues/3517
  946. (global-magit-file-mode -1))
  947. (with-eval-after-load 'magit-section
  948. (set-face-background 'magit-section-highlight
  949. nil))
  950. ;; Sane colors
  951. (with-eval-after-load 'magit-diff
  952. (set-face-background 'magit-diff-context nil)
  953. (set-face-background 'magit-diff-context-highlight nil)
  954. (set-face-foreground 'magit-diff-hunk-heading nil)
  955. (set-face-background 'magit-diff-hunk-heading nil)
  956. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  957. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  958. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  959. (set-face-foreground 'magit-diff-added "green")
  960. (set-face-background 'magit-diff-added nil)
  961. (set-face-foreground 'magit-diff-added-highlight "green")
  962. (set-face-background 'magit-diff-added-highlight nil)
  963. (set-face-foreground 'magit-diff-removed "red")
  964. (set-face-background 'magit-diff-removed nil)
  965. (set-face-foreground 'magit-diff-removed-highlight "red")
  966. (set-face-background 'magit-diff-removed-highlight nil)
  967. (set-face-background 'magit-diff-lines-boundary "blue")
  968. )
  969. (when (boundp 'git-rebase-filename-regexp)
  970. (add-to-list 'auto-mode-alist
  971. `(,git-rebase-filename-regexp . text-mode)))
  972. (when (safe-require-or-eval 'aggressive-indent)
  973. (defvar aggressive-indent-excluded-modes)
  974. (setq aggressive-indent-excluded-modes
  975. `(diff-mode
  976. toml-mode
  977. conf-mode
  978. dockerfile-mode
  979. groovy-mode
  980. scala-mode
  981. ,@aggressive-indent-excluded-modes))
  982. (global-aggressive-indent-mode 1))
  983. (when (autoload-eval-lazily 'ggtags)
  984. (add-hook 'c-mode-common-hook
  985. (lambda ()
  986. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  987. (ggtags-mode 1))))
  988. (add-hook 'python-mode-hook
  989. (lambda ()
  990. (ggtags-mode 1))))
  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