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

3096 行
103 KiB

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