選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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