Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

3133 Zeilen
105 KiB

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