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

3191 lines
106 KiB

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