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

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