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.
 
 
 
 
 
 

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