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

3154 line
106 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (load "bytecomp") ;; Required for WSL environment
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  7. ;; TODO: Use custom-set-variables in place of set-variable
  8. (setq debug-on-error t)
  9. ;; make directories
  10. (unless (file-directory-p (expand-file-name user-emacs-directory))
  11. (make-directory (expand-file-name user-emacs-directory)))
  12. ;; Custom file
  13. (setq custom-file (expand-file-name "custom.el" user-emacs-directory))
  14. (when (file-readable-p custom-file)
  15. (load custom-file))
  16. (require 'cl-lib)
  17. (require 'simple)
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;; Some macros for internals
  20. (defvar after-first-visit-hook nil
  21. "Run only once at the first visit of file.")
  22. (defvar after-first-visit-hook--done nil
  23. "Non-nil when `after-first-visit-hook' has already been called.")
  24. (defun after-first-visit-hook-run ()
  25. "Run `after-first-visit-hook' and clear its config."
  26. (when (not after-first-visit-hook--done)
  27. (run-hooks 'after-first-visit-hook))
  28. (setq after-first-visit-hook--done t)
  29. (remove-hook 'find-file-hook
  30. 'after-first-visit-hook-run))
  31. (add-hook 'find-file-hook
  32. 'after-first-visit-hook-run)
  33. (defmacro eval-after-init (&rest body)
  34. "If `after-init-hook' has been run, run BODY immediately.
  35. Otherwize hook it."
  36. (declare (indent 0) (debug t))
  37. `(if after-init-time
  38. ;; Currently after-init-hook is run just after setting after-init-hook
  39. (progn
  40. ,@body)
  41. (add-hook 'after-init-hook
  42. (lambda ()
  43. ,@body))))
  44. ;; (defmacro safe-require-or-eval (feature)
  45. ;; "Require FEATURE if available.
  46. ;; At compile time the feature will be loaded immediately."
  47. ;; `(eval-and-compile
  48. ;; (message "safe-require-or-eval: Trying to require %s" ,feature)
  49. ;; (require ,feature nil t)))
  50. ;; (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  51. ;; "Define autoloading FEATURE that defines FUNCTIONS.
  52. ;; FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  53. ;; the function same as FEATURE is defined as autoloaded function. BODY is passed
  54. ;; to `eval-after-load'.
  55. ;; After this macro is expanded, this returns the path to library if FEATURE
  56. ;; found, otherwise returns nil."
  57. ;; (declare (indent 2) (debug t))
  58. ;; (let* ((libname (symbol-name (eval feature)))
  59. ;; (libpath (locate-library libname)))
  60. ;; `(progn
  61. ;; (when (locate-library ,libname)
  62. ;; ,@(mapcar (lambda (f)
  63. ;; `(unless (fboundp ',f)
  64. ;; (progn
  65. ;; (message "Autoloaded function `%S' defined (%s)"
  66. ;; (quote ,f)
  67. ;; ,libpath)
  68. ;; (autoload (quote ,f)
  69. ;; ,libname
  70. ;; ,(concat "Autoloaded function defined in \""
  71. ;; libpath
  72. ;; "\".")
  73. ;; t))))
  74. ;; (or (eval functions)
  75. ;; `(,(eval feature)))))
  76. ;; (eval-after-load ,feature
  77. ;; (quote (progn
  78. ;; ,@body)))
  79. ;; (locate-library ,libname))))
  80. ;; (when (autoload-eval-lazily 'tetris nil
  81. ;; (message "Tetris loaded!"))
  82. ;; (message "Tetris found!"))
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. ;; package
  85. (require 'package)
  86. (set-variable 'package-archives
  87. `(,@package-archives
  88. ("melpa" . "https://melpa.org/packages/")
  89. ;; Somehow fails to download via https
  90. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  91. (package-initialize)
  92. ;; Use package-install-selected-packages to install these
  93. (let ((my '(
  94. vimrc-mode
  95. markdown-mode
  96. yaml-mode
  97. gnuplot-mode
  98. php-mode
  99. erlang
  100. js2-mode
  101. js-doc
  102. git-commit
  103. gitignore-mode
  104. adoc-mode
  105. go-mode
  106. ;; It seems malabar has been merged into jdee and this package
  107. ;; already removed
  108. ;; malabar-mode
  109. gosh-mode
  110. scala-mode
  111. web-mode
  112. toml-mode
  113. json-mode
  114. color-moccur
  115. ggtags
  116. flycheck
  117. auto-highlight-symbol
  118. hl-todo
  119. ;; Currently not available
  120. ;; pp-c-l
  121. xclip
  122. foreign-regexp
  123. multi-term
  124. term-run
  125. editorconfig
  126. git-ps1-mode
  127. restart-emacs
  128. fill-column-indicator
  129. pkgbuild-mode
  130. minibuffer-line
  131. which-key
  132. ;; I think this works in place of my autosave lib
  133. super-save
  134. pipenv
  135. imenu-list
  136. page-break-lines
  137. ;; aggressive-indent
  138. dired-filter
  139. wgrep
  140. magit
  141. git-gutter
  142. end-mark
  143. sl
  144. ;; TODO: Configure pony-tpl-mode
  145. pony-mode
  146. gited
  147. highlight-indentation
  148. diminish
  149. fic-mode
  150. term-cursor
  151. pydoc
  152. color-identifiers-mode
  153. dired-k
  154. blacken
  155. back-button
  156. with-venv
  157. nyan-mode
  158. diredfl
  159. hardhat
  160. counsel
  161. ivy-prescient
  162. editorconfig
  163. editorconfig-custom-majormode
  164. git-command
  165. prompt-text
  166. ;; 10sr repository
  167. ;; 10sr-extras
  168. terminal-title
  169. dired-list-all-mode
  170. pack
  171. set-modeline-color
  172. read-only-only-mode
  173. smart-revert
  174. autosave
  175. ;;window-organizer
  176. ilookup
  177. pasteboard
  178. awk-preview
  179. recently
  180. fuzzy-finder
  181. )))
  182. (set-variable 'package-selected-packages
  183. (cl-remove-duplicates (append package-selected-packages
  184. my
  185. ()))))
  186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  187. ;; my-idle-hook
  188. (defvar my-idle-hook nil
  189. "Hook run when idle for several secs.")
  190. (defvar my-idle-hook-sec 5
  191. "Second to run `my-idle-hook'.")
  192. (run-with-idle-timer my-idle-hook-sec
  193. t
  194. (lambda ()
  195. (run-hooks 'my-idle-hook)))
  196. ;; (add-hook 'my-idle-hook
  197. ;; (lambda ()
  198. ;; (message "idle hook message")))
  199. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  200. ;; start and quit
  201. (setq inhibit-startup-message t)
  202. (setq initial-buffer-choice 'messages-buffer)
  203. (setq confirm-kill-emacs 'y-or-n-p)
  204. ;; (setq gc-cons-threshold (* 1024 1024 16))
  205. (setq garbage-collection-messages nil)
  206. (when window-system
  207. (add-to-list 'default-frame-alist '(cursor-type . box))
  208. (add-to-list 'default-frame-alist '(background-color . "white"))
  209. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  210. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  211. ;; does not work?
  212. )
  213. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  214. (menu-bar-mode 1)
  215. (define-key ctl-x-map "M" 'menu-bar-open)
  216. (defalias 'menu 'menu-bar-open)
  217. (and (fboundp 'tool-bar-mode)
  218. (tool-bar-mode 0))
  219. (and (fboundp 'set-scroll-bar-mode)
  220. (set-scroll-bar-mode nil))
  221. (eval-after-init
  222. (message "%s %s" invocation-name emacs-version)
  223. (message "Invocation directory: %s" default-directory)
  224. (message "%s was taken to initialize emacs." (emacs-init-time))
  225. ;; (view-echo-area-messages)
  226. ;; (view-emacs-news)
  227. )
  228. (with-current-buffer "*Messages*"
  229. (emacs-lock-mode 'kill))
  230. (cd ".") ; when using windows use / instead of \ in `default-directory'
  231. ;; locale
  232. (set-language-environment "Japanese")
  233. (set-default-coding-systems 'utf-8-unix)
  234. (prefer-coding-system 'utf-8-unix)
  235. (setq system-time-locale "C")
  236. ;; my prefix map
  237. (defvar my-prefix-map nil
  238. "My prefix map.")
  239. (define-prefix-command 'my-prefix-map)
  240. (global-set-key (kbd "C-^") 'my-prefix-map)
  241. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  242. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  243. ;; (comint-show-maximum-output)
  244. ;; kill scratch
  245. (eval-after-init
  246. (let ((buf (get-buffer "*scratch*")))
  247. (when buf
  248. (kill-buffer buf))))
  249. ;; modifier keys
  250. ;; (setq mac-option-modifier 'control)
  251. ;; display
  252. (setq visible-bell t)
  253. (setq ring-bell-function 'ignore)
  254. (mouse-avoidance-mode 'banish)
  255. (setq echo-keystrokes 0.1)
  256. (defun reload-init-file ()
  257. "Reload Emacs init file."
  258. (interactive)
  259. (when (and user-init-file
  260. (file-readable-p user-init-file))
  261. (load-file user-init-file)))
  262. (require 'session nil t)
  263. ;; server
  264. (set-variable 'server-name (concat "server"
  265. (number-to-string (emacs-pid))))
  266. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  267. ;; In Darwin environment, init fails with message like 'Service name too long'
  268. ;; when server-use-tcp is nil
  269. (when (or (eq system-type
  270. 'cygwin)
  271. (eq system-type
  272. 'darwin))
  273. (set-variable 'server-use-tcp t))
  274. ;; MSYS2 fix
  275. (when (eq system-type
  276. 'windows-nt)
  277. (setq shell-file-name
  278. (executable-find "bash"))
  279. '(setq function-key-map
  280. `(,@function-key-map ([pause] . [?\C-c])
  281. ))
  282. (define-key key-translation-map
  283. (kbd "<pause>")
  284. (kbd "C-c"))
  285. '(keyboard-translate [pause]
  286. (kbd "C-c")p)
  287. ;; TODO: move to other place later
  288. (when (not window-system)
  289. (setq interprogram-paste-function nil)
  290. (setq interprogram-cut-function nil)))
  291. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  292. ;; global keys
  293. (global-set-key (kbd "<up>") 'scroll-down-line)
  294. (global-set-key (kbd "<down>") 'scroll-up-line)
  295. (global-set-key (kbd "<left>") 'scroll-down)
  296. (global-set-key (kbd "<right>") 'scroll-up)
  297. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  298. ;; (global-set-key (kbd "C-\\") help-map)
  299. (define-key ctl-x-map (kbd "DEL") help-map)
  300. (define-key ctl-x-map (kbd "C-h") help-map)
  301. ;; This is often fired mistakenly
  302. (define-key ctl-x-map "h" 'ignore) ;; Previously mark-whole-buffer
  303. (define-key help-map "a" 'apropos)
  304. ;; disable annoying keys
  305. (global-set-key [prior] 'ignore)
  306. (global-set-key (kbd "<next>") 'ignore)
  307. (global-set-key [menu] 'ignore)
  308. (global-set-key [down-mouse-1] 'ignore)
  309. (global-set-key [down-mouse-2] 'ignore)
  310. (global-set-key [down-mouse-3] 'ignore)
  311. (global-set-key [mouse-1] 'ignore)
  312. (global-set-key [mouse-2] 'ignore)
  313. (global-set-key [mouse-3] 'ignore)
  314. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  315. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  316. ;; Interactively evaluate Emacs Lisp expressions
  317. (define-key ctl-x-map "i" 'ielm)
  318. (when (fboundp 'which-key-mode)
  319. (which-key-mode))
  320. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  321. ;; editor
  322. ;; Basically it should not set globally (instead use something like file local
  323. ;; variables or editorconfig), but for most cases I just need this...
  324. (defun my-set-require-final-newline ()
  325. "Set `require-final-newline'."
  326. (set (make-local-variable 'require-final-newline)
  327. mode-require-final-newline))
  328. (add-hook 'prog-mode-hook
  329. 'my-set-require-final-newline)
  330. (add-hook 'text-mode-hook
  331. 'my-set-require-final-newline)
  332. (add-hook 'conf-mode-hook
  333. 'my-set-require-final-newline)
  334. ;; Used from term-cursor
  335. ;; hbar is too hard to find...
  336. (defun my-cursor-type-change (&rest args)
  337. "ARGS are discarded."
  338. ;; TODO: Support wdired and wgrep
  339. (if buffer-read-only
  340. (setq cursor-type 'hbar)
  341. (setq cursor-type 'box)))
  342. ;; (add-hook 'switch-buffer-functions
  343. ;; 'my-cursor-type-change)
  344. ;; (add-hook 'read-only-mode-hook
  345. ;; 'my-cursor-type-change)
  346. ;; (when (fboundp 'global-term-cursor-mode)
  347. ;; (global-term-cursor-mode 1))
  348. ;; ;; (term-cursor--eval)
  349. (setq kill-whole-line t)
  350. (setq scroll-conservatively 35
  351. scroll-margin 2)
  352. (setq-default major-mode 'text-mode)
  353. (setq next-line-add-newlines nil)
  354. (setq kill-read-only-ok t)
  355. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  356. ;; (setq-default line-spacing 0.2)
  357. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  358. ;; (setq-default tab-width 4)
  359. (setq-default indent-tabs-mode nil)
  360. (setq-default indent-line-function 'indent-to-left-margin)
  361. ;; (setq-default indent-line-function nil)
  362. (setq-default truncate-lines nil)
  363. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  364. (delete-selection-mode 1)
  365. (cua-mode 0)
  366. (setq line-move-visual nil)
  367. (setq create-lockfiles nil)
  368. (setq set-mark-command-repeat-pop t)
  369. (setq-default fill-column 80)
  370. (add-hook 'before-save-hook
  371. 'time-stamp)
  372. ;; Add Time-stamp: <> to insert timestamp there
  373. (set-variable 'time-stamp-format
  374. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  375. ;; key bindings
  376. ;; moving around
  377. ;;(keyboard-translate ?\M-j ?\C-j)
  378. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  379. (define-key esc-map "p" 'backward-paragraph)
  380. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  381. (define-key esc-map "n" 'forward-paragraph)
  382. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  383. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  384. (global-set-key (kbd "C-<left>") 'scroll-down)
  385. (global-set-key (kbd "C-<right>") 'scroll-up)
  386. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  387. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  388. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  389. ;; C-h and DEL
  390. (global-set-key (kbd "C-h") (kbd "DEL"))
  391. ;; (normal-erase-is-backspace-mode 1)
  392. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  393. (global-set-key (kbd "C-m") 'newline-and-indent)
  394. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  395. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  396. (define-key esc-map "u" 'undo)
  397. (define-key esc-map "i" (kbd "ESC TAB"))
  398. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  399. ;; (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  400. ;; (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  401. (set-variable 'search-default-mode t)
  402. ;; TODO: Do not depend on ivy function
  403. ;; (set-variable 'search-default-mode
  404. ;; (lambda (str lax)
  405. ;; (ivy--regex-fuzzy (replace-regexp-in-string (rx (one-or-more whitespace))
  406. ;; ""
  407. ;; str))))
  408. (when (fboundp 'undo-fu-only-undo)
  409. (global-set-key (kbd "C-_") 'undo-fu-only-undo))
  410. (when (fboundp 'undo-fu-only-redo)
  411. (global-set-key (kbd "C-M-_") 'undo-fu-only-redo))
  412. (require 'page-ext nil t)
  413. (when (fboundp 'global-page-break-lines-mode)
  414. (add-hook 'after-first-visit-hook
  415. 'global-page-break-lines-mode))
  416. (with-eval-after-load 'page-break-lines
  417. (set-face-foreground 'page-break-lines
  418. "cyan")
  419. )
  420. (defun my-insert-page-break ()
  421. "Insert ^L."
  422. (interactive)
  423. (insert "\^L\n"))
  424. (when (fboundp 'global-git-gutter-mode)
  425. (add-hook 'after-first-visit-hook
  426. 'global-git-gutter-mode))
  427. (with-eval-after-load 'git-gutter
  428. (declare-function global-git-gutter-mode "git-gutter")
  429. (custom-set-variables
  430. '(git-gutter:lighter " Gttr"))
  431. (custom-set-variables
  432. '(git-gutter:update-interval 2))
  433. (custom-set-variables
  434. '(git-gutter:unchanged-sign " "))
  435. (when (>= (display-color-cells)
  436. 256)
  437. (let ((c "color-233"))
  438. (set-face-background 'git-gutter:modified c)
  439. (set-face-background 'git-gutter:added c)
  440. (set-face-background 'git-gutter:deleted c)
  441. (set-face-background 'git-gutter:unchanged c)))
  442. (set-face-background 'git-gutter:modified "magenta")
  443. (set-face-background 'git-gutter:added "green")
  444. (set-face-background 'git-gutter:deleted "red")
  445. )
  446. ;; (when (fboundp 'fancy-narrow-mode)
  447. ;; (add-hook 'after-first-visit-hook
  448. ;; 'fancy-narrow-mode))
  449. ;; https://solist.work/blog/posts/mark-ring/
  450. (set-variable 'mark-ring-max 32)
  451. (defun my-exchange-point-and-mark ()
  452. "`exchange-point-and-mark' without mark activation."
  453. (interactive)
  454. (exchange-point-and-mark)
  455. (deactivate-mark))
  456. (define-key ctl-x-map (kbd "C-x") 'my-exchange-point-and-mark)
  457. (when (fboundp 'counsel-mark-ring)
  458. (define-key ctl-x-map "m" 'counsel-mark-ring))
  459. (with-eval-after-load 'ivy
  460. (defvar ivy-sort-functions-alist)
  461. (add-to-list 'ivy-sort-functions-alist
  462. '(counsel-mark-ring)))
  463. (run-with-idle-timer 10 t
  464. (lambda ()
  465. (push-mark)
  466. ;; (when (fboundp 'visible-mark-move-overlays)
  467. ;; (visible-mark-move-overlays))
  468. ))
  469. (add-hook 'switch-buffer-functions
  470. (lambda (&rest _)
  471. (unless (or (mark t)
  472. (minibufferp))
  473. (push-mark))))
  474. (add-hook 'switch-buffer-functions
  475. (lambda (&rest _)
  476. (when (minibufferp)
  477. ;; Remove mark in minibuffer
  478. (set-mark nil))))
  479. (when (fboundp 'back-button-mode)
  480. (back-button-mode 1))
  481. (when (fboundp 'back-button-local-forward)
  482. (global-set-key (kbd "<right>") 'back-button-local-forward))
  483. (when (fboundp 'back-button-local-backward)
  484. (global-set-key (kbd "<left>") 'back-button-local-backward))
  485. (when (fboundp 'global-visible-mark-mode)
  486. (set-variable 'visible-mark-max 2)
  487. ;; (set-variable 'visible-mark-faces '(visible-mark-face1 visible-mark-face2))
  488. ;; http://emacs.rubikitch.com/visible-mark/
  489. ;; transient-mark-modeでC-SPC C-SPC、あるいはC-SPC C-gすると消えるバグ修正
  490. (defun visible-mark-move-overlays--avoid-disappear (&rest them)
  491. "Fix.
  492. THEM are function and its args."
  493. (let ((mark-active t)) (apply them)))
  494. (advice-add 'visible-mark-move-overlays
  495. :around
  496. 'visible-mark-move-overlays--avoid-disappear)
  497. ;; (global-visible-mark-mode 1)
  498. )
  499. ;; visible-mark-mode
  500. ;; visible-mark-overlays
  501. ;; mark-ring
  502. ;; (equal mark-ring (cl-copy-list mark-ring))
  503. (when (fboundp 'global-hardhat-mode)
  504. (with-eval-after-load 'hardhat
  505. (defvar hardhat-fullpath-protected-regexps)
  506. (add-to-list 'hardhat-fullpath-protected-regexps
  507. "/\\.venv/")
  508. (defvar hardhat-fullpath-editable-regexps)
  509. (add-to-list 'hardhat-fullpath-editable-regexps
  510. "/\\.git/EDIT_INDEX\\.diff\\'")
  511. (defvar hardhat-basename-editable-regexps)
  512. (add-to-list 'hardhat-basename-editable-regexps
  513. "\\`Pipfile.lock\\'")
  514. )
  515. (global-hardhat-mode 1))
  516. (with-eval-after-load 'ignoramus
  517. (defvar ignoramus-file-basename-exact-names)
  518. (set-variable 'ignoramus-file-basename-exact-names
  519. (delete "profile"
  520. ignoramus-file-basename-exact-names))
  521. (set-variable 'ignoramus-file-basename-exact-names
  522. (delete "Profile"
  523. ignoramus-file-basename-exact-names))
  524. )
  525. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  526. ;; title and mode-line
  527. (when (fboundp 'terminal-title-mode)
  528. ;; if TERM is not screen use default value
  529. (if (getenv "TMUX")
  530. ;; if use tmux locally just basename of current dir
  531. (set-variable 'terminal-title-format
  532. '((file-name-nondirectory (directory-file-name
  533. default-directory))))
  534. (if (and (let ((tty-type (frame-parameter nil
  535. 'tty-type)))
  536. (and tty-type
  537. (equal (car (split-string tty-type
  538. "-"))
  539. "screen")))
  540. (not (getenv "SSH_CONNECTION")))
  541. (set-variable 'terminal-title-format
  542. '((file-name-nondirectory (directory-file-name
  543. default-directory))))
  544. ;; seems that TMUX is used locally and ssh to remote host
  545. (set-variable 'terminal-title-format
  546. `("em:"
  547. ,user-login-name
  548. "@"
  549. ,(car (split-string (system-name)
  550. "\\."))
  551. ":"
  552. default-directory))
  553. )
  554. )
  555. (terminal-title-mode))
  556. (setq eol-mnemonic-dos "\\r\\n")
  557. (setq eol-mnemonic-mac "\\r")
  558. (setq eol-mnemonic-unix "")
  559. (which-function-mode 1)
  560. (line-number-mode 0)
  561. (column-number-mode 0)
  562. (size-indication-mode 0)
  563. (setq mode-line-position
  564. '(:eval (format ":%%l:%%c /%d%s"
  565. (count-lines (point-max)
  566. (point-min))
  567. (if (buffer-narrowed-p)
  568. "[N]"
  569. "")
  570. )))
  571. (when (fboundp 'diminish)
  572. (eval-after-init
  573. (diminish 'recently-mode)
  574. (diminish 'editorconfig-mode)
  575. (diminish 'which-key-mode)
  576. )
  577. (with-eval-after-load 'whitespace
  578. (diminish 'global-whitespace-mode))
  579. (with-eval-after-load 'page-break-lines
  580. (diminish 'page-break-lines-mode))
  581. (with-eval-after-load 'auto-highlight-symbol
  582. (diminish 'auto-highlight-symbol-mode))
  583. (with-eval-after-load 'color-identifiers-mode
  584. (diminish 'color-identifiers-mode))
  585. (with-eval-after-load 'highlight-indentation
  586. (diminish 'highlight-indentation-mode))
  587. (with-eval-after-load 'back-button
  588. (diminish 'back-button-mode))
  589. (with-eval-after-load 'git-gutter
  590. (diminish 'git-gutter-mode))
  591. (with-eval-after-load 'autorevert
  592. (diminish 'auto-revert-mode))
  593. )
  594. (setq mode-line-front-space "")
  595. ;; (setq mode-line-end-spaces "")
  596. ;; Set current frame name to empty string
  597. (setq-default mode-line-format
  598. (let* ((l mode-line-format)
  599. (l (cl-substitute " " " "
  600. l
  601. :test 'equal))
  602. (l (cl-substitute " " " "
  603. l
  604. :test 'equal))
  605. )
  606. l))
  607. (set-frame-parameter nil 'name "")
  608. ;; See color-name-rgb-alist for available color names
  609. ;; http://www.raebear.net/computers/emacs-colors/
  610. ;; https://www.emacswiki.org/emacs/ListColors
  611. ;; (list-colors-display is not a complete list)
  612. (defconst my-mode-line-background-default
  613. (face-background 'mode-line)
  614. "Default color of mode-line at init.")
  615. (defun my-mode-line-color-update (&rest args)
  616. "ARGS are discarded"
  617. (let ((ro "skyblue")
  618. (rw my-mode-line-background-default))
  619. (if (or (not buffer-read-only)
  620. (and (eq major-mode 'wdired-mode)))
  621. (set-face-background 'mode-line
  622. rw)
  623. (set-face-background 'mode-line
  624. ro))))
  625. (add-hook 'switch-buffer-functions
  626. 'my-mode-line-color-update)
  627. (add-hook 'read-only-mode-hook
  628. 'my-mode-line-color-update)
  629. (add-hook 'wdired-mode-hook
  630. 'my-mode-line-color-update)
  631. (advice-add 'wdired-change-to-dired-mode
  632. :after
  633. 'my-mode-line-color-update)
  634. (advice-add 'wgrep-change-to-wgrep-mode
  635. :after
  636. 'my-mode-line-color-update)
  637. (advice-add 'wgrep-to-original-mode
  638. :after
  639. 'my-mode-line-color-update)
  640. (with-eval-after-load 'hardhat
  641. ;; hardhat-mode-hook does not work as expected...
  642. (advice-add 'hardhat-local-hook
  643. :after
  644. 'my-mode-line-color-update))
  645. (set-face-background 'header-line
  646. my-mode-line-background-default)
  647. ;; sky-color-clock
  648. ;; https://tsuu32.hatenablog.com/entry/2019/11/07/020005
  649. (declare-function sky-color-clock "sky-color-clock")
  650. (declare-function sky-color-clock-initialize "sky-color-clock")
  651. (defun sky-color-clock--form ()
  652. "Gen string for right aligned form."
  653. (let* ((sky-color-clock-str
  654. (propertize (sky-color-clock) 'help-echo (format-time-string "Sky color clock\n%F (%a)")))
  655. (mode-line-right-margin
  656. (propertize " " 'display `(space :align-to (- right-fringe ,(length sky-color-clock-str))))))
  657. (concat mode-line-right-margin sky-color-clock-str)))
  658. (when (require 'sky-color-clock nil t)
  659. (sky-color-clock-initialize 35) ; Tokyo, Japan
  660. (set-variable 'sky-color-clock-format "%H:%M")
  661. (set-variable 'sky-color-clock-enable-emoji-icon nil)
  662. (setq mode-line-end-spaces '(:eval (sky-color-clock--form))))
  663. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  664. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  665. ;; minibuffer
  666. (setq insert-default-directory t)
  667. (setq completion-ignore-case t
  668. read-file-name-completion-ignore-case t
  669. read-buffer-completion-ignore-case t)
  670. (setq resize-mini-windows t)
  671. (temp-buffer-resize-mode 1)
  672. (savehist-mode 1)
  673. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  674. (set-variable 'help-at-pt-display-when-idle t)
  675. (fset 'yes-or-no-p 'y-or-n-p)
  676. ;; complete symbol when `eval'
  677. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  678. (define-key minibuffer-local-map (kbd "C-u")
  679. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  680. ;; I dont know these bindings are good
  681. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  682. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  683. (with-eval-after-load 'minibuffer-line
  684. (set-face-underline 'minibuffer-line nil)
  685. )
  686. (when (fboundp 'minibuffer-line-mode)
  687. (set-variable 'minibuffer-line-refresh-interval
  688. 25)
  689. ;; Set idle timer
  690. (defvar my-minibuffer-line--idle-timer nil)
  691. (defvar minibuffer-line-mode)
  692. (add-hook 'minibuffer-line-mode-hook
  693. (lambda ()
  694. (when my-minibuffer-line--idle-timer
  695. (cancel-timer my-minibuffer-line--idle-timer)
  696. (setq my-minibuffer-line--idle-timer nil))
  697. (when minibuffer-line-mode
  698. (setq my-minibuffer-line--idle-timer
  699. (run-with-idle-timer 0.5
  700. t
  701. 'minibuffer-line--update)))))
  702. (set-variable 'minibuffer-line-format
  703. `(,(concat user-login-name
  704. "@"
  705. (car (split-string (system-name)
  706. "\\."))
  707. ":")
  708. (:eval (abbreviate-file-name (or buffer-file-name
  709. default-directory)))
  710. (:eval (and (fboundp 'git-ps1-mode-get-current)
  711. (git-ps1-mode-get-current " [GIT:%s]")))
  712. " "
  713. (:eval (format-time-string display-time-format))))
  714. (when (eval-and-compile (require 'nyan-mode nil t))
  715. (set-variable 'minibuffer-line-format
  716. '((:eval (progn
  717. (list (nyan-create))))))
  718. (defun my-nyan-set-length (&rest _)
  719. "Set `nyan-mode' length to window width."
  720. (set-variable 'nyan-bar-length
  721. (- (frame-parameter nil 'width) 4)))
  722. ;; (my-nyan-set-length)
  723. ;; (add-hook 'after-init-hook
  724. ;; 'my-nyan-set-length)
  725. (add-hook 'window-configuration-change-hook
  726. 'my-nyan-set-length)
  727. (add-hook 'switch-buffer-functions
  728. 'my-nyan-set-length)
  729. )
  730. (minibuffer-line-mode 1)
  731. )
  732. (when (fboundp 'prompt-text-mode)
  733. (set-variable 'prompt-text-format
  734. `(,(concat ""
  735. user-login-name
  736. "@"
  737. (car (split-string (system-name)
  738. "\\."))
  739. ":")
  740. (:eval (abbreviate-file-name (or buffer-file-name
  741. default-directory)))
  742. (:eval (and (fboundp 'git-ps1-mode-get-current)
  743. (git-ps1-mode-get-current " [GIT:%s]")))
  744. " "
  745. (:eval (format-time-string display-time-format))
  746. "\n"
  747. (:eval (symbol-name this-command))
  748. ": "))
  749. (prompt-text-mode 1))
  750. (with-eval-after-load 'helm
  751. (defvar helm-map)
  752. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  753. (setq-default header-line-format
  754. '(:eval (let ((f (or (buffer-file-name)
  755. default-directory)))
  756. (when f
  757. (abbreviate-file-name f)))))
  758. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  759. ;; letters, font-lock mode and fonts
  760. (when (fboundp 'color-identifiers-mode)
  761. (add-hook 'prog-mode-hook
  762. 'color-identifiers-mode))
  763. (setq text-quoting-style 'grave)
  764. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  765. ;; (set-window-margins (selected-window) 1 1)
  766. (unless window-system
  767. (setq frame-background-mode 'dark))
  768. (and (or (eq system-type 'Darwin)
  769. (eq system-type 'darwin))
  770. (fboundp 'mac-set-input-method-parameter)
  771. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  772. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  773. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  774. (boundp 'input-method-inactivate-hook))
  775. (add-hook 'input-method-activate-hook
  776. (lambda () (set-cursor-color "red")))
  777. (add-hook 'input-method-inactivate-hook
  778. (lambda () (set-cursor-color "black"))))
  779. (when (fboundp 'show-paren-mode)
  780. (add-hook 'after-first-visit-hook
  781. 'show-paren-mode))
  782. (set-variable 'show-paren-delay 0.5)
  783. (set-variable 'show-paren-style 'parenthesis) ; mixed is hard to read
  784. ;; (set-face-background 'show-paren-match
  785. ;; "black")
  786. ;; ;; (face-foreground 'default))
  787. ;; (set-face-foreground 'show-paren-match
  788. ;; "white")
  789. ;; (set-face-inverse-video-p 'show-paren-match
  790. ;; t)
  791. (transient-mark-mode 1)
  792. (global-font-lock-mode 1)
  793. (setq font-lock-global-modes
  794. '(not
  795. help-mode
  796. eshell-mode
  797. ;;term-mode
  798. Man-mode
  799. ))
  800. ;; (standard-display-ascii ?\n "$\n")
  801. ;; (defvar my-eol-face
  802. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  803. ;; )
  804. ;; (defvar my-tab-face
  805. ;; '(("\t" . '(0 highlight t nil))))
  806. (defvar my-jspace-face
  807. '(("\u3000" . '(0 highlight t nil))))
  808. (add-hook 'font-lock-mode-hook
  809. (lambda ()
  810. ;; (font-lock-add-keywords nil my-eol-face)
  811. (font-lock-add-keywords nil my-jspace-face)
  812. ))
  813. (set-variable 'font-lock-maximum-decoration
  814. '(
  815. ;; (python-mode . 2)
  816. (t . 2)
  817. ))
  818. (when (fboundp 'global-whitespace-mode)
  819. (add-hook 'after-first-visit-hook
  820. 'global-whitespace-mode))
  821. (add-hook 'dired-mode-hook
  822. ;; Other way to disable in dired buffers?
  823. (lambda () (set-variable 'whitespace-style nil t)))
  824. (with-eval-after-load 'whitespace
  825. (defvar whitespace-display-mappings)
  826. (defvar whitespace-mode)
  827. (add-to-list 'whitespace-display-mappings
  828. ;; We need t since last one takes precedence
  829. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  830. ;; (add-to-list 'whitespace-display-mappings
  831. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  832. (set-variable 'whitespace-style '(face
  833. trailing ; trailing blanks
  834. ;; tabs
  835. ;; spaces
  836. ;; lines
  837. lines-tail ; lines over 80
  838. newline ; newlines
  839. ;; empty ; empty lines at beg or end of buffer
  840. ;; big-indent
  841. ;; space-mark
  842. tab-mark
  843. newline-mark ; use display table for newline
  844. ))
  845. ;; (setq whitespace-newline 'font-lock-comment-face)
  846. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  847. (defun my-whitesspace-mode-reload ()
  848. "Reload whitespace-mode config."
  849. (interactive)
  850. (when whitespace-mode
  851. (whitespace-mode 0)
  852. (whitespace-mode 1)))
  853. (set-variable 'whitespace-line-column nil)
  854. (if (>= (display-color-cells)
  855. 256)
  856. (set-face-foreground 'whitespace-newline "color-109")
  857. ;; (progn
  858. ;; (set-face-bold-p 'whitespace-newline
  859. ;; t))
  860. ))
  861. (and nil
  862. '(require 'fill-column-indicator nil t)
  863. (setq fill-column-indicator))
  864. (defun my-gen-hl-line-color-dark ()
  865. "Generate color for current line in black background."
  866. (let* ((candidates (mapcar 'number-to-string (number-sequence 1 6)))
  867. (limit (length candidates))
  868. (r 0) (g 0) (b 0))
  869. (while (and (<= (abs (- r g)) 1)
  870. (<= (abs (- g b)) 1)
  871. (<= (abs (- b r)) 1))
  872. (setq r (random limit))
  873. (setq g (random limit))
  874. (setq b (random limit)))
  875. (format "#%s%s%s"
  876. (nth r candidates)
  877. (nth g candidates)
  878. (nth b candidates)
  879. )))
  880. ;; (my-gen-hl-line-color-dark)
  881. ;; highlight current line
  882. ;; http://wiki.riywo.com/index.php?Meadow
  883. (face-spec-set 'hl-line
  884. `((((min-colors 256)
  885. (background dark))
  886. ;; Rotate midnightblue
  887. (:background ,(my-gen-hl-line-color-dark)))
  888. (((min-colors 256)
  889. (background light))
  890. ;; TODO: What is should be?
  891. (:background "color-234"))
  892. (t
  893. (:underline "black"))))
  894. (set-variable 'hl-line-global-modes
  895. '(not
  896. term-mode))
  897. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  898. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  899. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  900. ;;(require 'set-modeline-color nil t)
  901. ;; (let ((fg (face-foreground 'default))
  902. ;; (bg (face-background 'default)))
  903. ;; (set-face-background 'mode-line-inactive
  904. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  905. ;; (set-face-foreground 'mode-line-inactive
  906. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  907. ;; (set-face-underline 'mode-line-inactive
  908. ;; t)
  909. ;; (set-face-underline 'vertical-border
  910. ;; nil)
  911. ;; (when (require 'end-mark nil t)
  912. ;; (global-end-mark-mode))
  913. ;; M-x highlight-* to highlight things
  914. (global-hi-lock-mode 1)
  915. (unless (fboundp 'highlight-region-text)
  916. (defun highlight-region-text (beg end)
  917. "Highlight text between BEG and END."
  918. (interactive "r")
  919. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  920. end)))
  921. (setq deactivate-mark t)))
  922. (when (fboundp 'auto-highlight-symbol-mode)
  923. (add-hook 'prog-mode-hook
  924. 'auto-highlight-symbol-mode))
  925. ;; Not work in combination with flyspell-mode
  926. ;; (when (fboundp 'global-auto-highlight-symbol-mode)
  927. ;; (add-hook 'after-first-visit-hook
  928. ;; 'global-auto-highlight-symbol-mode))
  929. (set-variable 'ahs-idle-interval 0.6)
  930. (when (fboundp 'highlight-indentation-mode)
  931. (dolist (hook
  932. '(
  933. prog-mode-hook
  934. text-mode-hook
  935. ))
  936. ;; Makes display slow?
  937. ;; (add-hook hook
  938. ;; 'highlight-indentation-mode)
  939. ))
  940. (with-eval-after-load 'highlight-indentation
  941. (set-face-background 'highlight-indentation-face "color-236"))
  942. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  943. (when (fboundp 'fic-mode)
  944. (add-hook 'prog-mode-hook
  945. 'fic-mode))
  946. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  947. ;; file handling
  948. (auto-insert-mode 1)
  949. ;; fzf
  950. ;; Too slow in term buffer!
  951. ;; (set-variable 'fzf/executable "sk")
  952. ;; (set-variable 'fzf/args "--color bw --print-query")
  953. ;; Modified from hardcoded default to include:
  954. ;; - directories
  955. ;; - hidden files
  956. ;; - root directory (.)
  957. ;; - parent directory (..)
  958. ;; ripgrep cannot list directories...
  959. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  960. (defvar my-fzf-default-command nil
  961. "My fzf FZF_DEFAULT_COMMAND.")
  962. (set-variable 'my-fzf-default-command
  963. (let ((find (or (executable-find "bfs") ;; Breadth-first find https://github.com/tavianator/bfs
  964. ;; Use gfind if available?
  965. "find"))
  966. (fd (or (executable-find "fdfind")
  967. (executable-find "fd"))))
  968. (if fd
  969. (concat "set -eu; set -o pipefail; "
  970. "echo .; "
  971. "echo ..; "
  972. "command " fd " "
  973. "--follow --hidden --no-ignore "
  974. "--color always "
  975. "2>/dev/null")
  976. (concat "set -eu; set -o pipefail; "
  977. "echo .; "
  978. "echo ..; "
  979. "command " find " -L . "
  980. "-mindepth 1 "
  981. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  982. "-o -print "
  983. "2> /dev/null "
  984. "| "
  985. "cut -b3-"))))
  986. (set-variable 'fzf/window-height 45)
  987. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  988. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  989. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  990. (declare-function fzf "fzf" t)
  991. (defun my-fzf-or-find-file ()
  992. "Call fzf if usable or call `find-file'."
  993. (declare (interactive-only t))
  994. (interactive)
  995. (if (and (executable-find "fzf")
  996. (fboundp 'fzf)
  997. (not (file-remote-p default-directory)))
  998. (let ((process-environment (cl-copy-list process-environment)))
  999. (setenv "FZF_DEFAULT_COMMAND" my-fzf-default-command)
  1000. (my-fzf))
  1001. (call-interactively 'find-file)))
  1002. (defvar fzf/position-bottom)
  1003. (declare-function fzf/start "fzf")
  1004. (declare-function term-reset-size "term")
  1005. (defun my-fzf ()
  1006. "Invoke `fzf' with my configurations."
  1007. (require 'fzf)
  1008. (let ((dir (or (ignore-errors
  1009. (require 'projectile)
  1010. (projectile-project-root))
  1011. default-directory)))
  1012. (set-variable 'fzf/window-height 12)
  1013. (set-variable 'fzf/args
  1014. (concat ;; "--print-query "
  1015. "--ansi "
  1016. "--color='bg+:-1' "
  1017. "--inline-info "
  1018. "--cycle "
  1019. ;; TODO: fix
  1020. "--reverse "
  1021. ;; "--print0 "
  1022. (format "--prompt='[%s]> ' "
  1023. dir)))
  1024. (fzf/start dir)
  1025. (let* ((buf (or (get-buffer "*fzf*")
  1026. (error "FZF buffer not found")))
  1027. (current-window (or (get-buffer-window buf)
  1028. (error "FZF window not found")))
  1029. (height (window-height current-window))
  1030. (root-window nil)
  1031. (new-window nil))
  1032. (with-current-buffer buf
  1033. (setq mode-line-format nil))
  1034. (jump-to-register :fzf-windows)
  1035. ;; (delete-window current-window)
  1036. (setq root-window (frame-root-window))
  1037. (setq new-window (split-window root-window
  1038. (- height)
  1039. 'below))
  1040. (select-window new-window)
  1041. (switch-to-buffer buf)
  1042. (term-reset-size (window-height)
  1043. (window-width))
  1044. )))
  1045. ;; (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  1046. (defun my-fzf-all-lines ()
  1047. "Fzf all lines."
  1048. (interactive)
  1049. (unless (executable-find "rg")
  1050. (error "rg not found"))
  1051. (let ((process-environment (cl-copy-list process-environment)))
  1052. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  1053. (fzf)))
  1054. ;; (define-key ctl-x-map "S" 'my-fzf-all-lines)
  1055. ;; fuzzy-finder
  1056. (set-variable 'fuzzy-finder-default-command
  1057. (concat "fzf "
  1058. "--ansi "
  1059. "--color='bg+:-1' "
  1060. "--inline-info "
  1061. "--cycle "
  1062. "--reverse "
  1063. "--prompt=\"[`pwd`]> \" "))
  1064. (set-variable 'fuzzy-finder-default-input-command
  1065. (let ((find (or (executable-find "bfs") ;; Breadth-first find https://github.com/tavianator/bfs
  1066. ;; Use gfind if available?
  1067. "find"))
  1068. (fd (or (executable-find "fdfind")
  1069. (executable-find "fd"))))
  1070. (if fd
  1071. (concat "set -eu; set -o pipefail; "
  1072. "echo .; "
  1073. "echo ..; "
  1074. "command " fd " "
  1075. "--follow --hidden --no-ignore "
  1076. "--color always "
  1077. "2>/dev/null")
  1078. (concat "set -eu; set -o pipefail; "
  1079. "echo .; "
  1080. "echo ..; "
  1081. "command " find " -L . "
  1082. "-mindepth 1 "
  1083. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  1084. "-o -print "
  1085. "2> /dev/null "
  1086. "| "
  1087. "cut -b3-"))))
  1088. (defun my-fuzzy-finder-or-find-file ()
  1089. "Call `fuzzy-finder' if usable or call `find-file'."
  1090. (declare (interactive-only t))
  1091. (interactive)
  1092. (if (and (executable-find "fzf")
  1093. (fboundp 'fuzzy-finder)
  1094. (not (file-remote-p default-directory)))
  1095. ;; TODO: Use projectile version
  1096. (fuzzy-finder)
  1097. (call-interactively 'find-file)))
  1098. (define-key ctl-x-map "f" 'my-fuzzy-finder-or-find-file)
  1099. (defun my-fuzzy-finder-ripgrep-lines ()
  1100. "Fzf all lines."
  1101. (interactive)
  1102. (unless (executable-find "rg")
  1103. (error "rg not found"))
  1104. (fuzzy-finder :input-command "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^"))
  1105. (define-key ctl-x-map "S" 'my-fuzzy-finder-ripgrep-lines)
  1106. ;; recently
  1107. ;; TODO: Enable after first visit file?
  1108. (with-eval-after-load 'recently
  1109. (defvar recently-excludes)
  1110. (add-to-list 'recently-excludes
  1111. (rx-to-string (list 'and
  1112. 'string-start
  1113. (expand-file-name package-user-dir))
  1114. t)))
  1115. (when (fboundp 'recently-mode)
  1116. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1117. (set-variable 'recently-max 1000)
  1118. (recently-mode 1))
  1119. (defvar my-cousel-recently-history nil "History of `my-counsel-recently'.")
  1120. (declare-function recently-list "recently" t)
  1121. (when (and (require 'recently nil t)
  1122. (fboundp 'ivy-read))
  1123. (defun my-counsel-recently ()
  1124. "Consel `recently'."
  1125. (interactive)
  1126. (ivy-read "Recently: " (mapcar 'abbreviate-file-name (recently-list))
  1127. :require-match t
  1128. :history 'my-cousel-recently-history
  1129. :preselect default-directory
  1130. :action (lambda (x) (find-file x))
  1131. :caller 'my-counsel-recently))
  1132. (define-key ctl-x-map (kbd "C-r") 'my-counsel-recently)
  1133. )
  1134. (when (fboundp 'editorconfig-mode)
  1135. (add-hook 'after-first-visit-hook
  1136. 'editorconfig-mode)
  1137. (add-hook 'after-first-visit-hook
  1138. 'editorconfig-mode-apply
  1139. t)) ;; Do after enabling editorconfig-mode
  1140. (set-variable 'editorconfig-get-properties-function
  1141. 'editorconfig-core-get-properties-hash)
  1142. (set-variable 'editorconfig-mode-lighter "")
  1143. (when (fboundp 'ws-butler-mode)
  1144. (set-variable 'editorconfig-trim-whitespaces-mode
  1145. 'ws-butler-mode))
  1146. (with-eval-after-load 'org-src
  1147. ;; [*.org\[\*Org Src*\[ c \]*\]]
  1148. (add-hook 'org-src-mode-hook
  1149. 'editorconfig-mode-apply t))
  1150. (when (fboundp 'editorconfig-custom-majormode)
  1151. (add-hook 'editorconfig-after-apply-functions
  1152. 'editorconfig-custom-majormode))
  1153. ;; Add readonly=true to set read-only-mode
  1154. (add-hook 'editorconfig-after-apply-functions
  1155. (lambda (props)
  1156. (let ((r (gethash 'readonly props)))
  1157. (when (and (string= r "true")
  1158. (not buffer-read-only))
  1159. (read-only-mode 1)))))
  1160. (add-hook 'editorconfig-hack-properties-functions
  1161. '(lambda (props)
  1162. (when (derived-mode-p 'makefile-mode)
  1163. (puthash 'indent_style "tab" props))
  1164. (when (derived-mode-p 'diff-mode)
  1165. (puthash 'trim_trailing_whitespace "false" props)
  1166. (puthash 'insert_final_newline "false" props)
  1167. )
  1168. ))
  1169. (when (fboundp 'editorconfig-auto-apply-enable)
  1170. (add-hook 'editorconfig-conf-mode-hook
  1171. 'editorconfig-auto-apply-enable))
  1172. ;; (when (fboundp 'editorconfig-charset-extras)
  1173. ;; (add-hook 'editorconfig-custom-hooks
  1174. ;; 'editorconfig-charset-extras))
  1175. (setq revert-without-query '(".+"))
  1176. ;; save cursor position
  1177. (when (fboundp 'save-place-mode)
  1178. (autoload 'save-place-find-file-hook "saveplace")
  1179. (add-hook 'after-first-visit-hook
  1180. 'save-place-mode)
  1181. (add-hook 'after-first-visit-hook
  1182. 'save-place-find-file-hook
  1183. t))
  1184. (set-variable 'save-place-file (concat user-emacs-directory
  1185. "places"))
  1186. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  1187. (setq make-backup-files t)
  1188. (setq vc-make-backup-files t)
  1189. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  1190. (setq backup-directory-alist
  1191. (cons (cons "." (expand-file-name (concat user-emacs-directory
  1192. "backup")))
  1193. backup-directory-alist))
  1194. (setq version-control 't)
  1195. (setq delete-old-versions t)
  1196. (setq kept-new-versions 20)
  1197. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  1198. "auto-save/")))
  1199. ;; (setq delete-auto-save-files t)
  1200. (setq auto-save-visited-interval 8)
  1201. (auto-save-visited-mode 1)
  1202. ;; (add-to-list 'auto-save-file-name-transforms
  1203. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  1204. ;; (setq auto-save-interval 3)
  1205. ;; (auto-save-mode 1)
  1206. (add-to-list 'completion-ignored-extensions ".bak")
  1207. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  1208. (setq delete-by-moving-to-trash t)
  1209. ;; trash-directory "~/.emacs.d/trash")
  1210. (add-hook 'after-save-hook
  1211. 'executable-make-buffer-file-executable-if-script-p)
  1212. (when (fboundp 'smart-revert-on)
  1213. (smart-revert-on))
  1214. ;; autosave
  1215. ;; auto-save-visited-mode can be used instead?
  1216. ;; (when (require 'autosave nil t)
  1217. ;; (autosave-set 8))
  1218. ;; bookmarks
  1219. (set-variable 'bookmark-default-file
  1220. (expand-file-name (concat user-emacs-directory
  1221. "bmk")))
  1222. (set-variable 'bookmark-sort-flag nil)
  1223. (defun my-bookmark-set ()
  1224. "My `bookmark-set'."
  1225. (interactive)
  1226. (cl-assert (or buffer-file-name
  1227. default-directory))
  1228. (let ((name (file-name-nondirectory (or buffer-file-name
  1229. (directory-file-name default-directory))))
  1230. (linenum (count-lines (point-min)
  1231. (point)))
  1232. (linetext (buffer-substring-no-properties (point-at-bol)
  1233. (point-at-eol))))
  1234. (bookmark-set (format "%s:%d:%s"
  1235. name linenum linetext)
  1236. nil)))
  1237. ;; Done by advice instead
  1238. ;; (set-variable 'bookmark-save-flag
  1239. ;; 1)
  1240. (with-eval-after-load 'recentf
  1241. (defvar recentf-exclude)
  1242. (defvar bookmark-default-file)
  1243. (add-to-list 'recentf-exclude
  1244. (regexp-quote bookmark-default-file)))
  1245. (defvar bookmark-default-file)
  1246. (defun my-bookmark-set--advice (orig-func &rest args)
  1247. "Function for `bookmark-set-internal'.
  1248. ORIG-FUNC is the target function, and ARGS is the argument when it is called."
  1249. (bookmark-load bookmark-default-file)
  1250. (apply orig-func args)
  1251. (bookmark-save))
  1252. (with-eval-after-load 'bookmark
  1253. (advice-add 'bookmark-set-internal
  1254. :around
  1255. 'my-bookmark-set--advice))
  1256. (define-key ctl-x-map "b" 'list-bookmarks)
  1257. (when (fboundp 'counsel-bookmark)
  1258. (define-key ctl-x-map "b" 'counsel-bookmark))
  1259. (define-key ctl-x-map "B" 'my-bookmark-set)
  1260. ;; vc
  1261. (set-variable 'vc-handled-backends '(RCS))
  1262. (set-variable 'vc-rcs-register-switches "-l")
  1263. (set-variable 'vc-rcs-checkin-switches "-l")
  1264. (set-variable 'vc-command-messages t)
  1265. (when (fboundp 'neotree)
  1266. (define-key ctl-x-map (kbd "C-n") 'neotree))
  1267. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1268. ;; share clipboard with x
  1269. ;; this page describes this in details, but only these sexps seem to be needed
  1270. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  1271. (and nil
  1272. (not window-system)
  1273. (not (eq window-system 'mac))
  1274. (getenv "DISPLAY")
  1275. (not (equal (getenv "DISPLAY") ""))
  1276. (executable-find "xclip")
  1277. ;; (< emacs-major-version 24)
  1278. '(require 'xclip nil t)
  1279. nil
  1280. (turn-on-xclip))
  1281. (declare-function turn-on-pasteboard "pasteboard")
  1282. (and (eq system-type 'darwin)
  1283. (require 'pasteboard nil t)
  1284. (turn-on-pasteboard))
  1285. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1286. ;; some modes and hooks
  1287. ;; Include some extra modes
  1288. (require 'generic-x)
  1289. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  1290. (defun my-scalafmt ()
  1291. (interactive)
  1292. (cl-assert buffer-file-name)
  1293. (cl-assert (not (buffer-modified-p)))
  1294. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  1295. (configoption (if configdir
  1296. (concat " --config "
  1297. (shell-quote-argument (expand-file-name configdir))
  1298. ".scalafmt.conf"
  1299. )
  1300. ""))
  1301. (str (concat "scalafmt -f "
  1302. (shell-quote-argument buffer-file-name)
  1303. configoption
  1304. " -i --exclude ensime")))
  1305. (message str)
  1306. (shell-command-to-string str))
  1307. (message "scalafmt done")
  1308. (revert-buffer nil t))
  1309. (when (fboundp 'web-mode)
  1310. (add-to-list 'auto-mode-alist
  1311. '("\\.html\\.j2\\'" . web-mode))
  1312. (add-to-list 'auto-mode-alist
  1313. ;; Django Template Language
  1314. '("\\.dtl\\'" . web-mode))
  1315. )
  1316. (when (locate-library "wgrep")
  1317. (set-variable 'wgrep-auto-save-buffer t)
  1318. (with-eval-after-load 'grep
  1319. (defvar grep-mode-map)
  1320. (define-key grep-mode-map
  1321. "e"
  1322. 'wgrep-change-to-wgrep-mode)))
  1323. (when (fboundp 'grep-context-mode)
  1324. (add-hook 'compilation-mode-hook #'grep-context-mode))
  1325. (with-eval-after-load 'remember
  1326. (defvar remember-mode-map)
  1327. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  1328. (set-variable 'remember-notes-initial-major-mode
  1329. 'change-log-mode)
  1330. (declare-function global-magit-file-mode "magit-files")
  1331. (with-eval-after-load 'magit-files
  1332. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  1333. ;; existing keybindings.
  1334. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  1335. ;; to disable thie mode here, but do anyway.
  1336. ;; See also https://github.com/magit/magit/issues/3517
  1337. (global-magit-file-mode -1))
  1338. (with-eval-after-load 'magit-section
  1339. (set-face-background 'magit-section-highlight
  1340. nil))
  1341. ;; Sane colors
  1342. (with-eval-after-load 'magit-diff
  1343. (set-face-background 'magit-diff-context nil)
  1344. (set-face-background 'magit-diff-context-highlight nil)
  1345. (set-face-foreground 'magit-diff-hunk-heading nil)
  1346. (set-face-background 'magit-diff-hunk-heading nil)
  1347. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  1348. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  1349. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  1350. (set-face-foreground 'magit-diff-added "green")
  1351. (set-face-background 'magit-diff-added nil)
  1352. (set-face-foreground 'magit-diff-added-highlight "green")
  1353. (set-face-background 'magit-diff-added-highlight nil)
  1354. (set-face-foreground 'magit-diff-removed "red")
  1355. (set-face-background 'magit-diff-removed nil)
  1356. (set-face-foreground 'magit-diff-removed-highlight "red")
  1357. (set-face-background 'magit-diff-removed-highlight nil)
  1358. (set-face-background 'magit-diff-lines-boundary "blue")
  1359. )
  1360. (declare-function magit-show-commit "magit")
  1361. (defun my-magit-messenger (file line)
  1362. "Magit messenger."
  1363. (interactive (list buffer-file-name
  1364. (line-number-at-pos)))
  1365. (cl-assert file)
  1366. (cl-assert line)
  1367. (let* ((blame-args '("-w"))
  1368. (id (with-temp-buffer
  1369. (let ((exit (apply 'call-process
  1370. "git" ;; PROGRAM
  1371. nil ;; INFILE
  1372. t ;; DESTINATION
  1373. nil ;; DISPLAY
  1374. "--no-pager" ;; ARGS
  1375. "blame"
  1376. "-L"
  1377. (format "%d,+1" line)
  1378. "--porcelain"
  1379. file
  1380. blame-args
  1381. )))
  1382. (goto-char (point-min))
  1383. (cl-assert (eq exit 0)
  1384. "Failed: %s" (buffer-substring (point)
  1385. (point-at-eol)))
  1386. (save-match-data
  1387. (re-search-forward (rx buffer-start
  1388. (one-or-more hex-digit)))
  1389. (match-string 0))))))
  1390. (magit-show-commit id)))
  1391. (when (boundp 'git-rebase-filename-regexp)
  1392. (add-to-list 'auto-mode-alist
  1393. `(,git-rebase-filename-regexp . text-mode)))
  1394. (when (fboundp 'ggtags-mode)
  1395. (add-hook 'c-mode-common-hook
  1396. 'ggtags-mode)
  1397. (add-hook 'python-mode-hook
  1398. 'ggtags-mode)
  1399. (add-hook 'js-mode-hook
  1400. 'ggtags-mode)
  1401. (add-hook 'scheme-mode-hook
  1402. 'ggtags-mode)
  1403. )
  1404. (when (fboundp 'imenu-list-minor-mode)
  1405. (defvar imenu-list-buffer-name)
  1406. (defun my-imenu-list-toggle ()
  1407. "My 'imenu-list` toggle."
  1408. (interactive)
  1409. (require 'imenu-list)
  1410. (if (eq (window-buffer)
  1411. (get-buffer imenu-list-buffer-name))
  1412. (imenu-list-minor-mode -1)
  1413. (imenu-list-minor-mode 1)))
  1414. ;; (set-variable 'imenu-list-auto-resize t)
  1415. (set-variable 'imenu-list-focus-after-activation t)
  1416. ;; (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle)
  1417. (define-key ctl-x-map (kbd "C-l") 'imenu-list-smart-toggle)
  1418. )
  1419. (add-hook 'emacs-lisp-mode-hook
  1420. (lambda ()
  1421. (setq imenu-generic-expression
  1422. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1423. ,@imenu-generic-expression))))
  1424. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1425. (with-eval-after-load 'compile
  1426. (defvar compilation-filter-start)
  1427. (defvar compilation-error-regexp-alist)
  1428. (eval-and-compile (require 'ansi-color))
  1429. (add-hook 'compilation-filter-hook
  1430. (lambda ()
  1431. (let ((inhibit-read-only t))
  1432. (ansi-color-apply-on-region compilation-filter-start
  1433. (point)))))
  1434. (add-to-list 'compilation-error-regexp-alist
  1435. ;; ansible-lint
  1436. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1437. (add-to-list 'compilation-error-regexp-alist
  1438. ;; pydocstyle
  1439. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1440. )
  1441. (declare-function company-cancel "company")
  1442. (when (fboundp 'global-company-mode)
  1443. (add-hook 'after-first-visit-hook
  1444. 'global-company-mode))
  1445. (declare-function company-manual-begin "company")
  1446. (with-eval-after-load 'company
  1447. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1448. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1449. (set-variable 'company-idle-delay nil)
  1450. (set-variable 'company-minimum-prefix-length 2)
  1451. (set-variable 'company-selection-wrap-around t)
  1452. (defvar company-mode-map)
  1453. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1454. ;; (with-eval-after-load 'python
  1455. ;; (defvar python-indent-trigger-commands)
  1456. ;; ;; TODO: This disables completion in puthon?
  1457. ;; (add-to-list 'python-indent-trigger-commands
  1458. ;; 'company-indent-or-complete-common))
  1459. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1460. (defvar company-active-map)
  1461. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1462. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1463. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1464. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1465. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1466. (defvar company-mode)
  1467. (defvar company-candidates)
  1468. (defvar company-candidates-length)
  1469. ;; (popup-tip "Hello, World!")
  1470. (defun my-company-lighter-current-length ()
  1471. "Get current candidate length."
  1472. (interactive)
  1473. (let ((l nil)
  1474. (inhibit-message t))
  1475. (when (and company-mode
  1476. (not (minibufferp))
  1477. ;; Do nothing when already in company completion
  1478. (not company-candidates))
  1479. ;; FIXME: Somehow it cannto catch errors from ggtags
  1480. (ignore-errors
  1481. ;; (company-auto-begin)
  1482. (company-manual-begin)
  1483. (setq l company-candidates-length)
  1484. (company-cancel)))
  1485. (if l
  1486. (format "[%d]" l)
  1487. "")))
  1488. (defvar company-lighter)
  1489. (set-variable 'company-lighter-base "Cmp")
  1490. ;; (add-to-list 'company-lighter
  1491. ;; '(:eval (my-company-lighter-current-length))
  1492. ;; t)
  1493. ;; This breaks japanese text input
  1494. ;; (set-variable 'my-company-length-popup-tip-timer
  1495. ;; (run-with-idle-timer 0.2 t
  1496. ;; 'my-company-length-popup-tip))
  1497. ;; (current-active-maps)
  1498. ;; (lookup-key)
  1499. '(mapcar (lambda (map)
  1500. (lookup-key map (kbd "C-i")))
  1501. (current-active-maps))
  1502. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1503. (set-face-attribute 'company-tooltip nil
  1504. :foreground "black" :background "lightgrey")
  1505. (set-face-attribute 'company-tooltip-common nil
  1506. :foreground "black" :background "lightgrey")
  1507. (set-face-attribute 'company-tooltip-common-selection nil
  1508. :foreground "white" :background "steelblue")
  1509. (set-face-attribute 'company-tooltip-selection nil
  1510. :foreground "black" :background "steelblue")
  1511. (set-face-attribute 'company-preview-common nil
  1512. :background nil :foreground "lightgrey" :underline t)
  1513. (set-face-attribute 'company-scrollbar-fg nil
  1514. :background "orange")
  1515. (set-face-attribute 'company-scrollbar-bg nil
  1516. :background "gray40")
  1517. )
  1518. ;; https://github.com/lunaryorn/flycheck
  1519. ;; TODO: Any way to disable auto check?
  1520. ;; Update flycheck-hooks-alist?
  1521. (when (fboundp 'global-flycheck-mode)
  1522. (add-hook 'after-first-visit-hook
  1523. 'global-flycheck-mode))
  1524. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1525. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1526. ;; (with-eval-after-load 'flycheck
  1527. ;; (when (fboundp 'flycheck-black-check-setup)
  1528. ;; (flycheck-black-check-setup)))
  1529. (when (fboundp 'ilookup-open-word)
  1530. (define-key ctl-x-map "d" 'ilookup-open-word))
  1531. (set-variable 'ac-ignore-case nil)
  1532. (when (fboundp 'term-run-shell-command)
  1533. (define-key ctl-x-map "t" 'term-run-shell-command))
  1534. (add-to-list 'safe-local-variable-values
  1535. '(encoding utf-8))
  1536. (setq enable-local-variables :safe)
  1537. ;; Detect file type from shebang and set major-mode.
  1538. (add-to-list 'interpreter-mode-alist
  1539. '("python3" . python-mode))
  1540. (add-to-list 'interpreter-mode-alist
  1541. '("python2" . python-mode))
  1542. (with-eval-after-load 'python
  1543. (defvar python-mode-map (make-sparse-keymap))
  1544. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1545. ;; I want to use this, but this breaks normal self-insert-command
  1546. ;; (set-variable 'py-indent-list-style
  1547. ;; 'one-level-to-beginning-of-statement)
  1548. (set-variable 'pydoc-command
  1549. "python3 -m pydoc")
  1550. (declare-function with-venv-advice-add "with-venv")
  1551. (with-eval-after-load 'pydoc
  1552. (when (require 'with-venv nil t)
  1553. (with-venv-advice-add 'pydoc)))
  1554. (set-variable 'flycheck-python-mypy-config '("mypy.ini" ".mypy.ini" "setup.cfg"))
  1555. (set-variable 'flycheck-flake8rc '("setup.cfg" "tox.ini" ".flake8rc"))
  1556. (set-variable 'flycheck-python-pylint-executable "python3")
  1557. (set-variable 'flycheck-python-pycompile-executable "python3")
  1558. (set-variable 'python-indent-guess-indent-offset nil)
  1559. (with-eval-after-load 'blacken
  1560. (when (require 'with-venv nil t)
  1561. (with-venv-advice-add 'blacken-buffer)))
  1562. (with-eval-after-load 'ansible-doc
  1563. (when (require 'with-venv nil t)
  1564. (with-venv-advice-add 'ansible-doc)))
  1565. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1566. (defun my-isortify ()
  1567. (interactive)
  1568. (cl-assert buffer-file-name)
  1569. (cl-assert (not (buffer-modified-p)))
  1570. (call-process "python" ;; PROGRAM
  1571. nil ;; INFILE
  1572. nil ;; DESTINATION
  1573. nil ;; DISPLAY
  1574. "-m" "isort" buffer-file-name)
  1575. (message "isortify done")
  1576. (revert-buffer nil t))
  1577. (when (fboundp 'with-venv-advice-add)
  1578. ;; TODO: Lazy load with-venv
  1579. (with-venv-advice-add 'my-isortify))
  1580. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1581. (defun my-set-venv-flycheck-executable-find ()
  1582. "Set flycheck executabie find."
  1583. (interactive)
  1584. (when (fboundp 'with-venv)
  1585. (set-variable 'flycheck-executable-find
  1586. '(lambda (e)
  1587. (with-venv
  1588. (executable-find e)))
  1589. t)))
  1590. (defun my-update-flycheck-flake8-error-level-alist ()
  1591. "Update `flycheck-flake8-error-level-alist'."
  1592. (defvar flycheck-flake8-error-level-alist)
  1593. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1594. ;; '("^D.*$" . warning))
  1595. (set-variable 'flycheck-flake8-error-level-alist
  1596. nil)
  1597. )
  1598. (add-hook 'python-mode-hook
  1599. 'my-set-venv-flycheck-executable-find)
  1600. (add-hook 'python-mode-hook
  1601. 'my-update-flycheck-flake8-error-level-alist)
  1602. (when (fboundp 'with-venv-info-mode)
  1603. (add-hook 'python-mode-hook
  1604. 'with-venv-info-mode))
  1605. ;; Run multiple chekcers
  1606. ;; https://github.com/flycheck/flycheck/issues/186
  1607. (add-hook 'python-mode-hook
  1608. (lambda ()
  1609. ;; Currently on python-mode eldoc-mode sometimes print
  1610. ;; wired message on "from" keyword:
  1611. ;;var from = require("./from")
  1612. (eldoc-mode -1)))
  1613. ;; http://fukuyama.co/foreign-regexp
  1614. '(and (require 'foreign-regexp nil t)
  1615. (progn
  1616. (setq foreign-regexp/regexp-type 'perl)
  1617. '(setq reb-re-syntax 'foreign-regexp)
  1618. ))
  1619. (with-eval-after-load 'sql
  1620. (require 'sql-indent nil t))
  1621. (add-to-list 'auto-mode-alist
  1622. '("\\.hql\\'" . sql-mode))
  1623. (when (fboundp 'git-command)
  1624. (define-key ctl-x-map "g" 'git-command))
  1625. (when (fboundp 'gited-list)
  1626. (defalias 'gited 'gited-list))
  1627. (when (fboundp 'global-git-commit-mode)
  1628. (add-hook 'after-first-visit-hook
  1629. 'global-git-commit-mode)
  1630. (add-hook 'after-first-visit-hook
  1631. 'git-commit-setup-check-buffer))
  1632. (with-eval-after-load 'git-commit
  1633. (add-hook 'git-commit-setup-hook
  1634. 'turn-off-auto-fill t))
  1635. (with-eval-after-load 'rst
  1636. (defvar rst-mode-map)
  1637. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1638. (with-eval-after-load 'jdee
  1639. (add-hook 'jdee-mode-hook
  1640. (lambda ()
  1641. (make-local-variable 'global-mode-string)
  1642. (add-to-list 'global-mode-string
  1643. mode-line-position))))
  1644. ;; Cannot enable error thrown. Why???
  1645. ;; https://github.com/m0smith/malabar-mode#Installation
  1646. ;; (when (require 'malabar-mode nil t)
  1647. ;; (add-to-list 'load-path
  1648. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1649. ;; (require 'cedet-devel-load nil t)
  1650. ;; (eval-after-init (activate-malabar-mode)))
  1651. (with-eval-after-load 'make-mode
  1652. (defvar makefile-mode-map (make-sparse-keymap))
  1653. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1654. ;; this functions is set in write-file-functions, i cannot find any
  1655. ;; good way to remove this.
  1656. (fset 'makefile-warn-suspicious-lines 'ignore))
  1657. (with-eval-after-load 'verilog-mode
  1658. (defvar verilog-mode-map (make-sparse-keymap))
  1659. (define-key verilog-mode-map ";" 'self-insert-command))
  1660. (setq diff-switches "-u")
  1661. (autoload 'diff-goto-source "diff-mode" nil t)
  1662. (with-eval-after-load 'diff-mode
  1663. ;; (when (and (eq major-mode
  1664. ;; 'diff-mode)
  1665. ;; (not buffer-file-name))
  1666. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1667. ;; (view-mode 1))
  1668. (set-face-attribute 'diff-header nil
  1669. :foreground nil
  1670. :background nil
  1671. :weight 'bold)
  1672. (set-face-attribute 'diff-file-header nil
  1673. :foreground nil
  1674. :background nil
  1675. :weight 'bold)
  1676. (set-face-foreground 'diff-index "blue")
  1677. (set-face-attribute 'diff-hunk-header nil
  1678. :foreground "cyan"
  1679. :weight 'normal)
  1680. (set-face-attribute 'diff-context nil
  1681. ;; :foreground "white"
  1682. :foreground nil
  1683. :weight 'normal)
  1684. (set-face-foreground 'diff-removed "red")
  1685. (set-face-foreground 'diff-added "green")
  1686. (set-face-background 'diff-removed nil)
  1687. (set-face-background 'diff-added nil)
  1688. (set-face-attribute 'diff-changed nil
  1689. :foreground "magenta"
  1690. :weight 'normal)
  1691. (set-face-attribute 'diff-refine-changed nil
  1692. :foreground nil
  1693. :background nil
  1694. :weight 'bold
  1695. :inverse-video t)
  1696. ;; Annoying !
  1697. ;;(diff-auto-refine-mode)
  1698. )
  1699. ;; (ffap-bindings)
  1700. (with-eval-after-load 'browse-url
  1701. (set-variable 'browse-url-browser-function
  1702. 'eww-browse-url))
  1703. (set-variable 'sh-here-document-word "__EOC__")
  1704. (with-eval-after-load 'adoc-mode
  1705. (defvar adoc-mode-map)
  1706. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1707. (when (fboundp 'adoc-mode)
  1708. (setq auto-mode-alist
  1709. `(("\\.adoc\\'" . adoc-mode)
  1710. ("\\.asciidoc\\'" . adoc-mode)
  1711. ,@auto-mode-alist)))
  1712. (with-eval-after-load 'markup-faces
  1713. ;; Is this too match ?
  1714. (set-face-foreground 'markup-meta-face
  1715. "color-245")
  1716. (set-face-foreground 'markup-meta-hide-face
  1717. "color-245")
  1718. )
  1719. ;; TODO: check if this is required
  1720. (with-eval-after-load 'groovy-mode
  1721. (defvar groovy-mode-map)
  1722. (define-key groovy-mode-map "(" 'self-insert-command)
  1723. (define-key groovy-mode-map ")" 'self-insert-command)
  1724. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1725. )
  1726. (when (fboundp 'groovy-mode)
  1727. (add-to-list 'auto-mode-alist
  1728. '("build\\.gradle\\'" . groovy-mode)))
  1729. (add-to-list 'auto-mode-alist
  1730. '("\\.gawk\\'" . awk-mode))
  1731. (with-eval-after-load 'yaml-mode
  1732. (defvar yaml-mode-map (make-sparse-keymap))
  1733. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1734. (with-eval-after-load 'html-mode
  1735. (defvar html-mode-map (make-sparse-keymap))
  1736. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1737. (with-eval-after-load 'text-mode
  1738. (define-key text-mode-map (kbd "C-m") 'newline))
  1739. (with-eval-after-load 'info
  1740. (defvar Info-additional-directory-list)
  1741. (dolist (dir (directory-files (concat user-emacs-directory
  1742. "info")
  1743. t
  1744. "^[^.].*"))
  1745. (when (file-directory-p dir)
  1746. (add-to-list 'Info-additional-directory-list
  1747. dir)))
  1748. (let ((dir (expand-file-name "~/.brew/share/info")))
  1749. (when (file-directory-p dir)
  1750. (add-to-list 'Info-additional-directory-list
  1751. dir))))
  1752. (with-eval-after-load 'apropos
  1753. (defvar apropos-mode-map (make-sparse-keymap))
  1754. (define-key apropos-mode-map "n" 'next-line)
  1755. (define-key apropos-mode-map "p" 'previous-line))
  1756. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1757. ;; (define-key isearch-mode-map
  1758. ;; (kbd "C-j") 'isearch-other-control-char)
  1759. ;; (define-key isearch-mode-map
  1760. ;; (kbd "C-k") 'isearch-other-control-char)
  1761. ;; (define-key isearch-mode-map
  1762. ;; (kbd "C-h") 'isearch-other-control-char)
  1763. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1764. (define-key isearch-mode-map (kbd "M-r")
  1765. 'isearch-query-replace-regexp)
  1766. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1767. (setq lazy-highlight-cleanup nil)
  1768. ;; face for isearch highlighing
  1769. (set-face-attribute 'lazy-highlight
  1770. nil
  1771. :foreground `unspecified
  1772. :background `unspecified
  1773. :underline t
  1774. ;; :weight `bold
  1775. )
  1776. (add-hook 'outline-mode-hook
  1777. (lambda ()
  1778. (when (string-match "\\.md\\'" buffer-file-name)
  1779. (setq-local outline-regexp "#+ "))))
  1780. (add-hook 'outline-mode-hook
  1781. 'outline-show-all)
  1782. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1783. (with-eval-after-load 'markdown-mode
  1784. (defvar gfm-mode-map)
  1785. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1786. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1787. )
  1788. (when (fboundp 'gfm-mode)
  1789. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1790. (add-hook 'markdown-mode-hook
  1791. 'outline-minor-mode)
  1792. (add-hook 'markdown-mode-hook
  1793. (lambda ()
  1794. (setq-local comment-start ";")))
  1795. )
  1796. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1797. ;; M-$ to ispell word
  1798. ;; M-x flyspell-buffer to highlight all suspicious words
  1799. (when (executable-find "aspell")
  1800. (set-variable 'ispell-program-name "aspell")
  1801. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1802. (with-eval-after-load 'ispell
  1803. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1804. (when (fboundp 'flyspell-mode)
  1805. (add-hook 'text-mode-hook
  1806. 'flyspell-mode))
  1807. (when (fboundp 'flyspell-prog-mode)
  1808. (add-hook 'prog-mode-hook
  1809. 'flyspell-prog-mode))
  1810. ;; c-mode
  1811. ;; http://www.emacswiki.org/emacs/IndentingC
  1812. ;; http://en.wikipedia.org/wiki/Indent_style
  1813. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1814. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1815. (with-eval-after-load 'cc-vars
  1816. (defvar c-default-style nil)
  1817. (add-to-list 'c-default-style
  1818. '(c-mode . "k&r"))
  1819. (add-to-list 'c-default-style
  1820. '(c++-mode . "k&r")))
  1821. (with-eval-after-load 'js2-mode
  1822. ;; currently do not use js2-mode
  1823. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1824. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1825. ;; (defvar js2-mode-map (make-sparse-keymap))
  1826. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1827. ;; (interactive)
  1828. ;; (js2-enter-key)
  1829. ;; (indent-for-tab-command)))
  1830. ;; (add-hook (kill-local-variable 'before-save-hook)
  1831. ;; 'js2-before-save)
  1832. ;; (add-hook 'before-save-hook
  1833. ;; 'my-indent-buffer
  1834. ;; nil
  1835. ;; t)
  1836. )
  1837. (add-to-list 'interpreter-mode-alist
  1838. '("node" . js-mode))
  1839. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1840. (with-eval-after-load 'uniquify
  1841. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1842. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1843. (setq uniquify-min-dir-content 1))
  1844. (with-eval-after-load 'view
  1845. (defvar view-mode-map (make-sparse-keymap))
  1846. (define-key view-mode-map "j" 'scroll-up-line)
  1847. (define-key view-mode-map "k" 'scroll-down-line)
  1848. (define-key view-mode-map "v" 'toggle-read-only)
  1849. (define-key view-mode-map "q" 'bury-buffer)
  1850. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1851. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1852. ;; (define-key view-mode-map
  1853. ;; "n" 'nonincremental-repeat-search-forward)
  1854. ;; (define-key view-mode-map
  1855. ;; "N" 'nonincremental-repeat-search-backward)
  1856. ;; N conflicts with git-walktree
  1857. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1858. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1859. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1860. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1861. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1862. (global-set-key "\M-r" 'view-mode)
  1863. ;; (setq view-read-only t)
  1864. (with-eval-after-load 'term
  1865. (defvar term-raw-map (make-sparse-keymap))
  1866. (define-key term-raw-map (kbd "C-x")
  1867. (lookup-key (current-global-map)
  1868. (kbd "C-x"))))
  1869. (add-hook 'term-mode-hook
  1870. (lambda ()
  1871. ;; Stop current line highlighting
  1872. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1873. (set-variable 'scroll-margin 0 t)
  1874. ))
  1875. (set-variable 'term-buffer-maximum-size 20480)
  1876. (set-variable 'term-suppress-hard-newline t)
  1877. (add-hook 'Man-mode-hook
  1878. (lambda ()
  1879. (view-mode 1)
  1880. (setq truncate-lines nil)))
  1881. (set-variable 'Man-notify-method (if window-system
  1882. 'newframe
  1883. 'aggressive))
  1884. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1885. "woman_cache.el")))
  1886. ;; not work because man.el will be loaded when man called
  1887. (defalias 'man 'woman)
  1888. (add-to-list 'auto-mode-alist
  1889. '("/tox\\.ini\\'" . conf-unix-mode))
  1890. (add-to-list 'auto-mode-alist
  1891. '("/setup\\.cfg\\'" . conf-unix-mode))
  1892. (when (fboundp 'toml-mode)
  1893. (add-to-list 'auto-mode-alist
  1894. '("/tox\\.ini\\'" . toml-mode))
  1895. (add-to-list 'auto-mode-alist
  1896. '("/setup\\.cfg\\'" . toml-mode))
  1897. (add-to-list 'auto-mode-alist
  1898. '("/Pipfile\\'" . toml-mode))
  1899. (add-to-list 'auto-mode-alist
  1900. '("/poetry\\.lock\\'" . toml-mode))
  1901. )
  1902. (when (fboundp 'json-mode)
  1903. (add-to-list 'auto-mode-alist
  1904. '("/Pipfile\\.lock\\'" . json-mode)))
  1905. (add-hook 'go-mode-hook
  1906. (lambda()
  1907. (defvar go-mode-map)
  1908. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1909. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1910. (when (fboundp 'k8s-mode)
  1911. (add-to-list 'auto-mode-alist
  1912. '("\\.k8s\\'" . k8s-mode)))
  1913. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1914. ;; buffers
  1915. (defvar bs-configurations)
  1916. (declare-function bs-set-configuration "bs")
  1917. (declare-function bs-refresh "bs")
  1918. (declare-function bs-message-without-log "bs")
  1919. (declare-function bs--current-config-message "bs")
  1920. (with-eval-after-load 'bs
  1921. (add-to-list 'bs-configurations
  1922. '("specials" "^\\*" nil ".*" nil nil))
  1923. (add-to-list 'bs-configurations
  1924. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1925. (defvar bs-mode-map)
  1926. (defvar bs-current-configuration)
  1927. (define-key bs-mode-map (kbd "t")
  1928. ;; TODO: fix toggle feature
  1929. (lambda ()
  1930. (interactive)
  1931. (if (string= "specials"
  1932. bs-current-configuration)
  1933. (bs-set-configuration "files")
  1934. (bs-set-configuration "specials"))
  1935. (bs-refresh)
  1936. (bs-message-without-log "%s"
  1937. (bs--current-config-message))))
  1938. ;; (setq bs-configurations (list
  1939. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1940. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1941. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1942. )
  1943. (when (fboundp 'bs-show)
  1944. (defalias 'list-buffers 'bs-show)
  1945. (set-variable 'bs-default-configuration "files-and-specials")
  1946. (set-variable 'bs-default-sort-name "by nothing")
  1947. (add-hook 'bs-mode-hook
  1948. (lambda ()
  1949. (setq-local scroll-margin 0)
  1950. (setq-local header-line-format nil)
  1951. (setq-local mode-line-format nil)
  1952. )))
  1953. ;;(iswitchb-mode 1)
  1954. (icomplete-mode)
  1955. (defun iswitchb-buffer-display-other-window ()
  1956. "Do iswitchb in other window."
  1957. (interactive)
  1958. (let ((iswitchb-default-method 'display))
  1959. (call-interactively 'iswitchb-buffer)))
  1960. ;; buffer killing
  1961. ;; (defun my-delete-window-killing-buffer () nil)
  1962. (defun my-query-kill-current-buffer ()
  1963. "Interactively kill current buffer."
  1964. (interactive)
  1965. (if (y-or-n-p (concat "kill current buffer? :"))
  1966. (kill-buffer (current-buffer))))
  1967. (defun my-force-query-kill-current-buffer ()
  1968. "Interactively kill current buffer."
  1969. (interactive)
  1970. (when (y-or-n-p (concat "kill current buffer? :"))
  1971. (let ((kill-buffer-hook nil)
  1972. (kill-buffer-query-functions nil))
  1973. (kill-buffer (current-buffer)))))
  1974. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1975. ;; Originally C-x C-k -> kmacro-keymap
  1976. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  1977. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  1978. (substitute-key-definition 'kill-buffer
  1979. 'my-query-kill-current-buffer
  1980. global-map)
  1981. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1982. ;; dired
  1983. (defun my-file-head (filename &optional n)
  1984. "Return list of first N lines of file FILENAME."
  1985. ;; TODO: Fix for janapese text
  1986. ;; TODO: Fix for short text
  1987. (let ((num (or n 10))
  1988. (size 100)
  1989. (beg 0)
  1990. (end 0)
  1991. (result '())
  1992. (read -1))
  1993. (with-temp-buffer
  1994. (erase-buffer)
  1995. (while (or (<= (count-lines (point-min)
  1996. (point-max))
  1997. num)
  1998. (eq read 0))
  1999. (setq end (+ beg size))
  2000. (setq read (nth 1 (insert-file-contents-literally filename
  2001. nil
  2002. beg
  2003. end)))
  2004. (goto-char (point-max))
  2005. (setq beg (+ beg size)))
  2006. (goto-char (point-min))
  2007. (while (< (length result) num)
  2008. (let ((start (point)))
  2009. (forward-line 1)
  2010. (setq result
  2011. `(,@result ,(buffer-substring-no-properties start
  2012. (point))))))
  2013. result
  2014. ;; (buffer-substring-no-properties (point-min)
  2015. ;; (progn
  2016. ;; (forward-line num)
  2017. ;; (point)))
  2018. )))
  2019. ;; (apply 'concat (my-file-head "./shrc" 10)
  2020. (declare-function dired-get-filename "dired" t)
  2021. (defun my-dired-echo-file-head (arg)
  2022. "Echo head of current file.
  2023. ARG is num to show, or defaults to 7."
  2024. (interactive "P")
  2025. (let ((f (dired-get-filename)))
  2026. (message "%s"
  2027. (apply 'concat
  2028. (my-file-head f
  2029. 7)))))
  2030. (declare-function dired-get-marked-files "dired")
  2031. (defun my-dired-diff ()
  2032. "Show diff of marked file and file of current line."
  2033. (interactive)
  2034. (let ((files (dired-get-marked-files nil nil nil t)))
  2035. (if (eq (car files)
  2036. t)
  2037. (diff (cadr files) (dired-get-filename))
  2038. (message "One file must be marked!"))))
  2039. (defun dired-get-file-info ()
  2040. "Print information of current line file."
  2041. (interactive)
  2042. (let* ((file (dired-get-filename t))
  2043. (quoted (shell-quote-argument file)))
  2044. (if (file-directory-p file)
  2045. (progn
  2046. (message "Calculating disk usage...")
  2047. (let ((du (or (executable-find "gdu")
  2048. (executable-find "du")
  2049. (error "du not found"))))
  2050. (shell-command (concat du
  2051. " -hsD "
  2052. quoted))))
  2053. (shell-command (concat "file "
  2054. quoted)))))
  2055. (defun my-dired-scroll-up ()
  2056. "Scroll up."
  2057. (interactive)
  2058. (my-dired-previous-line (- (window-height) 1)))
  2059. (defun my-dired-scroll-down ()
  2060. "Scroll down."
  2061. (interactive)
  2062. (my-dired-next-line (- (window-height) 1)))
  2063. ;; (defun my-dired-forward-line (arg)
  2064. ;; ""
  2065. ;; (interactive "p"))
  2066. (declare-function dired-get-subdir "dired")
  2067. (declare-function dired-move-to-filename "dired")
  2068. (defun my-dired-previous-line (arg)
  2069. "Move ARG lines up."
  2070. (interactive "p")
  2071. (if (> arg 0)
  2072. (progn
  2073. (if (eq (line-number-at-pos)
  2074. 1)
  2075. (goto-char (point-max))
  2076. (forward-line -1))
  2077. (my-dired-previous-line (if (or (dired-get-filename nil t)
  2078. (dired-get-subdir))
  2079. (- arg 1)
  2080. arg)))
  2081. (dired-move-to-filename)))
  2082. (defun my-dired-next-line (arg)
  2083. "Move ARG lines down."
  2084. (interactive "p")
  2085. (if (> arg 0)
  2086. (progn
  2087. (if (eq (point)
  2088. (point-max))
  2089. (goto-char (point-min))
  2090. (forward-line 1))
  2091. (my-dired-next-line (if (or (dired-get-filename nil t)
  2092. (dired-get-subdir))
  2093. (- arg 1)
  2094. arg)))
  2095. (dired-move-to-filename)))
  2096. (defun my-tramp-remote-find-file (f)
  2097. "Open F."
  2098. (interactive (list (read-file-name "My Find File Tramp: "
  2099. "/scp:"
  2100. nil ;; "/scp:"
  2101. (confirm-nonexistent-file-or-buffer))))
  2102. (find-file f))
  2103. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  2104. (if (eq window-system 'mac)
  2105. (setq dired-listing-switches "-lhF")
  2106. (setq dired-listing-switches "-lhF --time-style=long-iso")
  2107. )
  2108. (setq dired-listing-switches "-lhF")
  2109. ;; when using dired-find-alternate-file
  2110. ;; reuse current dired buffer for the file to open
  2111. ;; (put 'dired-find-alternate-file 'disabled nil)
  2112. (set-variable 'dired-ls-F-marks-symlinks t)
  2113. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  2114. (set-variable 'ls-lisp-dirs-first t)
  2115. (set-variable 'ls-lisp-use-localized-time-format t)
  2116. (set-variable 'ls-lisp-format-time-list
  2117. '("%Y-%m-%d %H:%M"
  2118. "%Y-%m-%d "))
  2119. (set-variable 'dired-dwim-target t)
  2120. (set-variable 'dired-isearch-filenames t)
  2121. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  2122. (set-variable 'dired-hide-details-hide-information-lines nil)
  2123. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  2124. (set-variable 'dired-recursive-deletes 'always)
  2125. ;; (add-hook 'dired-after-readin-hook
  2126. ;; 'my-replace-nasi-none)
  2127. (with-eval-after-load 'dired
  2128. (require 'ls-lisp nil t)
  2129. (defvar dired-mode-map (make-sparse-keymap))
  2130. ;; dired-do-chgrp sometimes cause system hung
  2131. (define-key dired-mode-map "G" 'ignore)
  2132. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  2133. (define-key dired-mode-map "i" 'dired-get-file-info)
  2134. ;; (define-key dired-mode-map "f" 'find-file)
  2135. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  2136. (define-key dired-mode-map "z" 'fzf)
  2137. (define-key dired-mode-map "!" 'shell-command)
  2138. (define-key dired-mode-map "&" 'async-shell-command)
  2139. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  2140. (define-key dired-mode-map "=" 'my-dired-diff)
  2141. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  2142. (define-key dired-mode-map "b" 'gtkbm)
  2143. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  2144. (define-key dired-mode-map (kbd "TAB") 'other-window)
  2145. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  2146. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  2147. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  2148. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  2149. (substitute-key-definition 'dired-next-line
  2150. 'my-dired-next-line
  2151. dired-mode-map)
  2152. (substitute-key-definition 'dired-previous-line
  2153. 'my-dired-previous-line
  2154. dired-mode-map)
  2155. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  2156. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  2157. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  2158. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  2159. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  2160. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  2161. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  2162. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  2163. (add-hook 'dired-mode-hook
  2164. (lambda ()
  2165. (when (fboundp 'dired-hide-details-mode)
  2166. (dired-hide-details-mode t)
  2167. (local-set-key "l" 'dired-hide-details-mode))
  2168. (let ((file "._Icon\015"))
  2169. (when nil
  2170. '(file-readable-p file)
  2171. (delete-file file)))))
  2172. (when (fboundp 'pack-dired-dwim)
  2173. (with-eval-after-load 'dired
  2174. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  2175. (when (fboundp 'dired-list-all-mode)
  2176. (setq dired-listing-switches "-lhF")
  2177. (with-eval-after-load 'dired
  2178. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  2179. (when (fboundp 'dired-filter-mode)
  2180. (add-hook 'dired-mode-hook
  2181. 'dired-filter-mode))
  2182. (set-variable 'dired-filter-stack nil)
  2183. ;; Currently disabled in favor of dired-from-git-ls-files
  2184. ;; (define-key ctl-x-map "f" 'find-dired)
  2185. (defvar my-dired-git-ls-files-history
  2186. "History for `my-dired-git-ls-files'." nil)
  2187. (defun my-dired-git-ls-files (arg)
  2188. "Dired from git ls-files."
  2189. (interactive (list
  2190. (read-shell-command "git ls-files: "
  2191. "git ls-files -z ")))
  2192. (pop-to-buffer-same-window
  2193. (dired-noselect `(,default-directory
  2194. ,@(split-string (shell-command-to-string arg)
  2195. "\0" t))
  2196. ""))
  2197. )
  2198. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  2199. (with-eval-after-load 'dired
  2200. (defvar dired-mode-map (make-sparse-keymap))
  2201. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  2202. (with-eval-after-load 'pack
  2203. (set-variable 'pack-silence
  2204. t)
  2205. (defvar pack-program-alist)
  2206. (add-to-list 'pack-program-alist
  2207. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  2208. (when (executable-find "aunpack")
  2209. (add-to-list 'pack-program-alist
  2210. ' ("\\.zip\\'"
  2211. :pack ("zip" "-r" archive sources)
  2212. :pack-append ("zip" "-r" archive sources)
  2213. :unpack ("aunpack" archive))))
  2214. )
  2215. ;; dired-k
  2216. ;; Current HEAD of original repo is broken
  2217. ;; https://github.com/syohex/emacs-dired-k/issues/45
  2218. (declare-function dired-k-no-revert "dired-k")
  2219. (when (fboundp 'dired-k)
  2220. (set-variable 'dired-k-style 'git)
  2221. ;; What is the best way of doing this?
  2222. (with-eval-after-load 'dired-k
  2223. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2224. ;; (set-variable 'dired-k-size-colors
  2225. ;; `((,most-positive-fixnum)))
  2226. ;; (set-variable 'dired-k-date-colors
  2227. ;; `((,most-positive-fixnum)))
  2228. (let* ((pkg (cadr (assq 'dired-k
  2229. package-alist)))
  2230. (version-str (package-version-join (package-desc-version pkg))))
  2231. ;; Currently dired-k HEAD of original repository is broken so do not use that
  2232. (when (string= version-str
  2233. "20171017.1228")
  2234. ;; always execute dired-k when dired buffer is opened and reverted
  2235. (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2236. ;; This still create index.lock files...........................
  2237. ;; (add-hook 'switch-buffer-functions
  2238. ;; (lambda (prev cur)
  2239. ;; (when (derived-mode-p 'dired-mode)
  2240. ;; (dired-k-no-revert))))
  2241. )))
  2242. ;; (when (eval-and-compile (require 'dired-rainbow nil t))
  2243. ;; (dired-rainbow-define gtags "brightblack" "GTAGS"))
  2244. ;; ?
  2245. ;; (set-variable 'dired-omit-files
  2246. ;; (rx (or (regexp "^\\.?#")
  2247. ;; (regexp "^\\.$")
  2248. ;; (regexp "^\\.\\.$")
  2249. ;; (regexp "^GPATH$")
  2250. ;; (regexp "^GRTAGS$")
  2251. ;; (regexp "^GTAGS$")
  2252. ;; )))
  2253. (with-eval-after-load 'diredfl
  2254. (set-face-foreground 'diredfl-file-name nil)
  2255. )
  2256. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2257. ;; misc funcs
  2258. (define-key ctl-x-map "T" 'git-worktree)
  2259. (define-key ctl-x-map "W" 'git-walktree)
  2260. (when (fboundp 'browse-url-default-macosx-browser)
  2261. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  2262. (defalias 'qcalc 'quick-calc)
  2263. (defun memo (&optional dir)
  2264. "Open memo.txt in DIR."
  2265. (interactive)
  2266. (pop-to-buffer (find-file-noselect (concat (if dir
  2267. (file-name-as-directory dir)
  2268. "")
  2269. "memo.txt"))))
  2270. ;; TODO: remember-projectile
  2271. (set (defvar my-privnotes-path nil
  2272. "My privnotes repository path.")
  2273. (expand-file-name "~/my/privnotes"))
  2274. (defun my-privnotes-readme (dir)
  2275. "Open my privnotes DIR."
  2276. (interactive (list
  2277. (read-file-name "Privnotes: "
  2278. (expand-file-name (format-time-string "%Y%m%d_")
  2279. my-privnotes-path))))
  2280. (let ((path (expand-file-name "README.md" dir)))
  2281. (with-current-buffer (find-file path)
  2282. (unless (file-exists-p path)
  2283. (insert (file-name-base dir)
  2284. "\n"
  2285. "=======\n"
  2286. "\n\n")))))
  2287. (define-key ctl-x-map "p" 'my-privnotes-readme)
  2288. (set (defvar my-rgrep-alist nil
  2289. "Alist of rgrep command.
  2290. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2291. condition to choose COMMAND when evaluated.")
  2292. `(
  2293. ;; ripgrep
  2294. ("rg"
  2295. (executable-find "rg")
  2296. "rg -nH --no-heading --hidden --glob '!.git/' --smart-case -M 1280 ")
  2297. ;; git grep
  2298. ("gitgrep"
  2299. (eq 0
  2300. (shell-command "git rev-parse --git-dir"))
  2301. "git --no-pager grep -nH -e ")
  2302. ;; sift
  2303. ("sift"
  2304. (executable-find "sift")
  2305. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2306. ;; the silver searcher
  2307. ("ag"
  2308. (executable-find "ag")
  2309. "ag --nogroup --nopager --filename ")
  2310. ;; ack
  2311. ("ack"
  2312. (executable-find "ack")
  2313. "ack --nogroup --nopager --with-filename ")
  2314. ;; gnu global
  2315. ("global"
  2316. (and (require 'ggtags nil t)
  2317. (executable-find "global")
  2318. (ggtags-current-project-root))
  2319. "global --result grep ")
  2320. ;; grep
  2321. ("grep"
  2322. t
  2323. ,(concat "find . "
  2324. "-path '*/.git' -prune -o "
  2325. "-path '*/.svn' -prune -o "
  2326. "-type f -print0 | "
  2327. "xargs -0 grep -nH -e "))
  2328. )
  2329. )
  2330. (defvar my-rgrep-default nil
  2331. "Default command name for my-rgrep.")
  2332. (defun my-rgrep-grep-command (&optional name alist)
  2333. "Return recursive grep command for current directory or nil.
  2334. If NAME is given, use that without testing.
  2335. Commands are searched from ALIST."
  2336. (if alist
  2337. (if name
  2338. ;; if name is given search that from alist and return the command
  2339. (nth 2 (assoc name
  2340. alist))
  2341. ;; if name is not given try test in 1th elem
  2342. (let ((car (car alist))
  2343. (cdr (cdr alist)))
  2344. (if (eval (nth 1 car))
  2345. ;; if the condition is true return the command
  2346. (nth 2 car)
  2347. ;; try next one
  2348. (and cdr
  2349. (my-rgrep-grep-command name cdr)))))
  2350. ;; if alist is not given set default value
  2351. (my-rgrep-grep-command name my-rgrep-alist)))
  2352. (declare-function projectile-project-p "projectile")
  2353. (declare-function projectile-with-default-dir "projectile")
  2354. (declare-function projectile-project-root "projectile")
  2355. (defun my-rgrep (command-args)
  2356. "My recursive grep. Run COMMAND-ARGS.
  2357. If prefix argument is given, use current symbol as default search target
  2358. and search from projectile root (if projectile is available)."
  2359. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2360. nil)))
  2361. (if cmd
  2362. (list (read-shell-command "grep command: "
  2363. (concat cmd
  2364. (if current-prefix-arg
  2365. (thing-at-point 'symbol t)
  2366. ""))
  2367. 'grep-find-history))
  2368. (error "My-Rgrep: Command for rgrep not found")
  2369. )))
  2370. (if (and current-prefix-arg
  2371. (eval-and-compile (require 'projectile nil t))
  2372. (projectile-project-p))
  2373. (projectile-with-default-dir (projectile-project-root)
  2374. (compilation-start command-args
  2375. 'grep-mode))
  2376. (compilation-start command-args
  2377. 'grep-mode)))
  2378. (defun my-rgrep-thing-at-point-projectile-root ()
  2379. "My recursive grep to find thing at point from project root."
  2380. (interactive)
  2381. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2382. nil))
  2383. (command-args
  2384. (if cmd
  2385. (concat cmd
  2386. (or (thing-at-point 'symbol t)
  2387. (error "No symbol at point")))
  2388. (error "My-Rgrep: Command for rgrep not found"))))
  2389. (if (eval-and-compile (require 'projectile nil t))
  2390. (projectile-with-default-dir (or (projectile-project-root)
  2391. default-directory)
  2392. (compilation-start command-args
  2393. 'grep-mode))
  2394. (compilation-start command-args
  2395. 'grep-mode))))
  2396. (defmacro define-my-rgrep (name)
  2397. "Define rgrep for NAME."
  2398. `(defun ,(intern (concat "my-rgrep-"
  2399. name)) ()
  2400. ,(format "My recursive grep by %s."
  2401. name)
  2402. (interactive)
  2403. (let ((my-rgrep-default ,name))
  2404. (if (called-interactively-p 'any)
  2405. (call-interactively 'my-rgrep)
  2406. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2407. )
  2408. (define-my-rgrep "ack")
  2409. (define-my-rgrep "ag")
  2410. (define-my-rgrep "rg")
  2411. (define-my-rgrep "sift")
  2412. (define-my-rgrep "gitgrep")
  2413. (define-my-rgrep "grep")
  2414. (define-my-rgrep "global")
  2415. (define-key ctl-x-map "s" 'my-rgrep)
  2416. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2417. (defun my-occur (regexp &optional region)
  2418. "My occur command to search REGEXP to search REGION."
  2419. (interactive (list (read-string "List lines matching regexp: "
  2420. (thing-at-point 'symbol t))))
  2421. (occur regexp nil region))
  2422. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2423. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2424. (defalias 'make 'compile)
  2425. (define-key ctl-x-map "c" 'compile)
  2426. (autoload 'pb/push-item "pushbullet")
  2427. (defun my-pushbullet-note (text &optional title)
  2428. "Push TEXT with optional TITLE."
  2429. (interactive "sText to Push: ")
  2430. (pb/push-item '("") text "note" (or title "")))
  2431. ;;;;;;;;;;;;;;;;;;;;;
  2432. ;; git-bug
  2433. (defconst git-bug-ls-regexp
  2434. (eval-when-compile
  2435. (rx bol
  2436. (submatch (one-or-more alphanumeric)) ; id
  2437. ;; (one-or-more any)
  2438. (one-or-more space)
  2439. (submatch (or "open" "close")) ; status
  2440. (one-or-more space)
  2441. (submatch (maximal-match (zero-or-more print))) ; title
  2442. "\t"
  2443. (submatch (one-or-more alphanumeric)) ; user
  2444. (one-or-more space)
  2445. "C:"
  2446. (submatch (one-or-more digit)) ; Comment num
  2447. (one-or-more space)
  2448. "L:"
  2449. (submatch (one-or-more digit)) ; Label num
  2450. eol
  2451. ))
  2452. "Regexp to parse line of output of git-bug ls.
  2453. Used by `git-bug-ls'.")
  2454. (declare-function string-trim "subr-x")
  2455. (defun git-bug-bugs ()
  2456. "Get list of git-bug bugs."
  2457. (with-temp-buffer
  2458. (git-bug--call-process "bug" "ls")
  2459. (goto-char (point-min))
  2460. (let ((bugs nil))
  2461. (while (not (eq (point) (point-max)))
  2462. (save-match-data
  2463. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  2464. (setq bugs `(,@bugs
  2465. ,(list
  2466. :id (match-string 1)
  2467. :status (match-string 2)
  2468. :title (string-trim (match-string 3))
  2469. :user (match-string 4)
  2470. :comment-num (match-string 5)
  2471. :label-num (match-string 6)
  2472. )))))
  2473. (forward-line 1)
  2474. (goto-char (point-at-bol)))
  2475. bugs)))
  2476. (defun git-bug-ls ()
  2477. "Open and select git bug list buffer."
  2478. (interactive)
  2479. (pop-to-buffer (git-bug-ls-noselect)))
  2480. (defun git-bug-ls--set-tabulated-list-mode-variables ()
  2481. "Not implemented.")
  2482. (defun git-bug-ls-mode ()
  2483. "Not implemented.")
  2484. (defun git-bug-ls-noselect (&optional directory)
  2485. "Open git bug list buffer.
  2486. If optional arg DIRECTORY is given change current directory to there before
  2487. initializing."
  2488. (setq directory (expand-file-name (or directory
  2489. default-directory)))
  2490. (cl-assert (file-directory-p directory))
  2491. (let* ((root (git-bug--get-repository-root directory))
  2492. (name (file-name-nondirectory root))
  2493. (bname (format "*GitBug<%s>*" name)))
  2494. (with-current-buffer (get-buffer-create bname)
  2495. (cd root)
  2496. (git-bug-ls--set-tabulated-list-mode-variables)
  2497. (git-bug-ls-mode)
  2498. (current-buffer))))
  2499. (defun git-bug--get-repository-root (dir)
  2500. "Resolve repository root of DIR.
  2501. If DIR is not inside of any git repository, signal an error."
  2502. (cl-assert (file-directory-p dir))
  2503. (with-temp-buffer
  2504. (cd dir)
  2505. (git-bug--call-process "rev-parse" "--show-toplevel")
  2506. (goto-char (point-min))
  2507. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  2508. (defun git-bug--call-process (&rest args)
  2509. "Start git process synchronously with ARGS.
  2510. Raise error when git process ends with non-zero status.
  2511. Any output will be written to current buffer."
  2512. (let ((status (apply 'call-process
  2513. "git"
  2514. nil
  2515. t
  2516. nil
  2517. args)))
  2518. (cl-assert (eq status 0)
  2519. nil
  2520. (buffer-substring-no-properties (point-min) (point-max)))))
  2521. ;;;;;;;;;;;;;;;;;;;
  2522. ;; peek-file-mode
  2523. (defvar peek-file-buffers
  2524. ()
  2525. "Peek buffers.")
  2526. (defun peek-file (file)
  2527. "Peek FILE."
  2528. (interactive "fFile to peek: ")
  2529. (with-current-buffer (find-file file)
  2530. (peek-file-mode)))
  2531. (define-minor-mode peek-file-mode
  2532. "Peek file mode."
  2533. :lighter "PK"
  2534. (view-mode peek-file-mode)
  2535. (add-to-list 'peek-file-buffers
  2536. (current-buffer))
  2537. (add-hook 'switch-buffer-functions
  2538. 'peek-file-remove-buffers))
  2539. (defun peek-file-remove-buffers (&args _)
  2540. "Remove peek file buffers."
  2541. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2542. (unless (get-buffer-window buf t)
  2543. (setq peek-file-buffers
  2544. (delq buf
  2545. peek-file-buffers))
  2546. (with-current-buffer buf
  2547. (when peek-file-mode
  2548. (kill-buffer))))))
  2549. ;;;;;;;;;;;;;;;;;;;;
  2550. ;; remember-projectile
  2551. ;; TODO: Add global-minor-mode
  2552. (defvar remember-data-file)
  2553. (defun my-set-remember-data-file-buffer-local ()
  2554. "Set `remember-data-file'."
  2555. (when (require 'projectile nil t)
  2556. (setq-local remember-data-file
  2557. (expand-file-name ".remember.notes"
  2558. (projectile-project-root)))))
  2559. (add-hook 'after-change-major-mode-hook
  2560. 'my-set-remember-data-file-buffer-local)
  2561. (define-key ctl-x-map "R" 'remember)
  2562. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2563. ;; ivy
  2564. (set-variable 'ivy-format-functions-alist
  2565. '((t . (lambda (cands) (ivy--format-function-generic
  2566. (lambda (str)
  2567. (concat "+> "
  2568. (ivy--add-face str 'ivy-current-match)
  2569. ))
  2570. (lambda (str)
  2571. (concat "| " str))
  2572. cands
  2573. "\n")))))
  2574. (set-variable 'ivy-wrap t)
  2575. (set-variable 'ivy-sort-max-size 500)
  2576. (when (fboundp 'ivy-rich-mode)
  2577. (ivy-rich-mode 1))
  2578. (with-eval-after-load 'ivy
  2579. (defvar ivy-minibuffer-map)
  2580. (define-key ivy-minibuffer-map (kbd "C-u")
  2581. (lambda () (interactive) (delete-region (point-at-bol) (point)))))
  2582. (set-variable 'ivy-on-del-error-function 'ignore)
  2583. (when (fboundp 'counsel-M-x)
  2584. (define-key esc-map "x" 'counsel-M-x)
  2585. )
  2586. (declare-function ivy-thing-at-point "ivy")
  2587. (when (and (fboundp 'ivy-read)
  2588. (locate-library "counsel"))
  2589. (defvar counsel-describe-map)
  2590. (defun my-counsel-describe-symbol ()
  2591. "Forwaord to `describe-symbol'."
  2592. (interactive)
  2593. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2594. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2595. (ivy-read "Describe symbol: " obarray
  2596. ;; From describe-symbol definition
  2597. :predicate (lambda (vv)
  2598. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2599. describe-symbol-backends))
  2600. :require-match t
  2601. :history 'counsel-describe-symbol-history
  2602. :keymap counsel-describe-map
  2603. :preselect (ivy-thing-at-point)
  2604. :action (lambda (x)
  2605. (describe-symbol (intern x)))
  2606. :caller 'my-counsel-describe-symbol))
  2607. (define-key help-map "o" 'my-counsel-describe-symbol)
  2608. )
  2609. (declare-function ivy-configure "ivy")
  2610. (with-eval-after-load 'counsel ;; Hook to counsel, not ivy
  2611. ;; (ivy-configure 'my-counsel-describe-symbol
  2612. ;; :sort-fn 'my-ivy-length)
  2613. (ivy-configure 'counsel-M-x
  2614. :initial-input ""
  2615. ;; :sort-fn 'my-ivy-length
  2616. )
  2617. )
  2618. (when (fboundp 'counsel-imenu)
  2619. (define-key ctl-x-map "l" 'counsel-imenu))
  2620. (when (fboundp 'swiper)
  2621. (define-key esc-map (kbd "C-s") 'swiper))
  2622. (with-eval-after-load 'ivy
  2623. ;; ivy-prescient requires counsel already loaded
  2624. (require 'counsel nil t)
  2625. (when (require 'ivy-prescient nil t)
  2626. (set-variable 'prescient-filter-method
  2627. '(fuzzy literal regexp initialism))
  2628. (ivy-prescient-mode 1)))
  2629. ;; ?
  2630. (define-key input-decode-map "\e[1;5C" [C-right])
  2631. (define-key input-decode-map "\e[1;5D" [C-left])
  2632. ;;;;;;;;;;;;;;;;;;;;;;;;
  2633. ;; mozc
  2634. (global-set-key (kbd "C-c m e") 'ignore)
  2635. (global-set-key (kbd "C-c m d") 'ignore)
  2636. ;; mozc
  2637. (when (locate-library "mozc")
  2638. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2639. (with-eval-after-load 'mozc
  2640. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2641. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2642. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2643. )
  2644. (setq default-input-method "japanese-mozc")
  2645. (custom-set-variables '(mozc-leim-title "あ"))
  2646. (defun turn-on-input-method ()
  2647. (interactive)
  2648. (activate-input-method default-input-method))
  2649. (defun turn-off-input-method ()
  2650. (interactive)
  2651. (deactivate-input-method))
  2652. ;; (setq mozc-candidate-style 'echo-area)
  2653. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2654. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2655. (global-set-key (kbd "<f7>") 'turn-off-input-method)
  2656. (global-set-key (kbd "<f8>") 'turn-on-input-method)
  2657. (require 'mozc-popup)
  2658. (set-variable 'mozc-candidate-style 'popup)
  2659. ;; これいる?
  2660. (when (require 'mozc-im nil t)
  2661. (setq default-input-method "japanese-mozc-im")
  2662. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2663. )
  2664. )
  2665. ;;;;;;;;;;;;;;
  2666. ;; mmv
  2667. ;; https://www.emacswiki.org/emacs/MakingMarkVisible
  2668. ;;;; Make the mark visible, and the visibility toggleable. ('mmv' means 'make
  2669. ;;;; mark visible'.) By Patrick Gundlach, Teemu Leisti, and Stefan.
  2670. (defgroup mmv nil
  2671. "Make mark visible."
  2672. :group 'tools)
  2673. (defvar mmv-face-foreground
  2674. (face-foreground 'hi-yellow)
  2675. "Foreground color for `mmv-face'.")
  2676. (defvar mmv-face-background
  2677. (face-background 'hi-yellow)
  2678. "Background color for `mmv-face'.")
  2679. (defface mmv-face
  2680. `((t :background ,mmv-face-background :foreground ,mmv-face-foreground))
  2681. "Face used for showing the mark's position."
  2682. :group 'mmv)
  2683. (defvar-local mmv-mark-overlay nil
  2684. "The overlay for showing the mark's position.")
  2685. (defvar-local mmv-is-mark-visible t
  2686. "The overlay is visible only when this variable's value is t.")
  2687. (defun mmv-draw-mark (&rest _)
  2688. "Make the mark's position stand out by means of a one-character-long overlay.
  2689. If the value of variable `mmv-is-mark-visible' is nil, the mark will be
  2690. invisible."
  2691. (unless mmv-mark-overlay
  2692. (setq mmv-mark-overlay (make-overlay 0 0 nil t))
  2693. (overlay-put mmv-mark-overlay 'face 'mmv-face)
  2694. (overlay-put mmv-mark-overlay 'priority 10)) ;; bigger than highlight-indentation-current-column-overlay-priority
  2695. (let ((mark-position (mark t)))
  2696. (cond
  2697. ((null mark-position) (delete-overlay mmv-mark-overlay))
  2698. ((and (< mark-position (point-max))
  2699. (not (eq ?\n (char-after mark-position))))
  2700. (overlay-put mmv-mark-overlay 'after-string nil)
  2701. (move-overlay mmv-mark-overlay mark-position (1+ mark-position)))
  2702. (t
  2703. ;; This branch is called when the mark is at the end of a line or at the
  2704. ;; end of the buffer. We use a bit of trickery to avoid the higlight
  2705. ;; extending from the mark all the way to the right end of the frame.
  2706. (overlay-put mmv-mark-overlay 'after-string
  2707. (propertize " " 'face (overlay-get mmv-mark-overlay 'face)))
  2708. (move-overlay mmv-mark-overlay mark-position mark-position)))))
  2709. ;; Makes display very slow?
  2710. (add-hook 'pre-redisplay-functions #'mmv-draw-mark)
  2711. (defun mmv-toggle-mark-visibility ()
  2712. "Toggles the mark's visiblity and redraws it (whether invisible or visible)."
  2713. (interactive)
  2714. (setq mmv-is-mark-visible (not mmv-is-mark-visible))
  2715. (if mmv-is-mark-visible
  2716. (set-face-attribute 'mmv-face nil :background mmv-face-background :foreground mmv-face-foreground)
  2717. (set-face-attribute 'mmv-face nil :background 'unspecified :foreground 'unspecified))
  2718. (mmv-draw-mark))
  2719. ;; Local Variables:
  2720. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2721. ;; flycheck-checker: emacs-lisp
  2722. ;; End:
  2723. ;;; emancs.el ends here