Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

3075 wiersze
103 KiB

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