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.
 
 
 
 
 
 

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