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

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