選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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