Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

3199 righe
107 KiB

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