Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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