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

2389 righe
78 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. web-mode
  90. color-moccur
  91. ggtags
  92. flycheck
  93. auto-highlight-symbol
  94. hl-todo
  95. ;; Currently not available
  96. ;; pp-c-l
  97. xclip
  98. foreign-regexp
  99. multi-term
  100. term-run
  101. editorconfig
  102. git-ps1-mode
  103. restart-emacs
  104. fill-column-indicator
  105. pkgbuild-mode
  106. minibuffer-line
  107. which-key
  108. ;; I think this works in place of my autosave lib
  109. super-save
  110. pipenv
  111. imenu-list
  112. page-break-lines
  113. aggressive-indent
  114. dired-filter
  115. wgrep
  116. magit
  117. git-gutter
  118. end-mark
  119. sl
  120. ;; TODO: Configure pony-tpl-mode
  121. pony-mode
  122. gited
  123. highlight-indentation
  124. diminish
  125. fzf
  126. fic-mode
  127. term-cursor
  128. pydoc
  129. swoop
  130. color-identifiers-mode
  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. (when (fboundp 'color-identifiers-mode)
  583. (add-hook 'prog-mode-hook
  584. 'color-identifiers-mode))
  585. (setq text-quoting-style 'grave)
  586. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  587. ;; (set-window-margins (selected-window) 1 1)
  588. (unless window-system
  589. (setq frame-background-mode 'dark))
  590. (and (or (eq system-type 'Darwin)
  591. (eq system-type 'darwin))
  592. (fboundp 'mac-set-input-method-parameter)
  593. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  594. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  595. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  596. (boundp 'input-method-inactivate-hook))
  597. (add-hook 'input-method-activate-hook
  598. (lambda () (set-cursor-color "red")))
  599. (add-hook 'input-method-inactivate-hook
  600. (lambda () (set-cursor-color "black"))))
  601. (when (safe-require-or-eval 'paren)
  602. (show-paren-mode 1)
  603. (setq show-paren-delay 0.5
  604. show-paren-style 'parenthesis) ; mixed is hard to read
  605. ;; (set-face-background 'show-paren-match
  606. ;; "black")
  607. ;; ;; (face-foreground 'default))
  608. ;; (set-face-foreground 'show-paren-match
  609. ;; "white")
  610. ;; (set-face-inverse-video-p 'show-paren-match
  611. ;; t)
  612. )
  613. (transient-mark-mode 1)
  614. (global-font-lock-mode 1)
  615. (setq font-lock-global-modes
  616. '(not
  617. help-mode
  618. eshell-mode
  619. ;;term-mode
  620. Man-mode
  621. magit-diff-mode
  622. magit-revision-mode))
  623. ;; (standard-display-ascii ?\n "$\n")
  624. ;; (defvar my-eol-face
  625. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  626. ;; )
  627. ;; (defvar my-tab-face
  628. ;; '(("\t" . '(0 highlight t nil))))
  629. (defvar my-jspace-face
  630. '(("\u3000" . '(0 highlight t nil))))
  631. (add-hook 'font-lock-mode-hook
  632. (lambda ()
  633. ;; (font-lock-add-keywords nil my-eol-face)
  634. (font-lock-add-keywords nil my-jspace-face)
  635. ))
  636. (when (safe-require-or-eval 'whitespace)
  637. (add-to-list 'whitespace-display-mappings
  638. ;; We need t since last one takes precedence
  639. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  640. ;; (add-to-list 'whitespace-display-mappings
  641. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  642. (setq whitespace-style '(face
  643. trailing ; trailing blanks
  644. ;; tabs
  645. ;; spaces
  646. ;; lines
  647. lines-tail ; lines over 80
  648. newline ; newlines
  649. ;; empty ; empty lines at beg or end of buffer
  650. ;; big-indent
  651. ;; space-mark
  652. tab-mark
  653. newline-mark ; use display table for newline
  654. ))
  655. ;; (setq whitespace-newline 'font-lock-comment-face)
  656. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  657. (defun my-whitesspace-mode-reload ()
  658. "Reload whitespace-mode config."
  659. (interactive)
  660. (when whitespace-mode
  661. (whitespace-mode 0)
  662. (whitespace-mode 1)))
  663. (set-variable 'whitespace-line-column nil)
  664. (global-whitespace-mode t)
  665. (add-hook 'dired-mode-hook
  666. (lambda ()
  667. (set (make-local-variable 'whitespace-style) nil)))
  668. (if (>= (display-color-cells)
  669. 256)
  670. (set-face-foreground 'whitespace-newline "color-109")
  671. ;; (progn
  672. ;; (set-face-bold-p 'whitespace-newline
  673. ;; t))
  674. ))
  675. (and nil
  676. '(safe-require-or-eval 'fill-column-indicator)
  677. (setq fill-column-indicator))
  678. (defun my-gen-hl-line-color-dark ()
  679. "Generate color for current line in black background."
  680. (let* ((candidates (mapcar 'number-to-string (number-sequence 1 6)))
  681. (limit (length candidates))
  682. (r 0) (g 0) (b 0))
  683. (while (and (<= (abs (- r g)) 1)
  684. (<= (abs (- g b)) 1)
  685. (<= (abs (- b r)) 1))
  686. (setq r (random limit))
  687. (setq g (random limit))
  688. (setq b (random limit)))
  689. (format "#%s%s%s"
  690. (nth r candidates)
  691. (nth g candidates)
  692. (nth b candidates)
  693. )))
  694. ;; (my-gen-hl-line-color-dark)
  695. ;; highlight current line
  696. ;; http://wiki.riywo.com/index.php?Meadow
  697. (face-spec-set 'hl-line
  698. `((((min-colors 256)
  699. (background dark))
  700. ;; Rotate midnightblue
  701. (:background ,(my-gen-hl-line-color-dark)))
  702. (((min-colors 256)
  703. (background light))
  704. ;; TODO: What is should be?
  705. (:background "color-234"))
  706. (t
  707. (:underline "black"))))
  708. (set-variable 'hl-line-global-modes
  709. '(not
  710. term-mode))
  711. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  712. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  713. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  714. ;;(safe-require-or-eval 'set-modeline-color)
  715. ;; (let ((fg (face-foreground 'default))
  716. ;; (bg (face-background 'default)))
  717. ;; (set-face-background 'mode-line-inactive
  718. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  719. ;; (set-face-foreground 'mode-line-inactive
  720. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  721. ;; (set-face-underline 'mode-line-inactive
  722. ;; t)
  723. ;; (set-face-underline 'vertical-border
  724. ;; nil)
  725. ;; (when (safe-require-or-eval 'end-mark)
  726. ;; (global-end-mark-mode))
  727. ;; M-x highlight-* to highlight things
  728. (global-hi-lock-mode 1)
  729. (unless (fboundp 'highlight-region-text)
  730. (defun highlight-region-text (beg end)
  731. "Highlight text between BEG and END."
  732. (interactive "r")
  733. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  734. end)))
  735. (setq deactivate-mark t)))
  736. (when (safe-require-or-eval 'auto-highlight-symbol)
  737. (set-variable 'ahs-idle-interval 0.6)
  738. (global-auto-highlight-symbol-mode 1))
  739. (when (safe-require-or-eval 'highlight-indentation)
  740. (set-face-background 'highlight-indentation-face "color-236")
  741. (dolist (hook
  742. '(
  743. prog-mode-hook
  744. text-mode-hook
  745. ))
  746. (add-hook hook
  747. 'highlight-indentation-mode)))
  748. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  749. (when (fboundp 'fic-mode)
  750. (add-hook 'prog-mode-hook
  751. 'fic-mode))
  752. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  753. ;; file handling
  754. (auto-insert-mode 1)
  755. ;; fzf
  756. ;; Too slow in term buffer!
  757. ;; (set-variable 'fzf/executable "sk")
  758. ;; (set-variable 'fzf/args "--color bw --print-query")
  759. ;; Modified from hardcoded default to include:
  760. ;; - directories
  761. ;; - hidden files
  762. ;; - root directory (.)
  763. ;; - parent directory (..)
  764. ;; ripgrep cannot list directories...
  765. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  766. (let* ((find (if (executable-find "bfs")
  767. ;; Breadth-first find https://github.com/tavianator/bfs
  768. "bfs"
  769. ;; Use gfind if available?
  770. "find"))
  771. (findcmd (concat "set -eu; set -o pipefail; "
  772. "echo .; "
  773. "echo ..; "
  774. "command " find " -L . "
  775. "-mindepth 1 "
  776. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  777. "-o -print "
  778. "2> /dev/null "
  779. "| "
  780. "cut -b3-"))
  781. (fdcmd (concat "set -eu; set -o pipefail; "
  782. "echo .; "
  783. "echo ..; "
  784. "command fd "
  785. "--follow --hidden --no-ignore "
  786. "--color always "
  787. "2>/dev/null")))
  788. (if (executable-find "fd")
  789. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  790. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  791. (set-variable 'fzf/window-height 45)
  792. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  793. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  794. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  795. (defun my-fzf-or-find-file ()
  796. "Call fzf if usable or call find-file."
  797. (declare (interactive-only t))
  798. (interactive)
  799. (if (and (executable-find "fzf")
  800. (fboundp 'fzf)
  801. (not (file-remote-p default-directory)))
  802. (fzf)
  803. (call-interactively 'find-file)))
  804. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  805. (defun my-fzf-all-lines ()
  806. "Fzf all lines."
  807. (interactive)
  808. (let ((process-environment (cl-copy-list process-environment)))
  809. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  810. (fzf)))
  811. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  812. ;; recently
  813. (when (safe-require-or-eval 'recently)
  814. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  815. (set-variable 'recently-max 1000)
  816. (recently-mode 1))
  817. (when (safe-require-or-eval 'editorconfig)
  818. (set-variable 'editorconfig-get-properties-function
  819. 'editorconfig-core-get-properties-hash)
  820. (editorconfig-mode 1)
  821. (set-variable 'editorconfig-mode-lighter "")
  822. (when (fboundp 'ws-butler-mode)
  823. (set-variable 'editorconfig-trim-whitespaces-mode
  824. 'ws-butler-mode))
  825. (with-eval-after-load 'org-src
  826. ;; [*.org\[\*Org Src*\[ c \]*\]]
  827. (add-hook 'org-src-mode-hook
  828. 'editorconfig-mode-apply t)))
  829. (when (fboundp 'editorconfig-custom-majormode)
  830. (add-hook 'editorconfig-after-apply-functions
  831. 'editorconfig-custom-majormode))
  832. ;; Add readonly=true to set read-only-mode
  833. (add-hook 'editorconfig-after-apply-functions
  834. (lambda (props)
  835. (let ((r (gethash 'readonly props)))
  836. (when (and (string= r "true")
  837. (not buffer-read-only))
  838. (read-only-mode 1)))))
  839. (add-hook 'editorconfig-hack-properties-functions
  840. '(lambda (props)
  841. (when (derived-mode-p 'makefile-mode)
  842. (puthash 'indent_style "tab" props))))
  843. (when (fboundp 'editorconfig-auto-apply-enable)
  844. (add-hook 'editorconfig-conf-mode-hook
  845. 'editorconfig-auto-apply-enable))
  846. ;; (when (fboundp 'editorconfig-charset-extras)
  847. ;; (add-hook 'editorconfig-custom-hooks
  848. ;; 'editorconfig-charset-extras))
  849. (setq revert-without-query '(".+"))
  850. ;; save cursor position
  851. (when (safe-require-or-eval 'saveplace)
  852. (setq-default save-place t)
  853. (setq save-place-file (concat user-emacs-directory
  854. "places")))
  855. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  856. (setq make-backup-files t)
  857. (setq vc-make-backup-files t)
  858. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  859. (setq backup-directory-alist
  860. (cons (cons "." (expand-file-name (concat user-emacs-directory
  861. "backup")))
  862. backup-directory-alist))
  863. (setq version-control 't)
  864. (setq delete-old-versions t)
  865. (setq kept-new-versions 20)
  866. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  867. "auto-save/")))
  868. ;; (setq delete-auto-save-files t)
  869. (setq auto-save-visited-interval 8)
  870. (auto-save-visited-mode 1)
  871. ;; (add-to-list 'auto-save-file-name-transforms
  872. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  873. ;; (setq auto-save-interval 3)
  874. ;; (auto-save-mode 1)
  875. (add-to-list 'completion-ignored-extensions ".bak")
  876. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  877. (setq delete-by-moving-to-trash t)
  878. ;; trash-directory "~/.emacs.d/trash")
  879. (add-hook 'after-save-hook
  880. 'executable-make-buffer-file-executable-if-script-p)
  881. (set-variable 'bookmark-default-file
  882. (expand-file-name (concat user-emacs-directory
  883. "bmk")))
  884. (set-variable 'bookmark-save-flag
  885. 1)
  886. (with-eval-after-load 'recentf
  887. (defvar recentf-exclude)
  888. (defvar bookmark-default-file)
  889. (add-to-list 'recentf-exclude
  890. (regexp-quote bookmark-default-file)))
  891. (when (safe-require-or-eval 'smart-revert)
  892. (smart-revert-on))
  893. ;; autosave
  894. ;; auto-save-visited-mode can be used instead?
  895. ;; (when (safe-require-or-eval 'autosave)
  896. ;; (autosave-set 8))
  897. ;; bookmarks
  898. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  899. ;; vc
  900. (set-variable 'vc-handled-backends '(RCS))
  901. (set-variable 'vc-rcs-register-switches "-l")
  902. (set-variable 'vc-rcs-checkin-switches "-l")
  903. (set-variable 'vc-command-messages t)
  904. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  905. ;; share clipboard with x
  906. ;; this page describes this in details, but only these sexps seem to be needed
  907. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  908. (and nil
  909. (not window-system)
  910. (not (eq window-system 'mac))
  911. (getenv "DISPLAY")
  912. (not (equal (getenv "DISPLAY") ""))
  913. (executable-find "xclip")
  914. ;; (< emacs-major-version 24)
  915. '(safe-require-or-eval 'xclip)
  916. nil
  917. (turn-on-xclip))
  918. (and (eq system-type 'darwin)
  919. (safe-require-or-eval 'pasteboard)
  920. (turn-on-pasteboard))
  921. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  922. ;; some modes and hooks
  923. ;; Include some extra modes
  924. (require 'generic-x)
  925. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  926. (defun my-scalafmt ()
  927. (interactive)
  928. (cl-assert buffer-file-name)
  929. (cl-assert (not (buffer-modified-p)))
  930. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  931. (configoption (if configdir
  932. (concat " --config "
  933. (shell-quote-argument (expand-file-name configdir))
  934. ".scalafmt.conf"
  935. )
  936. ""))
  937. (str (concat "scalafmt -f "
  938. (shell-quote-argument buffer-file-name)
  939. configoption
  940. " -i --exclude ensime")))
  941. (message str)
  942. (shell-command-to-string str))
  943. (message "scalafmt done")
  944. (revert-buffer nil t))
  945. (when (fboundp 'web-mode)
  946. (add-to-list 'auto-mode-alist
  947. '("\\.html\\.j2\\'" . web-mode))
  948. )
  949. (when (autoload-eval-lazily 'wgrep)
  950. (set-variable 'wgrep-auto-save-buffer t)
  951. (with-eval-after-load 'grep
  952. (defvar grep-mode-map)
  953. (define-key grep-mode-map
  954. "e"
  955. 'wgrep-change-to-wgrep-mode)))
  956. (when (fboundp 'grep-context-mode)
  957. (add-hook 'compilation-mode-hook #'grep-context-mode))
  958. (with-eval-after-load 'remember
  959. (defvar remember-mode-map)
  960. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  961. (set-variable 'remember-notes-initial-major-mode
  962. 'change-log-mode)
  963. (with-eval-after-load 'magit-files
  964. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  965. ;; existing keybindings.
  966. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  967. ;; to disable thie mode here, but do anyway.
  968. ;; See also https://github.com/magit/magit/issues/3517
  969. (global-magit-file-mode -1))
  970. (with-eval-after-load 'magit-section
  971. (set-face-background 'magit-section-highlight
  972. nil))
  973. ;; Sane colors
  974. (with-eval-after-load 'magit-diff
  975. (set-face-background 'magit-diff-context nil)
  976. (set-face-background 'magit-diff-context-highlight nil)
  977. (set-face-foreground 'magit-diff-hunk-heading nil)
  978. (set-face-background 'magit-diff-hunk-heading nil)
  979. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  980. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  981. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  982. (set-face-foreground 'magit-diff-added "green")
  983. (set-face-background 'magit-diff-added nil)
  984. (set-face-foreground 'magit-diff-added-highlight "green")
  985. (set-face-background 'magit-diff-added-highlight nil)
  986. (set-face-foreground 'magit-diff-removed "red")
  987. (set-face-background 'magit-diff-removed nil)
  988. (set-face-foreground 'magit-diff-removed-highlight "red")
  989. (set-face-background 'magit-diff-removed-highlight nil)
  990. (set-face-background 'magit-diff-lines-boundary "blue")
  991. )
  992. (when (boundp 'git-rebase-filename-regexp)
  993. (add-to-list 'auto-mode-alist
  994. `(,git-rebase-filename-regexp . text-mode)))
  995. (when (safe-require-or-eval 'aggressive-indent)
  996. (defvar aggressive-indent-excluded-modes)
  997. (setq aggressive-indent-excluded-modes
  998. `(diff-mode
  999. toml-mode
  1000. conf-mode
  1001. dockerfile-mode
  1002. groovy-mode
  1003. scala-mode
  1004. ,@aggressive-indent-excluded-modes))
  1005. (global-aggressive-indent-mode 1))
  1006. (when (fboundp 'ggtags-mode)
  1007. (add-hook 'c-mode-common-hook
  1008. 'ggtags-mode)
  1009. (add-hook 'python-mode-hook
  1010. 'ggtags-mode)
  1011. (add-hook 'js-mode-hook
  1012. 'ggtags-mode)
  1013. (add-hook 'scheme-mode-hook
  1014. 'ggtags-mode)
  1015. )
  1016. (when (autoload-eval-lazily 'imenu-list)
  1017. ;; (set-variable 'imenu-list-auto-resize t)
  1018. (set-variable 'imenu-list-focus-after-activation t)
  1019. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  1020. (add-hook 'emacs-lisp-mode-hook
  1021. (lambda ()
  1022. (setq imenu-generic-expression
  1023. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1024. ,@imenu-generic-expression))))
  1025. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1026. (with-eval-after-load 'compile
  1027. (defvar compilation-filter-start)
  1028. (defvar compilation-error-regexp-alist)
  1029. (require 'ansi-color)
  1030. (add-hook 'compilation-filter-hook
  1031. (lambda ()
  1032. (let ((inhibit-read-only t))
  1033. (ansi-color-apply-on-region compilation-filter-start
  1034. (point)))))
  1035. (add-to-list 'compilation-error-regexp-alist
  1036. ;; ansible-lint
  1037. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1038. (add-to-list 'compilation-error-regexp-alist
  1039. ;; pydocstyle
  1040. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1041. )
  1042. (when (safe-require-or-eval 'company)
  1043. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1044. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1045. (global-company-mode)
  1046. (set-variable 'company-idle-delay nil)
  1047. (set-variable 'company-minimum-prefix-length 2)
  1048. (set-variable 'company-selection-wrap-around t)
  1049. (defvar company-mode-map)
  1050. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1051. ;; (with-eval-after-load 'python
  1052. ;; (defvar python-indent-trigger-commands)
  1053. ;; ;; TODO: This disables completion in puthon?
  1054. ;; (add-to-list 'python-indent-trigger-commands
  1055. ;; 'company-indent-or-complete-common))
  1056. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1057. (defvar company-active-map)
  1058. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1059. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1060. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1061. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1062. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1063. (defvar company-mode)
  1064. (defvar company-candidates)
  1065. (defvar company-candidates-length)
  1066. ;; (popup-tip "Hello, World!")
  1067. (defun my-company-lighter-current-length ()
  1068. "Get current candidate length."
  1069. (interactive)
  1070. (let ((l nil)
  1071. (inhibit-message t))
  1072. (when (and company-mode
  1073. (not (minibufferp))
  1074. ;; Do nothing when already in company completion
  1075. (not company-candidates))
  1076. ;; FIXME: Somehow it cannto catch errors from ggtags
  1077. (ignore-errors
  1078. ;; (company-auto-begin)
  1079. (company-manual-begin)
  1080. (setq l company-candidates-length)
  1081. (company-cancel)))
  1082. (if l
  1083. (format "[%d]" l)
  1084. "")))
  1085. (defvar company-lighter)
  1086. (set-variable 'company-lighter-base "Cmp")
  1087. ;; (add-to-list 'company-lighter
  1088. ;; '(:eval (my-company-lighter-current-length))
  1089. ;; t)
  1090. ;; This breaks japanese text input
  1091. ;; (set-variable 'my-company-length-popup-tip-timer
  1092. ;; (run-with-idle-timer 0.2 t
  1093. ;; 'my-company-length-popup-tip))
  1094. ;; (current-active-maps)
  1095. ;; (lookup-key)
  1096. '(mapcar (lambda (map)
  1097. (lookup-key map (kbd "C-i")))
  1098. (current-active-maps))
  1099. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1100. (set-face-attribute 'company-tooltip nil
  1101. :foreground "black" :background "lightgrey")
  1102. (set-face-attribute 'company-tooltip-common nil
  1103. :foreground "black" :background "lightgrey")
  1104. (set-face-attribute 'company-tooltip-common-selection nil
  1105. :foreground "white" :background "steelblue")
  1106. (set-face-attribute 'company-tooltip-selection nil
  1107. :foreground "black" :background "steelblue")
  1108. (set-face-attribute 'company-preview-common nil
  1109. :background nil :foreground "lightgrey" :underline t)
  1110. (set-face-attribute 'company-scrollbar-fg nil
  1111. :background "orange")
  1112. (set-face-attribute 'company-scrollbar-bg nil
  1113. :background "gray40")
  1114. )
  1115. ;; https://github.com/lunaryorn/flycheck
  1116. (when (safe-require-or-eval 'flycheck)
  1117. (call-after-init (global-flycheck-mode)))
  1118. ;; (with-eval-after-load 'flycheck
  1119. ;; (when (fboundp 'flycheck-black-check-setup)
  1120. ;; (flycheck-black-check-setup)))
  1121. (when (autoload-eval-lazily 'ilookup)
  1122. (define-key ctl-x-map "d" 'ilookup-open-word))
  1123. (set-variable 'ac-ignore-case nil)
  1124. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1125. (define-key ctl-x-map "t" 'term-run-shell-command))
  1126. (add-to-list 'safe-local-variable-values
  1127. '(encoding utf-8))
  1128. (setq enable-local-variables :safe)
  1129. ;; Detect file type from shebang and set major-mode.
  1130. (add-to-list 'interpreter-mode-alist
  1131. '("python3" . python-mode))
  1132. (add-to-list 'interpreter-mode-alist
  1133. '("python2" . python-mode))
  1134. (with-eval-after-load 'python
  1135. (defvar python-mode-map (make-sparse-keymap))
  1136. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1137. ;; I want to use this, but this breaks normal self-insert-command
  1138. ;; (set-variable 'py-indent-list-style
  1139. ;; 'one-level-to-beginning-of-statement)
  1140. (set-variable 'pydoc-command
  1141. "python3 -m pydoc")
  1142. (with-eval-after-load 'pydoc
  1143. (when (require 'with-venv nil t)
  1144. (with-venv-advice-add 'pydoc)))
  1145. (when (autoload-eval-lazily 'pipenv)
  1146. ;; (declare-function pipenv-projectile-after-switch-default "pipenv")
  1147. ;; (add-hook 'python-mode-hook
  1148. ;; (lambda ()
  1149. ;; (pipenv-mode 1)
  1150. ;; (pipenv-projectile-after-switch-default)))
  1151. )
  1152. (set-variable 'flycheck-python-mypy-ini ".mypy.ini")
  1153. (set-variable 'flycheck-python-pylint-executable "python3")
  1154. (set-variable 'flycheck-python-pycompile-executable "python3")
  1155. (set-variable 'python-indent-guess-indent-offset nil)
  1156. (with-eval-after-load 'blacken
  1157. (when (require 'with-venv nil t)
  1158. (with-venv-advice-add 'blacken-buffer)))
  1159. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1160. (defun my-set-venv-flycheck-executable-find ()
  1161. "Set flycheck executabie find."
  1162. (interactive)
  1163. (when (and (require 'with-venv nil t)
  1164. ;; Hack not to set when venv dir not found
  1165. (with-venv-find-venv-dir))
  1166. (set-variable 'flycheck-executable-find
  1167. '(lambda (e)
  1168. (with-venv
  1169. (executable-find e)))
  1170. t)))
  1171. (defun my-update-flycheck-flake8-error-level-alist ()
  1172. "Update `flycheck-flake8-error-level-alist'."
  1173. (defvar flycheck-flake8-error-level-alist)
  1174. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1175. ;; '("^D.*$" . warning))
  1176. (set-variable 'flycheck-flake8-error-level-alist
  1177. nil)
  1178. )
  1179. (add-hook 'python-mode-hook
  1180. 'my-set-venv-flycheck-executable-find)
  1181. (add-hook 'python-mode-hook
  1182. 'my-update-flycheck-flake8-error-level-alist)
  1183. ;; Run multiple chekcers
  1184. ;; https://github.com/flycheck/flycheck/issues/186
  1185. ;; http://fukuyama.co/foreign-regexp
  1186. '(and (safe-require-or-eval 'foreign-regexp)
  1187. (progn
  1188. (setq foreign-regexp/regexp-type 'perl)
  1189. '(setq reb-re-syntax 'foreign-regexp)
  1190. ))
  1191. (autoload-eval-lazily 'sql '(sql-mode)
  1192. (require 'sql-indent nil t))
  1193. (add-to-list 'auto-mode-alist
  1194. '("\\.hql\\'" . sql-mode))
  1195. (when (autoload-eval-lazily 'git-command)
  1196. (define-key ctl-x-map "g" 'git-command))
  1197. ;; This keybind cause unexpected call really many many times
  1198. ;; (when (autoload-eval-lazily 'gited)
  1199. ;; (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1200. (defalias 'gited 'gited-list)
  1201. (when (safe-require-or-eval 'git-commit)
  1202. (global-git-commit-mode 1))
  1203. (with-eval-after-load 'git-commit
  1204. (add-hook 'git-commit-setup-hook
  1205. 'turn-off-auto-fill t))
  1206. (autoload-eval-lazily 'sl)
  1207. (with-eval-after-load 'rst
  1208. (defvar rst-mode-map)
  1209. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1210. (with-eval-after-load 'jdee
  1211. (add-hook 'jdee-mode-hook
  1212. (lambda ()
  1213. (make-local-variable 'global-mode-string)
  1214. (add-to-list 'global-mode-string
  1215. mode-line-position))))
  1216. ;; Cannot enable error thrown. Why???
  1217. ;; https://github.com/m0smith/malabar-mode#Installation
  1218. ;; (when (autoload-eval-lazily 'malabar-mode)
  1219. ;; (add-to-list 'load-path
  1220. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1221. ;; (safe-require-or-eval 'cedet-devel-load)
  1222. ;; (call-after-init (activate-malabar-mode)))
  1223. (with-eval-after-load 'make-mode
  1224. (defvar makefile-mode-map (make-sparse-keymap))
  1225. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1226. ;; this functions is set in write-file-functions, i cannot find any
  1227. ;; good way to remove this.
  1228. (fset 'makefile-warn-suspicious-lines 'ignore))
  1229. (with-eval-after-load 'verilog-mode
  1230. (defvar verilog-mode-map (make-sparse-keymap))
  1231. (define-key verilog-mode-map ";" 'self-insert-command))
  1232. (setq diff-switches "-u")
  1233. (with-eval-after-load 'diff-mode
  1234. ;; (when (and (eq major-mode
  1235. ;; 'diff-mode)
  1236. ;; (not buffer-file-name))
  1237. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1238. ;; (view-mode 1))
  1239. (set-face-attribute 'diff-header nil
  1240. :foreground nil
  1241. :background nil
  1242. :weight 'bold)
  1243. (set-face-attribute 'diff-file-header nil
  1244. :foreground nil
  1245. :background nil
  1246. :weight 'bold)
  1247. (set-face-foreground 'diff-index "blue")
  1248. (set-face-attribute 'diff-hunk-header nil
  1249. :foreground "cyan"
  1250. :weight 'normal)
  1251. (set-face-attribute 'diff-context nil
  1252. ;; :foreground "white"
  1253. :foreground nil
  1254. :weight 'normal)
  1255. (set-face-foreground 'diff-removed "red")
  1256. (set-face-foreground 'diff-added "green")
  1257. (set-face-background 'diff-removed nil)
  1258. (set-face-background 'diff-added nil)
  1259. (set-face-attribute 'diff-changed nil
  1260. :foreground "magenta"
  1261. :weight 'normal)
  1262. (set-face-attribute 'diff-refine-changed nil
  1263. :foreground nil
  1264. :background nil
  1265. :weight 'bold
  1266. :inverse-video t)
  1267. ;; Annoying !
  1268. ;;(diff-auto-refine-mode)
  1269. )
  1270. ;; (ffap-bindings)
  1271. (set-variable 'browse-url-browser-function
  1272. 'eww-browse-url)
  1273. (set-variable 'sh-here-document-word "__EOC__")
  1274. (when (autoload-eval-lazily 'adoc-mode
  1275. nil
  1276. (defvar adoc-mode-map (make-sparse-keymap))
  1277. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1278. (setq auto-mode-alist
  1279. `(("\\.adoc\\'" . adoc-mode)
  1280. ("\\.asciidoc\\'" . adoc-mode)
  1281. ,@auto-mode-alist)))
  1282. (with-eval-after-load 'markup-faces
  1283. ;; Is this too match ?
  1284. (set-face-foreground 'markup-meta-face
  1285. "color-245")
  1286. (set-face-foreground 'markup-meta-hide-face
  1287. "color-245")
  1288. )
  1289. ;; TODO: check if this is required
  1290. (when (autoload-eval-lazily 'groovy-mode nil
  1291. (defvar groovy-mode-map (make-sparse-keymap))
  1292. (define-key groovy-mode-map "(" 'self-insert-command)
  1293. (define-key groovy-mode-map ")" 'self-insert-command)
  1294. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1295. )
  1296. (add-to-list 'auto-mode-alist
  1297. '("build\\.gradle\\'" . groovy-mode)))
  1298. (add-to-list 'auto-mode-alist
  1299. '("\\.gawk\\'" . awk-mode))
  1300. (with-eval-after-load 'yaml-mode
  1301. (defvar yaml-mode-map (make-sparse-keymap))
  1302. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1303. (with-eval-after-load 'html-mode
  1304. (defvar html-mode-map (make-sparse-keymap))
  1305. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1306. (with-eval-after-load 'text-mode
  1307. (define-key text-mode-map (kbd "C-m") 'newline))
  1308. (autoload-eval-lazily 'info nil
  1309. (defvar Info-additional-directory-list)
  1310. (dolist (dir (directory-files (concat user-emacs-directory
  1311. "info")
  1312. t
  1313. "^[^.].*"))
  1314. (when (file-directory-p dir)
  1315. (add-to-list 'Info-additional-directory-list
  1316. dir)))
  1317. (let ((dir (expand-file-name "~/.brew/share/info")))
  1318. (when (file-directory-p dir)
  1319. (add-to-list 'Info-additional-directory-list
  1320. dir))))
  1321. (with-eval-after-load 'apropos
  1322. (defvar apropos-mode-map (make-sparse-keymap))
  1323. (define-key apropos-mode-map "n" 'next-line)
  1324. (define-key apropos-mode-map "p" 'previous-line))
  1325. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1326. ;; (define-key isearch-mode-map
  1327. ;; (kbd "C-j") 'isearch-other-control-char)
  1328. ;; (define-key isearch-mode-map
  1329. ;; (kbd "C-k") 'isearch-other-control-char)
  1330. ;; (define-key isearch-mode-map
  1331. ;; (kbd "C-h") 'isearch-other-control-char)
  1332. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1333. (define-key isearch-mode-map (kbd "M-r")
  1334. 'isearch-query-replace-regexp)
  1335. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1336. (setq lazy-highlight-cleanup nil)
  1337. ;; face for isearch highlighing
  1338. (set-face-attribute 'lazy-highlight
  1339. nil
  1340. :foreground `unspecified
  1341. :background `unspecified
  1342. :underline t
  1343. ;; :weight `bold
  1344. )
  1345. (add-hook 'outline-mode-hook
  1346. (lambda ()
  1347. (when (string-match "\\.md\\'" buffer-file-name)
  1348. (set (make-local-variable 'outline-regexp) "#+ "))))
  1349. (add-hook 'outline-mode-hook
  1350. 'outline-show-all)
  1351. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1352. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1353. (when (autoload-eval-lazily 'markdown-mode
  1354. '(markdown-mode gfm-mode)
  1355. (defvar gfm-mode-map (make-sparse-keymap))
  1356. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1357. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1358. )
  1359. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1360. (set-variable 'markdown-command (or (executable-find "markdown")
  1361. (executable-find "markdown.pl")
  1362. ""))
  1363. (add-hook 'markdown-mode-hook
  1364. (lambda ()
  1365. (outline-minor-mode 1)
  1366. (flyspell-mode)
  1367. (set (make-local-variable 'comment-start) ";")))
  1368. )
  1369. ;; c-mode
  1370. ;; http://www.emacswiki.org/emacs/IndentingC
  1371. ;; http://en.wikipedia.org/wiki/Indent_style
  1372. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1373. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1374. (with-eval-after-load 'cc-vars
  1375. (defvar c-default-style nil)
  1376. (add-to-list 'c-default-style
  1377. '(c-mode . "k&r"))
  1378. (add-to-list 'c-default-style
  1379. '(c++-mode . "k&r")))
  1380. (autoload-eval-lazily 'js2-mode nil
  1381. ;; currently do not use js2-mode
  1382. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1383. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1384. ;; (defvar js2-mode-map (make-sparse-keymap))
  1385. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1386. ;; (interactive)
  1387. ;; (js2-enter-key)
  1388. ;; (indent-for-tab-command)))
  1389. ;; (add-hook (kill-local-variable 'before-save-hook)
  1390. ;; 'js2-before-save)
  1391. ;; (add-hook 'before-save-hook
  1392. ;; 'my-indent-buffer
  1393. ;; nil
  1394. ;; t)
  1395. )
  1396. (add-to-list 'interpreter-mode-alist
  1397. '("node" . js-mode))
  1398. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1399. (with-eval-after-load 'uniquify
  1400. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1401. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1402. (setq uniquify-min-dir-content 1))
  1403. (with-eval-after-load 'view
  1404. (defvar view-mode-map (make-sparse-keymap))
  1405. (define-key view-mode-map "j" 'scroll-up-line)
  1406. (define-key view-mode-map "k" 'scroll-down-line)
  1407. (define-key view-mode-map "v" 'toggle-read-only)
  1408. (define-key view-mode-map "q" 'bury-buffer)
  1409. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1410. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1411. ;; (define-key view-mode-map
  1412. ;; "n" 'nonincremental-repeat-search-forward)
  1413. ;; (define-key view-mode-map
  1414. ;; "N" 'nonincremental-repeat-search-backward)
  1415. ;; N conflicts with git-walktree
  1416. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1417. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1418. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1419. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1420. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1421. (global-set-key "\M-r" 'view-mode)
  1422. ;; (setq view-read-only t)
  1423. (with-eval-after-load 'term
  1424. (defvar term-raw-map (make-sparse-keymap))
  1425. (define-key term-raw-map (kbd "C-x")
  1426. (lookup-key (current-global-map)
  1427. (kbd "C-x"))))
  1428. (add-hook 'term-mode-hook
  1429. (lambda ()
  1430. ;; Stop current line highlighting
  1431. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1432. (set-variable 'scroll-margin 0 t)
  1433. ))
  1434. (set-variable 'term-buffer-maximum-size 20480)
  1435. (set-variable 'term-suppress-hard-newline t)
  1436. (add-hook 'Man-mode-hook
  1437. (lambda ()
  1438. (view-mode 1)
  1439. (setq truncate-lines nil)))
  1440. (set-variable 'Man-notify-method (if window-system
  1441. 'newframe
  1442. 'aggressive))
  1443. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1444. "woman_cache.el")))
  1445. ;; not work because man.el will be loaded when man called
  1446. (defalias 'man 'woman)
  1447. (add-to-list 'auto-mode-alist
  1448. '("tox\\.ini\\'" . conf-unix-mode))
  1449. (when (autoload-eval-lazily 'toml-mode)
  1450. (add-to-list 'auto-mode-alist
  1451. '("/tox\\.ini\\'" . toml-mode))
  1452. (add-to-list 'auto-mode-alist
  1453. '("/Pipfile\\'" . toml-mode))
  1454. (add-to-list 'auto-mode-alist
  1455. '("/poetry\\.lock\\'" . toml-mode))
  1456. )
  1457. (when (autoload-eval-lazily 'json-mode)
  1458. (add-to-list 'auto-mode-alist
  1459. '("/Pipfile\\.lock\\'" . json-mode)))
  1460. (add-hook 'go-mode-hook
  1461. (lambda()
  1462. (defvar go-mode-map)
  1463. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1464. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1465. (when (autoload-eval-lazily 'k8s-mode)
  1466. (add-to-list 'auto-mode-alist
  1467. '("\\.k8s\\'" . k8s-mode)))
  1468. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1469. ;; buffers
  1470. (defvar bs-configurations)
  1471. (declare-function bs-set-configuration "bs")
  1472. (declare-function bs-refresh "bs")
  1473. (declare-function bs-message-without-log "bs")
  1474. (declare-function bs--current-config-message "bs")
  1475. (when (autoload-eval-lazily 'bs '(bs-show)
  1476. (add-to-list 'bs-configurations
  1477. '("specials" "^\\*" nil ".*" nil nil))
  1478. (add-to-list 'bs-configurations
  1479. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1480. (defvar bs-mode-map)
  1481. (defvar bs-current-configuration)
  1482. (define-key bs-mode-map (kbd "t")
  1483. ;; TODO: fix toggle feature
  1484. (lambda ()
  1485. (interactive)
  1486. (if (string= "specials"
  1487. bs-current-configuration)
  1488. (bs-set-configuration "files")
  1489. (bs-set-configuration "specials"))
  1490. (bs-refresh)
  1491. (bs-message-without-log "%s"
  1492. (bs--current-config-message))))
  1493. ;; (setq bs-configurations (list
  1494. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1495. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1496. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1497. )
  1498. (defalias 'list-buffers 'bs-show)
  1499. (set-variable 'bs-default-configuration "files-and-specials")
  1500. (set-variable 'bs-default-sort-name "by nothing")
  1501. (add-hook 'bs-mode-hook
  1502. (lambda ()
  1503. (set (make-local-variable 'scroll-margin) 0))))
  1504. ;;(iswitchb-mode 1)
  1505. (icomplete-mode)
  1506. (defun iswitchb-buffer-display-other-window ()
  1507. "Do iswitchb in other window."
  1508. (interactive)
  1509. (let ((iswitchb-default-method 'display))
  1510. (call-interactively 'iswitchb-buffer)))
  1511. ;; buffer killing
  1512. ;; (defun my-delete-window-killing-buffer () nil)
  1513. (defun my-query-kill-current-buffer ()
  1514. "Interactively kill current buffer."
  1515. (interactive)
  1516. (if (y-or-n-p (concat "kill current buffer? :"))
  1517. (kill-buffer (current-buffer))))
  1518. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1519. (substitute-key-definition 'kill-buffer
  1520. 'my-query-kill-current-buffer
  1521. global-map)
  1522. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1523. ;; dired
  1524. (defun my-file-head (filename &optional n)
  1525. "Return list of first N lines of file FILENAME."
  1526. ;; TODO: Fix for janapese text
  1527. ;; TODO: Fix for short text
  1528. (let ((num (or n 10))
  1529. (size 100)
  1530. (beg 0)
  1531. (end 0)
  1532. (result '())
  1533. (read -1))
  1534. (with-temp-buffer
  1535. (erase-buffer)
  1536. (while (or (<= (count-lines (point-min)
  1537. (point-max))
  1538. num)
  1539. (eq read 0))
  1540. (setq end (+ beg size))
  1541. (setq read (nth 1 (insert-file-contents-literally filename
  1542. nil
  1543. beg
  1544. end)))
  1545. (goto-char (point-max))
  1546. (setq beg (+ beg size)))
  1547. (goto-char (point-min))
  1548. (while (< (length result) num)
  1549. (let ((start (point)))
  1550. (forward-line 1)
  1551. (setq result
  1552. `(,@result ,(buffer-substring-no-properties start
  1553. (point))))))
  1554. result
  1555. ;; (buffer-substring-no-properties (point-min)
  1556. ;; (progn
  1557. ;; (forward-line num)
  1558. ;; (point)))
  1559. )))
  1560. ;; (apply 'concat (my-file-head "./shrc" 10)
  1561. (defun my-dired-echo-file-head (arg)
  1562. "Echo head of current file.
  1563. ARG is num to show, or defaults to 7."
  1564. (interactive "P")
  1565. (let ((f (dired-get-filename)))
  1566. (message "%s"
  1567. (apply 'concat
  1568. (my-file-head f
  1569. 7)))))
  1570. (defun my-dired-diff ()
  1571. "Show diff of marked file and file of current line."
  1572. (interactive)
  1573. (let ((files (dired-get-marked-files nil nil nil t)))
  1574. (if (eq (car files)
  1575. t)
  1576. (diff (cadr files) (dired-get-filename))
  1577. (message "One file must be marked!"))))
  1578. (defun dired-get-file-info ()
  1579. "Print information of current line file."
  1580. (interactive)
  1581. (let ((f (shell-quote-argument (dired-get-filename t))))
  1582. (if (file-directory-p f)
  1583. (progn
  1584. (message "Calculating disk usage...")
  1585. (shell-command (concat "du -hsD "
  1586. f)))
  1587. (shell-command (concat "file "
  1588. f)))))
  1589. (defun my-dired-scroll-up ()
  1590. "Scroll up."
  1591. (interactive)
  1592. (my-dired-previous-line (- (window-height) 1)))
  1593. (defun my-dired-scroll-down ()
  1594. "Scroll down."
  1595. (interactive)
  1596. (my-dired-next-line (- (window-height) 1)))
  1597. ;; (defun my-dired-forward-line (arg)
  1598. ;; ""
  1599. ;; (interactive "p"))
  1600. (defun my-dired-previous-line (arg)
  1601. "Move ARG lines up."
  1602. (interactive "p")
  1603. (if (> arg 0)
  1604. (progn
  1605. (if (eq (line-number-at-pos)
  1606. 1)
  1607. (goto-char (point-max))
  1608. (forward-line -1))
  1609. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1610. (dired-get-subdir))
  1611. (- arg 1)
  1612. arg)))
  1613. (dired-move-to-filename)))
  1614. (defun my-dired-next-line (arg)
  1615. "Move ARG lines down."
  1616. (interactive "p")
  1617. (if (> arg 0)
  1618. (progn
  1619. (if (eq (point)
  1620. (point-max))
  1621. (goto-char (point-min))
  1622. (forward-line 1))
  1623. (my-dired-next-line (if (or (dired-get-filename nil t)
  1624. (dired-get-subdir))
  1625. (- arg 1)
  1626. arg)))
  1627. (dired-move-to-filename)))
  1628. (defun my-tramp-remote-find-file (f)
  1629. "Open F."
  1630. (interactive (list (read-file-name "My Find File Tramp: "
  1631. "/scp:"
  1632. nil ;; "/scp:"
  1633. (confirm-nonexistent-file-or-buffer))))
  1634. (find-file f))
  1635. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1636. (if (eq window-system 'mac)
  1637. (setq dired-listing-switches "-lhF")
  1638. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1639. )
  1640. (setq dired-listing-switches "-lhF")
  1641. ;; when using dired-find-alternate-file
  1642. ;; reuse current dired buffer for the file to open
  1643. ;; (put 'dired-find-alternate-file 'disabled nil)
  1644. (set-variable 'dired-ls-F-marks-symlinks t)
  1645. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1646. (set-variable 'ls-lisp-dirs-first t)
  1647. (set-variable 'ls-lisp-use-localized-time-format t)
  1648. (set-variable 'ls-lisp-format-time-list
  1649. '("%Y-%m-%d %H:%M"
  1650. "%Y-%m-%d "))
  1651. (set-variable 'dired-dwim-target t)
  1652. (set-variable 'dired-isearch-filenames t)
  1653. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1654. (set-variable 'dired-hide-details-hide-information-lines nil)
  1655. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1656. (set-variable 'dired-recursive-deletes 'always)
  1657. ;; (add-hook 'dired-after-readin-hook
  1658. ;; 'my-replace-nasi-none)
  1659. (with-eval-after-load 'dired
  1660. (safe-require-or-eval 'ls-lisp)
  1661. (defvar dired-mode-map (make-sparse-keymap))
  1662. ;; dired-do-chgrp sometimes cause system hung
  1663. (define-key dired-mode-map "G" 'ignore)
  1664. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1665. (define-key dired-mode-map "i" 'dired-get-file-info)
  1666. ;; (define-key dired-mode-map "f" 'find-file)
  1667. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1668. (define-key dired-mode-map "z" 'fzf)
  1669. (define-key dired-mode-map "!" 'shell-command)
  1670. (define-key dired-mode-map "&" 'async-shell-command)
  1671. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1672. (define-key dired-mode-map "=" 'my-dired-diff)
  1673. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1674. (define-key dired-mode-map "b" 'gtkbm)
  1675. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1676. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1677. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1678. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1679. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1680. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1681. (substitute-key-definition 'dired-next-line
  1682. 'my-dired-next-line
  1683. dired-mode-map)
  1684. (substitute-key-definition 'dired-previous-line
  1685. 'my-dired-previous-line
  1686. dired-mode-map)
  1687. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1688. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1689. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1690. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1691. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1692. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1693. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1694. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1695. (add-hook 'dired-mode-hook
  1696. (lambda ()
  1697. (when (fboundp 'dired-hide-details-mode)
  1698. (dired-hide-details-mode t)
  1699. (local-set-key "l" 'dired-hide-details-mode))
  1700. (let ((file "._Icon\015"))
  1701. (when nil
  1702. '(file-readable-p file)
  1703. (delete-file file)))))
  1704. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1705. (defvar pack-program-alist)
  1706. (add-to-list 'pack-program-alist
  1707. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1708. (set-variable 'pack-silence
  1709. t)
  1710. (with-eval-after-load 'dired
  1711. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1712. (when (autoload-eval-lazily 'dired-list-all-mode)
  1713. (setq dired-listing-switches "-lhF")
  1714. (with-eval-after-load 'dired
  1715. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1716. (when (autoload-eval-lazily 'dired-filter)
  1717. (add-hook 'dired-mode-hook
  1718. 'dired-filter-mode))
  1719. (set-variable 'dired-filter-stack nil)
  1720. ;; Currently disabled in favor of dired-from-git-ls-files
  1721. ;; (define-key ctl-x-map "f" 'find-dired)
  1722. (defvar my-dired-git-ls-files-history
  1723. "History for `my-dired-git-ls-files'." nil)
  1724. (defun my-dired-git-ls-files (arg)
  1725. "Dired from git ls-files."
  1726. (interactive (list
  1727. (read-shell-command "git ls-files: "
  1728. "git ls-files -z ")))
  1729. (pop-to-buffer-same-window
  1730. (dired-noselect `(,default-directory
  1731. ,@(split-string (shell-command-to-string arg)
  1732. "\0" t))
  1733. ""))
  1734. )
  1735. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1736. (with-eval-after-load 'dired
  1737. (defvar dired-mode-map (make-sparse-keymap))
  1738. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1739. ;; (define-minor-mode my-dired-glob-filter)
  1740. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1741. ;; misc funcs
  1742. (define-key ctl-x-map "T" 'git-worktree)
  1743. (define-key ctl-x-map "W" 'git-walktree)
  1744. (when (fboundp 'browse-url-default-macosx-browser)
  1745. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1746. (defalias 'qcalc 'quick-calc)
  1747. (defun memo (&optional dir)
  1748. "Open memo.txt in DIR."
  1749. (interactive)
  1750. (pop-to-buffer (find-file-noselect (concat (if dir
  1751. (file-name-as-directory dir)
  1752. "")
  1753. "memo.txt"))))
  1754. ;; TODO: remember-projectile
  1755. (set (defvar my-privnotes-path nil
  1756. "My privnotes repository path.")
  1757. (expand-file-name "~/my/privnotes"))
  1758. (defun my-privnotes-readme (dir)
  1759. "Open my privnotes DIR."
  1760. (interactive (list
  1761. (read-file-name "Privnotes: "
  1762. (expand-file-name (format-time-string "%Y%m%d_")
  1763. my-privnotes-path))))
  1764. (let ((path (expand-file-name "README.md" dir)))
  1765. (with-current-buffer (find-file path)
  1766. (unless (file-exists-p path)
  1767. (insert (file-name-base dir)
  1768. "\n"
  1769. "=======\n"
  1770. "\n\n")))))
  1771. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1772. (set (defvar my-rgrep-alist nil
  1773. "Alist of rgrep command.
  1774. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1775. condition to choose COMMAND when evaluated.")
  1776. `(
  1777. ;; ripgrep
  1778. ("rg"
  1779. (executable-find "rg")
  1780. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  1781. ;; git grep
  1782. ("gitgrep"
  1783. (eq 0
  1784. (shell-command "git rev-parse --git-dir"))
  1785. "git --no-pager grep -nH -e ")
  1786. ;; sift
  1787. ("sift"
  1788. (executable-find "sift")
  1789. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1790. ;; the silver searcher
  1791. ("ag"
  1792. (executable-find "ag")
  1793. "ag --nogroup --nopager --filename ")
  1794. ;; ack
  1795. ("ack"
  1796. (executable-find "ack")
  1797. "ack --nogroup --nopager --with-filename ")
  1798. ;; gnu global
  1799. ("global"
  1800. (and (require 'ggtags nil t)
  1801. (executable-find "global")
  1802. (ggtags-current-project-root))
  1803. "global --result grep ")
  1804. ;; grep
  1805. ("grep"
  1806. t
  1807. ,(concat "find . "
  1808. "-path '*/.git' -prune -o "
  1809. "-path '*/.svn' -prune -o "
  1810. "-type f -print0 | "
  1811. "xargs -0 grep -nH -e "))
  1812. )
  1813. )
  1814. (defvar my-rgrep-default nil
  1815. "Default command name for my-rgrep.")
  1816. (defun my-rgrep-grep-command (&optional name alist)
  1817. "Return recursive grep command for current directory or nil.
  1818. If NAME is given, use that without testing.
  1819. Commands are searched from ALIST."
  1820. (if alist
  1821. (if name
  1822. ;; if name is given search that from alist and return the command
  1823. (nth 2 (assoc name
  1824. alist))
  1825. ;; if name is not given try test in 1th elem
  1826. (let ((car (car alist))
  1827. (cdr (cdr alist)))
  1828. (if (eval (nth 1 car))
  1829. ;; if the condition is true return the command
  1830. (nth 2 car)
  1831. ;; try next one
  1832. (and cdr
  1833. (my-rgrep-grep-command name cdr)))))
  1834. ;; if alist is not given set default value
  1835. (my-rgrep-grep-command name my-rgrep-alist)))
  1836. (defun my-rgrep (command-args)
  1837. "My recursive grep. Run COMMAND-ARGS.
  1838. If prefix argument is given, use current symbol as default search target
  1839. and search from projectile root (if projectile is available)."
  1840. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1841. nil)))
  1842. (if cmd
  1843. (list (read-shell-command "grep command: "
  1844. (concat cmd
  1845. (if current-prefix-arg
  1846. (thing-at-point 'symbol t)
  1847. ""))
  1848. 'grep-find-history))
  1849. (error "My-Rgrep: Command for rgrep not found")
  1850. )))
  1851. (if (and current-prefix-arg
  1852. (safe-require-or-eval 'projectile)
  1853. (projectile-project-p))
  1854. (projectile-with-default-dir (projectile-project-root)
  1855. (compilation-start command-args
  1856. 'grep-mode))
  1857. (compilation-start command-args
  1858. 'grep-mode)))
  1859. (defun my-rgrep-thing-at-point-projectile-root ()
  1860. "My recursive grep to find thing at point from project root."
  1861. (interactive)
  1862. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1863. nil))
  1864. (command-args
  1865. (if cmd
  1866. (concat cmd
  1867. (or (thing-at-point 'symbol t)
  1868. (error "No symbol at point")))
  1869. (error "My-Rgrep: Command for rgrep not found"))))
  1870. (if (safe-require-or-eval 'projectile)
  1871. (projectile-with-default-dir (or (projectile-project-root)
  1872. default-directory)
  1873. (compilation-start command-args
  1874. 'grep-mode))
  1875. (compilation-start command-args
  1876. 'grep-mode))))
  1877. (defmacro define-my-rgrep (name)
  1878. "Define rgrep for NAME."
  1879. `(defun ,(intern (concat "my-rgrep-"
  1880. name)) ()
  1881. ,(format "My recursive grep by %s."
  1882. name)
  1883. (interactive)
  1884. (let ((my-rgrep-default ,name))
  1885. (if (called-interactively-p 'any)
  1886. (call-interactively 'my-rgrep)
  1887. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1888. )
  1889. (define-my-rgrep "ack")
  1890. (define-my-rgrep "ag")
  1891. (define-my-rgrep "rg")
  1892. (define-my-rgrep "sift")
  1893. (define-my-rgrep "gitgrep")
  1894. (define-my-rgrep "grep")
  1895. (define-my-rgrep "global")
  1896. (define-key ctl-x-map "s" 'my-rgrep)
  1897. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1898. (defun my-occur (regexp &optional region)
  1899. "My occur command to search REGEXP."
  1900. (interactive (list (read-string "List lines matching regexp: "
  1901. (thing-at-point 'symbol t))))
  1902. (occur regexp nil region))
  1903. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1904. (when (fboundp 'swoop)
  1905. (define-key ctl-x-map (kbd "C-o") 'swoop)
  1906. )
  1907. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1908. (defalias 'make 'compile)
  1909. (define-key ctl-x-map "c" 'compile)
  1910. (defun my-pushbullet-note (text &optional title)
  1911. "Push TEXT."
  1912. (interactive "sText to Push: ")
  1913. (pb/push-item '("") text "note" (or title "")))
  1914. ;;;;;;;;;;;;;;;;;;;;;
  1915. ;; git-bug
  1916. (defconst git-bug-ls-regexp
  1917. (eval-when-compile
  1918. (rx bol
  1919. (submatch (one-or-more alphanumeric)) ; id
  1920. ;; (one-or-more any)
  1921. (one-or-more space)
  1922. (submatch (or "open" "close")) ; status
  1923. (one-or-more space)
  1924. (submatch (maximal-match (zero-or-more print))) ; title
  1925. "\t"
  1926. (submatch (one-or-more alphanumeric)) ; user
  1927. (one-or-more space)
  1928. "C:"
  1929. (submatch (one-or-more digit)) ; Comment num
  1930. (one-or-more space)
  1931. "L:"
  1932. (submatch (one-or-more digit)) ; Label num
  1933. eol
  1934. ))
  1935. "Regexp to parse line of output of git-bug ls.
  1936. Used by `git-bug-ls'.")
  1937. (defun git-bug-bugs ()
  1938. "Get list of git-bug bugs."
  1939. (with-temp-buffer
  1940. (git-bug--call-process "bug" "ls")
  1941. (goto-char (point-min))
  1942. (let ((bugs nil))
  1943. (while (not (eq (point) (point-max)))
  1944. (save-match-data
  1945. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  1946. (setq bugs `(,@bugs
  1947. ,(list
  1948. :id (match-string 1)
  1949. :status (match-string 2)
  1950. :title (string-trim (match-string 3))
  1951. :user (match-string 4)
  1952. :comment-num (match-string 5)
  1953. :label-num (match-string 6)
  1954. )))))
  1955. (forward-line 1)
  1956. (goto-char (point-at-bol)))
  1957. bugs)))
  1958. (defun git-bug-ls ()
  1959. "Open and select git bug list buffer."
  1960. (interactive)
  1961. (pop-to-buffer (git-bug-ls-noselect)))
  1962. (defun git-bug-ls-noselect (&optional directory)
  1963. "Open git bug list buffer.
  1964. If optional arg DIRECTORY is given change current directory to there before
  1965. initializing."
  1966. (setq directory (expand-file-name (or directory
  1967. default-directory)))
  1968. (cl-assert (file-directory-p directory))
  1969. (let* ((root (git-bug--get-repository-root directory))
  1970. (name (file-name-nondirectory root))
  1971. (bname (format "*GitBug<%s>*" name)))
  1972. (with-current-buffer (get-buffer-create bname)
  1973. (cd root)
  1974. (git-bug-ls--set-tabulated-list-mode-variables)
  1975. (git-bug-ls-mode)
  1976. (current-buffer))))
  1977. (defun git-bug--get-repository-root (dir)
  1978. "Resolve repository root of DIR.
  1979. If DIR is not inside of any git repository, signal an error."
  1980. (cl-assert (file-directory-p dir))
  1981. (with-temp-buffer
  1982. (cd dir)
  1983. (git-bug--call-process "rev-parse" "--show-toplevel")
  1984. (goto-char (point-min))
  1985. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1986. (defun git-bug--call-process (&rest args)
  1987. "Start git process synchronously with ARGS.
  1988. Raise error when git process ends with non-zero status.
  1989. Any output will be written to current buffer."
  1990. (let ((status (apply 'call-process
  1991. "git"
  1992. nil
  1993. t
  1994. nil
  1995. args)))
  1996. (cl-assert (eq status 0)
  1997. nil
  1998. (buffer-substring-no-properties (point-min) (point-max)))))
  1999. ;;;;;;;;;;;;;;;;;;;
  2000. ;; peek-file-mode
  2001. (defun peek-file (file)
  2002. "Peek FILE."
  2003. (interactive)
  2004. (message "%s" file))
  2005. ;;;;;;;;;;;;;;;;;;;;
  2006. ;; remember-projectile
  2007. ;; TODO: Add global-minor-mode
  2008. (defvar remember-data-file)
  2009. (defun my-set-remember-data-file-buffer-local ()
  2010. "Set `remember-data-file'."
  2011. (when (require 'projectile nil t)
  2012. (setq-local remember-data-file
  2013. (expand-file-name ".remember.notes"
  2014. (projectile-project-root)))))
  2015. (add-hook 'after-change-major-mode-hook
  2016. 'my-set-remember-data-file-buffer-local)
  2017. (define-key ctl-x-map "R" 'remember)
  2018. ;;;;;;;;;;;;;;;;;;;;;;;
  2019. ;; flycheck-pydocstyle
  2020. (require 'flycheck nil t)
  2021. ;; TODO: Use `source' and config file
  2022. (flycheck-define-checker python-pydocstyle
  2023. "Docstring style checker."
  2024. :command ("python" "-m" "pydocstyle"
  2025. source-inplace)
  2026. :error-patterns
  2027. (
  2028. (error line-start (file-name) ":" line " " (one-or-more not-newline) "\n" (one-or-more blank) (message) line-end)
  2029. )
  2030. :enabled (lambda ()
  2031. (or (not (flycheck-python-needs-module-p 'python-pydocstyle))
  2032. (flycheck-python-find-module 'python-black-check "pydocstyle")))
  2033. :verify (lambda (_) (flycheck-python-verify-module 'python-black-check "pydocstyle"))
  2034. :modes python-mode)
  2035. (defun flycheck-pydocstyle-setup ()
  2036. "Setup Flycheck pydocstyle."
  2037. (interactive)
  2038. (add-to-list 'flycheck-checkers
  2039. 'python-pydocstyle))
  2040. ;; (flycheck-pydocstyle-setup)
  2041. ;; Local Variables:
  2042. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2043. ;; flycheck-checker: emacs-lisp
  2044. ;; End:
  2045. ;;; emancs.el ends here