Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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