25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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