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

1979 linhas
63 KiB

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