Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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