You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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