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

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