25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2241 lines
73 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. ;; TODO: Use custom-set-variables in place of set-variable
  7. (setq debug-on-error t)
  8. ;; make directories
  9. (unless (file-directory-p (expand-file-name user-emacs-directory))
  10. (make-directory (expand-file-name user-emacs-directory)))
  11. (require 'cl-lib)
  12. (require 'simple)
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;; Some macros for internals
  15. ;; TODO: Rename to eval-after-init
  16. (defmacro call-after-init (&rest body)
  17. "If `after-init-hook' has been run, run BODY immediately.
  18. Otherwize hook it."
  19. (declare (indent 0) (debug t))
  20. `(if after-init-time
  21. ;; Currently after-init-hook is run just after setting after-init-hook
  22. (progn
  23. ,@body)
  24. (add-hook 'after-init-hook
  25. (lambda ()
  26. ,@body))))
  27. ;; TODO: Remove: Just checking fboundp should be enough for most cases
  28. (defmacro safe-require-or-eval (feature)
  29. "Require FEATURE if available.
  30. At compile time the feature will be loaded immediately."
  31. `(eval-and-compile
  32. (message "safe-require-or-eval: Trying to require %s" ,feature)
  33. (require ,feature nil t)))
  34. ;; TODO: Remove: Defining autoload is no more needed for most cases
  35. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  36. "Define autoloading FEATURE that defines FUNCTIONS.
  37. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  38. the function same as FEATURE is defined as autoloaded function. BODY is passed
  39. to `eval-after-load'.
  40. After this macro is expanded, this returns the path to library if FEATURE
  41. found, otherwise returns nil."
  42. (declare (indent 2) (debug t))
  43. (let* ((libname (symbol-name (eval feature)))
  44. (libpath (locate-library libname)))
  45. `(progn
  46. (when (locate-library ,libname)
  47. ,@(mapcar (lambda (f)
  48. `(unless (fboundp ',f)
  49. (progn
  50. (message "Autoloaded function `%S' defined (%s)"
  51. (quote ,f)
  52. ,libpath)
  53. (autoload (quote ,f)
  54. ,libname
  55. ,(concat "Autoloaded function defined in \""
  56. libpath
  57. "\".")
  58. t))))
  59. (or (eval functions)
  60. `(,(eval feature)))))
  61. (eval-after-load ,feature
  62. (quote (progn
  63. ,@body)))
  64. (locate-library ,libname))))
  65. (when (autoload-eval-lazily 'tetris nil
  66. (message "Tetris loaded!"))
  67. (message "Tetris found!"))
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. ;; package
  70. (set (defvar 10sr-package-list)
  71. '(
  72. vimrc-mode
  73. markdown-mode
  74. yaml-mode
  75. gnuplot-mode
  76. php-mode
  77. erlang
  78. js2-mode
  79. js-doc
  80. git-commit
  81. gitignore-mode
  82. adoc-mode
  83. go-mode
  84. ;; It seems malabar has been merged into jdee and this package
  85. ;; already removed
  86. ;; malabar-mode
  87. gosh-mode
  88. scala-mode
  89. ;;ensime
  90. web-mode
  91. color-moccur
  92. ggtags
  93. flycheck
  94. auto-highlight-symbol
  95. hl-todo
  96. ;; Currently not available
  97. ;; pp-c-l
  98. xclip
  99. foreign-regexp
  100. multi-term
  101. term-run
  102. editorconfig
  103. git-ps1-mode
  104. restart-emacs
  105. fill-column-indicator
  106. pkgbuild-mode
  107. minibuffer-line
  108. which-key
  109. ;; I think this works in place of my autosave lib
  110. super-save
  111. pipenv
  112. imenu-list
  113. page-break-lines
  114. aggressive-indent
  115. dired-filter
  116. wgrep
  117. magit
  118. git-gutter
  119. end-mark
  120. sl
  121. ;; TODO: Configure pony-tpl-mode
  122. pony-mode
  123. gited
  124. highlight-indentation
  125. diminish
  126. fzf
  127. fic-mode
  128. term-cursor
  129. editorconfig
  130. editorconfig-custom-majormode
  131. git-command
  132. prompt-text
  133. ;; 10sr repository
  134. ;; 10sr-extras
  135. terminal-title
  136. dired-list-all-mode
  137. pack
  138. set-modeline-color
  139. read-only-only-mode
  140. smart-revert
  141. autosave
  142. ;;window-organizer
  143. ilookup
  144. pasteboard
  145. awk-preview
  146. recently
  147. ))
  148. (when (safe-require-or-eval 'package)
  149. (setq package-archives
  150. `(,@package-archives
  151. ("melpa" . "https://melpa.org/packages/")
  152. ;; Somehow fails to download via https
  153. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  154. (package-initialize)
  155. (defun my-auto-install-package ()
  156. "Install packages semi-automatically."
  157. (interactive)
  158. (package-refresh-contents)
  159. (mapc (lambda (pkg)
  160. (or (package-installed-p pkg)
  161. (package-install pkg)))
  162. 10sr-package-list))
  163. )
  164. ;; (lazy-load-eval 'sudoku)
  165. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  166. ;; my-idle-hook
  167. (defvar my-idle-hook nil
  168. "Hook run when idle for several secs.")
  169. (defvar my-idle-hook-sec 5
  170. "Second to run `my-idle-hook'.")
  171. (run-with-idle-timer my-idle-hook-sec
  172. t
  173. (lambda ()
  174. (run-hooks 'my-idle-hook)))
  175. ;; (add-hook 'my-idle-hook
  176. ;; (lambda ()
  177. ;; (message "idle hook message")))
  178. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  179. ;; start and quit
  180. (setq inhibit-startup-message t)
  181. (setq initial-buffer-choice 'messages-buffer)
  182. (setq confirm-kill-emacs 'y-or-n-p)
  183. (setq gc-cons-threshold (* 1024 1024 16))
  184. (setq garbage-collection-messages nil)
  185. (when window-system
  186. (add-to-list 'default-frame-alist '(cursor-type . box))
  187. (add-to-list 'default-frame-alist '(background-color . "white"))
  188. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  189. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  190. ;; does not work?
  191. )
  192. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  193. (menu-bar-mode 1)
  194. (define-key ctl-x-map "m" '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. (call-after-init
  200. (message "%s %s" invocation-name emacs-version)
  201. (message "Invocation 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. (with-current-buffer "*Messages*"
  207. (emacs-lock-mode 'kill))
  208. (cd ".") ; when using windows use / instead of \ in `default-directory'
  209. ;; locale
  210. (set-language-environment "Japanese")
  211. (set-default-coding-systems 'utf-8-unix)
  212. (prefer-coding-system 'utf-8-unix)
  213. (setq system-time-locale "C")
  214. ;; my prefix map
  215. (defvar my-prefix-map nil
  216. "My prefix map.")
  217. (define-prefix-command 'my-prefix-map)
  218. (global-set-key (kbd "C-^") 'my-prefix-map)
  219. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  220. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  221. ;; (comint-show-maximum-output)
  222. ;; kill scratch
  223. (call-after-init
  224. (let ((buf (get-buffer "*scratch*")))
  225. (when buf
  226. (kill-buffer buf))))
  227. ;; modifier keys
  228. ;; (setq mac-option-modifier 'control)
  229. ;; display
  230. (setq visible-bell t)
  231. (setq ring-bell-function 'ignore)
  232. (mouse-avoidance-mode 'banish)
  233. (setq echo-keystrokes 0.1)
  234. (defun reload-init-file ()
  235. "Reload Emacs init file."
  236. (interactive)
  237. (when (and user-init-file
  238. (file-readable-p user-init-file))
  239. (load-file user-init-file)))
  240. (safe-require-or-eval 'session)
  241. ;; server
  242. (set-variable 'server-name (concat "server"
  243. (number-to-string (emacs-pid))))
  244. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  245. ;; In Darwin environment, init fails with message like 'Service name too long'
  246. ;; when server-use-tcp is nil
  247. (when (or (eq system-type
  248. 'cygwin)
  249. (eq system-type
  250. 'darwin))
  251. (set-variable 'server-use-tcp t))
  252. ;; MSYS2 fix
  253. (when (eq system-type
  254. 'windows-nt)
  255. (setq shell-file-name
  256. (executable-find "bash"))
  257. '(setq function-key-map
  258. `(,@function-key-map ([pause] . [?\C-c])
  259. ))
  260. (define-key key-translation-map
  261. (kbd "<pause>")
  262. (kbd "C-c"))
  263. '(keyboard-translate [pause]
  264. (kbd "C-c")p)
  265. ;; TODO: move to other place later
  266. (when (not window-system)
  267. (setq interprogram-paste-function nil)
  268. (setq interprogram-cut-function nil)))
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270. ;; global keys
  271. (global-set-key (kbd "<up>") 'scroll-down-line)
  272. (global-set-key (kbd "<down>") 'scroll-up-line)
  273. (global-set-key (kbd "<left>") 'scroll-down)
  274. (global-set-key (kbd "<right>") 'scroll-up)
  275. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  276. (global-set-key (kbd "C-\\") help-map)
  277. (define-key ctl-x-map (kbd "DEL") help-map)
  278. (define-key ctl-x-map (kbd "C-h") help-map)
  279. (define-key help-map "a" 'apropos)
  280. ;; disable annoying keys
  281. (global-set-key [prior] 'ignore)
  282. (global-set-key (kbd "<next>") 'ignore)
  283. (global-set-key [menu] 'ignore)
  284. (global-set-key [down-mouse-1] 'ignore)
  285. (global-set-key [down-mouse-2] 'ignore)
  286. (global-set-key [down-mouse-3] 'ignore)
  287. (global-set-key [mouse-1] 'ignore)
  288. (global-set-key [mouse-2] 'ignore)
  289. (global-set-key [mouse-3] 'ignore)
  290. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  291. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  292. ;; Interactively evaluate Emacs Lisp expressions
  293. (define-key ctl-x-map "i" 'ielm)
  294. (when (safe-require-or-eval 'which-key)
  295. (which-key-mode))
  296. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  297. ;; editor
  298. ;; Basically it should not set globally (instead use something like file local
  299. ;; variables or editorconfig), but for most cases I just need this...
  300. (setq-default require-final-newline t)
  301. (defun my-set-require-final-newline ()
  302. "Set `require-final-newline'."
  303. (set (make-local-variable 'require-final-newline)
  304. mode-require-final-newline))
  305. (add-hook 'prog-mode-hook
  306. 'my-set-require-final-newline)
  307. (add-hook 'conf-mode-hook
  308. 'my-set-require-final-newline)
  309. ;; Used from term-cursor
  310. ;; hbar is too hard to find...
  311. (defun my-cursor-type-change (&rest args)
  312. "ARGS are discarded."
  313. ;; TODO: Support wdired and wgrep
  314. (if buffer-read-only
  315. (setq cursor-type 'hbar)
  316. (setq cursor-type 'box)))
  317. ;; (add-hook 'switch-buffer-functions
  318. ;; 'my-cursor-type-change)
  319. ;; (add-hook 'read-only-mode-hook
  320. ;; 'my-cursor-type-change)
  321. ;; (when (fboundp 'global-term-cursor-mode)
  322. ;; (global-term-cursor-mode 1))
  323. ;; ;; (term-cursor--eval)
  324. (setq kill-whole-line t)
  325. (setq scroll-conservatively 35
  326. scroll-margin 2)
  327. (setq-default major-mode 'text-mode)
  328. (setq next-line-add-newlines nil)
  329. (setq kill-read-only-ok t)
  330. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  331. ;; (setq-default line-spacing 0.2)
  332. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  333. ;; (setq-default tab-width 4)
  334. (setq-default indent-tabs-mode nil)
  335. (setq-default indent-line-function 'indent-to-left-margin)
  336. ;; (setq-default indent-line-function nil)
  337. (setq-default truncate-lines nil)
  338. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  339. (delete-selection-mode 1)
  340. (cua-mode 0)
  341. (setq line-move-visual nil)
  342. (setq create-lockfiles nil)
  343. (add-hook 'before-save-hook
  344. 'time-stamp)
  345. ;; Add Time-stamp: <> to insert timestamp there
  346. (set-variable 'time-stamp-format
  347. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  348. ;; key bindings
  349. ;; moving around
  350. ;;(keyboard-translate ?\M-j ?\C-j)
  351. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  352. (define-key esc-map "p" 'backward-paragraph)
  353. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  354. (define-key esc-map "n" 'forward-paragraph)
  355. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  356. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  357. (global-set-key (kbd "C-<left>") 'scroll-down)
  358. (global-set-key (kbd "C-<right>") 'scroll-up)
  359. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  360. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  361. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  362. ;; C-h and DEL
  363. (global-set-key (kbd "C-h") (kbd "DEL"))
  364. ;; (normal-erase-is-backspace-mode 1)
  365. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  366. (global-set-key (kbd "C-m") 'newline-and-indent)
  367. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  368. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  369. (define-key esc-map "u" 'undo)
  370. (define-key esc-map "i" (kbd "ESC TAB"))
  371. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  372. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  373. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  374. (require 'page-ext nil t)
  375. (when (safe-require-or-eval 'page-break-lines)
  376. (global-page-break-lines-mode 1))
  377. (when (safe-require-or-eval 'git-gutter)
  378. (declare-function global-git-gutter-mode "git-gutter")
  379. (custom-set-variables
  380. '(git-gutter:lighter " Gttr"))
  381. (custom-set-variables
  382. '(git-gutter:update-interval 2))
  383. (custom-set-variables
  384. '(git-gutter:unchanged-sign " "))
  385. (when (>= (display-color-cells)
  386. 256)
  387. (let ((c "color-233"))
  388. (set-face-background 'git-gutter:modified c)
  389. (set-face-background 'git-gutter:added c)
  390. (set-face-background 'git-gutter:deleted c)
  391. (set-face-background 'git-gutter:unchanged c)))
  392. (global-git-gutter-mode 1)
  393. )
  394. ;; (when (safe-require-or-eval 'fancy-narrow)
  395. ;; (fancy-narrow-mode 1))
  396. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  397. ;; title and mode-line
  398. (when (safe-require-or-eval 'terminal-title)
  399. ;; if TERM is not screen use default value
  400. (if (getenv "TMUX")
  401. ;; if use tmux locally just basename of current dir
  402. (set-variable 'terminal-title-format
  403. '((file-name-nondirectory (directory-file-name
  404. default-directory))))
  405. (if (and (let ((tty-type (frame-parameter nil
  406. 'tty-type)))
  407. (and tty-type
  408. (equal (car (split-string tty-type
  409. "-"))
  410. "screen")))
  411. (not (getenv "SSH_CONNECTION")))
  412. (set-variable 'terminal-title-format
  413. '((file-name-nondirectory (directory-file-name
  414. default-directory))))
  415. ;; seems that TMUX is used locally and ssh to remote host
  416. (set-variable 'terminal-title-format
  417. `("em:"
  418. ,user-login-name
  419. "@"
  420. ,(car (split-string (system-name)
  421. "\\."))
  422. ":"
  423. default-directory))
  424. )
  425. )
  426. (terminal-title-mode))
  427. (setq eol-mnemonic-dos "\\r\\n")
  428. (setq eol-mnemonic-mac "\\r")
  429. (setq eol-mnemonic-unix "")
  430. (which-function-mode 1)
  431. (line-number-mode 0)
  432. (column-number-mode 0)
  433. (size-indication-mode 0)
  434. (setq mode-line-position
  435. '(:eval (format ":%%l:%%c /%d%s"
  436. (count-lines (point-max)
  437. (point-min))
  438. (if (buffer-narrowed-p)
  439. "[N]"
  440. "")
  441. )))
  442. (when (safe-require-or-eval 'diminish)
  443. ;; FIXME: Eval after enabling mode
  444. (call-after-init
  445. (diminish 'recently-mode)
  446. (diminish 'editorconfig-mode)
  447. (diminish 'auto-highlight-symbol-mode)
  448. (diminish 'global-whitespace-mode)
  449. (diminish 'which-key-mode)
  450. (diminish 'page-break-lines-mode)
  451. (diminish 'highlight-indentation-mode)))
  452. (setq mode-line-front-space "")
  453. ;; (setq mode-line-end-spaces "")
  454. ;; Set current frame name to empty string
  455. (setq-default mode-line-format
  456. (let* ((l mode-line-format)
  457. (l (cl-substitute " " " "
  458. l
  459. :test 'equal))
  460. (l (cl-substitute " " " "
  461. l
  462. :test 'equal))
  463. )
  464. l))
  465. (set-frame-parameter nil 'name "")
  466. ;; See color-name-rgb-alist for available color names
  467. ;; http://www.raebear.net/computers/emacs-colors/
  468. ;; https://www.emacswiki.org/emacs/ListColors
  469. ;; (list-colors-display is not a complete list)
  470. (defconst my-mode-line-background-default
  471. (face-background 'mode-line)
  472. "Default color of mode-line at init.")
  473. (defun my-mode-line-color-update (&rest args)
  474. "ARGS are discarded"
  475. (let ((ro "skyblue")
  476. (rw my-mode-line-background-default))
  477. (if (or (not buffer-read-only)
  478. (and (eq major-mode 'wdired-mode)))
  479. (set-face-background 'mode-line
  480. rw)
  481. (set-face-background 'mode-line
  482. ro))))
  483. (add-hook 'switch-buffer-functions
  484. 'my-mode-line-color-update)
  485. (add-hook 'read-only-mode-hook
  486. 'my-mode-line-color-update)
  487. (add-hook 'wdired-mode-hook
  488. 'my-mode-line-color-update)
  489. (advice-add 'wdired-change-to-dired-mode
  490. :after
  491. 'my-mode-line-color-update)
  492. (advice-add 'wgrep-change-to-wgrep-mode
  493. :after
  494. 'my-mode-line-color-update)
  495. (advice-add 'wgrep-to-original-mode
  496. :after
  497. 'my-mode-line-color-update)
  498. (set-face-background 'header-line
  499. my-mode-line-background-default)
  500. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  501. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  502. ;; minibuffer
  503. (setq insert-default-directory t)
  504. (setq completion-ignore-case t
  505. read-file-name-completion-ignore-case t
  506. read-buffer-completion-ignore-case t)
  507. (setq resize-mini-windows t)
  508. (temp-buffer-resize-mode 1)
  509. (savehist-mode 1)
  510. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  511. (set-variable 'help-at-pt-display-when-idle t)
  512. (fset 'yes-or-no-p 'y-or-n-p)
  513. ;; complete symbol when `eval'
  514. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  515. (define-key minibuffer-local-map (kbd "C-u")
  516. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  517. ;; I dont know these bindings are good
  518. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  519. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  520. (when (safe-require-or-eval 'minibuffer-line)
  521. (set-face-underline 'minibuffer-line nil)
  522. (set-variable 'minibuffer-line-refresh-interval
  523. 25)
  524. ;; Set idle timer
  525. (defvar my-minibuffer-line--idle-timer nil)
  526. (defvar minibuffer-line-mode)
  527. (add-hook 'minibuffer-line-mode-hook
  528. (lambda ()
  529. (when my-minibuffer-line--idle-timer
  530. (cancel-timer my-minibuffer-line--idle-timer)
  531. (setq my-minibuffer-line--idle-timer nil))
  532. (when minibuffer-line-mode
  533. (setq my-minibuffer-line--idle-timer
  534. (run-with-idle-timer 0.5
  535. t
  536. 'minibuffer-line--update)))))
  537. (set-variable 'minibuffer-line-format
  538. `(,(concat user-login-name
  539. "@"
  540. (car (split-string (system-name)
  541. "\\."))
  542. ":")
  543. (:eval (abbreviate-file-name (or buffer-file-name
  544. default-directory)))
  545. (:eval (and (fboundp 'git-ps1-mode-get-current)
  546. (git-ps1-mode-get-current " [GIT:%s]")))
  547. " "
  548. (:eval (format-time-string display-time-format))))
  549. (minibuffer-line-mode 1)
  550. )
  551. (when (safe-require-or-eval 'prompt-text)
  552. (set-variable 'prompt-text-format
  553. `(,(concat ""
  554. user-login-name
  555. "@"
  556. (car (split-string (system-name)
  557. "\\."))
  558. ":")
  559. (:eval (abbreviate-file-name (or buffer-file-name
  560. default-directory)))
  561. (:eval (and (fboundp 'git-ps1-mode-get-current)
  562. (git-ps1-mode-get-current " [GIT:%s]")))
  563. " "
  564. (:eval (format-time-string display-time-format))
  565. "\n"
  566. (:eval (symbol-name this-command))
  567. ": "))
  568. (prompt-text-mode 1))
  569. (autoload-eval-lazily 'helm nil
  570. (defvar helm-map)
  571. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  572. (setq-default header-line-format
  573. '(:eval (let ((f (or (buffer-file-name)
  574. default-directory)))
  575. (when f
  576. (abbreviate-file-name f)))))
  577. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  578. ;; letters, font-lock mode and fonts
  579. (setq text-quoting-style 'grave)
  580. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  581. ;; (set-window-margins (selected-window) 1 1)
  582. (unless window-system
  583. (setq frame-background-mode 'dark))
  584. (and (or (eq system-type 'Darwin)
  585. (eq system-type 'darwin))
  586. (fboundp 'mac-set-input-method-parameter)
  587. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  588. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  589. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  590. (boundp 'input-method-inactivate-hook))
  591. (add-hook 'input-method-activate-hook
  592. (lambda () (set-cursor-color "red")))
  593. (add-hook 'input-method-inactivate-hook
  594. (lambda () (set-cursor-color "black"))))
  595. (when (safe-require-or-eval 'paren)
  596. (show-paren-mode 1)
  597. (setq show-paren-delay 0.5
  598. show-paren-style 'parenthesis) ; mixed is hard to read
  599. ;; (set-face-background 'show-paren-match
  600. ;; "black")
  601. ;; ;; (face-foreground 'default))
  602. ;; (set-face-foreground 'show-paren-match
  603. ;; "white")
  604. ;; (set-face-inverse-video-p 'show-paren-match
  605. ;; t)
  606. )
  607. (transient-mark-mode 1)
  608. (global-font-lock-mode 1)
  609. (setq font-lock-global-modes
  610. '(not
  611. help-mode
  612. eshell-mode
  613. ;;term-mode
  614. Man-mode
  615. magit-diff-mode
  616. magit-revision-mode))
  617. ;; (standard-display-ascii ?\n "$\n")
  618. ;; (defvar my-eol-face
  619. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  620. ;; )
  621. ;; (defvar my-tab-face
  622. ;; '(("\t" . '(0 highlight t nil))))
  623. (defvar my-jspace-face
  624. '(("\u3000" . '(0 highlight t nil))))
  625. (add-hook 'font-lock-mode-hook
  626. (lambda ()
  627. ;; (font-lock-add-keywords nil my-eol-face)
  628. (font-lock-add-keywords nil my-jspace-face)
  629. ))
  630. (when (safe-require-or-eval 'whitespace)
  631. (add-to-list 'whitespace-display-mappings
  632. ;; We need t since last one takes precedence
  633. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  634. ;; (add-to-list 'whitespace-display-mappings
  635. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  636. (setq whitespace-style '(face
  637. trailing ; trailing blanks
  638. ;; tabs
  639. ;; spaces
  640. ;; lines
  641. lines-tail ; lines over 80
  642. newline ; newlines
  643. ;; empty ; empty lines at beg or end of buffer
  644. ;; big-indent
  645. ;; space-mark
  646. tab-mark
  647. newline-mark ; use display table for newline
  648. ))
  649. ;; (setq whitespace-newline 'font-lock-comment-face)
  650. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  651. (defun my-whitesspace-mode-reload ()
  652. "Reload whitespace-mode config."
  653. (interactive)
  654. (when whitespace-mode
  655. (whitespace-mode 0)
  656. (whitespace-mode 1)))
  657. (set-variable 'whitespace-line-column nil)
  658. (global-whitespace-mode t)
  659. (add-hook 'dired-mode-hook
  660. (lambda ()
  661. (set (make-local-variable 'whitespace-style) nil)))
  662. (if (>= (display-color-cells)
  663. 256)
  664. (set-face-foreground 'whitespace-newline "color-109")
  665. ;; (progn
  666. ;; (set-face-bold-p 'whitespace-newline
  667. ;; t))
  668. ))
  669. (and nil
  670. '(safe-require-or-eval 'fill-column-indicator)
  671. (setq fill-column-indicator))
  672. (defun my-gen-hl-line-color-dark ()
  673. "Generate color for current line in black background."
  674. (let* ((candidates (mapcar 'number-to-string (number-sequence 0 6)))
  675. (limit (length candidates)))
  676. (format "#%s%s%s"
  677. (nth (random limit) candidates)
  678. (nth (random limit) candidates)
  679. (nth (random limit) candidates)
  680. )))
  681. ;; (my-gen-hl-line-color-dark)
  682. ;; highlight current line
  683. ;; http://wiki.riywo.com/index.php?Meadow
  684. (face-spec-set 'hl-line
  685. `((((min-colors 256)
  686. (background dark))
  687. ;; Rotate midnightblue
  688. (:background ,(my-gen-hl-line-color-dark)))
  689. (((min-colors 256)
  690. (background light))
  691. ;; TODO: What is should be?
  692. (:background "color-234"))
  693. (t
  694. (:underline "black"))))
  695. (set-variable 'hl-line-global-modes
  696. '(not
  697. term-mode))
  698. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  699. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  700. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  701. ;;(safe-require-or-eval 'set-modeline-color)
  702. ;; (let ((fg (face-foreground 'default))
  703. ;; (bg (face-background 'default)))
  704. ;; (set-face-background 'mode-line-inactive
  705. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  706. ;; (set-face-foreground 'mode-line-inactive
  707. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  708. ;; (set-face-underline 'mode-line-inactive
  709. ;; t)
  710. ;; (set-face-underline 'vertical-border
  711. ;; nil)
  712. ;; (when (safe-require-or-eval 'end-mark)
  713. ;; (global-end-mark-mode))
  714. ;; M-x highlight-* to highlight things
  715. (global-hi-lock-mode 1)
  716. (unless (fboundp 'highlight-region-text)
  717. (defun highlight-region-text (beg end)
  718. "Highlight text between BEG and END."
  719. (interactive "r")
  720. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  721. end)))
  722. (setq deactivate-mark t)))
  723. (when (safe-require-or-eval 'auto-highlight-symbol)
  724. (set-variable 'ahs-idle-interval 0.6)
  725. (global-auto-highlight-symbol-mode 1))
  726. (when (safe-require-or-eval 'highlight-indentation)
  727. (set-face-background 'highlight-indentation-face "color-236")
  728. (dolist (hook
  729. '(
  730. prog-mode-hook
  731. text-mode-hook
  732. ))
  733. (add-hook hook
  734. 'highlight-indentation-mode)))
  735. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  736. (when (fboundp 'fic-mode)
  737. (add-hook 'prog-mode-hook
  738. 'fic-mode))
  739. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  740. ;; file handling
  741. (auto-insert-mode 1)
  742. ;; fzf
  743. ;; Too slow in term buffer!
  744. ;; (set-variable 'fzf/executable "sk")
  745. ;; (set-variable 'fzf/args "--color bw --print-query")
  746. ;; Modified from hardcoded default to include:
  747. ;; - directories
  748. ;; - hidden files
  749. ;; - root directory (.)
  750. ;; - parent directory (..)
  751. ;; ripgrep cannot list directories...
  752. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  753. (let* ((find (if (executable-find "bfs")
  754. ;; Breadth-first find https://github.com/tavianator/bfs
  755. "bfs"
  756. ;; Use gfind if available?
  757. "find"))
  758. (findcmd (concat "set -eu; set -o pipefail; "
  759. "echo .; "
  760. "echo ..; "
  761. "command " find " -L . "
  762. "-mindepth 1 "
  763. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  764. "-o -print "
  765. "2> /dev/null "
  766. "| "
  767. "cut -b3-"))
  768. (fdcmd (concat "set -eu; set -o pipefail; "
  769. "echo .; "
  770. "echo ..; "
  771. "command fd "
  772. "--follow --hidden --no-ignore "
  773. "--color always "
  774. "2>/dev/null")))
  775. (if (executable-find "fd")
  776. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  777. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  778. (set-variable 'fzf/window-height 45)
  779. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  780. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  781. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  782. (defun my-fzf-or-find-file ()
  783. "Call fzf if usable or call find-file."
  784. (declare (interactive-only t))
  785. (interactive)
  786. (if (and (executable-find "fzf")
  787. (fboundp 'fzf)
  788. (not (file-remote-p default-directory)))
  789. (fzf)
  790. (call-interactively 'find-file)))
  791. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  792. (defun my-fzf-all-lines ()
  793. "Fzf all lines."
  794. (interactive)
  795. (let ((process-environment (cl-copy-list process-environment)))
  796. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  797. (fzf)))
  798. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  799. ;; recently
  800. (when (safe-require-or-eval 'recently)
  801. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  802. (set-variable 'recently-max 1000)
  803. (recently-mode 1))
  804. (when (safe-require-or-eval 'editorconfig)
  805. (set-variable 'editorconfig-get-properties-function
  806. 'editorconfig-core-get-properties-hash)
  807. (editorconfig-mode 1)
  808. (set-variable 'editorconfig-mode-lighter "")
  809. (when (fboundp 'ws-butler-mode)
  810. (set-variable 'editorconfig-trim-whitespaces-mode
  811. 'ws-butler-mode))
  812. (with-eval-after-load 'org-src
  813. ;; [*.org\[\*Org Src*\[ c \]*\]]
  814. (add-hook 'org-src-mode-hook
  815. 'editorconfig-mode-apply t)))
  816. (when (fboundp 'editorconfig-custom-majormode)
  817. (add-hook 'editorconfig-after-apply-functions
  818. 'editorconfig-custom-majormode))
  819. ;; Add readonly=true to set read-only-mode
  820. (add-hook 'editorconfig-after-apply-functions
  821. (lambda (props)
  822. (let ((r (gethash 'readonly props)))
  823. (when (and (string= r "true")
  824. (not buffer-read-only))
  825. (read-only-mode 1)))))
  826. (add-hook 'editorconfig-hack-properties-functions
  827. '(lambda (props)
  828. (when (derived-mode-p 'makefile-mode)
  829. (puthash 'indent_style "tab" props))))
  830. (when (fboundp 'editorconfig-auto-apply-enable)
  831. (add-hook 'editorconfig-conf-mode-hook
  832. 'editorconfig-auto-apply-enable))
  833. ;; (when (fboundp 'editorconfig-charset-extras)
  834. ;; (add-hook 'editorconfig-custom-hooks
  835. ;; 'editorconfig-charset-extras))
  836. (setq revert-without-query '(".+"))
  837. ;; save cursor position
  838. (when (safe-require-or-eval 'saveplace)
  839. (setq-default save-place t)
  840. (setq save-place-file (concat user-emacs-directory
  841. "places")))
  842. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  843. (setq make-backup-files t)
  844. (setq vc-make-backup-files t)
  845. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  846. (setq backup-directory-alist
  847. (cons (cons "." (expand-file-name (concat user-emacs-directory
  848. "backup")))
  849. backup-directory-alist))
  850. (setq version-control 't)
  851. (setq delete-old-versions t)
  852. (setq kept-new-versions 20)
  853. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  854. "auto-save/")))
  855. ;; (setq delete-auto-save-files t)
  856. (setq auto-save-visited-interval 8)
  857. (auto-save-visited-mode 1)
  858. ;; (add-to-list 'auto-save-file-name-transforms
  859. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  860. ;; (setq auto-save-interval 3)
  861. ;; (auto-save-mode 1)
  862. (add-to-list 'completion-ignored-extensions ".bak")
  863. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  864. (setq delete-by-moving-to-trash t)
  865. ;; trash-directory "~/.emacs.d/trash")
  866. (add-hook 'after-save-hook
  867. 'executable-make-buffer-file-executable-if-script-p)
  868. (set-variable 'bookmark-default-file
  869. (expand-file-name (concat user-emacs-directory
  870. "bmk")))
  871. (set-variable 'bookmark-save-flag
  872. 1)
  873. (with-eval-after-load 'recentf
  874. (defvar recentf-exclude)
  875. (defvar bookmark-default-file)
  876. (add-to-list 'recentf-exclude
  877. (regexp-quote bookmark-default-file)))
  878. (when (safe-require-or-eval 'smart-revert)
  879. (smart-revert-on))
  880. ;; autosave
  881. ;; auto-save-visited-mode can be used instead?
  882. ;; (when (safe-require-or-eval 'autosave)
  883. ;; (autosave-set 8))
  884. ;; bookmarks
  885. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  886. ;; vc
  887. (set-variable 'vc-handled-backends '(RCS))
  888. (set-variable 'vc-rcs-register-switches "-l")
  889. (set-variable 'vc-rcs-checkin-switches "-l")
  890. (set-variable 'vc-command-messages t)
  891. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  892. ;; share clipboard with x
  893. ;; this page describes this in details, but only these sexps seem to be needed
  894. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  895. (and nil
  896. (not window-system)
  897. (not (eq window-system 'mac))
  898. (getenv "DISPLAY")
  899. (not (equal (getenv "DISPLAY") ""))
  900. (executable-find "xclip")
  901. ;; (< emacs-major-version 24)
  902. '(safe-require-or-eval 'xclip)
  903. nil
  904. (turn-on-xclip))
  905. (and (eq system-type 'darwin)
  906. (safe-require-or-eval 'pasteboard)
  907. (turn-on-pasteboard))
  908. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  909. ;; some modes and hooks
  910. ;; Include some extra modes
  911. (require 'generic-x)
  912. (when (fboundp 'web-mode)
  913. (add-to-list 'auto-mode-alist
  914. '("\\.html\\.j2\\'" . web-mode))
  915. )
  916. (when (autoload-eval-lazily 'wgrep)
  917. (set-variable 'wgrep-auto-save-buffer t)
  918. (with-eval-after-load 'grep
  919. (defvar grep-mode-map)
  920. (define-key grep-mode-map
  921. "e"
  922. 'wgrep-change-to-wgrep-mode)))
  923. (when (fboundp 'grep-context-mode)
  924. (add-hook 'compilation-mode-hook #'grep-context-mode))
  925. (with-eval-after-load 'remember
  926. (defvar remember-mode-map (make-sparse-keymap))
  927. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  928. (with-eval-after-load 'magit-files
  929. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  930. ;; existing keybindings.
  931. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  932. ;; to disable thie mode here, but do anyway.
  933. ;; See also https://github.com/magit/magit/issues/3517
  934. (global-magit-file-mode -1))
  935. (with-eval-after-load 'magit-section
  936. (set-face-background 'magit-section-highlight
  937. nil))
  938. (with-eval-after-load 'magit-diff
  939. (set-face-background 'magit-diff-added-highlight
  940. nil)
  941. (set-face-background 'magit-diff-removed-highlight
  942. nil)
  943. (set-face-background 'magit-diff-context-highlight
  944. nil)
  945. )
  946. (when (boundp 'git-rebase-filename-regexp)
  947. (add-to-list 'auto-mode-alist
  948. `(,git-rebase-filename-regexp . text-mode)))
  949. (when (safe-require-or-eval 'aggressive-indent)
  950. (defvar aggressive-indent-excluded-modes)
  951. (setq aggressive-indent-excluded-modes
  952. `(diff-mode
  953. toml-mode
  954. conf-mode
  955. dockerfile-mode
  956. groovy-mode
  957. scala-mode
  958. ,@aggressive-indent-excluded-modes))
  959. (global-aggressive-indent-mode 1))
  960. (when (autoload-eval-lazily 'ggtags)
  961. (add-hook 'c-mode-common-hook
  962. (lambda ()
  963. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  964. (ggtags-mode 1))))
  965. (add-hook 'python-mode-hook
  966. (lambda ()
  967. (ggtags-mode 1))))
  968. (when (autoload-eval-lazily 'imenu-list)
  969. ;; (set-variable 'imenu-list-auto-resize t)
  970. (set-variable 'imenu-list-focus-after-activation t)
  971. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  972. (add-hook 'emacs-lisp-mode-hook
  973. (lambda ()
  974. (setq imenu-generic-expression
  975. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  976. ,@imenu-generic-expression))))
  977. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  978. (with-eval-after-load 'compile
  979. (defvar compilation-filter-start)
  980. (defvar compilation-error-regexp-alist)
  981. (require 'ansi-color)
  982. (add-hook 'compilation-filter-hook
  983. (lambda ()
  984. (let ((inhibit-read-only t))
  985. (ansi-color-apply-on-region compilation-filter-start
  986. (point)))))
  987. (add-to-list 'compilation-error-regexp-alist
  988. ;; ansible-lint
  989. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  990. ;; Workaround to avoid ensime error
  991. (defvar ensime-mode-key-prefix nil)
  992. (when (safe-require-or-eval 'company)
  993. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  994. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  995. (global-company-mode)
  996. (set-variable 'company-idle-delay nil)
  997. (set-variable 'company-minimum-prefix-length 2)
  998. (set-variable 'company-selection-wrap-around t)
  999. (defvar company-mode-map)
  1000. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  1001. (with-eval-after-load 'python
  1002. (defvar python-indent-trigger-commands)
  1003. ;; TODO: This disables completion in puthon?
  1004. (add-to-list 'python-indent-trigger-commands
  1005. 'company-indent-or-complete-common))
  1006. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  1007. (defvar company-active-map)
  1008. (define-key company-active-map (kbd "C-n") 'company-select-next)
  1009. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  1010. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  1011. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  1012. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  1013. (defvar company-mode)
  1014. (defvar company-candidates)
  1015. (defvar company-candidates-length)
  1016. ;; (popup-tip "Hello, World!")
  1017. (defun my-company-lighter-current-length ()
  1018. "Get current candidate length."
  1019. (interactive)
  1020. (let ((l nil)
  1021. (inhibit-message t))
  1022. (when (and company-mode
  1023. (not (minibufferp))
  1024. ;; Do nothing when already in company completion
  1025. (not company-candidates))
  1026. ;; FIXME: Somehow it cannto catch errors from ggtags
  1027. (ignore-errors
  1028. ;; (company-auto-begin)
  1029. (company-manual-begin)
  1030. (setq l company-candidates-length)
  1031. (company-cancel)))
  1032. (if l
  1033. (format "[%d]" l)
  1034. "")))
  1035. (defvar company-lighter)
  1036. (set-variable 'company-lighter-base "Cmp")
  1037. ;; (add-to-list 'company-lighter
  1038. ;; '(:eval (my-company-lighter-current-length))
  1039. ;; t)
  1040. ;; This breaks japanese text input
  1041. ;; (set-variable 'my-company-length-popup-tip-timer
  1042. ;; (run-with-idle-timer 0.2 t
  1043. ;; 'my-company-length-popup-tip))
  1044. ;; (current-active-maps)
  1045. ;; (lookup-key)
  1046. '(mapcar (lambda (map)
  1047. (lookup-key map (kbd "C-i")))
  1048. (current-active-maps))
  1049. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  1050. (set-face-attribute 'company-tooltip nil
  1051. :foreground "black" :background "lightgrey")
  1052. (set-face-attribute 'company-tooltip-common nil
  1053. :foreground "black" :background "lightgrey")
  1054. (set-face-attribute 'company-tooltip-common-selection nil
  1055. :foreground "white" :background "steelblue")
  1056. (set-face-attribute 'company-tooltip-selection nil
  1057. :foreground "black" :background "steelblue")
  1058. (set-face-attribute 'company-preview-common nil
  1059. :background nil :foreground "lightgrey" :underline t)
  1060. (set-face-attribute 'company-scrollbar-fg nil
  1061. :background "orange")
  1062. (set-face-attribute 'company-scrollbar-bg nil
  1063. :background "gray40")
  1064. )
  1065. ;; https://github.com/lunaryorn/flycheck
  1066. (when (safe-require-or-eval 'flycheck)
  1067. (call-after-init (global-flycheck-mode)))
  1068. (with-eval-after-load 'flycheck
  1069. (when (fboundp 'flycheck-black-check-setup)
  1070. (flycheck-black-check-setup)))
  1071. (set-variable 'flycheck-python-mypy-ini
  1072. ".mypy.ini")
  1073. (when (autoload-eval-lazily 'ilookup)
  1074. (define-key ctl-x-map "d" 'ilookup-open-word))
  1075. (set-variable 'ac-ignore-case nil)
  1076. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1077. (define-key ctl-x-map "t" 'term-run-shell-command))
  1078. (add-to-list 'safe-local-variable-values
  1079. '(encoding utf-8))
  1080. (setq enable-local-variables :safe)
  1081. ;; Detect file type from shebang and set major-mode.
  1082. (add-to-list 'interpreter-mode-alist
  1083. '("python3" . python-mode))
  1084. (add-to-list 'interpreter-mode-alist
  1085. '("python2" . python-mode))
  1086. (with-eval-after-load 'python
  1087. (defvar python-mode-map (make-sparse-keymap))
  1088. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1089. (when (autoload-eval-lazily 'pipenv)
  1090. ;; (declare-function pipenv-projectile-after-switch-default "pipenv")
  1091. ;; (add-hook 'python-mode-hook
  1092. ;; (lambda ()
  1093. ;; (pipenv-mode 1)
  1094. ;; (pipenv-projectile-after-switch-default)))
  1095. )
  1096. (set-variable 'flycheck-python-pycompile-executable "python3")
  1097. (set-variable 'python-indent-guess-indent-offset nil)
  1098. (with-eval-after-load 'blacken
  1099. (when (require 'with-venv nil t)
  1100. (with-venv-advice-add 'blacken-buffer)))
  1101. ;; http://fukuyama.co/foreign-regexp
  1102. '(and (safe-require-or-eval 'foreign-regexp)
  1103. (progn
  1104. (setq foreign-regexp/regexp-type 'perl)
  1105. '(setq reb-re-syntax 'foreign-regexp)
  1106. ))
  1107. (autoload-eval-lazily 'sql '(sql-mode)
  1108. (require 'sql-indent nil t))
  1109. (add-to-list 'auto-mode-alist
  1110. '("\\.hql\\'" . sql-mode))
  1111. (when (autoload-eval-lazily 'git-command)
  1112. (define-key ctl-x-map "g" 'git-command))
  1113. (when (autoload-eval-lazily 'gited)
  1114. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1115. (when (safe-require-or-eval 'git-commit)
  1116. (global-git-commit-mode 1))
  1117. (with-eval-after-load 'git-commit
  1118. (add-hook 'git-commit-setup-hook
  1119. 'turn-off-auto-fill t))
  1120. (autoload-eval-lazily 'sl)
  1121. (with-eval-after-load 'rst
  1122. (defvar rst-mode-map)
  1123. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1124. (with-eval-after-load 'jdee
  1125. (add-hook 'jdee-mode-hook
  1126. (lambda ()
  1127. (make-local-variable 'global-mode-string)
  1128. (add-to-list 'global-mode-string
  1129. mode-line-position))))
  1130. ;; Cannot enable error thrown. Why???
  1131. ;; https://github.com/m0smith/malabar-mode#Installation
  1132. ;; (when (autoload-eval-lazily 'malabar-mode)
  1133. ;; (add-to-list 'load-path
  1134. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1135. ;; (safe-require-or-eval 'cedet-devel-load)
  1136. ;; (call-after-init (activate-malabar-mode)))
  1137. (with-eval-after-load 'make-mode
  1138. (defvar makefile-mode-map (make-sparse-keymap))
  1139. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1140. ;; this functions is set in write-file-functions, i cannot find any
  1141. ;; good way to remove this.
  1142. (fset 'makefile-warn-suspicious-lines 'ignore))
  1143. (with-eval-after-load 'verilog-mode
  1144. (defvar verilog-mode-map (make-sparse-keymap))
  1145. (define-key verilog-mode-map ";" 'self-insert-command))
  1146. (setq diff-switches "-u")
  1147. (with-eval-after-load 'diff-mode
  1148. ;; (when (and (eq major-mode
  1149. ;; 'diff-mode)
  1150. ;; (not buffer-file-name))
  1151. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1152. ;; (view-mode 1))
  1153. (set-face-attribute 'diff-header nil
  1154. :foreground nil
  1155. :background nil
  1156. :weight 'bold)
  1157. (set-face-attribute 'diff-file-header nil
  1158. :foreground nil
  1159. :background nil
  1160. :weight 'bold)
  1161. (set-face-foreground 'diff-index "blue")
  1162. (set-face-attribute 'diff-hunk-header nil
  1163. :foreground "cyan"
  1164. :weight 'normal)
  1165. (set-face-attribute 'diff-context nil
  1166. ;; :foreground "white"
  1167. :foreground nil
  1168. :weight 'normal)
  1169. (set-face-foreground 'diff-removed "red")
  1170. (set-face-foreground 'diff-added "green")
  1171. (set-face-background 'diff-removed nil)
  1172. (set-face-background 'diff-added nil)
  1173. (set-face-attribute 'diff-changed nil
  1174. :foreground "magenta"
  1175. :weight 'normal)
  1176. (set-face-attribute 'diff-refine-changed nil
  1177. :foreground nil
  1178. :background nil
  1179. :weight 'bold
  1180. :inverse-video t)
  1181. ;; Annoying !
  1182. ;;(diff-auto-refine-mode)
  1183. )
  1184. ;; (ffap-bindings)
  1185. (set-variable 'browse-url-browser-function
  1186. 'eww-browse-url)
  1187. (set-variable 'sh-here-document-word "__EOC__")
  1188. (when (autoload-eval-lazily 'adoc-mode
  1189. nil
  1190. (defvar adoc-mode-map (make-sparse-keymap))
  1191. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1192. (setq auto-mode-alist
  1193. `(("\\.adoc\\'" . adoc-mode)
  1194. ("\\.asciidoc\\'" . adoc-mode)
  1195. ,@auto-mode-alist)))
  1196. (with-eval-after-load 'markup-faces
  1197. ;; Is this too match ?
  1198. (set-face-foreground 'markup-meta-face
  1199. "color-245")
  1200. (set-face-foreground 'markup-meta-hide-face
  1201. "color-245")
  1202. )
  1203. ;; TODO: check if this is required
  1204. (when (autoload-eval-lazily 'groovy-mode nil
  1205. (defvar groovy-mode-map (make-sparse-keymap))
  1206. (define-key groovy-mode-map "(" 'self-insert-command)
  1207. (define-key groovy-mode-map ")" 'self-insert-command)
  1208. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1209. )
  1210. (add-to-list 'auto-mode-alist
  1211. '("build\\.gradle\\'" . groovy-mode)))
  1212. (add-to-list 'auto-mode-alist
  1213. '("\\.gawk\\'" . awk-mode))
  1214. (with-eval-after-load 'yaml-mode
  1215. (defvar yaml-mode-map (make-sparse-keymap))
  1216. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1217. (with-eval-after-load 'html-mode
  1218. (defvar html-mode-map (make-sparse-keymap))
  1219. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1220. (with-eval-after-load 'text-mode
  1221. (define-key text-mode-map (kbd "C-m") 'newline))
  1222. (autoload-eval-lazily 'info nil
  1223. (defvar Info-additional-directory-list)
  1224. (dolist (dir (directory-files (concat user-emacs-directory
  1225. "info")
  1226. t
  1227. "^[^.].*"))
  1228. (when (file-directory-p dir)
  1229. (add-to-list 'Info-additional-directory-list
  1230. dir)))
  1231. (let ((dir (expand-file-name "~/.brew/share/info")))
  1232. (when (file-directory-p dir)
  1233. (add-to-list 'Info-additional-directory-list
  1234. dir))))
  1235. (with-eval-after-load 'apropos
  1236. (defvar apropos-mode-map (make-sparse-keymap))
  1237. (define-key apropos-mode-map "n" 'next-line)
  1238. (define-key apropos-mode-map "p" 'previous-line))
  1239. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1240. ;; (define-key isearch-mode-map
  1241. ;; (kbd "C-j") 'isearch-other-control-char)
  1242. ;; (define-key isearch-mode-map
  1243. ;; (kbd "C-k") 'isearch-other-control-char)
  1244. ;; (define-key isearch-mode-map
  1245. ;; (kbd "C-h") 'isearch-other-control-char)
  1246. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1247. (define-key isearch-mode-map (kbd "M-r")
  1248. 'isearch-query-replace-regexp)
  1249. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1250. (setq lazy-highlight-cleanup nil)
  1251. ;; face for isearch highlighing
  1252. (set-face-attribute 'lazy-highlight
  1253. nil
  1254. :foreground `unspecified
  1255. :background `unspecified
  1256. :underline t
  1257. ;; :weight `bold
  1258. )
  1259. (add-hook 'outline-mode-hook
  1260. (lambda ()
  1261. (when (string-match "\\.md\\'" buffer-file-name)
  1262. (set (make-local-variable 'outline-regexp) "#+ "))))
  1263. (add-hook 'outline-mode-hook
  1264. 'outline-show-all)
  1265. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1266. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1267. (when (autoload-eval-lazily 'markdown-mode
  1268. '(markdown-mode gfm-mode)
  1269. (defvar gfm-mode-map (make-sparse-keymap))
  1270. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1271. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1272. (set-variable 'markdown-command (or (executable-find "markdown")
  1273. (executable-find "markdown.pl")
  1274. ""))
  1275. (add-hook 'markdown-mode-hook
  1276. (lambda ()
  1277. (outline-minor-mode 1)
  1278. (flyspell-mode)
  1279. (set (make-local-variable 'comment-start) ";")))
  1280. )
  1281. ;; c-mode
  1282. ;; http://www.emacswiki.org/emacs/IndentingC
  1283. ;; http://en.wikipedia.org/wiki/Indent_style
  1284. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1285. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1286. (with-eval-after-load 'cc-vars
  1287. (defvar c-default-style nil)
  1288. (add-to-list 'c-default-style
  1289. '(c-mode . "k&r"))
  1290. (add-to-list 'c-default-style
  1291. '(c++-mode . "k&r")))
  1292. (autoload-eval-lazily 'js2-mode nil
  1293. ;; currently do not use js2-mode
  1294. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1295. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1296. ;; (defvar js2-mode-map (make-sparse-keymap))
  1297. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1298. ;; (interactive)
  1299. ;; (js2-enter-key)
  1300. ;; (indent-for-tab-command)))
  1301. ;; (add-hook (kill-local-variable 'before-save-hook)
  1302. ;; 'js2-before-save)
  1303. ;; (add-hook 'before-save-hook
  1304. ;; 'my-indent-buffer
  1305. ;; nil
  1306. ;; t)
  1307. )
  1308. (add-to-list 'interpreter-mode-alist
  1309. '("node" . js-mode))
  1310. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1311. (with-eval-after-load 'uniquify
  1312. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1313. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1314. (setq uniquify-min-dir-content 1))
  1315. (with-eval-after-load 'view
  1316. (defvar view-mode-map (make-sparse-keymap))
  1317. (define-key view-mode-map "j" 'scroll-up-line)
  1318. (define-key view-mode-map "k" 'scroll-down-line)
  1319. (define-key view-mode-map "v" 'toggle-read-only)
  1320. (define-key view-mode-map "q" 'bury-buffer)
  1321. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1322. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1323. ;; (define-key view-mode-map
  1324. ;; "n" 'nonincremental-repeat-search-forward)
  1325. ;; (define-key view-mode-map
  1326. ;; "N" 'nonincremental-repeat-search-backward)
  1327. ;; N conflicts with git-walktree
  1328. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1329. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1330. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1331. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1332. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1333. (global-set-key "\M-r" 'view-mode)
  1334. ;; (setq view-read-only t)
  1335. (with-eval-after-load 'term
  1336. (defvar term-raw-map (make-sparse-keymap))
  1337. (define-key term-raw-map (kbd "C-x")
  1338. (lookup-key (current-global-map)
  1339. (kbd "C-x"))))
  1340. (add-hook 'term-mode-hook
  1341. (lambda ()
  1342. ;; Stop current line highlighting
  1343. (set (make-local-variable (defvar hl-line-range-function))
  1344. (lambda () '(0 . 0)))
  1345. (set (make-local-variable 'scroll-margin)
  1346. 0)
  1347. (set-variable 'term-buffer-maximum-size 20480)
  1348. ))
  1349. (add-hook 'Man-mode-hook
  1350. (lambda ()
  1351. (view-mode 1)
  1352. (setq truncate-lines nil)))
  1353. (set-variable 'Man-notify-method (if window-system
  1354. 'newframe
  1355. 'aggressive))
  1356. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1357. "woman_cache.el")))
  1358. ;; not work because man.el will be loaded when man called
  1359. (defalias 'man 'woman)
  1360. (add-to-list 'auto-mode-alist
  1361. '("tox\\.ini\\'" . conf-unix-mode))
  1362. (when (autoload-eval-lazily 'toml-mode)
  1363. (add-to-list 'auto-mode-alist
  1364. '("/tox\\.ini\\'" . toml-mode))
  1365. (add-to-list 'auto-mode-alist
  1366. '("/Pipfile\\'" . toml-mode))
  1367. (add-to-list 'auto-mode-alist
  1368. '("/poetry\\.lock\\'" . toml-mode))
  1369. )
  1370. (when (autoload-eval-lazily 'json-mode)
  1371. (add-to-list 'auto-mode-alist
  1372. '("/Pipfile\\.lock\\'" . json-mode)))
  1373. (add-hook 'go-mode-hook
  1374. (lambda()
  1375. (defvar go-mode-map)
  1376. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1377. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1378. (when (autoload-eval-lazily 'k8s-mode)
  1379. (add-to-list 'auto-mode-alist
  1380. '("\\.k8s\\'" . k8s-mode)))
  1381. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1382. ;; buffers
  1383. (defvar bs-configurations)
  1384. (declare-function bs-set-configuration "bs")
  1385. (declare-function bs-refresh "bs")
  1386. (declare-function bs-message-without-log "bs")
  1387. (declare-function bs--current-config-message "bs")
  1388. (when (autoload-eval-lazily 'bs '(bs-show)
  1389. (add-to-list 'bs-configurations
  1390. '("specials" "^\\*" nil ".*" nil nil))
  1391. (add-to-list 'bs-configurations
  1392. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1393. (defvar bs-mode-map)
  1394. (defvar bs-current-configuration)
  1395. (define-key bs-mode-map (kbd "t")
  1396. ;; TODO: fix toggle feature
  1397. (lambda ()
  1398. (interactive)
  1399. (if (string= "specials"
  1400. bs-current-configuration)
  1401. (bs-set-configuration "files")
  1402. (bs-set-configuration "specials"))
  1403. (bs-refresh)
  1404. (bs-message-without-log "%s"
  1405. (bs--current-config-message))))
  1406. ;; (setq bs-configurations (list
  1407. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1408. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1409. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1410. )
  1411. (defalias 'list-buffers 'bs-show)
  1412. (set-variable 'bs-default-configuration "files-and-specials")
  1413. (set-variable 'bs-default-sort-name "by nothing")
  1414. (add-hook 'bs-mode-hook
  1415. (lambda ()
  1416. (set (make-local-variable 'scroll-margin) 0))))
  1417. ;;(iswitchb-mode 1)
  1418. (icomplete-mode)
  1419. (defun iswitchb-buffer-display-other-window ()
  1420. "Do iswitchb in other window."
  1421. (interactive)
  1422. (let ((iswitchb-default-method 'display))
  1423. (call-interactively 'iswitchb-buffer)))
  1424. ;; buffer killing
  1425. ;; (defun my-delete-window-killing-buffer () nil)
  1426. (defun my-query-kill-current-buffer ()
  1427. "Interactively kill current buffer."
  1428. (interactive)
  1429. (if (y-or-n-p (concat "kill current buffer? :"))
  1430. (kill-buffer (current-buffer))))
  1431. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1432. (substitute-key-definition 'kill-buffer
  1433. 'my-query-kill-current-buffer
  1434. global-map)
  1435. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1436. ;; dired
  1437. (defun my-file-head (filename &optional n)
  1438. "Return list of first N lines of file FILENAME."
  1439. ;; TODO: Fix for janapese text
  1440. ;; TODO: Fix for short text
  1441. (let ((num (or n 10))
  1442. (size 100)
  1443. (beg 0)
  1444. (end 0)
  1445. (result '())
  1446. (read -1))
  1447. (with-temp-buffer
  1448. (erase-buffer)
  1449. (while (or (<= (count-lines (point-min)
  1450. (point-max))
  1451. num)
  1452. (eq read 0))
  1453. (setq end (+ beg size))
  1454. (setq read (nth 1 (insert-file-contents-literally filename
  1455. nil
  1456. beg
  1457. end)))
  1458. (goto-char (point-max))
  1459. (setq beg (+ beg size)))
  1460. (goto-char (point-min))
  1461. (while (< (length result) num)
  1462. (let ((start (point)))
  1463. (forward-line 1)
  1464. (setq result
  1465. `(,@result ,(buffer-substring-no-properties start
  1466. (point))))))
  1467. result
  1468. ;; (buffer-substring-no-properties (point-min)
  1469. ;; (progn
  1470. ;; (forward-line num)
  1471. ;; (point)))
  1472. )))
  1473. ;; (apply 'concat (my-file-head "./shrc" 10)
  1474. (defun my-dired-echo-file-head (arg)
  1475. "Echo head of current file.
  1476. ARG is num to show, or defaults to 7."
  1477. (interactive "P")
  1478. (let ((f (dired-get-filename)))
  1479. (message "%s"
  1480. (apply 'concat
  1481. (my-file-head f
  1482. 7)))))
  1483. (defun my-dired-diff ()
  1484. "Show diff of marked file and file of current line."
  1485. (interactive)
  1486. (let ((files (dired-get-marked-files nil nil nil t)))
  1487. (if (eq (car files)
  1488. t)
  1489. (diff (cadr files) (dired-get-filename))
  1490. (message "One file must be marked!"))))
  1491. (defun dired-get-file-info ()
  1492. "Print information of current line file."
  1493. (interactive)
  1494. (let ((f (shell-quote-argument (dired-get-filename t))))
  1495. (if (file-directory-p f)
  1496. (progn
  1497. (message "Calculating disk usage...")
  1498. (shell-command (concat "du -hsD "
  1499. f)))
  1500. (shell-command (concat "file "
  1501. f)))))
  1502. (defun my-dired-scroll-up ()
  1503. "Scroll up."
  1504. (interactive)
  1505. (my-dired-previous-line (- (window-height) 1)))
  1506. (defun my-dired-scroll-down ()
  1507. "Scroll down."
  1508. (interactive)
  1509. (my-dired-next-line (- (window-height) 1)))
  1510. ;; (defun my-dired-forward-line (arg)
  1511. ;; ""
  1512. ;; (interactive "p"))
  1513. (defun my-dired-previous-line (arg)
  1514. "Move ARG lines up."
  1515. (interactive "p")
  1516. (if (> arg 0)
  1517. (progn
  1518. (if (eq (line-number-at-pos)
  1519. 1)
  1520. (goto-char (point-max))
  1521. (forward-line -1))
  1522. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1523. (dired-get-subdir))
  1524. (- arg 1)
  1525. arg)))
  1526. (dired-move-to-filename)))
  1527. (defun my-dired-next-line (arg)
  1528. "Move ARG lines down."
  1529. (interactive "p")
  1530. (if (> arg 0)
  1531. (progn
  1532. (if (eq (point)
  1533. (point-max))
  1534. (goto-char (point-min))
  1535. (forward-line 1))
  1536. (my-dired-next-line (if (or (dired-get-filename nil t)
  1537. (dired-get-subdir))
  1538. (- arg 1)
  1539. arg)))
  1540. (dired-move-to-filename)))
  1541. (defun my-tramp-remote-find-file (f)
  1542. "Open F."
  1543. (interactive (list (read-file-name "My Find File Tramp: "
  1544. "/scp:"
  1545. nil ;; "/scp:"
  1546. (confirm-nonexistent-file-or-buffer))))
  1547. (find-file f))
  1548. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1549. (if (eq window-system 'mac)
  1550. (setq dired-listing-switches "-lhF")
  1551. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1552. )
  1553. (setq dired-listing-switches "-lhF")
  1554. ;; when using dired-find-alternate-file
  1555. ;; reuse current dired buffer for the file to open
  1556. ;; (put 'dired-find-alternate-file 'disabled nil)
  1557. (set-variable 'dired-ls-F-marks-symlinks t)
  1558. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1559. (set-variable 'ls-lisp-dirs-first t)
  1560. (set-variable 'ls-lisp-use-localized-time-format t)
  1561. (set-variable 'ls-lisp-format-time-list
  1562. '("%Y-%m-%d %H:%M"
  1563. "%Y-%m-%d "))
  1564. (set-variable 'dired-dwim-target t)
  1565. (set-variable 'dired-isearch-filenames t)
  1566. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1567. (set-variable 'dired-hide-details-hide-information-lines nil)
  1568. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1569. (set-variable 'dired-recursive-deletes 'always)
  1570. ;; (add-hook 'dired-after-readin-hook
  1571. ;; 'my-replace-nasi-none)
  1572. (with-eval-after-load 'dired
  1573. (safe-require-or-eval 'ls-lisp)
  1574. (defvar dired-mode-map (make-sparse-keymap))
  1575. ;; dired-do-chgrp sometimes cause system hung
  1576. (define-key dired-mode-map "G" 'ignore)
  1577. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1578. (define-key dired-mode-map "i" 'dired-get-file-info)
  1579. ;; (define-key dired-mode-map "f" 'find-file)
  1580. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1581. (define-key dired-mode-map "z" 'fzf)
  1582. (define-key dired-mode-map "!" 'shell-command)
  1583. (define-key dired-mode-map "&" 'async-shell-command)
  1584. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1585. (define-key dired-mode-map "=" 'my-dired-diff)
  1586. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1587. (define-key dired-mode-map "b" 'gtkbm)
  1588. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1589. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1590. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1591. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1592. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1593. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1594. (substitute-key-definition 'dired-next-line
  1595. 'my-dired-next-line
  1596. dired-mode-map)
  1597. (substitute-key-definition 'dired-previous-line
  1598. 'my-dired-previous-line
  1599. dired-mode-map)
  1600. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1601. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1602. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1603. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1604. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1605. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1606. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1607. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1608. (add-hook 'dired-mode-hook
  1609. (lambda ()
  1610. (when (fboundp 'dired-hide-details-mode)
  1611. (dired-hide-details-mode t)
  1612. (local-set-key "l" 'dired-hide-details-mode))
  1613. (let ((file "._Icon\015"))
  1614. (when nil
  1615. '(file-readable-p file)
  1616. (delete-file file)))))
  1617. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1618. (defvar pack-program-alist)
  1619. (add-to-list 'pack-program-alist
  1620. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1621. (set-variable 'pack-silence
  1622. t)
  1623. (with-eval-after-load 'dired
  1624. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1625. (when (autoload-eval-lazily 'dired-list-all-mode)
  1626. (setq dired-listing-switches "-lhF")
  1627. (with-eval-after-load 'dired
  1628. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1629. (when (autoload-eval-lazily 'dired-filter)
  1630. (add-hook 'dired-mode-hook
  1631. 'dired-filter-mode))
  1632. (set-variable 'dired-filter-stack nil)
  1633. ;; Currently disabled in favor of dired-from-git-ls-files
  1634. ;; (define-key ctl-x-map "f" 'find-dired)
  1635. (defvar my-dired-git-ls-files-history
  1636. "History for `my-dired-git-ls-files'." nil)
  1637. (defun my-dired-git-ls-files (arg)
  1638. "Dired from git ls-files."
  1639. (interactive (list
  1640. (read-shell-command "git ls-files: "
  1641. "git ls-files -z ")))
  1642. (pop-to-buffer-same-window
  1643. (dired-noselect `(,default-directory
  1644. ,@(split-string (shell-command-to-string arg)
  1645. "\0" t))
  1646. ""))
  1647. )
  1648. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1649. (with-eval-after-load 'dired
  1650. (defvar dired-mode-map (make-sparse-keymap))
  1651. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1652. ;; (define-minor-mode my-dired-glob-filter)
  1653. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1654. ;; misc funcs
  1655. (define-key ctl-x-map "T" 'git-worktree)
  1656. (define-key ctl-x-map "W" 'git-walktree)
  1657. (when (fboundp 'browse-url-default-macosx-browser)
  1658. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1659. (defalias 'qcalc 'quick-calc)
  1660. (defun memo (&optional dir)
  1661. "Open memo.txt in DIR."
  1662. (interactive)
  1663. (pop-to-buffer (find-file-noselect (concat (if dir
  1664. (file-name-as-directory dir)
  1665. "")
  1666. "memo.txt"))))
  1667. ;; TODO: remember-projectile
  1668. (set (defvar my-privnotes-path nil
  1669. "My privnotes repository path.")
  1670. (expand-file-name "~/my/privnotes"))
  1671. (defun my-privnotes-readme (dir)
  1672. "Open my privnotes DIR."
  1673. (interactive (list
  1674. (read-file-name "Privnotes: "
  1675. (expand-file-name (format-time-string "%Y%m%d_")
  1676. my-privnotes-path))))
  1677. (let ((path (expand-file-name "README.md" dir)))
  1678. (with-current-buffer (find-file path)
  1679. (unless (file-exists-p path)
  1680. (insert (file-name-base dir)
  1681. "\n"
  1682. "=======\n"
  1683. "\n\n")))))
  1684. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1685. (set (defvar my-rgrep-alist nil
  1686. "Alist of rgrep command.
  1687. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1688. condition to choose COMMAND when evaluated.")
  1689. `(
  1690. ;; ripgrep
  1691. ("rg"
  1692. (executable-find "rg")
  1693. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1694. ;; git grep
  1695. ("gitgrep"
  1696. (eq 0
  1697. (shell-command "git rev-parse --git-dir"))
  1698. "git --no-pager -c color.grep=always grep -nH -e ")
  1699. ;; sift
  1700. ("sift"
  1701. (executable-find "sift")
  1702. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1703. ;; the silver searcher
  1704. ("ag"
  1705. (executable-find "ag")
  1706. "ag --nogroup --nopager --filename ")
  1707. ;; ack
  1708. ("ack"
  1709. (executable-find "ack")
  1710. "ack --nogroup --nopager --with-filename ")
  1711. ;; gnu global
  1712. ("global"
  1713. (and (require 'ggtags nil t)
  1714. (executable-find "global")
  1715. (ggtags-current-project-root))
  1716. "global --result grep ")
  1717. ;; grep
  1718. ("grep"
  1719. t
  1720. ,(concat "find . "
  1721. "-path '*/.git' -prune -o "
  1722. "-path '*/.svn' -prune -o "
  1723. "-type f -print0 | "
  1724. "xargs -0 grep -nH -e "))
  1725. )
  1726. )
  1727. (defvar my-rgrep-default nil
  1728. "Default command name for my-rgrep.")
  1729. (defun my-rgrep-grep-command (&optional name alist)
  1730. "Return recursive grep command for current directory or nil.
  1731. If NAME is given, use that without testing.
  1732. Commands are searched from ALIST."
  1733. (if alist
  1734. (if name
  1735. ;; if name is given search that from alist and return the command
  1736. (nth 2 (assoc name
  1737. alist))
  1738. ;; if name is not given try test in 1th elem
  1739. (let ((car (car alist))
  1740. (cdr (cdr alist)))
  1741. (if (eval (nth 1 car))
  1742. ;; if the condition is true return the command
  1743. (nth 2 car)
  1744. ;; try next one
  1745. (and cdr
  1746. (my-rgrep-grep-command name cdr)))))
  1747. ;; if alist is not given set default value
  1748. (my-rgrep-grep-command name my-rgrep-alist)))
  1749. (defun my-rgrep (command-args)
  1750. "My recursive grep. Run COMMAND-ARGS.
  1751. If prefix argument is given, use current symbol as default search target
  1752. and search from projectile root (if projectile is available)."
  1753. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1754. nil)))
  1755. (if cmd
  1756. (list (read-shell-command "grep command: "
  1757. (concat cmd
  1758. (if current-prefix-arg
  1759. (thing-at-point 'symbol t)
  1760. ""))
  1761. 'grep-find-history))
  1762. (error "My-Rgrep: Command for rgrep not found")
  1763. )))
  1764. (if (and current-prefix-arg
  1765. (safe-require-or-eval 'projectile)
  1766. (projectile-project-p))
  1767. (projectile-with-default-dir (projectile-project-root)
  1768. (compilation-start command-args
  1769. 'grep-mode))
  1770. (compilation-start command-args
  1771. 'grep-mode)))
  1772. (defun my-rgrep-thing-at-point-projectile-root ()
  1773. "My recursive grep to find thing at point from project root."
  1774. (interactive)
  1775. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1776. nil))
  1777. (command-args
  1778. (if cmd
  1779. (concat cmd
  1780. (or (thing-at-point 'symbol t)
  1781. (error "No symbol at point")))
  1782. (error "My-Rgrep: Command for rgrep not found"))))
  1783. (if (safe-require-or-eval 'projectile)
  1784. (projectile-with-default-dir (or (projectile-project-root)
  1785. default-directory)
  1786. (compilation-start command-args
  1787. 'grep-mode))
  1788. (compilation-start command-args
  1789. 'grep-mode))))
  1790. (defmacro define-my-rgrep (name)
  1791. "Define rgrep for NAME."
  1792. `(defun ,(intern (concat "my-rgrep-"
  1793. name)) ()
  1794. ,(format "My recursive grep by %s."
  1795. name)
  1796. (interactive)
  1797. (let ((my-rgrep-default ,name))
  1798. (if (called-interactively-p 'any)
  1799. (call-interactively 'my-rgrep)
  1800. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1801. )
  1802. (define-my-rgrep "ack")
  1803. (define-my-rgrep "ag")
  1804. (define-my-rgrep "rg")
  1805. (define-my-rgrep "sift")
  1806. (define-my-rgrep "gitgrep")
  1807. (define-my-rgrep "grep")
  1808. (define-my-rgrep "global")
  1809. (define-key ctl-x-map "s" 'my-rgrep)
  1810. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1811. (defun my-occur (regexp &optional region)
  1812. "My occur command to search REGEXP."
  1813. (interactive (list (read-string "List lines matching regexp: "
  1814. (thing-at-point 'symbol t))))
  1815. (occur regexp nil region))
  1816. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1817. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1818. (defalias 'make 'compile)
  1819. (define-key ctl-x-map "c" 'compile)
  1820. (defun my-pushbullet-note (text &optional title)
  1821. "Push TEXT."
  1822. (interactive "sText to Push: ")
  1823. (pb/push-item '("") text "note" (or title "")))
  1824. ;;;;;;;;;;;;;;;;;;;;;
  1825. ;; git-bug
  1826. (defconst git-bug-ls-regexp
  1827. (eval-when-compile
  1828. (rx bol
  1829. (submatch (one-or-more alphanumeric)) ; id
  1830. ;; (one-or-more any)
  1831. (one-or-more space)
  1832. (submatch (or "open" "close")) ; status
  1833. (one-or-more space)
  1834. (submatch (maximal-match (zero-or-more print))) ; title
  1835. "\t"
  1836. (submatch (one-or-more alphanumeric)) ; user
  1837. (one-or-more space)
  1838. "C:"
  1839. (submatch (one-or-more digit)) ; Comment num
  1840. (one-or-more space)
  1841. "L:"
  1842. (submatch (one-or-more digit)) ; Label num
  1843. eol
  1844. ))
  1845. "Regexp to parse line of output of git-bug ls.
  1846. Used by `git-bug-ls'.")
  1847. (defun git-bug-bugs ()
  1848. "Get list of git-bug bugs."
  1849. (with-temp-buffer
  1850. (git-bug--call-process "bug" "ls")
  1851. (goto-char (point-min))
  1852. (let ((bugs nil))
  1853. (while (not (eq (point) (point-max)))
  1854. (save-match-data
  1855. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  1856. (setq bugs `(,@bugs
  1857. ,(list
  1858. :id (match-string 1)
  1859. :status (match-string 2)
  1860. :title (string-trim (match-string 3))
  1861. :user (match-string 4)
  1862. :comment-num (match-string 5)
  1863. :label-num (match-string 6)
  1864. )))))
  1865. (forward-line 1)
  1866. (goto-char (point-at-bol)))
  1867. bugs)))
  1868. (defun git-bug-ls-noselect (&optional directory)
  1869. "Open git bug list buffer.
  1870. If optional arg DIRECTORY is given change current directory to there before
  1871. initializing."
  1872. (setq directory (expand-file-name (or directory
  1873. default-directory)))
  1874. (cl-assert (file-directory-p directory))
  1875. (let* ((root (git-bug--get-repository-root directory))
  1876. (name (file-name-nondirectory root))
  1877. (bname (format "*GitBug<%s>*" name)))
  1878. (with-current-buffer (get-buffer-create bname)
  1879. (cd root)
  1880. (git-bug-ls--set-tabulated-list-mode-variables)
  1881. (git-bug-ls-mode)
  1882. (current-buffer))))
  1883. (defun git-bug--get-repository-root (dir)
  1884. "Resolve repository root of DIR.
  1885. If DIR is not inside of any git repository, signal an error."
  1886. (cl-assert (file-directory-p dir))
  1887. (with-temp-buffer
  1888. (cd dir)
  1889. (git-bug--call-process "rev-parse" "--show-toplevel")
  1890. (goto-char (point-min))
  1891. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1892. (defun git-bug--call-process (&rest args)
  1893. "Start git process synchronously with ARGS.
  1894. Raise error when git process ends with non-zero status.
  1895. Any output will be written to current buffer."
  1896. (let ((status (apply 'call-process
  1897. "git"
  1898. nil
  1899. t
  1900. nil
  1901. args)))
  1902. (cl-assert (eq status 0)
  1903. nil
  1904. (buffer-substring-no-properties (point-min) (point-max)))))
  1905. ;;;;;;;;;;;;;;;;;;;
  1906. ;; peek-file-mode
  1907. (defun peek-file (file)
  1908. "Peek FILE."
  1909. (interactive)
  1910. (message "%s" file))
  1911. ;;;;;;;;;;;;;;;;;;;;
  1912. ;; remember-projectile
  1913. ;; Local Variables:
  1914. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1915. ;; flycheck-checker: emacs-lisp
  1916. ;; End:
  1917. ;;; emancs.el ends here