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

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