Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

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