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.
 
 
 
 
 
 

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