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

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