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

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