Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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