您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

3257 行
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. (when (boundp 'git-rebase-filename-regexp)
  1422. (add-to-list 'auto-mode-alist
  1423. `(,git-rebase-filename-regexp . text-mode)))
  1424. (when (fboundp 'ggtags-mode)
  1425. (add-hook 'c-mode-common-hook
  1426. 'ggtags-mode)
  1427. (add-hook 'python-mode-hook
  1428. 'ggtags-mode)
  1429. (add-hook 'js-mode-hook
  1430. 'ggtags-mode)
  1431. (add-hook 'scheme-mode-hook
  1432. 'ggtags-mode)
  1433. )
  1434. (when (fboundp 'imenu-list-minor-mode)
  1435. (defvar imenu-list-buffer-name)
  1436. (defun my-imenu-list-toggle ()
  1437. "My 'imenu-list` toggle."
  1438. (interactive)
  1439. (require 'imenu-list)
  1440. (if (eq (window-buffer)
  1441. (get-buffer imenu-list-buffer-name))
  1442. (imenu-list-minor-mode -1)
  1443. (imenu-list-minor-mode 1)))
  1444. ;; (set-variable 'imenu-list-auto-resize t)
  1445. (set-variable 'imenu-list-focus-after-activation t)
  1446. ;; (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle)
  1447. (define-key ctl-x-map (kbd "C-l") 'imenu-list-smart-toggle)
  1448. )
  1449. (add-hook 'emacs-lisp-mode-hook
  1450. (lambda ()
  1451. (setq imenu-generic-expression
  1452. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1453. ,@imenu-generic-expression))))
  1454. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1455. (with-eval-after-load 'compile
  1456. (defvar compilation-filter-start)
  1457. (defvar compilation-error-regexp-alist)
  1458. (eval-and-compile (require 'ansi-color))
  1459. (add-hook 'compilation-filter-hook
  1460. (lambda ()
  1461. (let ((inhibit-read-only t))
  1462. (ansi-color-apply-on-region compilation-filter-start
  1463. (point)))))
  1464. (add-to-list 'compilation-error-regexp-alist
  1465. ;; ansible-lint
  1466. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1467. (add-to-list 'compilation-error-regexp-alist
  1468. ;; pydocstyle
  1469. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1470. )
  1471. (declare-function company-cancel "company")
  1472. (when (fboundp 'global-company-mode)
  1473. (add-hook 'after-first-visit-hook
  1474. 'global-company-mode))
  1475. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1476. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1477. (set-variable 'company-idle-delay nil)
  1478. (set-variable 'company-minimum-prefix-length 2)
  1479. (set-variable 'company-selection-wrap-around t)
  1480. (set-variable 'company-global-modes '(not term-char-mode
  1481. term-line-mode))
  1482. (declare-function company-manual-begin "company")
  1483. (with-eval-after-load 'company
  1484. (defvar company-mode-map)
  1485. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1486. ;; (with-eval-after-load 'python
  1487. ;; (defvar python-indent-trigger-commands)
  1488. ;; ;; TODO: This disables completion in python?
  1489. ;; (add-to-list 'python-indent-trigger-commands
  1490. ;; 'company-indent-or-complete-common))
  1491. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1492. (defvar company-active-map)
  1493. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1494. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1495. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1496. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1497. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1498. (defvar company-mode)
  1499. (defvar company-candidates)
  1500. (defvar company-candidates-length)
  1501. ;; (popup-tip "Hello, World!")
  1502. (defun my-company-lighter-current-length ()
  1503. "Get current candidate length."
  1504. (interactive)
  1505. (let ((l nil)
  1506. (inhibit-message t))
  1507. (when (and company-mode
  1508. (not (minibufferp))
  1509. ;; Do nothing when already in company completion
  1510. (not company-candidates))
  1511. ;; FIXME: Somehow it cannto catch errors from ggtags
  1512. (ignore-errors
  1513. ;; (company-auto-begin)
  1514. (company-manual-begin)
  1515. (setq l company-candidates-length)
  1516. (company-cancel)))
  1517. (if l
  1518. (format "[%d]" l)
  1519. "")))
  1520. (defvar company-lighter)
  1521. (set-variable 'company-lighter-base "Cmp")
  1522. ;; (add-to-list 'company-lighter
  1523. ;; '(:eval (my-company-lighter-current-length))
  1524. ;; t)
  1525. ;; This breaks japanese text input
  1526. ;; (set-variable 'my-company-length-popup-tip-timer
  1527. ;; (run-with-idle-timer 0.2 t
  1528. ;; 'my-company-length-popup-tip))
  1529. ;; (current-active-maps)
  1530. ;; (lookup-key)
  1531. '(mapcar (lambda (map)
  1532. (lookup-key map (kbd "C-i")))
  1533. (current-active-maps))
  1534. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1535. (set-face-attribute 'company-tooltip nil
  1536. :foreground "black" :background "lightgrey")
  1537. (set-face-attribute 'company-tooltip-common nil
  1538. :foreground "black" :background "lightgrey")
  1539. (set-face-attribute 'company-tooltip-common-selection nil
  1540. :foreground "white" :background "steelblue")
  1541. (set-face-attribute 'company-tooltip-selection nil
  1542. :foreground "black" :background "steelblue")
  1543. (set-face-attribute 'company-preview-common nil
  1544. :background nil :foreground "lightgrey" :underline t)
  1545. (set-face-attribute 'company-scrollbar-fg nil
  1546. :background "orange")
  1547. (set-face-attribute 'company-scrollbar-bg nil
  1548. :background "gray40")
  1549. )
  1550. ;; https://github.com/lunaryorn/flycheck
  1551. ;; TODO: Any way to disable auto check?
  1552. ;; Update flycheck-hooks-alist?
  1553. (when (fboundp 'global-flycheck-mode)
  1554. (add-hook 'after-first-visit-hook
  1555. 'global-flycheck-mode))
  1556. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1557. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1558. ;; (with-eval-after-load 'flycheck
  1559. ;; (when (fboundp 'flycheck-black-check-setup)
  1560. ;; (flycheck-black-check-setup)))
  1561. ;; (when (fboundp 'ilookup-open-word)
  1562. ;; (define-key ctl-x-map "d" 'ilookup-open-word)
  1563. ;; )
  1564. (set-variable 'ac-ignore-case nil)
  1565. (when (fboundp 'term-run-shell-command)
  1566. (define-key ctl-x-map "t" 'term-run-shell-command))
  1567. (add-to-list 'safe-local-variable-values
  1568. '(encoding utf-8))
  1569. (setq enable-local-variables :safe)
  1570. ;; Detect file type from shebang and set major-mode.
  1571. (add-to-list 'interpreter-mode-alist
  1572. '("python3" . python-mode))
  1573. (add-to-list 'interpreter-mode-alist
  1574. '("python2" . python-mode))
  1575. (with-eval-after-load 'python
  1576. (defvar python-mode-map (make-sparse-keymap))
  1577. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1578. ;; I want to use this, but this breaks normal self-insert-command
  1579. ;; (set-variable 'py-indent-list-style
  1580. ;; 'one-level-to-beginning-of-statement)
  1581. (set-variable 'pydoc-command
  1582. "python3 -m pydoc")
  1583. (declare-function with-venv-advice-add "with-venv")
  1584. (with-eval-after-load 'pydoc
  1585. ;; pydoc depends on python-shell-interpreter but it does not load this
  1586. (require 'python)
  1587. (when (require 'with-venv nil t)
  1588. (with-venv-advice-add 'pydoc)
  1589. ;; Used in interactive function of pydoc
  1590. (with-venv-advice-add 'pydoc-all-modules)))
  1591. (defvar my-cousel-pydoc-history nil "History of `my-counsel-pydoc'.")
  1592. (defun my-counsel-pydoc ()
  1593. "Counsel `pydoc'."
  1594. (interactive)
  1595. (eval-and-compile (require 'pydoc nil t))
  1596. (ivy-read "Recently: " (pydoc-all-modules)
  1597. :require-match t
  1598. :history 'my-cousel-pydoc-history
  1599. :action (lambda (x) (pydoc x))
  1600. :caller 'my-counsel-pydoc))
  1601. (set-variable 'flycheck-python-mypy-config '("mypy.ini" ".mypy.ini" "setup.cfg"))
  1602. (set-variable 'flycheck-flake8rc '("setup.cfg" "tox.ini" ".flake8rc"))
  1603. (set-variable 'flycheck-python-pylint-executable "python3")
  1604. (set-variable 'flycheck-python-pycompile-executable "python3")
  1605. (set-variable 'python-indent-guess-indent-offset nil)
  1606. (with-eval-after-load 'blacken
  1607. (when (require 'with-venv nil t)
  1608. (with-venv-advice-add 'blacken-buffer)))
  1609. (with-eval-after-load 'ansible-doc
  1610. (when (require 'with-venv nil t)
  1611. (with-venv-advice-add 'ansible-doc)))
  1612. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1613. (defun my-isortify ()
  1614. (interactive)
  1615. (cl-assert buffer-file-name)
  1616. (cl-assert (not (buffer-modified-p)))
  1617. (call-process "python" ;; PROGRAM
  1618. nil ;; INFILE
  1619. nil ;; DESTINATION
  1620. nil ;; DISPLAY
  1621. "-m" "isort" buffer-file-name)
  1622. (message "isortify done")
  1623. (revert-buffer nil t))
  1624. (when (fboundp 'with-venv-advice-add)
  1625. ;; TODO: Lazy load with-venv
  1626. (with-venv-advice-add 'my-isortify))
  1627. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1628. (defun my-set-venv-flycheck-executable-find ()
  1629. "Set flycheck executabie find."
  1630. (interactive)
  1631. (when (fboundp 'with-venv)
  1632. (set-variable 'flycheck-executable-find
  1633. '(lambda (e)
  1634. (with-venv
  1635. (executable-find e)))
  1636. t)))
  1637. (defun my-update-flycheck-flake8-error-level-alist ()
  1638. "Update `flycheck-flake8-error-level-alist'."
  1639. (defvar flycheck-flake8-error-level-alist)
  1640. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1641. ;; '("^D.*$" . warning))
  1642. (set-variable 'flycheck-flake8-error-level-alist
  1643. nil)
  1644. )
  1645. (add-hook 'python-mode-hook
  1646. 'my-set-venv-flycheck-executable-find)
  1647. (add-hook 'python-mode-hook
  1648. 'my-update-flycheck-flake8-error-level-alist)
  1649. (when (fboundp 'with-venv-info-mode)
  1650. (add-hook 'python-mode-hook
  1651. 'with-venv-info-mode))
  1652. ;; Run multiple chekcers
  1653. ;; https://github.com/flycheck/flycheck/issues/186
  1654. (add-hook 'python-mode-hook
  1655. (lambda ()
  1656. ;; Currently on python-mode eldoc-mode sometimes print
  1657. ;; wired message on "from" keyword:
  1658. ;;var from = require("./from")
  1659. (eldoc-mode -1)))
  1660. ;; http://fukuyama.co/foreign-regexp
  1661. '(and (require 'foreign-regexp nil t)
  1662. (progn
  1663. (setq foreign-regexp/regexp-type 'perl)
  1664. '(setq reb-re-syntax 'foreign-regexp)
  1665. ))
  1666. ;; sqlind does not support create role so disable it...
  1667. (set-variable 'sql-use-indent-support nil)
  1668. ;; (with-eval-after-load 'sql
  1669. ;; (require 'sql-indent nil t))
  1670. ;; (set-variable 'sqlind-basic-offset 4)
  1671. (add-to-list 'auto-mode-alist
  1672. '("\\.hql\\'" . sql-mode))
  1673. (set-variable 'sql-product 'postgres)
  1674. (set-variable 'sqlformat-command 'pgformatter)
  1675. (set-variable 'sqlformat-args '("--no-space-function" "--nogrouping"))
  1676. ;; Hard to use because when failed to format it does not tell how to fix that
  1677. ;; (set-variable 'sqlformat-command 'sqlfluff)
  1678. ;; (set-variable 'sqlformat-args '("--show-lint-violations" "-vvvv"))
  1679. ;; (with-eval-after-load 'sqlformat
  1680. ;; (when (fboundp 'with-venv-advice-add)
  1681. ;; (with-venv-advice-add 'sqlformat-region)))
  1682. (when (fboundp 'git-command)
  1683. (define-key ctl-x-map "g" 'git-command))
  1684. ;; (when (fboundp 'gited-list)
  1685. ;; (defalias 'gited 'gited-list))
  1686. (when (fboundp 'counsel-git-checkout)
  1687. (defalias 'my-git-si 'counsel-git-checkout))
  1688. (when (and (eval-and-compile (require 'git-commit nil t))
  1689. (fboundp 'global-git-commit-mode))
  1690. ;; Frequently this breaks git commit.
  1691. (global-git-commit-mode 0))
  1692. (with-eval-after-load 'git-commit
  1693. (add-hook 'git-commit-setup-hook
  1694. 'turn-off-auto-fill t))
  1695. (with-eval-after-load 'rst
  1696. (defvar rst-mode-map)
  1697. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1698. (with-eval-after-load 'jdee
  1699. (add-hook 'jdee-mode-hook
  1700. (lambda ()
  1701. (make-local-variable 'global-mode-string)
  1702. (add-to-list 'global-mode-string
  1703. mode-line-position))))
  1704. ;; Cannot enable error thrown. Why???
  1705. ;; https://github.com/m0smith/malabar-mode#Installation
  1706. ;; (when (require 'malabar-mode nil t)
  1707. ;; (add-to-list 'load-path
  1708. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1709. ;; (require 'cedet-devel-load nil t)
  1710. ;; (eval-after-init (activate-malabar-mode)))
  1711. (with-eval-after-load 'make-mode
  1712. (defvar makefile-mode-map (make-sparse-keymap))
  1713. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1714. ;; this functions is set in write-file-functions, i cannot find any
  1715. ;; good way to remove this.
  1716. (fset 'makefile-warn-suspicious-lines 'ignore))
  1717. (with-eval-after-load 'verilog-mode
  1718. (defvar verilog-mode-map (make-sparse-keymap))
  1719. (define-key verilog-mode-map ";" 'self-insert-command))
  1720. (setq diff-switches "-u")
  1721. (autoload 'diff-goto-source "diff-mode" nil t)
  1722. (with-eval-after-load 'diff-mode
  1723. ;; (when (and (eq major-mode
  1724. ;; 'diff-mode)
  1725. ;; (not buffer-file-name))
  1726. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1727. ;; (view-mode 1))
  1728. (set-face-attribute 'diff-header nil
  1729. :foreground nil
  1730. :background nil
  1731. :weight 'bold)
  1732. (set-face-attribute 'diff-file-header nil
  1733. :foreground nil
  1734. :background nil
  1735. :weight 'bold)
  1736. (set-face-foreground 'diff-index "blue")
  1737. (set-face-attribute 'diff-hunk-header nil
  1738. :foreground "cyan"
  1739. :weight 'normal)
  1740. (set-face-attribute 'diff-context nil
  1741. ;; :foreground "white"
  1742. :foreground nil
  1743. :weight 'normal)
  1744. (set-face-foreground 'diff-removed "red")
  1745. (set-face-foreground 'diff-added "green")
  1746. (set-face-background 'diff-removed nil)
  1747. (set-face-background 'diff-added nil)
  1748. (set-face-attribute 'diff-changed nil
  1749. :foreground "magenta"
  1750. :weight 'normal)
  1751. (set-face-attribute 'diff-refine-changed nil
  1752. :foreground nil
  1753. :background nil
  1754. :weight 'bold
  1755. :inverse-video t)
  1756. ;; Annoying !
  1757. ;;(diff-auto-refine-mode)
  1758. (set-variable 'diff-refine nil)
  1759. )
  1760. ;; (ffap-bindings)
  1761. (with-eval-after-load 'browse-url
  1762. (set-variable 'browse-url-browser-function
  1763. 'eww-browse-url))
  1764. (set-variable 'sh-here-document-word "__EOC__")
  1765. (with-eval-after-load 'adoc-mode
  1766. (defvar adoc-mode-map)
  1767. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1768. (when (fboundp 'adoc-mode)
  1769. (setq auto-mode-alist
  1770. `(("\\.adoc\\'" . adoc-mode)
  1771. ("\\.asciidoc\\'" . adoc-mode)
  1772. ,@auto-mode-alist)))
  1773. (with-eval-after-load 'markup-faces
  1774. ;; Is this too match ?
  1775. (set-face-foreground 'markup-meta-face
  1776. "color-245")
  1777. (set-face-foreground 'markup-meta-hide-face
  1778. "color-245")
  1779. )
  1780. ;; TODO: check if this is required
  1781. (with-eval-after-load 'groovy-mode
  1782. (defvar groovy-mode-map)
  1783. (define-key groovy-mode-map "(" 'self-insert-command)
  1784. (define-key groovy-mode-map ")" 'self-insert-command)
  1785. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1786. )
  1787. (when (fboundp 'groovy-mode)
  1788. (add-to-list 'auto-mode-alist
  1789. '("build\\.gradle\\'" . groovy-mode)))
  1790. (add-to-list 'auto-mode-alist
  1791. '("\\.gawk\\'" . awk-mode))
  1792. (with-eval-after-load 'yaml-mode
  1793. (defvar yaml-mode-map (make-sparse-keymap))
  1794. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1795. (when (fboundp 'yaml-mode)
  1796. (add-to-list 'auto-mode-alist
  1797. '("\\.yaml\\.gotmpl\\'" . yaml-mode)))
  1798. (with-eval-after-load 'html-mode
  1799. (defvar html-mode-map (make-sparse-keymap))
  1800. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1801. (with-eval-after-load 'text-mode
  1802. (define-key text-mode-map (kbd "C-m") 'newline))
  1803. (with-eval-after-load 'info
  1804. (defvar Info-additional-directory-list)
  1805. (dolist (dir (directory-files (concat user-emacs-directory
  1806. "info")
  1807. t
  1808. "^[^.].*"))
  1809. (when (file-directory-p dir)
  1810. (add-to-list 'Info-additional-directory-list
  1811. dir)))
  1812. (let ((dir (expand-file-name "~/.brew/share/info")))
  1813. (when (file-directory-p dir)
  1814. (add-to-list 'Info-additional-directory-list
  1815. dir))))
  1816. (with-eval-after-load 'apropos
  1817. (defvar apropos-mode-map (make-sparse-keymap))
  1818. (define-key apropos-mode-map "n" 'next-line)
  1819. (define-key apropos-mode-map "p" 'previous-line))
  1820. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1821. ;; (define-key isearch-mode-map
  1822. ;; (kbd "C-j") 'isearch-other-control-char)
  1823. ;; (define-key isearch-mode-map
  1824. ;; (kbd "C-k") 'isearch-other-control-char)
  1825. ;; (define-key isearch-mode-map
  1826. ;; (kbd "C-h") 'isearch-other-control-char)
  1827. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1828. (define-key isearch-mode-map (kbd "M-r")
  1829. 'isearch-query-replace-regexp)
  1830. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1831. (setq lazy-highlight-cleanup nil)
  1832. ;; face for isearch highlighting
  1833. (set-face-attribute 'lazy-highlight
  1834. nil
  1835. :foreground `unspecified
  1836. :background `unspecified
  1837. :underline t
  1838. ;; :weight `bold
  1839. )
  1840. (add-hook 'outline-mode-hook
  1841. (lambda ()
  1842. (when (string-match "\\.md\\'" buffer-file-name)
  1843. (setq-local outline-regexp "#+ "))))
  1844. (add-hook 'outline-mode-hook
  1845. 'outline-show-all)
  1846. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1847. (with-eval-after-load 'markdown-mode
  1848. (defvar gfm-mode-map)
  1849. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1850. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1851. )
  1852. (when (fboundp 'gfm-mode)
  1853. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1854. (add-hook 'markdown-mode-hook
  1855. 'outline-minor-mode)
  1856. (add-hook 'markdown-mode-hook
  1857. (lambda ()
  1858. (setq-local comment-start ";")))
  1859. )
  1860. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1861. ;; M-$ to ispell word
  1862. ;; M-x flyspell-buffer to highlight all suspicious words
  1863. (when (executable-find "aspell")
  1864. (set-variable 'ispell-program-name "aspell")
  1865. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1866. (with-eval-after-load 'ispell
  1867. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1868. (when (fboundp 'flyspell-mode)
  1869. (add-hook 'text-mode-hook
  1870. 'flyspell-mode))
  1871. (when (fboundp 'flyspell-prog-mode)
  1872. (add-hook 'prog-mode-hook
  1873. 'flyspell-prog-mode))
  1874. ;; c-mode
  1875. ;; http://www.emacswiki.org/emacs/IndentingC
  1876. ;; http://en.wikipedia.org/wiki/Indent_style
  1877. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1878. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1879. (with-eval-after-load 'cc-vars
  1880. (defvar c-default-style nil)
  1881. (add-to-list 'c-default-style
  1882. '(c-mode . "k&r"))
  1883. (add-to-list 'c-default-style
  1884. '(c++-mode . "k&r")))
  1885. (add-to-list 'auto-mode-alist
  1886. '("\\.gs\\'" . js-mode))
  1887. (with-eval-after-load 'js2-mode
  1888. ;; currently do not use js2-mode
  1889. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1890. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1891. ;; (defvar js2-mode-map (make-sparse-keymap))
  1892. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1893. ;; (interactive)
  1894. ;; (js2-enter-key)
  1895. ;; (indent-for-tab-command)))
  1896. ;; (add-hook (kill-local-variable 'before-save-hook)
  1897. ;; 'js2-before-save)
  1898. ;; (add-hook 'before-save-hook
  1899. ;; 'my-indent-buffer
  1900. ;; nil
  1901. ;; t)
  1902. )
  1903. (add-to-list 'interpreter-mode-alist
  1904. '("node" . js-mode))
  1905. (add-hook 'js-mode-hook
  1906. (lambda ()
  1907. ;; Stop current line highlighting
  1908. (set-variable 'js-indent-level 2 t)
  1909. ))
  1910. (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode))
  1911. (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))
  1912. ;; global-treesit-auto-mode
  1913. ;; https://zenn.dev/glassonion1/articles/20752bb8d2cf98
  1914. (set-variable 'treesit-language-source-alist
  1915. '((json "https://github.com/tree-sitter/tree-sitter-json")
  1916. (tsx "https://github.com/tree-sitter/tree-sitter-typescript" "master" "tsx/src")
  1917. (typescript "https://github.com/tree-sitter/tree-sitter-typescript" "master" "typescript/src")
  1918. (go "https://github.com/tree-sitter/tree-sitter-go")
  1919. (gomod "https://github.com/camdencheek/tree-sitter-go-mod")
  1920. (python "https://github.com/tree-sitter/tree-sitter-python")
  1921. ))
  1922. ;; treesit-install-language-grammar
  1923. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1924. (with-eval-after-load 'uniquify
  1925. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1926. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1927. (setq uniquify-min-dir-content 1))
  1928. (with-eval-after-load 'view
  1929. (defvar view-mode-map (make-sparse-keymap))
  1930. (define-key view-mode-map "j" 'scroll-up-line)
  1931. (define-key view-mode-map "k" 'scroll-down-line)
  1932. (define-key view-mode-map "v" 'toggle-read-only)
  1933. (define-key view-mode-map "q" 'bury-buffer)
  1934. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1935. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1936. ;; (define-key view-mode-map
  1937. ;; "n" 'nonincremental-repeat-search-forward)
  1938. ;; (define-key view-mode-map
  1939. ;; "N" 'nonincremental-repeat-search-backward)
  1940. ;; N conflicts with git-walktree
  1941. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1942. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1943. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1944. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1945. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1946. (global-set-key "\M-r" 'view-mode)
  1947. ;; (setq view-read-only t)
  1948. (with-eval-after-load 'term
  1949. (defvar term-raw-map (make-sparse-keymap))
  1950. (define-key term-raw-map (kbd "C-x")
  1951. (lookup-key (current-global-map)
  1952. (kbd "C-x"))))
  1953. (add-hook 'term-mode-hook
  1954. (lambda ()
  1955. ;; Stop current line highlighting
  1956. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1957. (set-variable 'scroll-margin 0 t)
  1958. ))
  1959. (set-variable 'term-buffer-maximum-size 20480)
  1960. (set-variable 'term-suppress-hard-newline t)
  1961. (add-hook 'Man-mode-hook
  1962. (lambda ()
  1963. (view-mode 1)
  1964. (setq truncate-lines nil)))
  1965. (set-variable 'Man-notify-method (if window-system
  1966. 'newframe
  1967. 'aggressive))
  1968. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1969. "woman_cache.el")))
  1970. ;; not work because man.el will be loaded when man called
  1971. (defalias 'man 'woman)
  1972. (add-to-list 'auto-mode-alist
  1973. '("/tox\\.ini\\'" . conf-unix-mode))
  1974. (add-to-list 'auto-mode-alist
  1975. '("/setup\\.cfg\\'" . conf-unix-mode))
  1976. (when (fboundp 'toml-mode)
  1977. (add-to-list 'auto-mode-alist
  1978. '("/tox\\.ini\\'" . toml-mode))
  1979. (add-to-list 'auto-mode-alist
  1980. '("/setup\\.cfg\\'" . toml-mode))
  1981. (add-to-list 'auto-mode-alist
  1982. '("/Pipfile\\'" . toml-mode))
  1983. (add-to-list 'auto-mode-alist
  1984. '("/poetry\\.lock\\'" . toml-mode))
  1985. )
  1986. (when (fboundp 'json-mode)
  1987. (add-to-list 'auto-mode-alist
  1988. '("/Pipfile\\.lock\\'" . json-mode)))
  1989. (add-hook 'json-mode-hook
  1990. (lambda ()
  1991. ;; Stop current line highlighting
  1992. (set-variable 'js-indent-level 2 t)
  1993. ))
  1994. (add-hook 'go-mode-hook
  1995. (lambda()
  1996. (defvar go-mode-map)
  1997. (add-hook 'before-save-hook 'gofmt-before-save nil t)
  1998. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1999. (when (fboundp 'k8s-mode)
  2000. (add-to-list 'auto-mode-alist
  2001. '("\\.k8s\\'" . k8s-mode)))
  2002. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2003. ;; buffers
  2004. (defvar bs-configurations)
  2005. (declare-function bs-set-configuration "bs")
  2006. (declare-function bs-refresh "bs")
  2007. (declare-function bs-message-without-log "bs")
  2008. (declare-function bs--current-config-message "bs")
  2009. (with-eval-after-load 'bs
  2010. (add-to-list 'bs-configurations
  2011. '("specials" "^\\*" nil ".*" nil nil))
  2012. (add-to-list 'bs-configurations
  2013. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  2014. (defvar bs-mode-map)
  2015. (defvar bs-current-configuration)
  2016. (define-key bs-mode-map (kbd "t")
  2017. ;; TODO: fix toggle feature
  2018. (lambda ()
  2019. (interactive)
  2020. (if (string= "specials"
  2021. bs-current-configuration)
  2022. (bs-set-configuration "files")
  2023. (bs-set-configuration "specials"))
  2024. (bs-refresh)
  2025. (bs-message-without-log "%s"
  2026. (bs--current-config-message))))
  2027. ;; (setq bs-configurations (list
  2028. ;; '("processes" nil get-buffer-process ".*" nil nil)
  2029. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  2030. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  2031. )
  2032. (when (fboundp 'bs-show)
  2033. (defalias 'list-buffers 'bs-show)
  2034. (set-variable 'bs-default-configuration "files-and-specials")
  2035. (set-variable 'bs-default-sort-name "by nothing")
  2036. (add-hook 'bs-mode-hook
  2037. (lambda ()
  2038. (setq-local scroll-margin 0)
  2039. (setq-local header-line-format nil)
  2040. (setq-local mode-line-format nil)
  2041. )))
  2042. ;;(iswitchb-mode 1)
  2043. (icomplete-mode)
  2044. (defun iswitchb-buffer-display-other-window ()
  2045. "Do iswitchb in other window."
  2046. (interactive)
  2047. (let ((iswitchb-default-method 'display))
  2048. (call-interactively 'iswitchb-buffer)))
  2049. ;; buffer killing
  2050. ;; (defun my-delete-window-killing-buffer () nil)
  2051. (defun my-query-kill-current-buffer ()
  2052. "Interactively kill current buffer."
  2053. (interactive)
  2054. (if (y-or-n-p (concat "kill current buffer? :"))
  2055. (kill-buffer (current-buffer))))
  2056. (defun my-force-query-kill-current-buffer ()
  2057. "Interactively kill current buffer."
  2058. (interactive)
  2059. (when (y-or-n-p (concat "kill current buffer? :"))
  2060. (let ((kill-buffer-hook nil)
  2061. (kill-buffer-query-functions nil))
  2062. (kill-buffer (current-buffer)))))
  2063. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  2064. ;; Originally C-x C-k -> kmacro-keymap
  2065. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  2066. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  2067. (substitute-key-definition 'kill-buffer
  2068. 'my-query-kill-current-buffer
  2069. global-map)
  2070. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2071. ;; dired
  2072. (defun dired-default-directory ()
  2073. "Open dired `default-directory'."
  2074. (interactive)
  2075. (pop-to-buffer (dired-noselect default-directory)))
  2076. (define-key ctl-x-map "D" 'dired-default-directory)
  2077. (defun my-file-head (filename &optional n)
  2078. "Return list of first N lines of file FILENAME."
  2079. ;; TODO: Fix for janapese text
  2080. ;; TODO: Fix for short text
  2081. (let ((num (or n 10))
  2082. (size 100)
  2083. (beg 0)
  2084. (end 0)
  2085. (result '())
  2086. (read -1))
  2087. (with-temp-buffer
  2088. (erase-buffer)
  2089. (while (or (<= (count-lines (point-min)
  2090. (point-max))
  2091. num)
  2092. (eq read 0))
  2093. (setq end (+ beg size))
  2094. (setq read (nth 1 (insert-file-contents-literally filename
  2095. nil
  2096. beg
  2097. end)))
  2098. (goto-char (point-max))
  2099. (setq beg (+ beg size)))
  2100. (goto-char (point-min))
  2101. (while (< (length result) num)
  2102. (let ((start (point)))
  2103. (forward-line 1)
  2104. (setq result
  2105. `(,@result ,(buffer-substring-no-properties start
  2106. (point))))))
  2107. result
  2108. ;; (buffer-substring-no-properties (point-min)
  2109. ;; (progn
  2110. ;; (forward-line num)
  2111. ;; (point)))
  2112. )))
  2113. ;; (apply 'concat (my-file-head "./shrc" 10)
  2114. (declare-function dired-get-filename "dired" t)
  2115. (defun my-dired-echo-file-head (arg)
  2116. "Echo head of current file.
  2117. ARG is num to show, or defaults to 7."
  2118. (interactive "P")
  2119. (let ((f (dired-get-filename)))
  2120. (message "%s"
  2121. (apply 'concat
  2122. (my-file-head f
  2123. 7)))))
  2124. (declare-function dired-get-marked-files "dired")
  2125. (defun my-dired-diff ()
  2126. "Show diff of marked file and file of current line."
  2127. (interactive)
  2128. (let ((files (dired-get-marked-files nil nil nil t)))
  2129. (if (eq (car files)
  2130. t)
  2131. (diff (cadr files) (dired-get-filename))
  2132. (message "One file must be marked!"))))
  2133. (defun dired-get-file-info ()
  2134. "Print information of current line file."
  2135. (interactive)
  2136. (let* ((file (dired-get-filename t))
  2137. (quoted (shell-quote-argument file)))
  2138. (if (file-directory-p file)
  2139. (progn
  2140. (message "Calculating disk usage...")
  2141. (let ((du (or (executable-find "gdu")
  2142. (executable-find "du")
  2143. (error "du not found"))))
  2144. (shell-command (concat du
  2145. " -hsD "
  2146. quoted))))
  2147. (shell-command (concat "file "
  2148. quoted)))))
  2149. (defun my-dired-scroll-up ()
  2150. "Scroll up."
  2151. (interactive)
  2152. (my-dired-previous-line (- (window-height) 1)))
  2153. (defun my-dired-scroll-down ()
  2154. "Scroll down."
  2155. (interactive)
  2156. (my-dired-next-line (- (window-height) 1)))
  2157. ;; (defun my-dired-forward-line (arg)
  2158. ;; ""
  2159. ;; (interactive "p"))
  2160. (declare-function dired-get-subdir "dired")
  2161. (declare-function dired-move-to-filename "dired")
  2162. (defun my-dired-previous-line (arg)
  2163. "Move ARG lines up."
  2164. (interactive "p")
  2165. (if (> arg 0)
  2166. (progn
  2167. (if (eq (line-number-at-pos)
  2168. 1)
  2169. (goto-char (point-max))
  2170. (forward-line -1))
  2171. (my-dired-previous-line (if (or (dired-get-filename nil t)
  2172. (dired-get-subdir))
  2173. (- arg 1)
  2174. arg)))
  2175. (dired-move-to-filename)))
  2176. (defun my-dired-next-line (arg)
  2177. "Move ARG lines down."
  2178. (interactive "p")
  2179. (if (> arg 0)
  2180. (progn
  2181. (if (eq (point)
  2182. (point-max))
  2183. (goto-char (point-min))
  2184. (forward-line 1))
  2185. (my-dired-next-line (if (or (dired-get-filename nil t)
  2186. (dired-get-subdir))
  2187. (- arg 1)
  2188. arg)))
  2189. (dired-move-to-filename)))
  2190. (defun my-tramp-remote-find-file (f)
  2191. "Open F."
  2192. (interactive (list (read-file-name "My Find File Tramp: "
  2193. "/scp:"
  2194. nil ;; "/scp:"
  2195. (confirm-nonexistent-file-or-buffer))))
  2196. (find-file f))
  2197. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  2198. (if (eq window-system 'mac)
  2199. (setq dired-listing-switches "-lhFA")
  2200. (setq dired-listing-switches "-lhFA --time-style=long-iso")
  2201. )
  2202. (setq dired-listing-switches "-lhFA")
  2203. ;; when using dired-find-alternate-file
  2204. ;; reuse current dired buffer for the file to open
  2205. ;; (put 'dired-find-alternate-file 'disabled nil)
  2206. (set-variable 'dired-ls-F-marks-symlinks t)
  2207. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  2208. (set-variable 'ls-lisp-dirs-first t)
  2209. (set-variable 'ls-lisp-use-localized-time-format t)
  2210. (set-variable 'ls-lisp-format-time-list
  2211. '("%Y-%m-%d %H:%M"
  2212. "%Y-%m-%d "))
  2213. (set-variable 'dired-dwim-target t)
  2214. (set-variable 'dired-isearch-filenames t)
  2215. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  2216. (set-variable 'dired-hide-details-hide-information-lines nil)
  2217. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  2218. (set-variable 'dired-recursive-deletes 'always)
  2219. ;; (add-hook 'dired-after-readin-hook
  2220. ;; 'my-replace-nasi-none)
  2221. (with-eval-after-load 'dired
  2222. (require 'ls-lisp nil t)
  2223. (defvar dired-mode-map (make-sparse-keymap))
  2224. ;; dired-do-chgrp sometimes cause system hung
  2225. (define-key dired-mode-map "G" 'ignore)
  2226. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  2227. (define-key dired-mode-map "i" 'dired-get-file-info)
  2228. ;; (define-key dired-mode-map "f" 'find-file)
  2229. (define-key dired-mode-map "f" 'my-fuzzy-finder-or-find-file)
  2230. (define-key dired-mode-map "!" 'shell-command)
  2231. (define-key dired-mode-map "&" 'async-shell-command)
  2232. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  2233. (define-key dired-mode-map "=" 'my-dired-diff)
  2234. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  2235. (define-key dired-mode-map "b" 'gtkbm)
  2236. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  2237. (define-key dired-mode-map (kbd "TAB") 'other-window)
  2238. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  2239. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  2240. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  2241. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  2242. (substitute-key-definition 'dired-next-line
  2243. 'my-dired-next-line
  2244. dired-mode-map)
  2245. (substitute-key-definition 'dired-previous-line
  2246. 'my-dired-previous-line
  2247. dired-mode-map)
  2248. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  2249. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  2250. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  2251. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  2252. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  2253. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  2254. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  2255. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  2256. (add-hook 'dired-mode-hook
  2257. (lambda ()
  2258. (when (fboundp 'dired-hide-details-mode)
  2259. (dired-hide-details-mode t)
  2260. (local-set-key "l" 'dired-hide-details-mode))
  2261. (when (fboundp 'dired-omit-mode)
  2262. (dired-omit-mode 1)
  2263. (local-set-key "a" 'dired-omit-mode))
  2264. (let ((file "._Icon\015"))
  2265. (when nil
  2266. '(file-readable-p file)
  2267. (delete-file file)))))
  2268. (when (fboundp 'pack-dired-dwim)
  2269. (with-eval-after-load 'dired
  2270. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  2271. ;; https://emacs.stackexchange.com/questions/68585/dired-mode-toggle-show-hidden-files-folders-by-keyboard-shortcut
  2272. (set-variable 'dired-omit-files
  2273. (rx (or (regexp "\\`[.]?#\\|\\`[.][.]?\\'")
  2274. (seq bos "." (not (any "."))))))
  2275. ;; (string-match-p dired-omit-files ".abc")
  2276. (when (fboundp 'dired-omit-mode)
  2277. (with-eval-after-load 'dired
  2278. ))
  2279. )
  2280. (when (fboundp 'dired-filter-mode)
  2281. (add-hook 'dired-mode-hook
  2282. 'dired-filter-mode))
  2283. (set-variable 'dired-filter-stack nil)
  2284. ;; Currently disabled in favor of dired-from-git-ls-files
  2285. ;; (define-key ctl-x-map "f" 'find-dired)
  2286. (defvar my-dired-git-ls-files-history
  2287. "History for `my-dired-git-ls-files'." nil)
  2288. (defun my-dired-git-ls-files (arg)
  2289. "Dired from git ls-files."
  2290. (interactive (list
  2291. (read-shell-command "git ls-files: "
  2292. "git ls-files -z ")))
  2293. (pop-to-buffer-same-window
  2294. (dired-noselect `(,default-directory
  2295. ,@(split-string (shell-command-to-string arg)
  2296. "\0" t))
  2297. ""))
  2298. )
  2299. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  2300. (with-eval-after-load 'dired
  2301. (defvar dired-mode-map (make-sparse-keymap))
  2302. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  2303. (with-eval-after-load 'pack
  2304. (set-variable 'pack-silence
  2305. t)
  2306. (defvar pack-program-alist)
  2307. (add-to-list 'pack-program-alist
  2308. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  2309. (when (executable-find "aunpack")
  2310. (add-to-list 'pack-program-alist
  2311. ' ("\\.zip\\'"
  2312. :pack ("zip" "-r" archive sources)
  2313. :pack-append ("zip" "-r" archive sources)
  2314. :unpack ("aunpack" archive))))
  2315. )
  2316. ;; dired-k
  2317. (declare-function dired-k-no-revert "dired-k")
  2318. (when (fboundp 'dired-k)
  2319. (set-variable 'dired-k-style 'git)
  2320. ;; What is the best way of doing this?
  2321. (with-eval-after-load 'dired-k
  2322. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2323. ;; (set-variable 'dired-k-size-colors
  2324. ;; `((,most-positive-fixnum)))
  2325. ;; (set-variable 'dired-k-date-colors
  2326. ;; `((,most-positive-fixnum)))
  2327. (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2328. )
  2329. ;; (when (eval-and-compile (require 'dired-rainbow nil t))
  2330. ;; (dired-rainbow-define gtags "brightblack" "GTAGS"))
  2331. (with-eval-after-load 'diredfl
  2332. (set-face-foreground 'diredfl-file-name nil)
  2333. )
  2334. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2335. ;; misc funcs
  2336. (define-key ctl-x-map "T" 'git-worktree)
  2337. (define-key ctl-x-map "W" 'git-walktree)
  2338. (defun mkcdd ()
  2339. "Make date directory and open it with dired."
  2340. (interactive)
  2341. (let ((d (format-time-string "%Y%m%d-%H%M%S")))
  2342. (make-directory d)
  2343. (find-file d)))
  2344. (when (fboundp 'browse-url-default-macosx-browser)
  2345. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  2346. (defalias 'qcalc 'quick-calc)
  2347. (defun memo (&optional dir)
  2348. "Open memo.txt in DIR."
  2349. (interactive)
  2350. (pop-to-buffer (find-file-noselect (expand-file-name "memo.txt"
  2351. (or dir
  2352. default-directory)))))
  2353. ;; TODO: remember-projectile
  2354. (set (defvar my-privnotes-path nil
  2355. "My privnotes repository path.")
  2356. (expand-file-name "~/my/privnotes"))
  2357. (defun my-privnotes-readme (dir)
  2358. "Open my privnotes DIR."
  2359. (interactive (list
  2360. (read-file-name "Privnotes: "
  2361. (expand-file-name (format-time-string "%Y%m%d_")
  2362. my-privnotes-path))))
  2363. (let ((path (expand-file-name "README.md" dir)))
  2364. (with-current-buffer (find-file path)
  2365. (unless (file-exists-p path)
  2366. (insert (file-name-base dir)
  2367. "\n"
  2368. "=======\n"
  2369. "\n\n")))))
  2370. (define-key ctl-x-map "p" 'my-privnotes-readme)
  2371. (set (defvar my-rgrep-alist nil
  2372. "Alist of rgrep command.
  2373. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2374. condition to choose COMMAND when evaluated.")
  2375. `(
  2376. ;; ripgrep
  2377. ("rg"
  2378. (executable-find "rg")
  2379. "rg -nH --no-heading --color=never --hidden --no-ignore-vcs --glob '!.git/' --smart-case -M 1280 ")
  2380. ;; git grep
  2381. ("gitgrep"
  2382. (eq 0
  2383. (shell-command "git rev-parse --git-dir"))
  2384. "git --no-pager grep -nH --color=never --ignore-case -e ")
  2385. ;; sift
  2386. ("sift"
  2387. (executable-find "sift")
  2388. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2389. ;; the silver searcher
  2390. ("ag"
  2391. (executable-find "ag")
  2392. "ag --nogroup --nopager --filename ")
  2393. ;; ack
  2394. ("ack"
  2395. (executable-find "ack")
  2396. "ack --nogroup --nopager --with-filename ")
  2397. ;; gnu global
  2398. ("global"
  2399. (and (require 'ggtags nil t)
  2400. (executable-find "global")
  2401. (ggtags-current-project-root))
  2402. "global --result grep ")
  2403. ;; grep
  2404. ("grep"
  2405. t
  2406. ,(concat "find . "
  2407. "-path '*/.git' -prune -o "
  2408. "-path '*/.svn' -prune -o "
  2409. "-type f -print0 | "
  2410. "xargs -0 grep -nH -e "))
  2411. )
  2412. )
  2413. (defvar my-rgrep-default nil
  2414. "Default command name for my-rgrep.")
  2415. (defun my-rgrep-grep-command (&optional name alist)
  2416. "Return recursive grep command for current directory or nil.
  2417. If NAME is given, use that without testing.
  2418. Commands are searched from ALIST."
  2419. (if alist
  2420. (if name
  2421. ;; if name is given search that from alist and return the command
  2422. (nth 2 (assoc name
  2423. alist))
  2424. ;; if name is not given try test in 1th elem
  2425. (let ((car (car alist))
  2426. (cdr (cdr alist)))
  2427. (if (eval (nth 1 car))
  2428. ;; if the condition is true return the command
  2429. (nth 2 car)
  2430. ;; try next one
  2431. (and cdr
  2432. (my-rgrep-grep-command name cdr)))))
  2433. ;; if alist is not given set default value
  2434. (my-rgrep-grep-command name my-rgrep-alist)))
  2435. (declare-function projectile-project-p "projectile")
  2436. (declare-function projectile-with-default-dir "projectile")
  2437. (declare-function projectile-project-root "projectile")
  2438. (defun my-rgrep (command-args)
  2439. "My recursive grep. Run COMMAND-ARGS.
  2440. If prefix argument is given, use current symbol as default search target
  2441. and search from projectile root (if projectile is available)."
  2442. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2443. nil)))
  2444. (if cmd
  2445. (list (read-shell-command "grep command: "
  2446. (concat cmd
  2447. (if current-prefix-arg
  2448. (thing-at-point 'symbol t)
  2449. ""))
  2450. 'grep-find-history))
  2451. (error "My-Rgrep: Command for rgrep not found")
  2452. )))
  2453. (if (and current-prefix-arg
  2454. (eval-and-compile (require 'projectile nil t))
  2455. (projectile-project-p))
  2456. (projectile-with-default-dir (projectile-project-root)
  2457. (compilation-start command-args
  2458. 'grep-mode))
  2459. (compilation-start command-args
  2460. 'grep-mode)))
  2461. (defun my-rgrep-thing-at-point-projectile-root ()
  2462. "My recursive grep to find thing at point from project root."
  2463. (interactive)
  2464. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2465. nil))
  2466. (command-args
  2467. (if cmd
  2468. (concat cmd
  2469. (or (thing-at-point 'symbol t)
  2470. (error "No symbol at point")))
  2471. (error "My-Rgrep: Command for rgrep not found"))))
  2472. (if (eval-and-compile (require 'projectile nil t))
  2473. (with-temp-buffer
  2474. (cd (or (projectile-project-root)
  2475. default-directory))
  2476. (compilation-start command-args
  2477. 'grep-mode))
  2478. (compilation-start command-args
  2479. 'grep-mode))))
  2480. (defmacro define-my-rgrep (name)
  2481. "Define rgrep for NAME."
  2482. `(defun ,(intern (concat "my-rgrep-"
  2483. name)) ()
  2484. ,(format "My recursive grep by %s."
  2485. name)
  2486. (interactive)
  2487. (let ((my-rgrep-default ,name))
  2488. (if (called-interactively-p 'any)
  2489. (call-interactively 'my-rgrep)
  2490. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2491. )
  2492. (define-my-rgrep "ack")
  2493. (define-my-rgrep "ag")
  2494. (define-my-rgrep "rg")
  2495. (define-my-rgrep "sift")
  2496. (define-my-rgrep "gitgrep")
  2497. (define-my-rgrep "grep")
  2498. (define-my-rgrep "global")
  2499. (define-key ctl-x-map "s" 'my-rgrep)
  2500. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2501. (defun my-occur (regexp &optional region)
  2502. "My occur command to search REGEXP to search REGION."
  2503. (interactive (list (read-string "List lines matching regexp: "
  2504. (thing-at-point 'symbol t))))
  2505. (occur regexp nil region))
  2506. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2507. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2508. (defalias 'make 'compile)
  2509. (define-key ctl-x-map "c" 'compile)
  2510. (autoload 'pb/push-item "pushbullet")
  2511. (defun my-pushbullet-note (text &optional title)
  2512. "Push TEXT with optional TITLE."
  2513. (interactive "sText to Push: ")
  2514. (pb/push-item '("") text "note" (or title "")))
  2515. ;;;;;;;;;;;;;;;;;;;
  2516. ;; peek-file-mode
  2517. (defvar peek-file-buffers
  2518. ()
  2519. "Peek buffers.")
  2520. (defun peek-file (file)
  2521. "Peek FILE."
  2522. (interactive "fFile to peek: ")
  2523. (with-current-buffer (find-file file)
  2524. (peek-file-mode)))
  2525. (define-minor-mode peek-file-mode
  2526. "Peek file mode."
  2527. :lighter "PK"
  2528. (view-mode peek-file-mode)
  2529. (add-to-list 'peek-file-buffers
  2530. (current-buffer))
  2531. (add-hook 'switch-buffer-functions
  2532. 'peek-file-remove-buffers))
  2533. (defun peek-file-remove-buffers (&args _)
  2534. "Remove peek file buffers."
  2535. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2536. (unless (get-buffer-window buf t)
  2537. (setq peek-file-buffers
  2538. (delq buf
  2539. peek-file-buffers))
  2540. (with-current-buffer buf
  2541. (when peek-file-mode
  2542. (kill-buffer))))))
  2543. (declare-function dired-get-file-for-visit "dired")
  2544. (with-eval-after-load 'dired
  2545. (defun dired-peek-file (&rest files)
  2546. "Dired `peak-file' FILES."
  2547. (interactive (list (dired-get-file-for-visit)))
  2548. (message "AAA %S" files)
  2549. (dolist (file files)
  2550. (peek-file file)))
  2551. (defvar dired-mode-map (make-sparse-keymap))
  2552. (define-key dired-mode-map "v" 'dired-peek-file))
  2553. ;;;;;;;;;;;;;;;;;;;;
  2554. ;; remember-projectile
  2555. ;; TODO: Add global-minor-mode
  2556. (defvar remember-data-file)
  2557. (defun my-set-remember-data-file-buffer-local ()
  2558. "Set `remember-data-file'."
  2559. (when (require 'projectile nil t)
  2560. (setq-local remember-data-file
  2561. (expand-file-name "remember.notes"
  2562. (projectile-project-root)))))
  2563. (add-hook 'after-change-major-mode-hook
  2564. 'my-set-remember-data-file-buffer-local)
  2565. (define-key ctl-x-map "R" 'remember)
  2566. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2567. ;; ivy
  2568. (set-variable 'ivy-format-functions-alist
  2569. '((t . (lambda (cands) (ivy--format-function-generic
  2570. (lambda (str)
  2571. (concat "+> "
  2572. (ivy--add-face str 'ivy-current-match)
  2573. ))
  2574. (lambda (str)
  2575. (concat "| " str))
  2576. cands
  2577. "\n")))))
  2578. (set-variable 'ivy-wrap t)
  2579. (set-variable 'ivy-sort-max-size 500)
  2580. (when (fboundp 'ivy-rich-mode)
  2581. (ivy-rich-mode 1))
  2582. (with-eval-after-load 'ivy
  2583. (defvar ivy-minibuffer-map)
  2584. (define-key ivy-minibuffer-map (kbd "C-u")
  2585. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  2586. (defvar ivy-sort-matches-functions-alist)
  2587. ;; (add-to-list 'ivy-sort-matches-functions-alist
  2588. ;; '(counsel-M-x . ivy--shorter-matches-first))
  2589. )
  2590. (set-variable 'ivy-on-del-error-function 'ignore)
  2591. (when (fboundp 'counsel-M-x)
  2592. (define-key esc-map "x" 'counsel-M-x)
  2593. )
  2594. (declare-function ivy-thing-at-point "ivy")
  2595. (when (and (fboundp 'ivy-read)
  2596. (locate-library "counsel"))
  2597. (defvar counsel-describe-map)
  2598. (defun my-counsel-describe-symbol ()
  2599. "Forwaord to `describe-symbol'."
  2600. (interactive)
  2601. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2602. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2603. (ivy-read "Describe symbol: " obarray
  2604. ;; From describe-symbol definition
  2605. :predicate (lambda (vv)
  2606. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2607. describe-symbol-backends))
  2608. :require-match t
  2609. :history 'counsel-describe-symbol-history
  2610. :keymap counsel-describe-map
  2611. :preselect (ivy-thing-at-point)
  2612. :action (lambda (x)
  2613. (describe-symbol (intern x)))
  2614. :caller 'my-counsel-describe-symbol))
  2615. (define-key help-map "o" 'my-counsel-describe-symbol)
  2616. )
  2617. (declare-function ivy-configure "ivy")
  2618. (with-eval-after-load 'counsel ;; Hook to counsel, not ivy
  2619. ;; (ivy-configure 'my-counsel-describe-symbol
  2620. ;; :sort-fn 'my-ivy-length)
  2621. ;; (ivy-configure 'counsel-M-x
  2622. ;; ;; :initial-input ""
  2623. ;; :sort-fn 'ivy-string<
  2624. ;; )
  2625. )
  2626. (when (fboundp 'counsel-imenu)
  2627. (define-key ctl-x-map "l" 'counsel-imenu))
  2628. (when (fboundp 'swiper)
  2629. (define-key esc-map (kbd "C-s") 'swiper))
  2630. (with-eval-after-load 'ivy
  2631. ;; ivy-prescient requires counsel already loaded
  2632. (require 'counsel nil t)
  2633. (when (fboundp 'ivy-prescient-mode)
  2634. ;; (set-variable 'prescient-sort-length-enable t)
  2635. ;; (set-variable 'prescient-sort-full-matches-first t)
  2636. ;; (set-variable 'ivy-prescient-enable-filtering t)
  2637. ;; (set-variable 'ivy-prescient-enable-sorting nil)
  2638. ;; (set-variable 'ivy-prescient-sort-commands t)
  2639. (set-variable 'prescient-filter-method
  2640. '(literal prefix literal-prefix regexp initialism fuzzy))
  2641. (when (fboundp 'prescient-persist-mode)
  2642. (prescient-persist-mode t))
  2643. (ivy-prescient-mode 1)
  2644. ;; (set-variable 'ivy-sort-functions-alist
  2645. ;; '((t . ivy-string<)))
  2646. ))
  2647. ;; ?
  2648. (define-key input-decode-map "\e[1;5C" [C-right])
  2649. (define-key input-decode-map "\e[1;5D" [C-left])
  2650. ;;;;;;;;;;;;;;;;;;;;;;;;
  2651. ;; mozc
  2652. (global-set-key (kbd "C-c m e") 'ignore)
  2653. (global-set-key (kbd "C-c m d") 'ignore)
  2654. ;; mozc
  2655. (when (locate-library "mozc")
  2656. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2657. (with-eval-after-load 'mozc
  2658. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2659. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2660. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2661. )
  2662. (setq default-input-method "japanese-mozc")
  2663. (custom-set-variables '(mozc-leim-title "あ"))
  2664. (defun turn-on-input-method ()
  2665. (interactive)
  2666. (activate-input-method default-input-method))
  2667. (defun turn-off-input-method ()
  2668. (interactive)
  2669. (deactivate-input-method))
  2670. ;; (setq mozc-candidate-style 'echo-area)
  2671. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2672. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2673. (global-set-key (kbd "<f7>") 'turn-off-input-method)
  2674. (global-set-key (kbd "<f8>") 'turn-on-input-method)
  2675. (require 'mozc-popup)
  2676. (set-variable 'mozc-candidate-style 'popup)
  2677. ;; これいる?
  2678. (when (require 'mozc-im nil t)
  2679. (setq default-input-method "japanese-mozc-im")
  2680. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2681. )
  2682. )
  2683. (defun browse-url-macosx-vivaldi-browser (url &rest args)
  2684. "Invoke the macOS Vlvaldi Web browser with URL.
  2685. ARGS are not used."
  2686. (interactive (browse-url-interactive-arg "URL: "))
  2687. (start-process (concat "vivaldi " url)
  2688. nil
  2689. "/Applications/Vivaldi.app/Contents/MacOS/Vivaldi"
  2690. url))
  2691. (declare-function vterm "vterm")
  2692. ;; 前の実行結果を残したまま次のコマンドを実行する方法はあるだろうか
  2693. (defun my-vterm-cmd (command)
  2694. "Start arbitrary command in vterm buffer."
  2695. (interactive "sCommand: ")
  2696. (let ((vterm-shell command)
  2697. (vterm-buffer-name "*vterm-cmd*")
  2698. (vterm-kill-buffer-on-exit nil))
  2699. (when (get-buffer vterm-buffer-name)
  2700. (kill-buffer (get-buffer vterm-buffer-name)))
  2701. (vterm)))
  2702. ;; (setq vterm-shell "bash -l")
  2703. ;; (setq vterm-kill-buffer-on-exit nil)
  2704. ;; ;; (setq vterm-term-environment-variable "screen-256color")
  2705. ;; これいつ動くの?
  2706. ;; 自動で project を switch させる方法はある?
  2707. (add-hook 'projectile-after-switch-project-hook
  2708. (lambda ()
  2709. (message "Projecttile switched to: %s"
  2710. (projectile-project-root))))
  2711. (when (fboundp 'projectile-mode)
  2712. (projectile-mode 1))
  2713. ;; (with-eval-after-load 'eglot
  2714. ;; (when (fboundp 'with-venv-advice-add)
  2715. ;; (with-venv-advice-add 'eglot--executable-find))
  2716. ;; (set-variable 'eldoc-echo-area-use-multiline-p nil)
  2717. ;; (set-variable 'eglot-extend-to-xref t))
  2718. (set-variable 'lsp-python-ms-auto-install-server t)
  2719. (set-variable 'lsp-python-ms-parse-dot-env-enabled t)
  2720. (set-variable 'lsp-python-ms-python-executable-cmd "python3")
  2721. ;; (add-hook 'python-mode-hook #'my-lsp-python-setup)
  2722. (defun my-lsp-python-setup ()
  2723. "Setup python ms."
  2724. (when (and (fboundp 'lsp)
  2725. (require 'lsp-python-ms nil t))
  2726. (lsp)))
  2727. (set-variable 'awk-preview-default-program
  2728. "# C-c C-l: Update preview C-c C-c: Commit and exit
  2729. # C-c C-r: Resest to original C-c C-k: Abort
  2730. BEGIN {
  2731. # FS = \",\"
  2732. }
  2733. {
  2734. # Replace string
  2735. # gsub(BEFORE, AFTER, $0)
  2736. print NR, $0
  2737. }
  2738. ")
  2739. (defun my-git-info-exclude ()
  2740. "Open .git/info/exlucde file."
  2741. (interactive)
  2742. (if-let* ((dir (locate-dominating-file default-directory
  2743. ".git/info/exclude")))
  2744. (find-file (expand-file-name ".git/info/exclude"
  2745. dir))
  2746. (error "No .git/info/exclude file found")))
  2747. '(progn
  2748. ;; https://web.sfc.wide.ad.jp/~sagawa/gnujdoc/elisp-manual-20-2.5/elisp-ja_39.html#SEC629
  2749. ;; https://emacs.stackexchange.com/questions/15078/inserting-before-an-after-string-overlay
  2750. ;; https://emacs.stackexchange.com/questions/3030/temporary-text-in-window-location-with-no-text-to-propertize-overlay
  2751. ;; https://ayatakesi.github.io/lispref/26.3/html/Overlay-Properties.html
  2752. ;; https://ayatakesi.github.io/lispref/26.3/html/Special-Properties.html#Special-Properties
  2753. ;; https://ayatakesi.github.io/lispref/28.1/html/Attribute-Functions.html
  2754. ;; https://ayatakesi.github.io/lispref/26.3/html/Display-Margins.html#Display-Margins
  2755. (setq my-ov (make-overlay (pos-bol) (pos-eol)))
  2756. (defface my-ov-face () "Face for my-ov.")
  2757. (set-face-attribute 'my-ov-face
  2758. nil
  2759. :background nil)
  2760. (set-face-attribute 'my-ov-face
  2761. nil
  2762. :foreground "yellow")
  2763. (let* ((ov my-ov)
  2764. (s (propertize "<"
  2765. 'face
  2766. 'my-ov-face))
  2767. (s (propertize " "
  2768. 'display
  2769. `((margin right-margin) ,s))))
  2770. (overlay-put ov 'after-string s)
  2771. ;; (overlay-put ov 'after-string
  2772. ;; (concat (propertize " " 'display
  2773. ;; '(space :align-to (+ left-fringe 1)))
  2774. ;; (propertize "*" 'display
  2775. ;; '(raise -1))
  2776. ;; ))
  2777. ;; s
  2778. ;; (setq left-margin-width 1)
  2779. ;; (setq right-margin-width 1)
  2780. ;; (set-window-buffer (get-buffer-window) (current-buffer))
  2781. ;; (window-margins (get-buffer-window))
  2782. (let ((win (get-buffer-window)))
  2783. (set-window-margins (get-buffer-window)
  2784. (car (window-margins win))
  2785. 1))
  2786. )
  2787. (progn
  2788. (overlay-put my-ov 'after-string nil)
  2789. (overlay-put my-ov 'before-string nil)
  2790. )
  2791. )
  2792. (message "Emacs started at %s"
  2793. (current-time-string))
  2794. (run-with-idle-timer (* 3 60 60) ;; 3 hours
  2795. t
  2796. (lambda ()
  2797. (message "Emacs does nothing for 3 hours: %s"
  2798. (current-time-string))))
  2799. ;; https://emacs-jp.github.io/tips/startup-optimization
  2800. ;; Restore to original value
  2801. (setq gc-cons-threshold my-orig-gc-cons-threshold)
  2802. (setq file-name-handler-alist my-orig-file-name-handler-alist)
  2803. (when (getenv "_EMACS_EL_PROFILE")
  2804. (profiler-report)
  2805. (profiler-stop))
  2806. ;; Local Variables:
  2807. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2808. ;; flycheck-checker: emacs-lisp
  2809. ;; End:
  2810. ;;; emancs.el ends here