您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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