Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

2239 righe
72 KiB

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