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.
 
 
 
 
 

3181 lines
106 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" (expand-file-name invocation-name invocation-directory) emacs-version)
  198. (message "Current 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. (progn
  1018. (set-variable 'fuzzy-finder-executable "fzf")
  1019. (set-variable 'fuzzy-finder-default-arguments
  1020. (concat "--ansi "
  1021. "--color='bg+:-1' "
  1022. "--inline-info "
  1023. "--cycle "
  1024. "--reverse "
  1025. "--multi "
  1026. "--print0 "
  1027. "--prompt=\"[`pwd`]FZF: \" "))
  1028. (set-variable 'fuzzy-finder-default-output-delimiter
  1029. "\0"))
  1030. ;; I like fzf because it has --cycle option
  1031. ;; (when (executable-find "sk") ;; skim
  1032. ;; (set-variable 'fuzzy-finder-executable "sk")
  1033. ;; (set-variable 'fuzzy-finder-default-arguments "--ansi --inline-info --cycle --multi --reverse --print0 --prompt=\"[`pwd`]SK: \" ")
  1034. ;; (set-variable 'fuzzy-finder-default-output-delimiter "\0")
  1035. ;; )
  1036. (set-variable 'fuzzy-finder-default-input-command
  1037. (let ((find (or (executable-find "bfs") ;; Breadth-first find https://github.com/tavianator/bfs
  1038. ;; Use gfind if available?
  1039. "find"))
  1040. (fd (or (executable-find "fdfind")
  1041. (executable-find "fd"))))
  1042. (if nil ;; fd
  1043. (concat "set -eu; set -o pipefail; "
  1044. "echo .; "
  1045. "echo ..; "
  1046. "command " fd " "
  1047. "--follow --hidden --no-ignore "
  1048. "--color always "
  1049. "2>/dev/null")
  1050. (concat "set -eu; set -o pipefail; "
  1051. "echo .; "
  1052. "echo ..; "
  1053. "command " find " -L . "
  1054. "-mindepth 1 "
  1055. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  1056. "-o -print "
  1057. "2> /dev/null "
  1058. "| "
  1059. "cut -b3-"))))
  1060. (declare-function fuzzy-finder
  1061. "fuzzy-finder")
  1062. (declare-function fuzzy-finder-find-files-projectile
  1063. "fuzzy-finder")
  1064. (defun my-fuzzy-finder-or-find-file ()
  1065. "Call `fuzzy-finder' if usable or call `find-file'."
  1066. (declare (interactive-only t))
  1067. (interactive)
  1068. (if (and (executable-find fuzzy-finder-executable)
  1069. (fboundp 'fuzzy-finder)
  1070. (not (file-remote-p default-directory)))
  1071. (fuzzy-finder-find-files-projectile)
  1072. (call-interactively 'find-file)))
  1073. (define-key ctl-x-map "f" 'my-fuzzy-finder-or-find-file)
  1074. (declare-function fuzzy-finder-action-find-files-goto-line
  1075. "fuzzy-finder")
  1076. (defun my-fuzzy-finder-ripgrep-lines ()
  1077. "Fzf all lines."
  1078. (interactive)
  1079. (unless (executable-find "rg")
  1080. (error "rg not found"))
  1081. (fuzzy-finder :input-command "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^"
  1082. :action 'fuzzy-finder-action-find-files-goto-line))
  1083. (define-key ctl-x-map "S" 'my-fuzzy-finder-ripgrep-lines)
  1084. (defun my-fuzzy-finder-dired ()
  1085. "Fuzzy finder directory."
  1086. (interactive)
  1087. (fuzzy-finder :input-command "fd --hidden --no-ignore --type directory"
  1088. :directory (expand-file-name "~")))
  1089. (define-key ctl-x-map "d" 'my-fuzzy-finder-dired)
  1090. ;; (set-variable 'fuzzy-finder-default-command "selecta")
  1091. ;; (set-variable 'fuzzy-finder-default-command "peco")
  1092. ;; (set-variable 'fuzzy-finder-default-command "percol")
  1093. ;; (set-variable 'fuzzy-finder-default-command "fzy")
  1094. ;; (set-variable 'fuzzy-finder-default-command "sk --ansi --no-hscroll --reverse")
  1095. ;; (set-variable 'fuzzy-finder-default-command "pick")
  1096. ;; recently
  1097. ;; TODO: Enable after first visit file?
  1098. (with-eval-after-load 'recently
  1099. (defvar recently-excludes)
  1100. (add-to-list 'recently-excludes
  1101. (rx-to-string (list 'and
  1102. 'string-start
  1103. (expand-file-name package-user-dir))
  1104. t)))
  1105. (when (fboundp 'recently-mode)
  1106. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1107. (set-variable 'recently-max 1000)
  1108. (recently-mode 1))
  1109. (defvar my-cousel-recently-history nil "History of `my-counsel-recently'.")
  1110. (declare-function recently-list "recently" t)
  1111. (when (and (require 'recently nil t)
  1112. (fboundp 'ivy-read))
  1113. (defun my-counsel-recently ()
  1114. "Counsel `recently'."
  1115. (interactive)
  1116. (ivy-read "Recently: " (mapcar 'abbreviate-file-name (recently-list))
  1117. :require-match t
  1118. :history 'my-cousel-recently-history
  1119. :preselect default-directory
  1120. :action (lambda (x) (find-file x))
  1121. :sort nil
  1122. :caller 'my-counsel-recently))
  1123. (define-key ctl-x-map (kbd "C-r") 'my-counsel-recently)
  1124. )
  1125. ;; (when (fboundp 'editorconfig-mode)
  1126. ;; (add-hook 'after-first-visit-hook
  1127. ;; 'editorconfig-mode)
  1128. ;; (add-hook 'after-first-visit-hook
  1129. ;; 'editorconfig-mode-apply
  1130. ;; t)) ;; Do after enabling editorconfig-mode
  1131. (when (eval-and-compile (require 'editorconfig nil t))
  1132. (set-variable 'editorconfig--enable-20210221-testing t)
  1133. (editorconfig-mode 1))
  1134. (set-variable 'editorconfig-get-properties-function
  1135. 'editorconfig-core-get-properties-hash)
  1136. (set-variable 'editorconfig-mode-lighter "")
  1137. (when (fboundp 'ws-butler-mode)
  1138. (set-variable 'editorconfig-trim-whitespaces-mode
  1139. 'ws-butler-mode))
  1140. (with-eval-after-load 'org-src
  1141. ;; [*.org\[\*Org Src*\[ c \]*\]]
  1142. (add-hook 'org-src-mode-hook
  1143. 'editorconfig-mode-apply t))
  1144. (when (fboundp 'editorconfig-custom-majormode)
  1145. (add-hook 'editorconfig-after-apply-functions
  1146. 'editorconfig-custom-majormode))
  1147. ;; Add readonly=true to set read-only-mode
  1148. (add-hook 'editorconfig-after-apply-functions
  1149. (lambda (props)
  1150. (let ((r (gethash 'readonly props)))
  1151. (when (and (string= r "true")
  1152. (not buffer-read-only))
  1153. (read-only-mode 1)))))
  1154. (add-hook 'editorconfig-after-apply-functions
  1155. (lambda (props)
  1156. (when (derived-mode-p 'makefile-mode)
  1157. (setq indent-tabs-mode t))
  1158. (when (derived-mode-p 'diff-mode)
  1159. (editorconfig-set-trailing-ws "false")
  1160. (editorconfig-set-trailing-nl "false")
  1161. )
  1162. ))
  1163. (when (fboundp 'editorconfig-auto-apply-enable)
  1164. (add-hook 'editorconfig-conf-mode-hook
  1165. 'editorconfig-auto-apply-enable))
  1166. ;; (when (fboundp 'editorconfig-charset-extras)
  1167. ;; (add-hook 'editorconfig-custom-hooks
  1168. ;; 'editorconfig-charset-extras))
  1169. (setq revert-without-query '(".+"))
  1170. ;; save cursor position
  1171. (when (fboundp 'save-place-mode)
  1172. (autoload 'save-place-find-file-hook "saveplace")
  1173. (add-hook 'after-first-visit-hook
  1174. 'save-place-mode)
  1175. (add-hook 'after-first-visit-hook
  1176. 'save-place-find-file-hook
  1177. t))
  1178. (set-variable 'save-place-file (concat user-emacs-directory
  1179. "places"))
  1180. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  1181. (setq make-backup-files t)
  1182. (setq vc-make-backup-files t)
  1183. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  1184. (setq backup-directory-alist
  1185. (cons (cons "." (expand-file-name (concat user-emacs-directory
  1186. "backup")))
  1187. backup-directory-alist))
  1188. (setq version-control 't)
  1189. (setq delete-old-versions t)
  1190. (setq kept-new-versions 20)
  1191. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  1192. "auto-save/")))
  1193. ;; (setq delete-auto-save-files t)
  1194. (setq auto-save-visited-interval 8)
  1195. (auto-save-visited-mode 1)
  1196. ;; (add-to-list 'auto-save-file-name-transforms
  1197. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  1198. ;; (setq auto-save-interval 3)
  1199. ;; (auto-save-mode 1)
  1200. (add-to-list 'completion-ignored-extensions ".bak")
  1201. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  1202. (setq delete-by-moving-to-trash t)
  1203. ;; trash-directory "~/.emacs.d/trash")
  1204. (add-hook 'after-save-hook
  1205. 'executable-make-buffer-file-executable-if-script-p)
  1206. (when (fboundp 'smart-revert-on)
  1207. (smart-revert-on))
  1208. ;; autosave
  1209. ;; auto-save-visited-mode can be used instead?
  1210. ;; (when (require 'autosave nil t)
  1211. ;; (autosave-set 8))
  1212. ;; bookmarks
  1213. ;; C-x B: Add bookmark
  1214. ;; C-x b: List bookmarks
  1215. (set-variable 'bookmark-default-file
  1216. (expand-file-name (concat user-emacs-directory
  1217. "bmk")))
  1218. (set-variable 'bookmark-sort-flag nil)
  1219. (defun my-bookmark-set ()
  1220. "My `bookmark-set'."
  1221. (interactive)
  1222. (cl-assert (or buffer-file-name
  1223. default-directory))
  1224. (let ((name (file-name-nondirectory (or buffer-file-name
  1225. (directory-file-name default-directory))))
  1226. (linenum (count-lines (point-min)
  1227. (point)))
  1228. (linetext (buffer-substring-no-properties (point-at-bol)
  1229. (point-at-eol))))
  1230. (bookmark-set (format "%s:%d:%s"
  1231. name linenum linetext)
  1232. nil)))
  1233. ;; Done by advice instead
  1234. ;; (set-variable 'bookmark-save-flag
  1235. ;; 1)
  1236. (with-eval-after-load 'recentf
  1237. (defvar recentf-exclude)
  1238. (defvar bookmark-default-file)
  1239. (add-to-list 'recentf-exclude
  1240. (regexp-quote bookmark-default-file)))
  1241. (defvar bookmark-default-file)
  1242. (defun my-bookmark-set--advice (orig-func &rest args)
  1243. "Function for `bookmark-set-internal'.
  1244. ORIG-FUNC is the target function, and ARGS is the argument when it is called."
  1245. (bookmark-load bookmark-default-file t)
  1246. (apply orig-func args)
  1247. (bookmark-save nil bookmark-default-file))
  1248. (with-eval-after-load 'bookmark
  1249. (advice-add 'bookmark-set-internal
  1250. :around
  1251. 'my-bookmark-set--advice)
  1252. (unless (file-readable-p bookmark-default-file)
  1253. (bookmark-save nil bookmark-default-file)))
  1254. (define-key ctl-x-map "b" 'list-bookmarks)
  1255. (when (fboundp 'counsel-bookmark)
  1256. (define-key ctl-x-map "b" 'counsel-bookmark))
  1257. (define-key ctl-x-map "B" 'my-bookmark-set)
  1258. ;; vc
  1259. (set-variable 'vc-handled-backends '(RCS))
  1260. (set-variable 'vc-rcs-register-switches "-l")
  1261. (set-variable 'vc-rcs-checkin-switches "-l")
  1262. (set-variable 'vc-command-messages t)
  1263. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1264. ;; share clipboard with x
  1265. ;; this page describes this in details, but only these sexps seem to be needed
  1266. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  1267. (and nil
  1268. (not window-system)
  1269. (not (eq window-system 'mac))
  1270. (getenv "DISPLAY")
  1271. (not (equal (getenv "DISPLAY") ""))
  1272. (executable-find "xclip")
  1273. ;; (< emacs-major-version 24)
  1274. '(require 'xclip nil t)
  1275. nil
  1276. (turn-on-xclip))
  1277. (declare-function turn-on-pasteboard "pasteboard")
  1278. (and (eq system-type 'darwin)
  1279. (require 'pasteboard nil t)
  1280. (turn-on-pasteboard))
  1281. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1282. ;; some modes and hooks
  1283. ;; Include some extra modes
  1284. (require 'generic-x)
  1285. ;; Derived from https://github.com/ensime/ensime-emacs/issues/591#issuecomment-291916753
  1286. (defun my-scalafmt ()
  1287. (interactive)
  1288. (cl-assert buffer-file-name)
  1289. (cl-assert (not (buffer-modified-p)))
  1290. (let* ((configdir (locate-dominating-file default-directory ".scalafmt.conf"))
  1291. (configoption (if configdir
  1292. (concat " --config "
  1293. (shell-quote-argument (expand-file-name configdir))
  1294. ".scalafmt.conf"
  1295. )
  1296. ""))
  1297. (str (concat "scalafmt -f "
  1298. (shell-quote-argument buffer-file-name)
  1299. configoption
  1300. " -i --exclude ensime")))
  1301. (message str)
  1302. (shell-command-to-string str))
  1303. (message "scalafmt done")
  1304. (revert-buffer nil t))
  1305. (when (fboundp 'web-mode)
  1306. (add-to-list 'auto-mode-alist
  1307. '("\\.html\\.j2\\'" . web-mode))
  1308. (add-to-list 'auto-mode-alist
  1309. ;; Django Template Language
  1310. '("\\.dtl\\'" . web-mode))
  1311. )
  1312. (when (locate-library "wgrep")
  1313. (set-variable 'wgrep-auto-save-buffer t)
  1314. (with-eval-after-load 'grep
  1315. (defvar grep-mode-map)
  1316. (define-key grep-mode-map
  1317. "e"
  1318. 'wgrep-change-to-wgrep-mode)))
  1319. (when (fboundp 'grep-context-mode)
  1320. (add-hook 'compilation-mode-hook #'grep-context-mode))
  1321. (with-eval-after-load 'remember
  1322. (defvar remember-mode-map)
  1323. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  1324. (set-variable 'remember-notes-initial-major-mode
  1325. 'change-log-mode)
  1326. (set-variable 'magit-define-global-key-bindings nil)
  1327. (with-eval-after-load 'magit-section
  1328. (set-face-background 'magit-section-highlight
  1329. nil))
  1330. ;; Sane colors
  1331. (with-eval-after-load 'magit-diff
  1332. (set-face-background 'magit-diff-context nil)
  1333. (set-face-background 'magit-diff-context-highlight nil)
  1334. (set-face-foreground 'magit-diff-hunk-heading nil)
  1335. (set-face-background 'magit-diff-hunk-heading nil)
  1336. (set-face-foreground 'magit-diff-hunk-heading-highlight nil)
  1337. (set-face-background 'magit-diff-hunk-heading-highlight nil)
  1338. ;; https://blog.shibayu36.org/entry/2016/03/27/220552
  1339. (set-face-foreground 'magit-diff-added "green")
  1340. (set-face-background 'magit-diff-added nil)
  1341. (set-face-foreground 'magit-diff-added-highlight "green")
  1342. (set-face-background 'magit-diff-added-highlight nil)
  1343. (set-face-foreground 'magit-diff-removed "red")
  1344. (set-face-background 'magit-diff-removed nil)
  1345. (set-face-foreground 'magit-diff-removed-highlight "red")
  1346. (set-face-background 'magit-diff-removed-highlight nil)
  1347. (set-face-background 'magit-diff-lines-boundary "blue")
  1348. )
  1349. (declare-function magit-show-commit "magit")
  1350. (defun my-magit-messenger (file line)
  1351. "Magit messenger."
  1352. (interactive (list buffer-file-name
  1353. (line-number-at-pos)))
  1354. (cl-assert file)
  1355. (cl-assert line)
  1356. (let* ((blame-args '("-w"))
  1357. (id (with-temp-buffer
  1358. (let ((exit (apply 'call-process
  1359. "git" ;; PROGRAM
  1360. nil ;; INFILE
  1361. t ;; DESTINATION
  1362. nil ;; DISPLAY
  1363. "--no-pager" ;; ARGS
  1364. "blame"
  1365. "-L"
  1366. (format "%d,+1" line)
  1367. "--porcelain"
  1368. file
  1369. blame-args
  1370. )))
  1371. (goto-char (point-min))
  1372. (cl-assert (eq exit 0)
  1373. "Failed: %s" (buffer-substring (point)
  1374. (point-at-eol)))
  1375. (save-match-data
  1376. (re-search-forward (rx buffer-start
  1377. (one-or-more hex-digit)))
  1378. (match-string 0))))))
  1379. (magit-show-commit id)))
  1380. (when (boundp 'git-rebase-filename-regexp)
  1381. (add-to-list 'auto-mode-alist
  1382. `(,git-rebase-filename-regexp . text-mode)))
  1383. (when (fboundp 'ggtags-mode)
  1384. (add-hook 'c-mode-common-hook
  1385. 'ggtags-mode)
  1386. (add-hook 'python-mode-hook
  1387. 'ggtags-mode)
  1388. (add-hook 'js-mode-hook
  1389. 'ggtags-mode)
  1390. (add-hook 'scheme-mode-hook
  1391. 'ggtags-mode)
  1392. )
  1393. (when (fboundp 'imenu-list-minor-mode)
  1394. (defvar imenu-list-buffer-name)
  1395. (defun my-imenu-list-toggle ()
  1396. "My 'imenu-list` toggle."
  1397. (interactive)
  1398. (require 'imenu-list)
  1399. (if (eq (window-buffer)
  1400. (get-buffer imenu-list-buffer-name))
  1401. (imenu-list-minor-mode -1)
  1402. (imenu-list-minor-mode 1)))
  1403. ;; (set-variable 'imenu-list-auto-resize t)
  1404. (set-variable 'imenu-list-focus-after-activation t)
  1405. ;; (define-key ctl-x-map (kbd "C-l") 'my-imenu-list-toggle)
  1406. (define-key ctl-x-map (kbd "C-l") 'imenu-list-smart-toggle)
  1407. )
  1408. (add-hook 'emacs-lisp-mode-hook
  1409. (lambda ()
  1410. (setq imenu-generic-expression
  1411. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  1412. ,@imenu-generic-expression))))
  1413. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  1414. (with-eval-after-load 'compile
  1415. (defvar compilation-filter-start)
  1416. (defvar compilation-error-regexp-alist)
  1417. (eval-and-compile (require 'ansi-color))
  1418. (add-hook 'compilation-filter-hook
  1419. (lambda ()
  1420. (let ((inhibit-read-only t))
  1421. (ansi-color-apply-on-region compilation-filter-start
  1422. (point)))))
  1423. (add-to-list 'compilation-error-regexp-alist
  1424. ;; ansible-lint
  1425. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2))
  1426. (add-to-list 'compilation-error-regexp-alist
  1427. ;; pydocstyle
  1428. '("^\\([^ \n]+\\):\\([0-9]+\\) " 1 2))
  1429. )
  1430. (declare-function company-cancel "company")
  1431. (when (fboundp 'global-company-mode)
  1432. (add-hook 'after-first-visit-hook
  1433. 'global-company-mode))
  1434. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  1435. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  1436. (set-variable 'company-idle-delay nil)
  1437. (set-variable 'company-minimum-prefix-length 2)
  1438. (set-variable 'company-selection-wrap-around t)
  1439. (set-variable 'company-global-modes '(not term-char-mode
  1440. term-line-mode))
  1441. (declare-function company-manual-begin "company")
  1442. (with-eval-after-load 'company
  1443. (defvar company-mode-map)
  1444. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1445. ;; (with-eval-after-load 'python
  1446. ;; (defvar python-indent-trigger-commands)
  1447. ;; ;; TODO: This disables completion in python?
  1448. ;; (add-to-list 'python-indent-trigger-commands
  1449. ;; 'company-indent-or-complete-common))
  1450. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1451. (defvar company-active-map)
  1452. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1453. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1454. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1455. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1456. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1457. (defvar company-mode)
  1458. (defvar company-candidates)
  1459. (defvar company-candidates-length)
  1460. ;; (popup-tip "Hello, World!")
  1461. (defun my-company-lighter-current-length ()
  1462. "Get current candidate length."
  1463. (interactive)
  1464. (let ((l nil)
  1465. (inhibit-message t))
  1466. (when (and company-mode
  1467. (not (minibufferp))
  1468. ;; Do nothing when already in company completion
  1469. (not company-candidates))
  1470. ;; FIXME: Somehow it cannto catch errors from ggtags
  1471. (ignore-errors
  1472. ;; (company-auto-begin)
  1473. (company-manual-begin)
  1474. (setq l company-candidates-length)
  1475. (company-cancel)))
  1476. (if l
  1477. (format "[%d]" l)
  1478. "")))
  1479. (defvar company-lighter)
  1480. (set-variable 'company-lighter-base "Cmp")
  1481. ;; (add-to-list 'company-lighter
  1482. ;; '(:eval (my-company-lighter-current-length))
  1483. ;; t)
  1484. ;; This breaks japanese text input
  1485. ;; (set-variable 'my-company-length-popup-tip-timer
  1486. ;; (run-with-idle-timer 0.2 t
  1487. ;; 'my-company-length-popup-tip))
  1488. ;; (current-active-maps)
  1489. ;; (lookup-key)
  1490. '(mapcar (lambda (map)
  1491. (lookup-key map (kbd "C-i")))
  1492. (current-active-maps))
  1493. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1494. (set-face-attribute 'company-tooltip nil
  1495. :foreground "black" :background "lightgrey")
  1496. (set-face-attribute 'company-tooltip-common nil
  1497. :foreground "black" :background "lightgrey")
  1498. (set-face-attribute 'company-tooltip-common-selection nil
  1499. :foreground "white" :background "steelblue")
  1500. (set-face-attribute 'company-tooltip-selection nil
  1501. :foreground "black" :background "steelblue")
  1502. (set-face-attribute 'company-preview-common nil
  1503. :background nil :foreground "lightgrey" :underline t)
  1504. (set-face-attribute 'company-scrollbar-fg nil
  1505. :background "orange")
  1506. (set-face-attribute 'company-scrollbar-bg nil
  1507. :background "gray40")
  1508. )
  1509. ;; https://github.com/lunaryorn/flycheck
  1510. ;; TODO: Any way to disable auto check?
  1511. ;; Update flycheck-hooks-alist?
  1512. (when (fboundp 'global-flycheck-mode)
  1513. (add-hook 'after-first-visit-hook
  1514. 'global-flycheck-mode))
  1515. ;; (set-variable 'flycheck-display-errors-delay 2.0)
  1516. ;; (fset 'flycheck-display-error-at-point-soon 'ignore)
  1517. ;; (with-eval-after-load 'flycheck
  1518. ;; (when (fboundp 'flycheck-black-check-setup)
  1519. ;; (flycheck-black-check-setup)))
  1520. ;; (when (fboundp 'ilookup-open-word)
  1521. ;; (define-key ctl-x-map "d" 'ilookup-open-word)
  1522. ;; )
  1523. (set-variable 'ac-ignore-case nil)
  1524. (when (fboundp 'term-run-shell-command)
  1525. (define-key ctl-x-map "t" 'term-run-shell-command))
  1526. (add-to-list 'safe-local-variable-values
  1527. '(encoding utf-8))
  1528. (setq enable-local-variables :safe)
  1529. ;; Detect file type from shebang and set major-mode.
  1530. (add-to-list 'interpreter-mode-alist
  1531. '("python3" . python-mode))
  1532. (add-to-list 'interpreter-mode-alist
  1533. '("python2" . python-mode))
  1534. (with-eval-after-load 'python
  1535. (defvar python-mode-map (make-sparse-keymap))
  1536. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1537. ;; I want to use this, but this breaks normal self-insert-command
  1538. ;; (set-variable 'py-indent-list-style
  1539. ;; 'one-level-to-beginning-of-statement)
  1540. (set-variable 'pydoc-command
  1541. "python3 -m pydoc")
  1542. (declare-function with-venv-advice-add "with-venv")
  1543. (with-eval-after-load 'pydoc
  1544. ;; pydoc depends on python-shell-interpreter but it does not load this
  1545. (require 'python)
  1546. (when (require 'with-venv nil t)
  1547. (with-venv-advice-add 'pydoc)
  1548. ;; Used in interactive function of pydoc
  1549. (with-venv-advice-add 'pydoc-all-modules)))
  1550. (defvar my-cousel-pydoc-history nil "History of `my-counsel-pydoc'.")
  1551. (defun my-counsel-pydoc ()
  1552. "Counsel `pydoc'."
  1553. (interactive)
  1554. (eval-and-compile (require 'pydoc nil t))
  1555. (ivy-read "Recently: " (pydoc-all-modules)
  1556. :require-match t
  1557. :history 'my-cousel-pydoc-history
  1558. :action (lambda (x) (pydoc x))
  1559. :caller 'my-counsel-pydoc))
  1560. (set-variable 'flycheck-python-mypy-config '("mypy.ini" ".mypy.ini" "setup.cfg"))
  1561. (set-variable 'flycheck-flake8rc '("setup.cfg" "tox.ini" ".flake8rc"))
  1562. (set-variable 'flycheck-python-pylint-executable "python3")
  1563. (set-variable 'flycheck-python-pycompile-executable "python3")
  1564. (set-variable 'python-indent-guess-indent-offset nil)
  1565. (with-eval-after-load 'blacken
  1566. (when (require 'with-venv nil t)
  1567. (with-venv-advice-add 'blacken-buffer)))
  1568. (with-eval-after-load 'ansible-doc
  1569. (when (require 'with-venv nil t)
  1570. (with-venv-advice-add 'ansible-doc)))
  1571. ;; `isortify-buffer' breaks buffer when it contains japanese text
  1572. (defun my-isortify ()
  1573. (interactive)
  1574. (cl-assert buffer-file-name)
  1575. (cl-assert (not (buffer-modified-p)))
  1576. (call-process "python" ;; PROGRAM
  1577. nil ;; INFILE
  1578. nil ;; DESTINATION
  1579. nil ;; DISPLAY
  1580. "-m" "isort" buffer-file-name)
  1581. (message "isortify done")
  1582. (revert-buffer nil t))
  1583. (when (fboundp 'with-venv-advice-add)
  1584. ;; TODO: Lazy load with-venv
  1585. (with-venv-advice-add 'my-isortify))
  1586. ;; https://github.com/lunaryorn/old-emacs-configuration/blob/master/lisp/flycheck-virtualenv.el
  1587. (defun my-set-venv-flycheck-executable-find ()
  1588. "Set flycheck executabie find."
  1589. (interactive)
  1590. (when (fboundp 'with-venv)
  1591. (set-variable 'flycheck-executable-find
  1592. '(lambda (e)
  1593. (with-venv
  1594. (executable-find e)))
  1595. t)))
  1596. (defun my-update-flycheck-flake8-error-level-alist ()
  1597. "Update `flycheck-flake8-error-level-alist'."
  1598. (defvar flycheck-flake8-error-level-alist)
  1599. ;; (add-to-list 'flycheck-flake8-error-level-alist
  1600. ;; '("^D.*$" . warning))
  1601. (set-variable 'flycheck-flake8-error-level-alist
  1602. nil)
  1603. )
  1604. (add-hook 'python-mode-hook
  1605. 'my-set-venv-flycheck-executable-find)
  1606. (add-hook 'python-mode-hook
  1607. 'my-update-flycheck-flake8-error-level-alist)
  1608. (when (fboundp 'with-venv-info-mode)
  1609. (add-hook 'python-mode-hook
  1610. 'with-venv-info-mode))
  1611. ;; Run multiple chekcers
  1612. ;; https://github.com/flycheck/flycheck/issues/186
  1613. (add-hook 'python-mode-hook
  1614. (lambda ()
  1615. ;; Currently on python-mode eldoc-mode sometimes print
  1616. ;; wired message on "from" keyword:
  1617. ;;var from = require("./from")
  1618. (eldoc-mode -1)))
  1619. ;; http://fukuyama.co/foreign-regexp
  1620. '(and (require 'foreign-regexp nil t)
  1621. (progn
  1622. (setq foreign-regexp/regexp-type 'perl)
  1623. '(setq reb-re-syntax 'foreign-regexp)
  1624. ))
  1625. ;; sqlind does not support create role so disable it...
  1626. (set-variable 'sql-use-indent-support nil)
  1627. ;; (with-eval-after-load 'sql
  1628. ;; (require 'sql-indent nil t))
  1629. ;; (set-variable 'sqlind-basic-offset 4)
  1630. (add-to-list 'auto-mode-alist
  1631. '("\\.hql\\'" . sql-mode))
  1632. (set-variable 'sql-product 'postgres)
  1633. (set-variable 'sqlformat-command 'pgformatter)
  1634. (set-variable 'sqlformat-args '("--no-space-function" "--nogrouping"))
  1635. ;; Hard to use because when failed to format it does not tell how to fix that
  1636. ;; (set-variable 'sqlformat-command 'sqlfluff)
  1637. ;; (set-variable 'sqlformat-args '("--show-lint-violations" "-vvvv"))
  1638. ;; (with-eval-after-load 'sqlformat
  1639. ;; (when (fboundp 'with-venv-advice-add)
  1640. ;; (with-venv-advice-add 'sqlformat-region)))
  1641. (when (fboundp 'git-command)
  1642. (define-key ctl-x-map "g" 'git-command))
  1643. ;; (when (fboundp 'gited-list)
  1644. ;; (defalias 'gited 'gited-list))
  1645. (when (fboundp 'counsel-git-checkout)
  1646. (defalias 'my-git-si 'counsel-git-checkout))
  1647. (when (and (eval-and-compile (require 'git-commit nil t))
  1648. (fboundp 'global-git-commit-mode))
  1649. ;; Frequently this breaks git commit.
  1650. (global-git-commit-mode 0))
  1651. (with-eval-after-load 'git-commit
  1652. (add-hook 'git-commit-setup-hook
  1653. 'turn-off-auto-fill t))
  1654. (with-eval-after-load 'rst
  1655. (defvar rst-mode-map)
  1656. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1657. (with-eval-after-load 'jdee
  1658. (add-hook 'jdee-mode-hook
  1659. (lambda ()
  1660. (make-local-variable 'global-mode-string)
  1661. (add-to-list 'global-mode-string
  1662. mode-line-position))))
  1663. ;; Cannot enable error thrown. Why???
  1664. ;; https://github.com/m0smith/malabar-mode#Installation
  1665. ;; (when (require 'malabar-mode nil t)
  1666. ;; (add-to-list 'load-path
  1667. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1668. ;; (require 'cedet-devel-load nil t)
  1669. ;; (eval-after-init (activate-malabar-mode)))
  1670. (with-eval-after-load 'make-mode
  1671. (defvar makefile-mode-map (make-sparse-keymap))
  1672. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1673. ;; this functions is set in write-file-functions, i cannot find any
  1674. ;; good way to remove this.
  1675. (fset 'makefile-warn-suspicious-lines 'ignore))
  1676. (with-eval-after-load 'verilog-mode
  1677. (defvar verilog-mode-map (make-sparse-keymap))
  1678. (define-key verilog-mode-map ";" 'self-insert-command))
  1679. (setq diff-switches "-u")
  1680. (autoload 'diff-goto-source "diff-mode" nil t)
  1681. (with-eval-after-load 'diff-mode
  1682. ;; (when (and (eq major-mode
  1683. ;; 'diff-mode)
  1684. ;; (not buffer-file-name))
  1685. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1686. ;; (view-mode 1))
  1687. (set-face-attribute 'diff-header nil
  1688. :foreground nil
  1689. :background nil
  1690. :weight 'bold)
  1691. (set-face-attribute 'diff-file-header nil
  1692. :foreground nil
  1693. :background nil
  1694. :weight 'bold)
  1695. (set-face-foreground 'diff-index "blue")
  1696. (set-face-attribute 'diff-hunk-header nil
  1697. :foreground "cyan"
  1698. :weight 'normal)
  1699. (set-face-attribute 'diff-context nil
  1700. ;; :foreground "white"
  1701. :foreground nil
  1702. :weight 'normal)
  1703. (set-face-foreground 'diff-removed "red")
  1704. (set-face-foreground 'diff-added "green")
  1705. (set-face-background 'diff-removed nil)
  1706. (set-face-background 'diff-added nil)
  1707. (set-face-attribute 'diff-changed nil
  1708. :foreground "magenta"
  1709. :weight 'normal)
  1710. (set-face-attribute 'diff-refine-changed nil
  1711. :foreground nil
  1712. :background nil
  1713. :weight 'bold
  1714. :inverse-video t)
  1715. ;; Annoying !
  1716. ;;(diff-auto-refine-mode)
  1717. (set-variable 'diff-refine nil)
  1718. )
  1719. ;; (ffap-bindings)
  1720. (with-eval-after-load 'browse-url
  1721. (set-variable 'browse-url-browser-function
  1722. 'eww-browse-url))
  1723. (set-variable 'sh-here-document-word "__EOC__")
  1724. (with-eval-after-load 'adoc-mode
  1725. (defvar adoc-mode-map)
  1726. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1727. (when (fboundp 'adoc-mode)
  1728. (setq auto-mode-alist
  1729. `(("\\.adoc\\'" . adoc-mode)
  1730. ("\\.asciidoc\\'" . adoc-mode)
  1731. ,@auto-mode-alist)))
  1732. (with-eval-after-load 'markup-faces
  1733. ;; Is this too match ?
  1734. (set-face-foreground 'markup-meta-face
  1735. "color-245")
  1736. (set-face-foreground 'markup-meta-hide-face
  1737. "color-245")
  1738. )
  1739. ;; TODO: check if this is required
  1740. (with-eval-after-load 'groovy-mode
  1741. (defvar groovy-mode-map)
  1742. (define-key groovy-mode-map "(" 'self-insert-command)
  1743. (define-key groovy-mode-map ")" 'self-insert-command)
  1744. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1745. )
  1746. (when (fboundp 'groovy-mode)
  1747. (add-to-list 'auto-mode-alist
  1748. '("build\\.gradle\\'" . groovy-mode)))
  1749. (add-to-list 'auto-mode-alist
  1750. '("\\.gawk\\'" . awk-mode))
  1751. (with-eval-after-load 'yaml-mode
  1752. (defvar yaml-mode-map (make-sparse-keymap))
  1753. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1754. (when (fboundp 'yaml-mode)
  1755. (add-to-list 'auto-mode-alist
  1756. '("\\.yaml\\.gotmpl\\'" . yaml-mode)))
  1757. (with-eval-after-load 'html-mode
  1758. (defvar html-mode-map (make-sparse-keymap))
  1759. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1760. (with-eval-after-load 'text-mode
  1761. (define-key text-mode-map (kbd "C-m") 'newline))
  1762. (with-eval-after-load 'info
  1763. (defvar Info-additional-directory-list)
  1764. (dolist (dir (directory-files (concat user-emacs-directory
  1765. "info")
  1766. t
  1767. "^[^.].*"))
  1768. (when (file-directory-p dir)
  1769. (add-to-list 'Info-additional-directory-list
  1770. dir)))
  1771. (let ((dir (expand-file-name "~/.brew/share/info")))
  1772. (when (file-directory-p dir)
  1773. (add-to-list 'Info-additional-directory-list
  1774. dir))))
  1775. (with-eval-after-load 'apropos
  1776. (defvar apropos-mode-map (make-sparse-keymap))
  1777. (define-key apropos-mode-map "n" 'next-line)
  1778. (define-key apropos-mode-map "p" 'previous-line))
  1779. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1780. ;; (define-key isearch-mode-map
  1781. ;; (kbd "C-j") 'isearch-other-control-char)
  1782. ;; (define-key isearch-mode-map
  1783. ;; (kbd "C-k") 'isearch-other-control-char)
  1784. ;; (define-key isearch-mode-map
  1785. ;; (kbd "C-h") 'isearch-other-control-char)
  1786. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1787. (define-key isearch-mode-map (kbd "M-r")
  1788. 'isearch-query-replace-regexp)
  1789. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1790. (setq lazy-highlight-cleanup nil)
  1791. ;; face for isearch highlighting
  1792. (set-face-attribute 'lazy-highlight
  1793. nil
  1794. :foreground `unspecified
  1795. :background `unspecified
  1796. :underline t
  1797. ;; :weight `bold
  1798. )
  1799. (add-hook 'outline-mode-hook
  1800. (lambda ()
  1801. (when (string-match "\\.md\\'" buffer-file-name)
  1802. (setq-local outline-regexp "#+ "))))
  1803. (add-hook 'outline-mode-hook
  1804. 'outline-show-all)
  1805. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1806. (with-eval-after-load 'markdown-mode
  1807. (defvar gfm-mode-map)
  1808. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1809. (define-key gfm-mode-map "`" nil) ;; markdown-electric-backquote
  1810. )
  1811. (when (fboundp 'gfm-mode)
  1812. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1813. (add-hook 'markdown-mode-hook
  1814. 'outline-minor-mode)
  1815. (add-hook 'markdown-mode-hook
  1816. (lambda ()
  1817. (setq-local comment-start ";")))
  1818. )
  1819. ;; http://keisanbutsuriya.hateblo.jp/entry/2015/02/10/152543
  1820. ;; M-$ to ispell word
  1821. ;; M-x flyspell-buffer to highlight all suspicious words
  1822. (when (executable-find "aspell")
  1823. (set-variable 'ispell-program-name "aspell")
  1824. (set-variable 'ispell-extra-args '("--lang=en_US")))
  1825. (with-eval-after-load 'ispell
  1826. (add-to-list 'ispell-skip-region-alist '("[^\000-\377]+")))
  1827. (when (fboundp 'flyspell-mode)
  1828. (add-hook 'text-mode-hook
  1829. 'flyspell-mode))
  1830. (when (fboundp 'flyspell-prog-mode)
  1831. (add-hook 'prog-mode-hook
  1832. 'flyspell-prog-mode))
  1833. ;; c-mode
  1834. ;; http://www.emacswiki.org/emacs/IndentingC
  1835. ;; http://en.wikipedia.org/wiki/Indent_style
  1836. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1837. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1838. (with-eval-after-load 'cc-vars
  1839. (defvar c-default-style nil)
  1840. (add-to-list 'c-default-style
  1841. '(c-mode . "k&r"))
  1842. (add-to-list 'c-default-style
  1843. '(c++-mode . "k&r")))
  1844. (add-to-list 'auto-mode-alist
  1845. '("\\.gs\\'" . js-mode))
  1846. (with-eval-after-load 'js2-mode
  1847. ;; currently do not use js2-mode
  1848. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1849. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1850. ;; (defvar js2-mode-map (make-sparse-keymap))
  1851. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1852. ;; (interactive)
  1853. ;; (js2-enter-key)
  1854. ;; (indent-for-tab-command)))
  1855. ;; (add-hook (kill-local-variable 'before-save-hook)
  1856. ;; 'js2-before-save)
  1857. ;; (add-hook 'before-save-hook
  1858. ;; 'my-indent-buffer
  1859. ;; nil
  1860. ;; t)
  1861. )
  1862. (add-to-list 'interpreter-mode-alist
  1863. '("node" . js-mode))
  1864. (add-hook 'js-mode-hook
  1865. (lambda ()
  1866. ;; Stop current line highlighting
  1867. (set-variable 'js-indent-level 2 t)
  1868. ))
  1869. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1870. (with-eval-after-load 'uniquify
  1871. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1872. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1873. (setq uniquify-min-dir-content 1))
  1874. (with-eval-after-load 'view
  1875. (defvar view-mode-map (make-sparse-keymap))
  1876. (define-key view-mode-map "j" 'scroll-up-line)
  1877. (define-key view-mode-map "k" 'scroll-down-line)
  1878. (define-key view-mode-map "v" 'toggle-read-only)
  1879. (define-key view-mode-map "q" 'bury-buffer)
  1880. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1881. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1882. ;; (define-key view-mode-map
  1883. ;; "n" 'nonincremental-repeat-search-forward)
  1884. ;; (define-key view-mode-map
  1885. ;; "N" 'nonincremental-repeat-search-backward)
  1886. ;; N conflicts with git-walktree
  1887. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1888. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1889. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1890. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1891. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1892. (global-set-key "\M-r" 'view-mode)
  1893. ;; (setq view-read-only t)
  1894. (with-eval-after-load 'term
  1895. (defvar term-raw-map (make-sparse-keymap))
  1896. (define-key term-raw-map (kbd "C-x")
  1897. (lookup-key (current-global-map)
  1898. (kbd "C-x"))))
  1899. (add-hook 'term-mode-hook
  1900. (lambda ()
  1901. ;; Stop current line highlighting
  1902. (set-variable 'hl-line-range-function (lambda () '(0 . 0)) t)
  1903. (set-variable 'scroll-margin 0 t)
  1904. ))
  1905. (set-variable 'term-buffer-maximum-size 20480)
  1906. (set-variable 'term-suppress-hard-newline t)
  1907. (add-hook 'Man-mode-hook
  1908. (lambda ()
  1909. (view-mode 1)
  1910. (setq truncate-lines nil)))
  1911. (set-variable 'Man-notify-method (if window-system
  1912. 'newframe
  1913. 'aggressive))
  1914. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1915. "woman_cache.el")))
  1916. ;; not work because man.el will be loaded when man called
  1917. (defalias 'man 'woman)
  1918. (add-to-list 'auto-mode-alist
  1919. '("/tox\\.ini\\'" . conf-unix-mode))
  1920. (add-to-list 'auto-mode-alist
  1921. '("/setup\\.cfg\\'" . conf-unix-mode))
  1922. (when (fboundp 'toml-mode)
  1923. (add-to-list 'auto-mode-alist
  1924. '("/tox\\.ini\\'" . toml-mode))
  1925. (add-to-list 'auto-mode-alist
  1926. '("/setup\\.cfg\\'" . toml-mode))
  1927. (add-to-list 'auto-mode-alist
  1928. '("/Pipfile\\'" . toml-mode))
  1929. (add-to-list 'auto-mode-alist
  1930. '("/poetry\\.lock\\'" . toml-mode))
  1931. )
  1932. (when (fboundp 'json-mode)
  1933. (add-to-list 'auto-mode-alist
  1934. '("/Pipfile\\.lock\\'" . json-mode)))
  1935. (add-hook 'json-mode-hook
  1936. (lambda ()
  1937. ;; Stop current line highlighting
  1938. (set-variable 'js-indent-level 2 t)
  1939. ))
  1940. (add-hook 'go-mode-hook
  1941. (lambda()
  1942. (defvar go-mode-map)
  1943. (add-hook 'before-save-hook 'gofmt-before-save nil t)
  1944. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1945. (when (fboundp 'k8s-mode)
  1946. (add-to-list 'auto-mode-alist
  1947. '("\\.k8s\\'" . k8s-mode)))
  1948. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1949. ;; buffers
  1950. (defvar bs-configurations)
  1951. (declare-function bs-set-configuration "bs")
  1952. (declare-function bs-refresh "bs")
  1953. (declare-function bs-message-without-log "bs")
  1954. (declare-function bs--current-config-message "bs")
  1955. (with-eval-after-load 'bs
  1956. (add-to-list 'bs-configurations
  1957. '("specials" "^\\*" nil ".*" nil nil))
  1958. (add-to-list 'bs-configurations
  1959. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1960. (defvar bs-mode-map)
  1961. (defvar bs-current-configuration)
  1962. (define-key bs-mode-map (kbd "t")
  1963. ;; TODO: fix toggle feature
  1964. (lambda ()
  1965. (interactive)
  1966. (if (string= "specials"
  1967. bs-current-configuration)
  1968. (bs-set-configuration "files")
  1969. (bs-set-configuration "specials"))
  1970. (bs-refresh)
  1971. (bs-message-without-log "%s"
  1972. (bs--current-config-message))))
  1973. ;; (setq bs-configurations (list
  1974. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1975. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1976. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1977. )
  1978. (when (fboundp 'bs-show)
  1979. (defalias 'list-buffers 'bs-show)
  1980. (set-variable 'bs-default-configuration "files-and-specials")
  1981. (set-variable 'bs-default-sort-name "by nothing")
  1982. (add-hook 'bs-mode-hook
  1983. (lambda ()
  1984. (setq-local scroll-margin 0)
  1985. (setq-local header-line-format nil)
  1986. (setq-local mode-line-format nil)
  1987. )))
  1988. ;;(iswitchb-mode 1)
  1989. (icomplete-mode)
  1990. (defun iswitchb-buffer-display-other-window ()
  1991. "Do iswitchb in other window."
  1992. (interactive)
  1993. (let ((iswitchb-default-method 'display))
  1994. (call-interactively 'iswitchb-buffer)))
  1995. ;; buffer killing
  1996. ;; (defun my-delete-window-killing-buffer () nil)
  1997. (defun my-query-kill-current-buffer ()
  1998. "Interactively kill current buffer."
  1999. (interactive)
  2000. (if (y-or-n-p (concat "kill current buffer? :"))
  2001. (kill-buffer (current-buffer))))
  2002. (defun my-force-query-kill-current-buffer ()
  2003. "Interactively kill current buffer."
  2004. (interactive)
  2005. (when (y-or-n-p (concat "kill current buffer? :"))
  2006. (let ((kill-buffer-hook nil)
  2007. (kill-buffer-query-functions nil))
  2008. (kill-buffer (current-buffer)))))
  2009. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  2010. ;; Originally C-x C-k -> kmacro-keymap
  2011. ;; (global-set-key "\C-x\C-k" 'kmacro-keymap)
  2012. (global-set-key (kbd "C-x C-k") 'my-query-kill-current-buffer)
  2013. (substitute-key-definition 'kill-buffer
  2014. 'my-query-kill-current-buffer
  2015. global-map)
  2016. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2017. ;; dired
  2018. (defun my-file-head (filename &optional n)
  2019. "Return list of first N lines of file FILENAME."
  2020. ;; TODO: Fix for janapese text
  2021. ;; TODO: Fix for short text
  2022. (let ((num (or n 10))
  2023. (size 100)
  2024. (beg 0)
  2025. (end 0)
  2026. (result '())
  2027. (read -1))
  2028. (with-temp-buffer
  2029. (erase-buffer)
  2030. (while (or (<= (count-lines (point-min)
  2031. (point-max))
  2032. num)
  2033. (eq read 0))
  2034. (setq end (+ beg size))
  2035. (setq read (nth 1 (insert-file-contents-literally filename
  2036. nil
  2037. beg
  2038. end)))
  2039. (goto-char (point-max))
  2040. (setq beg (+ beg size)))
  2041. (goto-char (point-min))
  2042. (while (< (length result) num)
  2043. (let ((start (point)))
  2044. (forward-line 1)
  2045. (setq result
  2046. `(,@result ,(buffer-substring-no-properties start
  2047. (point))))))
  2048. result
  2049. ;; (buffer-substring-no-properties (point-min)
  2050. ;; (progn
  2051. ;; (forward-line num)
  2052. ;; (point)))
  2053. )))
  2054. ;; (apply 'concat (my-file-head "./shrc" 10)
  2055. (declare-function dired-get-filename "dired" t)
  2056. (defun my-dired-echo-file-head (arg)
  2057. "Echo head of current file.
  2058. ARG is num to show, or defaults to 7."
  2059. (interactive "P")
  2060. (let ((f (dired-get-filename)))
  2061. (message "%s"
  2062. (apply 'concat
  2063. (my-file-head f
  2064. 7)))))
  2065. (declare-function dired-get-marked-files "dired")
  2066. (defun my-dired-diff ()
  2067. "Show diff of marked file and file of current line."
  2068. (interactive)
  2069. (let ((files (dired-get-marked-files nil nil nil t)))
  2070. (if (eq (car files)
  2071. t)
  2072. (diff (cadr files) (dired-get-filename))
  2073. (message "One file must be marked!"))))
  2074. (defun dired-get-file-info ()
  2075. "Print information of current line file."
  2076. (interactive)
  2077. (let* ((file (dired-get-filename t))
  2078. (quoted (shell-quote-argument file)))
  2079. (if (file-directory-p file)
  2080. (progn
  2081. (message "Calculating disk usage...")
  2082. (let ((du (or (executable-find "gdu")
  2083. (executable-find "du")
  2084. (error "du not found"))))
  2085. (shell-command (concat du
  2086. " -hsD "
  2087. quoted))))
  2088. (shell-command (concat "file "
  2089. quoted)))))
  2090. (defun my-dired-scroll-up ()
  2091. "Scroll up."
  2092. (interactive)
  2093. (my-dired-previous-line (- (window-height) 1)))
  2094. (defun my-dired-scroll-down ()
  2095. "Scroll down."
  2096. (interactive)
  2097. (my-dired-next-line (- (window-height) 1)))
  2098. ;; (defun my-dired-forward-line (arg)
  2099. ;; ""
  2100. ;; (interactive "p"))
  2101. (declare-function dired-get-subdir "dired")
  2102. (declare-function dired-move-to-filename "dired")
  2103. (defun my-dired-previous-line (arg)
  2104. "Move ARG lines up."
  2105. (interactive "p")
  2106. (if (> arg 0)
  2107. (progn
  2108. (if (eq (line-number-at-pos)
  2109. 1)
  2110. (goto-char (point-max))
  2111. (forward-line -1))
  2112. (my-dired-previous-line (if (or (dired-get-filename nil t)
  2113. (dired-get-subdir))
  2114. (- arg 1)
  2115. arg)))
  2116. (dired-move-to-filename)))
  2117. (defun my-dired-next-line (arg)
  2118. "Move ARG lines down."
  2119. (interactive "p")
  2120. (if (> arg 0)
  2121. (progn
  2122. (if (eq (point)
  2123. (point-max))
  2124. (goto-char (point-min))
  2125. (forward-line 1))
  2126. (my-dired-next-line (if (or (dired-get-filename nil t)
  2127. (dired-get-subdir))
  2128. (- arg 1)
  2129. arg)))
  2130. (dired-move-to-filename)))
  2131. (defun my-tramp-remote-find-file (f)
  2132. "Open F."
  2133. (interactive (list (read-file-name "My Find File Tramp: "
  2134. "/scp:"
  2135. nil ;; "/scp:"
  2136. (confirm-nonexistent-file-or-buffer))))
  2137. (find-file f))
  2138. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  2139. (if (eq window-system 'mac)
  2140. (setq dired-listing-switches "-lhFA")
  2141. (setq dired-listing-switches "-lhFA --time-style=long-iso")
  2142. )
  2143. (setq dired-listing-switches "-lhFA")
  2144. ;; when using dired-find-alternate-file
  2145. ;; reuse current dired buffer for the file to open
  2146. ;; (put 'dired-find-alternate-file 'disabled nil)
  2147. (set-variable 'dired-ls-F-marks-symlinks t)
  2148. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  2149. (set-variable 'ls-lisp-dirs-first t)
  2150. (set-variable 'ls-lisp-use-localized-time-format t)
  2151. (set-variable 'ls-lisp-format-time-list
  2152. '("%Y-%m-%d %H:%M"
  2153. "%Y-%m-%d "))
  2154. (set-variable 'dired-dwim-target t)
  2155. (set-variable 'dired-isearch-filenames t)
  2156. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  2157. (set-variable 'dired-hide-details-hide-information-lines nil)
  2158. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  2159. (set-variable 'dired-recursive-deletes 'always)
  2160. ;; (add-hook 'dired-after-readin-hook
  2161. ;; 'my-replace-nasi-none)
  2162. (with-eval-after-load 'dired
  2163. (require 'ls-lisp nil t)
  2164. (defvar dired-mode-map (make-sparse-keymap))
  2165. ;; dired-do-chgrp sometimes cause system hung
  2166. (define-key dired-mode-map "G" 'ignore)
  2167. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  2168. (define-key dired-mode-map "i" 'dired-get-file-info)
  2169. ;; (define-key dired-mode-map "f" 'find-file)
  2170. (define-key dired-mode-map "f" 'my-fuzzy-finder-or-find-file)
  2171. (define-key dired-mode-map "!" 'shell-command)
  2172. (define-key dired-mode-map "&" 'async-shell-command)
  2173. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  2174. (define-key dired-mode-map "=" 'my-dired-diff)
  2175. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  2176. (define-key dired-mode-map "b" 'gtkbm)
  2177. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  2178. (define-key dired-mode-map (kbd "TAB") 'other-window)
  2179. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  2180. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  2181. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  2182. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  2183. (substitute-key-definition 'dired-next-line
  2184. 'my-dired-next-line
  2185. dired-mode-map)
  2186. (substitute-key-definition 'dired-previous-line
  2187. 'my-dired-previous-line
  2188. dired-mode-map)
  2189. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  2190. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  2191. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  2192. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  2193. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  2194. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  2195. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  2196. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  2197. (add-hook 'dired-mode-hook
  2198. (lambda ()
  2199. (when (fboundp 'dired-hide-details-mode)
  2200. (dired-hide-details-mode t)
  2201. (local-set-key "l" 'dired-hide-details-mode))
  2202. (when (fboundp 'dired-omit-mode)
  2203. (dired-omit-mode 1)
  2204. (local-set-key "a" 'dired-omit-mode))
  2205. (let ((file "._Icon\015"))
  2206. (when nil
  2207. '(file-readable-p file)
  2208. (delete-file file)))))
  2209. (when (fboundp 'pack-dired-dwim)
  2210. (with-eval-after-load 'dired
  2211. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  2212. ;; https://emacs.stackexchange.com/questions/68585/dired-mode-toggle-show-hidden-files-folders-by-keyboard-shortcut
  2213. (set-variable 'dired-omit-files
  2214. (rx (or (regexp "\\`[.]?#\\|\\`[.][.]?\\'")
  2215. (seq bos "." (not (any "."))))))
  2216. ;; (string-match-p dired-omit-files ".abc")
  2217. (when (fboundp 'dired-omit-mode)
  2218. (with-eval-after-load 'dired
  2219. ))
  2220. )
  2221. (when (fboundp 'dired-filter-mode)
  2222. (add-hook 'dired-mode-hook
  2223. 'dired-filter-mode))
  2224. (set-variable 'dired-filter-stack nil)
  2225. ;; Currently disabled in favor of dired-from-git-ls-files
  2226. ;; (define-key ctl-x-map "f" 'find-dired)
  2227. (defvar my-dired-git-ls-files-history
  2228. "History for `my-dired-git-ls-files'." nil)
  2229. (defun my-dired-git-ls-files (arg)
  2230. "Dired from git ls-files."
  2231. (interactive (list
  2232. (read-shell-command "git ls-files: "
  2233. "git ls-files -z ")))
  2234. (pop-to-buffer-same-window
  2235. (dired-noselect `(,default-directory
  2236. ,@(split-string (shell-command-to-string arg)
  2237. "\0" t))
  2238. ""))
  2239. )
  2240. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  2241. (with-eval-after-load 'dired
  2242. (defvar dired-mode-map (make-sparse-keymap))
  2243. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  2244. (with-eval-after-load 'pack
  2245. (set-variable 'pack-silence
  2246. t)
  2247. (defvar pack-program-alist)
  2248. (add-to-list 'pack-program-alist
  2249. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf"))
  2250. (when (executable-find "aunpack")
  2251. (add-to-list 'pack-program-alist
  2252. ' ("\\.zip\\'"
  2253. :pack ("zip" "-r" archive sources)
  2254. :pack-append ("zip" "-r" archive sources)
  2255. :unpack ("aunpack" archive))))
  2256. )
  2257. ;; dired-k
  2258. (declare-function dired-k-no-revert "dired-k")
  2259. (when (fboundp 'dired-k)
  2260. (set-variable 'dired-k-style 'git)
  2261. ;; What is the best way of doing this?
  2262. (with-eval-after-load 'dired-k
  2263. (fset 'dired-k--highlight-by-file-attribyte 'ignore))
  2264. ;; (set-variable 'dired-k-size-colors
  2265. ;; `((,most-positive-fixnum)))
  2266. ;; (set-variable 'dired-k-date-colors
  2267. ;; `((,most-positive-fixnum)))
  2268. (add-hook 'dired-after-readin-hook #'dired-k-no-revert)
  2269. )
  2270. ;; (when (eval-and-compile (require 'dired-rainbow nil t))
  2271. ;; (dired-rainbow-define gtags "brightblack" "GTAGS"))
  2272. (with-eval-after-load 'diredfl
  2273. (set-face-foreground 'diredfl-file-name nil)
  2274. )
  2275. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2276. ;; misc funcs
  2277. (define-key ctl-x-map "T" 'git-worktree)
  2278. (define-key ctl-x-map "W" 'git-walktree)
  2279. (defun mkcdd ()
  2280. "Make date directory and open it with dired."
  2281. (interactive)
  2282. (let ((d (format-time-string "%Y%m%d-%H%M%S")))
  2283. (make-directory d)
  2284. (find-file d)))
  2285. (when (fboundp 'browse-url-default-macosx-browser)
  2286. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  2287. (defalias 'qcalc 'quick-calc)
  2288. (defun memo (&optional dir)
  2289. "Open memo.txt in DIR."
  2290. (interactive)
  2291. (pop-to-buffer (find-file-noselect (expand-file-name "memo.txt"
  2292. (or dir
  2293. default-directory)))))
  2294. ;; TODO: remember-projectile
  2295. (set (defvar my-privnotes-path nil
  2296. "My privnotes repository path.")
  2297. (expand-file-name "~/my/privnotes"))
  2298. (defun my-privnotes-readme (dir)
  2299. "Open my privnotes DIR."
  2300. (interactive (list
  2301. (read-file-name "Privnotes: "
  2302. (expand-file-name (format-time-string "%Y%m%d_")
  2303. my-privnotes-path))))
  2304. (let ((path (expand-file-name "README.md" dir)))
  2305. (with-current-buffer (find-file path)
  2306. (unless (file-exists-p path)
  2307. (insert (file-name-base dir)
  2308. "\n"
  2309. "=======\n"
  2310. "\n\n")))))
  2311. (define-key ctl-x-map "p" 'my-privnotes-readme)
  2312. (set (defvar my-rgrep-alist nil
  2313. "Alist of rgrep command.
  2314. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2315. condition to choose COMMAND when evaluated.")
  2316. `(
  2317. ;; ripgrep
  2318. ("rg"
  2319. (executable-find "rg")
  2320. "rg -nH --no-heading --hidden --no-ignore-parent --glob '!.git/' --smart-case -M 1280 ")
  2321. ;; git grep
  2322. ("gitgrep"
  2323. (eq 0
  2324. (shell-command "git rev-parse --git-dir"))
  2325. "git --no-pager grep -nH -e ")
  2326. ;; sift
  2327. ("sift"
  2328. (executable-find "sift")
  2329. ("sift --binary-skip --filename --line-number --git --smart-case "))
  2330. ;; the silver searcher
  2331. ("ag"
  2332. (executable-find "ag")
  2333. "ag --nogroup --nopager --filename ")
  2334. ;; ack
  2335. ("ack"
  2336. (executable-find "ack")
  2337. "ack --nogroup --nopager --with-filename ")
  2338. ;; gnu global
  2339. ("global"
  2340. (and (require 'ggtags nil t)
  2341. (executable-find "global")
  2342. (ggtags-current-project-root))
  2343. "global --result grep ")
  2344. ;; grep
  2345. ("grep"
  2346. t
  2347. ,(concat "find . "
  2348. "-path '*/.git' -prune -o "
  2349. "-path '*/.svn' -prune -o "
  2350. "-type f -print0 | "
  2351. "xargs -0 grep -nH -e "))
  2352. )
  2353. )
  2354. (defvar my-rgrep-default nil
  2355. "Default command name for my-rgrep.")
  2356. (defun my-rgrep-grep-command (&optional name alist)
  2357. "Return recursive grep command for current directory or nil.
  2358. If NAME is given, use that without testing.
  2359. Commands are searched from ALIST."
  2360. (if alist
  2361. (if name
  2362. ;; if name is given search that from alist and return the command
  2363. (nth 2 (assoc name
  2364. alist))
  2365. ;; if name is not given try test in 1th elem
  2366. (let ((car (car alist))
  2367. (cdr (cdr alist)))
  2368. (if (eval (nth 1 car))
  2369. ;; if the condition is true return the command
  2370. (nth 2 car)
  2371. ;; try next one
  2372. (and cdr
  2373. (my-rgrep-grep-command name cdr)))))
  2374. ;; if alist is not given set default value
  2375. (my-rgrep-grep-command name my-rgrep-alist)))
  2376. (declare-function projectile-project-p "projectile")
  2377. (declare-function projectile-with-default-dir "projectile")
  2378. (declare-function projectile-project-root "projectile")
  2379. (defun my-rgrep (command-args)
  2380. "My recursive grep. Run COMMAND-ARGS.
  2381. If prefix argument is given, use current symbol as default search target
  2382. and search from projectile root (if projectile is available)."
  2383. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2384. nil)))
  2385. (if cmd
  2386. (list (read-shell-command "grep command: "
  2387. (concat cmd
  2388. (if current-prefix-arg
  2389. (thing-at-point 'symbol t)
  2390. ""))
  2391. 'grep-find-history))
  2392. (error "My-Rgrep: Command for rgrep not found")
  2393. )))
  2394. (if (and current-prefix-arg
  2395. (eval-and-compile (require 'projectile nil t))
  2396. (projectile-project-p))
  2397. (projectile-with-default-dir (projectile-project-root)
  2398. (compilation-start command-args
  2399. 'grep-mode))
  2400. (compilation-start command-args
  2401. 'grep-mode)))
  2402. (defun my-rgrep-thing-at-point-projectile-root ()
  2403. "My recursive grep to find thing at point from project root."
  2404. (interactive)
  2405. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  2406. nil))
  2407. (command-args
  2408. (if cmd
  2409. (concat cmd
  2410. (or (thing-at-point 'symbol t)
  2411. (error "No symbol at point")))
  2412. (error "My-Rgrep: Command for rgrep not found"))))
  2413. (if (eval-and-compile (require 'projectile nil t))
  2414. (with-temp-buffer
  2415. (cd (or (projectile-project-root)
  2416. default-directory))
  2417. (compilation-start command-args
  2418. 'grep-mode))
  2419. (compilation-start command-args
  2420. 'grep-mode))))
  2421. (defmacro define-my-rgrep (name)
  2422. "Define rgrep for NAME."
  2423. `(defun ,(intern (concat "my-rgrep-"
  2424. name)) ()
  2425. ,(format "My recursive grep by %s."
  2426. name)
  2427. (interactive)
  2428. (let ((my-rgrep-default ,name))
  2429. (if (called-interactively-p 'any)
  2430. (call-interactively 'my-rgrep)
  2431. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2432. )
  2433. (define-my-rgrep "ack")
  2434. (define-my-rgrep "ag")
  2435. (define-my-rgrep "rg")
  2436. (define-my-rgrep "sift")
  2437. (define-my-rgrep "gitgrep")
  2438. (define-my-rgrep "grep")
  2439. (define-my-rgrep "global")
  2440. (define-key ctl-x-map "s" 'my-rgrep)
  2441. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  2442. (defun my-occur (regexp &optional region)
  2443. "My occur command to search REGEXP to search REGION."
  2444. (interactive (list (read-string "List lines matching regexp: "
  2445. (thing-at-point 'symbol t))))
  2446. (occur regexp nil region))
  2447. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  2448. (set-variable 'dumb-jump-prefer-searcher 'rg)
  2449. (defalias 'make 'compile)
  2450. (define-key ctl-x-map "c" 'compile)
  2451. (autoload 'pb/push-item "pushbullet")
  2452. (defun my-pushbullet-note (text &optional title)
  2453. "Push TEXT with optional TITLE."
  2454. (interactive "sText to Push: ")
  2455. (pb/push-item '("") text "note" (or title "")))
  2456. ;;;;;;;;;;;;;;;;;;;
  2457. ;; peek-file-mode
  2458. (defvar peek-file-buffers
  2459. ()
  2460. "Peek buffers.")
  2461. (defun peek-file (file)
  2462. "Peek FILE."
  2463. (interactive "fFile to peek: ")
  2464. (with-current-buffer (find-file file)
  2465. (peek-file-mode)))
  2466. (define-minor-mode peek-file-mode
  2467. "Peek file mode."
  2468. :lighter "PK"
  2469. (view-mode peek-file-mode)
  2470. (add-to-list 'peek-file-buffers
  2471. (current-buffer))
  2472. (add-hook 'switch-buffer-functions
  2473. 'peek-file-remove-buffers))
  2474. (defun peek-file-remove-buffers (&args _)
  2475. "Remove peek file buffers."
  2476. (cl-dolist (buf (cl-copy-list peek-file-buffers))
  2477. (unless (get-buffer-window buf t)
  2478. (setq peek-file-buffers
  2479. (delq buf
  2480. peek-file-buffers))
  2481. (with-current-buffer buf
  2482. (when peek-file-mode
  2483. (kill-buffer))))))
  2484. (declare-function dired-get-file-for-visit "dired")
  2485. (with-eval-after-load 'dired
  2486. (defun dired-peek-file (&rest files)
  2487. "Dired `peak-file' FILES."
  2488. (interactive (list (dired-get-file-for-visit)))
  2489. (message "AAA %S" files)
  2490. (dolist (file files)
  2491. (peek-file file)))
  2492. (defvar dired-mode-map (make-sparse-keymap))
  2493. (define-key dired-mode-map "v" 'dired-peek-file))
  2494. ;;;;;;;;;;;;;;;;;;;;
  2495. ;; remember-projectile
  2496. ;; TODO: Add global-minor-mode
  2497. (defvar remember-data-file)
  2498. (defun my-set-remember-data-file-buffer-local ()
  2499. "Set `remember-data-file'."
  2500. (when (require 'projectile nil t)
  2501. (setq-local remember-data-file
  2502. (expand-file-name "remember.notes"
  2503. (projectile-project-root)))))
  2504. (add-hook 'after-change-major-mode-hook
  2505. 'my-set-remember-data-file-buffer-local)
  2506. (define-key ctl-x-map "R" 'remember)
  2507. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2508. ;; ivy
  2509. (set-variable 'ivy-format-functions-alist
  2510. '((t . (lambda (cands) (ivy--format-function-generic
  2511. (lambda (str)
  2512. (concat "+> "
  2513. (ivy--add-face str 'ivy-current-match)
  2514. ))
  2515. (lambda (str)
  2516. (concat "| " str))
  2517. cands
  2518. "\n")))))
  2519. (set-variable 'ivy-wrap t)
  2520. (set-variable 'ivy-sort-max-size 500)
  2521. (when (fboundp 'ivy-rich-mode)
  2522. (ivy-rich-mode 1))
  2523. (with-eval-after-load 'ivy
  2524. (defvar ivy-minibuffer-map)
  2525. (define-key ivy-minibuffer-map (kbd "C-u")
  2526. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  2527. (defvar ivy-sort-matches-functions-alist)
  2528. ;; (add-to-list 'ivy-sort-matches-functions-alist
  2529. ;; '(counsel-M-x . ivy--shorter-matches-first))
  2530. )
  2531. (set-variable 'ivy-on-del-error-function 'ignore)
  2532. (when (fboundp 'counsel-M-x)
  2533. (define-key esc-map "x" 'counsel-M-x)
  2534. )
  2535. (declare-function ivy-thing-at-point "ivy")
  2536. (when (and (fboundp 'ivy-read)
  2537. (locate-library "counsel"))
  2538. (defvar counsel-describe-map)
  2539. (defun my-counsel-describe-symbol ()
  2540. "Forwaord to `describe-symbol'."
  2541. (interactive)
  2542. (cl-assert (eval-and-compile (require 'help-mode nil t))) ;; describe-symbol-backends
  2543. (cl-assert (eval-and-compile (require 'counsel nil t)))
  2544. (ivy-read "Describe symbol: " obarray
  2545. ;; From describe-symbol definition
  2546. :predicate (lambda (vv)
  2547. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  2548. describe-symbol-backends))
  2549. :require-match t
  2550. :history 'counsel-describe-symbol-history
  2551. :keymap counsel-describe-map
  2552. :preselect (ivy-thing-at-point)
  2553. :action (lambda (x)
  2554. (describe-symbol (intern x)))
  2555. :caller 'my-counsel-describe-symbol))
  2556. (define-key help-map "o" 'my-counsel-describe-symbol)
  2557. )
  2558. (declare-function ivy-configure "ivy")
  2559. (with-eval-after-load 'counsel ;; Hook to counsel, not ivy
  2560. ;; (ivy-configure 'my-counsel-describe-symbol
  2561. ;; :sort-fn 'my-ivy-length)
  2562. ;; (ivy-configure 'counsel-M-x
  2563. ;; ;; :initial-input ""
  2564. ;; :sort-fn 'ivy-string<
  2565. ;; )
  2566. )
  2567. (when (fboundp 'counsel-imenu)
  2568. (define-key ctl-x-map "l" 'counsel-imenu))
  2569. (when (fboundp 'swiper)
  2570. (define-key esc-map (kbd "C-s") 'swiper))
  2571. (with-eval-after-load 'ivy
  2572. ;; ivy-prescient requires counsel already loaded
  2573. (require 'counsel nil t)
  2574. (when (fboundp 'ivy-prescient-mode)
  2575. ;; (set-variable 'prescient-sort-length-enable t)
  2576. ;; (set-variable 'prescient-sort-full-matches-first t)
  2577. ;; (set-variable 'ivy-prescient-enable-filtering t)
  2578. ;; (set-variable 'ivy-prescient-enable-sorting nil)
  2579. ;; (set-variable 'ivy-prescient-sort-commands t)
  2580. (set-variable 'prescient-filter-method
  2581. '(literal prefix literal-prefix regexp initialism fuzzy))
  2582. (when (fboundp 'prescient-persist-mode)
  2583. (prescient-persist-mode t))
  2584. (ivy-prescient-mode 1)
  2585. ;; (set-variable 'ivy-sort-functions-alist
  2586. ;; '((t . ivy-string<)))
  2587. ))
  2588. ;; ?
  2589. (define-key input-decode-map "\e[1;5C" [C-right])
  2590. (define-key input-decode-map "\e[1;5D" [C-left])
  2591. ;;;;;;;;;;;;;;;;;;;;;;;;
  2592. ;; mozc
  2593. (global-set-key (kbd "C-c m e") 'ignore)
  2594. (global-set-key (kbd "C-c m d") 'ignore)
  2595. ;; mozc
  2596. (when (locate-library "mozc")
  2597. ;; https://tottoto.net/mac-emacs-karabiner-elements-japanese-input-method-config/
  2598. (with-eval-after-load 'mozc
  2599. ;; (define-key mozc-mode-map (kbd "C-h") 'backward-delete-char)
  2600. ;; (define-key mozc-mode-map (kbd "C-p") (kbd "<up>"))
  2601. ;; (define-key mozc-mode-map (kbd "C-n") (kbd "SPC"))
  2602. )
  2603. (setq default-input-method "japanese-mozc")
  2604. (custom-set-variables '(mozc-leim-title "あ"))
  2605. (defun turn-on-input-method ()
  2606. (interactive)
  2607. (activate-input-method default-input-method))
  2608. (defun turn-off-input-method ()
  2609. (interactive)
  2610. (deactivate-input-method))
  2611. ;; (setq mozc-candidate-style 'echo-area)
  2612. (global-set-key (kbd "C-c m e") 'turn-on-input-method)
  2613. (global-set-key (kbd "C-c m d") 'turn-off-input-method)
  2614. (global-set-key (kbd "<f7>") 'turn-off-input-method)
  2615. (global-set-key (kbd "<f8>") 'turn-on-input-method)
  2616. (require 'mozc-popup)
  2617. (set-variable 'mozc-candidate-style 'popup)
  2618. ;; これいる?
  2619. (when (require 'mozc-im nil t)
  2620. (setq default-input-method "japanese-mozc-im")
  2621. ;; (global-set-key (kbd "C-j") 'toggle-input-method)
  2622. )
  2623. )
  2624. (defun browse-url-macosx-vivaldi-browser (url &rest args)
  2625. "Invoke the macOS Vlvaldi Web browser with URL.
  2626. ARGS are not used."
  2627. (interactive (browse-url-interactive-arg "URL: "))
  2628. (start-process (concat "vivaldi " url)
  2629. nil
  2630. "/Applications/Vivaldi.app/Contents/MacOS/Vivaldi"
  2631. url))
  2632. (declare-function vterm "vterm")
  2633. ;; 前の実行結果を残したまま次のコマンドを実行する方法はあるだろうか
  2634. (defun my-vterm-cmd (command)
  2635. "Start arbitrary command in vterm buffer."
  2636. (interactive "sCommand: ")
  2637. (let ((vterm-shell command)
  2638. (vterm-buffer-name "*vterm-cmd*")
  2639. (vterm-kill-buffer-on-exit nil))
  2640. (when (get-buffer vterm-buffer-name)
  2641. (kill-buffer (get-buffer vterm-buffer-name)))
  2642. (vterm)))
  2643. ;; (setq vterm-shell "bash -l")
  2644. ;; (setq vterm-kill-buffer-on-exit nil)
  2645. ;; ;; (setq vterm-term-environment-variable "screen-256color")
  2646. ;; これいつ動くの?
  2647. ;; 自動で project を switch させる方法はある?
  2648. (add-hook 'projectile-after-switch-project-hook
  2649. (lambda ()
  2650. (message "Projecttile switched to: %s"
  2651. (projectile-project-root))))
  2652. (when (fboundp 'projectile-mode)
  2653. (projectile-mode 1))
  2654. ;; (with-eval-after-load 'eglot
  2655. ;; (when (fboundp 'with-venv-advice-add)
  2656. ;; (with-venv-advice-add 'eglot--executable-find))
  2657. ;; (set-variable 'eldoc-echo-area-use-multiline-p nil)
  2658. ;; (set-variable 'eglot-extend-to-xref t))
  2659. (set-variable 'lsp-python-ms-auto-install-server t)
  2660. (set-variable 'lsp-python-ms-parse-dot-env-enabled t)
  2661. (set-variable 'lsp-python-ms-python-executable-cmd "python3")
  2662. ;; (add-hook 'python-mode-hook #'my-lsp-python-setup)
  2663. (defun my-lsp-python-setup ()
  2664. "Setup python ms."
  2665. (when (and (fboundp 'lsp)
  2666. (require 'lsp-python-ms nil t))
  2667. (lsp)))
  2668. (set-variable 'awk-preview-default-program
  2669. "# C-c C-l: Update preview C-c C-c: Commit and exit
  2670. # C-c C-r: Resest to original C-c C-k: Abort
  2671. BEGIN {
  2672. # FS = \",\"
  2673. }
  2674. {
  2675. # Replace string
  2676. # gsub(BEFORE, AFTER, $0)
  2677. print NR, $0
  2678. }
  2679. ")
  2680. '(progn
  2681. ;; https://web.sfc.wide.ad.jp/~sagawa/gnujdoc/elisp-manual-20-2.5/elisp-ja_39.html#SEC629
  2682. ;; https://emacs.stackexchange.com/questions/15078/inserting-before-an-after-string-overlay
  2683. ;; https://emacs.stackexchange.com/questions/3030/temporary-text-in-window-location-with-no-text-to-propertize-overlay
  2684. ;; https://ayatakesi.github.io/lispref/26.3/html/Overlay-Properties.html
  2685. ;; https://ayatakesi.github.io/lispref/26.3/html/Special-Properties.html#Special-Properties
  2686. ;; https://ayatakesi.github.io/lispref/28.1/html/Attribute-Functions.html
  2687. ;; https://ayatakesi.github.io/lispref/26.3/html/Display-Margins.html#Display-Margins
  2688. (setq my-ov (make-overlay (pos-bol) (pos-eol)))
  2689. (defface my-ov-face () "Face for my-ov.")
  2690. (set-face-attribute 'my-ov-face
  2691. nil
  2692. :background nil)
  2693. (set-face-attribute 'my-ov-face
  2694. nil
  2695. :foreground "yellow")
  2696. (let* ((ov my-ov)
  2697. (s (propertize "<"
  2698. 'face
  2699. 'my-ov-face))
  2700. (s (propertize " "
  2701. 'display
  2702. `((margin right-margin) ,s))))
  2703. (overlay-put ov 'after-string s)
  2704. ;; (overlay-put ov 'after-string
  2705. ;; (concat (propertize " " 'display
  2706. ;; '(space :align-to (+ left-fringe 1)))
  2707. ;; (propertize "*" 'display
  2708. ;; '(raise -1))
  2709. ;; ))
  2710. ;; s
  2711. ;; (setq left-margin-width 1)
  2712. ;; (setq right-margin-width 1)
  2713. ;; (set-window-buffer (get-buffer-window) (current-buffer))
  2714. ;; (window-margins (get-buffer-window))
  2715. (let ((win (get-buffer-window)))
  2716. (set-window-margins (get-buffer-window)
  2717. (car (window-margins win))
  2718. 1))
  2719. )
  2720. (progn
  2721. (overlay-put my-ov 'after-string nil)
  2722. (overlay-put my-ov 'before-string nil)
  2723. )
  2724. )
  2725. (message "Emacs started at %s"
  2726. (current-time-string))
  2727. (run-with-idle-timer (* 3 60 60) ;; 3 hours
  2728. t
  2729. (lambda ()
  2730. (message "Emacs does nothing for 3 hours: %s"
  2731. (current-time-string))))
  2732. ;; https://emacs-jp.github.io/tips/startup-optimization
  2733. ;; Restore to original value
  2734. (setq gc-cons-threshold my-orig-gc-cons-threshold)
  2735. (setq file-name-handler-alist my-orig-file-name-handler-alist)
  2736. (when (getenv "_EMACS_EL_PROFILE")
  2737. (profiler-report)
  2738. (profiler-stop))
  2739. ;; Local Variables:
  2740. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2741. ;; flycheck-checker: emacs-lisp
  2742. ;; End:
  2743. ;;; emancs.el ends here