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.
 
 
 
 
 
 

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