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

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