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

3115 lines
104 KiB

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