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.
 
 
 
 
 
 

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