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

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