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

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