No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

2957 líneas
100 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. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. editorconfig
  116. editorconfig-custom-majormode
  117. git-command
  118. prompt-text
  119. ;; 10sr repository
  120. ;; 10sr-extras
  121. terminal-title
  122. recentf-show
  123. dired-list-all-mode
  124. pack
  125. set-modeline-color
  126. read-only-only-mode
  127. smart-revert
  128. autosave
  129. ;;window-organizer
  130. ilookup
  131. pasteboard
  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 nil)
  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. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  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 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. ;; (when (safe-require-or-eval 'end-mark)
  585. ;; (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (safe-require-or-eval 'wgrep)
  695. (defvar grep-mode-map)
  696. (define-key grep-mode-map
  697. "e"
  698. 'wgrep-change-to-wgrep-mode))
  699. (with-eval-after-load 'remember
  700. (defvar remember-mode-map (make-sparse-keymap))
  701. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  702. (with-eval-after-load 'magit-files
  703. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  704. ;; existing keybindings.
  705. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  706. ;; to disable thie mode here, but do anyway.
  707. ;; See also https://github.com/magit/magit/issues/3517
  708. (global-magit-file-mode -1))
  709. (with-eval-after-load 'magit-section
  710. (set-face-background 'magit-section-highlight
  711. nil))
  712. (with-eval-after-load 'magit-diff
  713. (set-face-background 'magit-diff-added-highlight
  714. nil)
  715. (set-face-background 'magit-diff-removed-highlight
  716. nil)
  717. (set-face-background 'magit-diff-context-highlight
  718. nil)
  719. )
  720. (when (boundp 'git-rebase-filename-regexp)
  721. (add-to-list 'auto-mode-alist
  722. `(,git-rebase-filename-regexp . text-mode)))
  723. (when (safe-require-or-eval 'aggressive-indent)
  724. (defvar aggressive-indent-excluded-modes)
  725. (setq aggressive-indent-excluded-modes
  726. `(diff-mode
  727. toml-mode
  728. conf-mode
  729. dockerfile-mode
  730. ,@aggressive-indent-excluded-modes))
  731. (global-aggressive-indent-mode 1))
  732. (when (autoload-eval-lazily 'ggtags)
  733. (add-hook 'c-mode-common-hook
  734. (lambda ()
  735. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  736. (ggtags-mode 1))))
  737. (add-hook 'python-mode-hook
  738. (lambda ()
  739. (ggtags-mode 1))))
  740. (when (autoload-eval-lazily 'imenu-list)
  741. ;; (set-variable 'imenu-list-auto-resize t)
  742. (set-variable 'imenu-list-focus-after-activation t)
  743. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  744. (add-hook 'emacs-lisp-mode-hook
  745. (lambda ()
  746. (setq imenu-generic-expression
  747. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  748. ,@imenu-generic-expression))))
  749. (with-eval-after-load 'compile
  750. (defvar compilation-filter-start)
  751. (defvar compilation-error-regexp-alist)
  752. (require 'ansi-color)
  753. (add-hook 'compilation-filter-hook
  754. (lambda ()
  755. (let ((inhibit-read-only t))
  756. (ansi-color-apply-on-region compilation-filter-start
  757. (point)))))
  758. (add-to-list 'compilation-error-regexp-alist
  759. ;; ansible-lint
  760. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  761. ;; Workaround to avoid ensime error
  762. (defvar ensime-mode-key-prefix nil)
  763. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  764. (when (safe-require-or-eval 'company)
  765. (global-company-mode)
  766. (set-variable 'company-idle-delay 0.5)
  767. (set-variable 'company-minimum-prefix-length 2)
  768. (set-variable 'company-selection-wrap-around t))
  769. ;; https://github.com/lunaryorn/flycheck
  770. (when (safe-require-or-eval 'flycheck)
  771. (call-after-init 'global-flycheck-mode))
  772. (when (autoload-eval-lazily 'ilookup)
  773. (define-key ctl-x-map "d" 'ilookup-open-word))
  774. (set-variable 'ac-ignore-case nil)
  775. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  776. (define-key ctl-x-map "t" 'term-run-shell-command))
  777. (add-to-list 'safe-local-variable-values
  778. '(encoding utf-8))
  779. (setq enable-local-variables :safe)
  780. ;; Detect file type from shebang and set major-mode.
  781. (add-to-list 'interpreter-mode-alist
  782. '("python3" . python-mode))
  783. (add-to-list 'interpreter-mode-alist
  784. '("python2" . python-mode))
  785. (with-eval-after-load 'python
  786. (defvar python-mode-map (make-sparse-keymap))
  787. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  788. (when (autoload-eval-lazily 'pipenv)
  789. (add-hook 'python-mode-hook
  790. (lambda ()
  791. (pipenv-mode 1)
  792. (pipenv-projectile-after-switch-default)))
  793. )
  794. (set-variable 'flycheck-python-pycompile-executable "python3")
  795. ;; http://fukuyama.co/foreign-regexp
  796. '(and (safe-require-or-eval 'foreign-regexp)
  797. (progn
  798. (setq foreign-regexp/regexp-type 'perl)
  799. '(setq reb-re-syntax 'foreign-regexp)
  800. ))
  801. (autoload-eval-lazily 'sql '(sql-mode)
  802. (require 'sql-indent nil t))
  803. (when (autoload-eval-lazily 'git-command)
  804. (define-key ctl-x-map "g" 'git-command))
  805. (when (safe-require-or-eval 'git-commit)
  806. (global-git-commit-mode 1))
  807. (with-eval-after-load 'git-commit
  808. (add-hook 'git-commit-setup-hook
  809. 'turn-off-auto-fill t))
  810. (autoload-eval-lazily 'sl)
  811. (with-eval-after-load 'rst
  812. (defvar rst-mode-map)
  813. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  814. (with-eval-after-load 'jdee
  815. (add-hook 'jdee-mode-hook
  816. (lambda ()
  817. (make-local-variable 'global-mode-string)
  818. (add-to-list 'global-mode-string
  819. mode-line-position))))
  820. ;; Cannot enable error thrown. Why???
  821. ;; https://github.com/m0smith/malabar-mode#Installation
  822. ;; (when (autoload-eval-lazily 'malabar-mode)
  823. ;; (add-to-list 'load-path
  824. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  825. ;; (safe-require-or-eval 'cedet-devel-load)
  826. ;; (call-after-init 'activate-malabar-mode))
  827. (with-eval-after-load 'make-mode
  828. (defvar makefile-mode-map (make-sparse-keymap))
  829. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  830. ;; this functions is set in write-file-functions, i cannot find any
  831. ;; good way to remove this.
  832. (fset 'makefile-warn-suspicious-lines 'ignore))
  833. (with-eval-after-load 'verilog-mode
  834. (defvar verilog-mode-map (make-sparse-keymap))
  835. (define-key verilog-mode-map ";" 'self-insert-command))
  836. (setq diff-switches "-u")
  837. (with-eval-after-load 'diff-mode
  838. ;; (when (and (eq major-mode
  839. ;; 'diff-mode)
  840. ;; (not buffer-file-name))
  841. ;; ;; do not pass when major-mode is derived mode of diff-mode
  842. ;; (view-mode 1))
  843. (set-face-attribute 'diff-header nil
  844. :foreground nil
  845. :background nil
  846. :weight 'bold)
  847. (set-face-attribute 'diff-file-header nil
  848. :foreground nil
  849. :background nil
  850. :weight 'bold)
  851. (set-face-foreground 'diff-index "blue")
  852. (set-face-attribute 'diff-hunk-header nil
  853. :foreground "cyan"
  854. :weight 'normal)
  855. (set-face-attribute 'diff-context nil
  856. ;; :foreground "white"
  857. :foreground nil
  858. :weight 'normal)
  859. (set-face-foreground 'diff-removed "red")
  860. (set-face-foreground 'diff-added "green")
  861. (set-face-background 'diff-removed nil)
  862. (set-face-background 'diff-added nil)
  863. (set-face-attribute 'diff-changed nil
  864. :foreground "magenta"
  865. :weight 'normal)
  866. (set-face-attribute 'diff-refine-changed nil
  867. :foreground nil
  868. :background nil
  869. :weight 'bold
  870. :inverse-video t)
  871. ;; Annoying !
  872. ;;(diff-auto-refine-mode)
  873. )
  874. ;; (ffap-bindings)
  875. (set-variable 'browse-url-browser-function
  876. 'eww-browse-url)
  877. (set-variable 'sh-here-document-word "__EOC__")
  878. (when (autoload-eval-lazily 'adoc-mode
  879. nil
  880. (defvar adoc-mode-map (make-sparse-keymap))
  881. (define-key adoc-mode-map (kbd "C-m") 'newline))
  882. (setq auto-mode-alist
  883. `(("\\.adoc\\'" . adoc-mode)
  884. ("\\.asciidoc\\'" . adoc-mode)
  885. ,@auto-mode-alist)))
  886. (with-eval-after-load 'markup-faces
  887. ;; Is this too match ?
  888. (set-face-foreground 'markup-meta-face
  889. "color-245")
  890. (set-face-foreground 'markup-meta-hide-face
  891. "color-245")
  892. )
  893. ;; TODO: check if this is required
  894. (when (autoload-eval-lazily 'groovy-mode nil
  895. (defvar groovy-mode-map (make-sparse-keymap))
  896. (define-key groovy-mode-map "(" 'self-insert-command)
  897. (define-key groovy-mode-map ")" 'self-insert-command)
  898. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  899. )
  900. (add-to-list 'auto-mode-alist
  901. '("build\\.gradle\\'" . groovy-mode)))
  902. (with-eval-after-load 'yaml-mode
  903. (defvar yaml-mode-map (make-sparse-keymap))
  904. (define-key yaml-mode-map (kbd "C-m") 'newline))
  905. (with-eval-after-load 'html-mode
  906. (defvar html-mode-map (make-sparse-keymap))
  907. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  908. (with-eval-after-load 'text-mode
  909. (define-key text-mode-map (kbd "C-m") 'newline))
  910. (autoload-eval-lazily 'info nil
  911. (defvar Info-additional-directory-list)
  912. (dolist (dir (directory-files (concat user-emacs-directory
  913. "info")
  914. t
  915. "^[^.].*"))
  916. (when (file-directory-p dir)
  917. (add-to-list 'Info-additional-directory-list
  918. dir)))
  919. (let ((dir (expand-file-name "~/.brew/share/info")))
  920. (when (file-directory-p dir)
  921. (add-to-list 'Info-additional-directory-list
  922. dir))))
  923. (with-eval-after-load 'apropos
  924. (defvar apropos-mode-map (make-sparse-keymap))
  925. (define-key apropos-mode-map "n" 'next-line)
  926. (define-key apropos-mode-map "p" 'previous-line))
  927. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  928. ;; (define-key isearch-mode-map
  929. ;; (kbd "C-j") 'isearch-other-control-char)
  930. ;; (define-key isearch-mode-map
  931. ;; (kbd "C-k") 'isearch-other-control-char)
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-h") 'isearch-other-control-char)
  934. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  935. (define-key isearch-mode-map (kbd "M-r")
  936. 'isearch-query-replace-regexp)
  937. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  938. (setq lazy-highlight-cleanup nil)
  939. ;; face for isearch highlighing
  940. (set-face-attribute 'lazy-highlight
  941. nil
  942. :foreground `unspecified
  943. :background `unspecified
  944. :underline t
  945. ;; :weight `bold
  946. )
  947. (add-hook 'outline-mode-hook
  948. (lambda ()
  949. (when (string-match "\\.md\\'" buffer-file-name)
  950. (set (make-local-variable 'outline-regexp) "#+ "))))
  951. (add-hook 'outline-mode-hook
  952. 'outline-show-all)
  953. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  954. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  955. (when (autoload-eval-lazily 'markdown-mode
  956. '(markdown-mode gfm-mode)
  957. (defvar gfm-mode-map (make-sparse-keymap))
  958. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  959. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  960. (set-variable 'markdown-command (or (executable-find "markdown")
  961. (executable-find "markdown.pl")
  962. ""))
  963. (add-hook 'markdown-mode-hook
  964. (lambda ()
  965. (outline-minor-mode 1)
  966. (flyspell-mode)
  967. (set (make-local-variable 'comment-start) ";")))
  968. )
  969. ;; c-mode
  970. ;; http://www.emacswiki.org/emacs/IndentingC
  971. ;; http://en.wikipedia.org/wiki/Indent_style
  972. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  973. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  974. (with-eval-after-load 'cc-vars
  975. (defvar c-default-style nil)
  976. (add-to-list 'c-default-style
  977. '(c-mode . "k&r"))
  978. (add-to-list 'c-default-style
  979. '(c++-mode . "k&r")))
  980. (autoload-eval-lazily 'js2-mode nil
  981. ;; currently do not use js2-mode
  982. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  983. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  984. ;; (defvar js2-mode-map (make-sparse-keymap))
  985. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  986. ;; (interactive)
  987. ;; (js2-enter-key)
  988. ;; (indent-for-tab-command)))
  989. ;; (add-hook (kill-local-variable 'before-save-hook)
  990. ;; 'js2-before-save)
  991. ;; (add-hook 'before-save-hook
  992. ;; 'my-indent-buffer
  993. ;; nil
  994. ;; t)
  995. )
  996. (add-to-list 'interpreter-mode-alist
  997. '("node" . js-mode))
  998. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  999. (with-eval-after-load 'uniquify
  1000. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1001. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1002. (setq uniquify-min-dir-content 1))
  1003. (with-eval-after-load 'view
  1004. (defvar view-mode-map (make-sparse-keymap))
  1005. (define-key view-mode-map "j" 'scroll-up-line)
  1006. (define-key view-mode-map "k" 'scroll-down-line)
  1007. (define-key view-mode-map "v" 'toggle-read-only)
  1008. (define-key view-mode-map "q" 'bury-buffer)
  1009. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1010. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1011. ;; (define-key view-mode-map
  1012. ;; "n" 'nonincremental-repeat-search-forward)
  1013. ;; (define-key view-mode-map
  1014. ;; "N" 'nonincremental-repeat-search-backward)
  1015. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1016. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1017. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1018. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1019. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1020. (global-set-key "\M-r" 'view-mode)
  1021. ;; (setq view-read-only t)
  1022. (with-eval-after-load 'term
  1023. (defvar term-raw-map (make-sparse-keymap))
  1024. (define-key term-raw-map (kbd "C-x")
  1025. (lookup-key (current-global-map)
  1026. (kbd "C-x"))))
  1027. (add-hook 'term-mode-hook
  1028. (lambda ()
  1029. ;; Stop current line highlighting
  1030. (set (make-local-variable (defvar hl-line-range-function))
  1031. (lambda () '(0 . 0)))
  1032. (set (make-local-variable 'scroll-margin)
  1033. 0)))
  1034. (add-hook 'Man-mode-hook
  1035. (lambda ()
  1036. (view-mode 1)
  1037. (setq truncate-lines nil)))
  1038. (set-variable 'Man-notify-method (if window-system
  1039. 'newframe
  1040. 'aggressive))
  1041. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1042. "woman_cache.el")))
  1043. ;; not work because man.el will be loaded when man called
  1044. (defalias 'man 'woman)
  1045. (add-to-list 'auto-mode-alist
  1046. '("tox\\.ini\\'" . conf-unix-mode))
  1047. (when (autoload-eval-lazily 'toml-mode)
  1048. (add-to-list 'auto-mode-alist
  1049. '("/tox\\.ini\\'" . toml-mode))
  1050. (add-to-list 'auto-mode-alist
  1051. '("/Pipfile\\'" . toml-mode)))
  1052. (when (autoload-eval-lazily 'json-mode)
  1053. (add-to-list 'auto-mode-alist
  1054. '("/Pipfile\\.lock\\'" . json-mode)))
  1055. (add-hook 'go-mode-hook
  1056. (lambda()
  1057. (defvar go-mode-map)
  1058. (add-hook 'before-save-hook' 'gofmt-before-save)
  1059. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1060. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1061. ;; buffers
  1062. (defvar bs-configurations)
  1063. (when (autoload-eval-lazily 'bs '(bs-show)
  1064. (add-to-list 'bs-configurations
  1065. '("specials" "^\\*" nil ".*" nil nil))
  1066. (add-to-list 'bs-configurations
  1067. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1068. (defvar bs-mode-map)
  1069. (defvar bs-current-configuration)
  1070. (define-key bs-mode-map (kbd "t")
  1071. ;; TODO: fix toggle feature
  1072. (lambda ()
  1073. (interactive)
  1074. (if (string= "specials"
  1075. bs-current-configuration)
  1076. (bs-set-configuration "files")
  1077. (bs-set-configuration "specials"))
  1078. (bs-refresh)
  1079. (bs-message-without-log "%s"
  1080. (bs--current-config-message))))
  1081. ;; (setq bs-configurations (list
  1082. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1083. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1084. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1085. )
  1086. (defalias 'list-buffers 'bs-show)
  1087. (set-variable 'bs-default-configuration "files-and-specials")
  1088. (set-variable 'bs-default-sort-name "by nothing")
  1089. (add-hook 'bs-mode-hook
  1090. (lambda ()
  1091. (set (make-local-variable 'scroll-margin) 0))))
  1092. ;;(iswitchb-mode 1)
  1093. (icomplete-mode)
  1094. (defun iswitchb-buffer-display-other-window ()
  1095. "Do iswitchb in other window."
  1096. (interactive)
  1097. (let ((iswitchb-default-method 'display))
  1098. (call-interactively 'iswitchb-buffer)))
  1099. ;; buffer killing
  1100. ;; (defun my-delete-window-killing-buffer () nil)
  1101. (defun my-query-kill-current-buffer ()
  1102. "Interactively kill current buffer."
  1103. (interactive)
  1104. (if (y-or-n-p (concat "kill current buffer? :"))
  1105. (kill-buffer (current-buffer))))
  1106. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1107. (substitute-key-definition 'kill-buffer
  1108. 'my-query-kill-current-buffer
  1109. global-map)
  1110. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1111. ;; recentf-mode
  1112. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1113. "recentf")))
  1114. (set-variable 'recentf-max-menu-items 20)
  1115. (set-variable 'recentf-max-saved-items 30)
  1116. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1117. (set-variable 'recentf-auto-cleanup 3)
  1118. ;; (safe-require-or-eval 'sync-recentf)
  1119. ;; (when (safe-require-or-eval 'recentf)
  1120. ;; (add-to-list 'recentf-exclude
  1121. ;; (regexp-quote recentf-save-file))
  1122. ;; (add-to-list 'recentf-exclude
  1123. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1124. ;; (add-to-list 'recentf-exclude
  1125. ;; "/sync-recentf-marker\\'")
  1126. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1127. ;; (remove-hook 'find-file-hook
  1128. ;; 'recentf-track-opened-file)
  1129. ;; (defun my-recentf-load-track-save-list ()
  1130. ;; "Load current recentf list from file, track current visiting file, then save
  1131. ;; the list."
  1132. ;; (recentf-load-list)
  1133. ;; (recentf-track-opened-file)
  1134. ;; (recentf-save-list))
  1135. ;; (add-hook 'find-file-hook
  1136. ;; 'my-recentf-load-track-save-list)
  1137. ;; (add-hook 'kill-emacs-hook
  1138. ;; 'recentf-load-list)
  1139. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1140. ;; ;; (add-hook 'find-file-hook
  1141. ;; ;; (lambda ()
  1142. ;; ;; (recentf-add-file default-directory)))
  1143. ;; (when (autoload-eval-lazily 'recentf-show)
  1144. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1145. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1146. ;; ;; 'recentf-load-list)
  1147. ;; )
  1148. ;; (recentf-mode 1)
  1149. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1150. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1151. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1152. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1153. ;; (add-hook 'recentf-dialog-mode-hook
  1154. ;; (lambda ()
  1155. ;; ;; (recentf-save-list)
  1156. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1157. ;; ;; 'my-recentf-cd-and-find-file)
  1158. ;; (cd "~/"))))
  1159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1160. ;; dired
  1161. (defun my-file-head (filename &optional n)
  1162. "Return list of first N lines of file FILENAME."
  1163. ;; TODO: Fix for janapese text
  1164. ;; TODO: Fix for short text
  1165. (let ((num (or n 10))
  1166. (size 100)
  1167. (beg 0)
  1168. (end 0)
  1169. (result '())
  1170. (read -1))
  1171. (with-temp-buffer
  1172. (erase-buffer)
  1173. (while (or (<= (count-lines (point-min)
  1174. (point-max))
  1175. num)
  1176. (eq read 0))
  1177. (setq end (+ beg size))
  1178. (setq read (nth 1 (insert-file-contents-literally filename
  1179. nil
  1180. beg
  1181. end)))
  1182. (goto-char (point-max))
  1183. (setq beg (+ beg size)))
  1184. (goto-char (point-min))
  1185. (while (< (length result) num)
  1186. (let ((start (point)))
  1187. (forward-line 1)
  1188. (setq result
  1189. `(,@result ,(buffer-substring-no-properties start
  1190. (point))))))
  1191. result
  1192. ;; (buffer-substring-no-properties (point-min)
  1193. ;; (progn
  1194. ;; (forward-line num)
  1195. ;; (point)))
  1196. )))
  1197. ;; (apply 'concat (my-file-head "./shrc" 10)
  1198. (defun my-dired-echo-file-head (arg)
  1199. "Echo head of current file.
  1200. ARG is num to show, or defaults to 7."
  1201. (interactive "P")
  1202. (let ((f (dired-get-filename)))
  1203. (message "%s"
  1204. (apply 'concat
  1205. (my-file-head f
  1206. 7)))))
  1207. (defun my-dired-diff ()
  1208. "Show diff of marked file and file of current line."
  1209. (interactive)
  1210. (let ((files (dired-get-marked-files nil nil nil t)))
  1211. (if (eq (car files)
  1212. t)
  1213. (diff (cadr files) (dired-get-filename))
  1214. (message "One file must be marked!"))))
  1215. (defun dired-get-file-info ()
  1216. "Print information of current line file."
  1217. (interactive)
  1218. (let ((f (shell-quote-argument (dired-get-filename t))))
  1219. (if (file-directory-p f)
  1220. (progn
  1221. (message "Calculating disk usage...")
  1222. (shell-command (concat "du -hsD "
  1223. f)))
  1224. (shell-command (concat "file "
  1225. f)))))
  1226. (defun my-dired-scroll-up ()
  1227. "Scroll up."
  1228. (interactive)
  1229. (my-dired-previous-line (- (window-height) 1)))
  1230. (defun my-dired-scroll-down ()
  1231. "Scroll down."
  1232. (interactive)
  1233. (my-dired-next-line (- (window-height) 1)))
  1234. ;; (defun my-dired-forward-line (arg)
  1235. ;; ""
  1236. ;; (interactive "p"))
  1237. (defun my-dired-previous-line (arg)
  1238. "Move ARG lines up."
  1239. (interactive "p")
  1240. (if (> arg 0)
  1241. (progn
  1242. (if (eq (line-number-at-pos)
  1243. 1)
  1244. (goto-char (point-max))
  1245. (forward-line -1))
  1246. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1247. (dired-get-subdir))
  1248. (- arg 1)
  1249. arg)))
  1250. (dired-move-to-filename)))
  1251. (defun my-dired-next-line (arg)
  1252. "Move ARG lines down."
  1253. (interactive "p")
  1254. (if (> arg 0)
  1255. (progn
  1256. (if (eq (point)
  1257. (point-max))
  1258. (goto-char (point-min))
  1259. (forward-line 1))
  1260. (my-dired-next-line (if (or (dired-get-filename nil t)
  1261. (dired-get-subdir))
  1262. (- arg 1)
  1263. arg)))
  1264. (dired-move-to-filename)))
  1265. (defun my-tramp-remote-find-file (f)
  1266. "Open F."
  1267. (interactive (list (read-file-name "My Find File Tramp: "
  1268. "/scp:"
  1269. nil ;; "/scp:"
  1270. (confirm-nonexistent-file-or-buffer))))
  1271. (find-file f))
  1272. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1273. (if (eq window-system 'mac)
  1274. (setq dired-listing-switches "-lhF")
  1275. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1276. )
  1277. (setq dired-listing-switches "-lhF")
  1278. ;; when using dired-find-alternate-file
  1279. ;; reuse current dired buffer for the file to open
  1280. ;; (put 'dired-find-alternate-file 'disabled nil)
  1281. (set-variable 'dired-ls-F-marks-symlinks t)
  1282. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1283. (set-variable 'ls-lisp-dirs-first t)
  1284. (set-variable 'ls-lisp-use-localized-time-format t)
  1285. (set-variable 'ls-lisp-format-time-list
  1286. '("%Y-%m-%d %H:%M"
  1287. "%Y-%m-%d "))
  1288. (set-variable 'dired-dwim-target t)
  1289. (set-variable 'dired-isearch-filenames t)
  1290. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1291. (set-variable 'dired-hide-details-hide-information-lines nil)
  1292. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1293. (set-variable 'dired-recursive-deletes 'always)
  1294. ;; (add-hook 'dired-after-readin-hook
  1295. ;; 'my-replace-nasi-none)
  1296. (with-eval-after-load 'dired
  1297. (safe-require-or-eval 'ls-lisp)
  1298. (defvar dired-mode-map (make-sparse-keymap))
  1299. ;; dired-do-chgrp sometimes cause system hung
  1300. (define-key dired-mode-map "G" 'ignore)
  1301. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1302. (define-key dired-mode-map "i" 'dired-get-file-info)
  1303. (define-key dired-mode-map "f" 'find-file)
  1304. (define-key dired-mode-map "!" 'shell-command)
  1305. (define-key dired-mode-map "&" 'async-shell-command)
  1306. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1307. (define-key dired-mode-map "=" 'my-dired-diff)
  1308. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1309. (define-key dired-mode-map "b" 'gtkbm)
  1310. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1311. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1312. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1313. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1314. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1315. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1316. (substitute-key-definition 'dired-next-line
  1317. 'my-dired-next-line
  1318. dired-mode-map)
  1319. (substitute-key-definition 'dired-previous-line
  1320. 'my-dired-previous-line
  1321. dired-mode-map)
  1322. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1323. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1324. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1325. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1326. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1327. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1328. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1329. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1330. (add-hook 'dired-mode-hook
  1331. (lambda ()
  1332. (when (fboundp 'dired-hide-details-mode)
  1333. (dired-hide-details-mode t)
  1334. (local-set-key "l" 'dired-hide-details-mode))
  1335. (let ((file "._Icon\015"))
  1336. (when nil
  1337. '(file-readable-p file)
  1338. (delete-file file)))))
  1339. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1340. (with-eval-after-load 'dired
  1341. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1342. (when (autoload-eval-lazily 'dired-list-all-mode)
  1343. (setq dired-listing-switches "-lhF")
  1344. (with-eval-after-load 'dired
  1345. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1346. (when (autoload-eval-lazily 'dired-filter)
  1347. (add-hook 'dired-mode-hook
  1348. 'dired-filter-mode))
  1349. ;; Currently disabled in favor of dired-from-git-ls-files
  1350. ;; (define-key ctl-x-map "f" 'find-dired)
  1351. ;; It works!
  1352. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1353. (defun my-dired-git-ls-files (args)
  1354. "Dired from git ls-files."
  1355. (interactive "sgit ls-files args: ")
  1356. (pop-to-buffer-same-window
  1357. (dired-noselect `(,default-directory
  1358. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1359. "\0" t))
  1360. ""))
  1361. )
  1362. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1363. (with-eval-after-load 'dired
  1364. (defvar dired-mode-map (make-sparse-keymap))
  1365. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1366. ;; (define-minor-mode my-dired-glob-filter)
  1367. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1368. ;; misc funcs
  1369. (defalias 'qcalc 'quick-calc)
  1370. (defun memo (&optional dir)
  1371. "Open memo.txt in DIR."
  1372. (interactive)
  1373. (pop-to-buffer (find-file-noselect (concat (if dir
  1374. (file-name-as-directory dir)
  1375. "")
  1376. "memo.txt"))))
  1377. (set (defvar my-rgrep-alist nil
  1378. "Alist of rgrep command.
  1379. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1380. condition to choose COMMAND when evaluated.")
  1381. `(
  1382. ;; ripgrep
  1383. ("rg"
  1384. (executable-find "rg")
  1385. "rg --hidden --no-heading --smart-case ")
  1386. ;; git grep
  1387. ("gitgrep"
  1388. (eq 0
  1389. (shell-command "git rev-parse --git-dir"))
  1390. "git --no-pager -c color.grep=always grep -nH -e ")
  1391. ;; sift
  1392. ("sift"
  1393. (executable-find "sift")
  1394. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1395. ;; the silver searcher
  1396. ("ag"
  1397. (executable-find "ag")
  1398. "ag --nogroup --nopager --filename ")
  1399. ;; ack
  1400. ("ack"
  1401. (executable-find "ack")
  1402. "ack --nogroup --nopager --with-filename ")
  1403. ;; gnu global
  1404. ("global"
  1405. (and (require 'ggtags nil t)
  1406. (executable-find "global")
  1407. (ggtags-current-project-root))
  1408. "global --result grep ")
  1409. ;; grep
  1410. ("grep"
  1411. t
  1412. ,(concat "find . "
  1413. "-path '*/.git' -prune -o "
  1414. "-path '*/.svn' -prune -o "
  1415. "-type f -print0 | "
  1416. "xargs -0 grep -nH -e "))
  1417. )
  1418. )
  1419. (defvar my-rgrep-default nil
  1420. "Default command name for my-rgrep.")
  1421. (defun my-rgrep-grep-command (&optional name alist)
  1422. "Return recursive grep command for current directory or nil.
  1423. If NAME is given, use that without testing.
  1424. Commands are searched from ALIST."
  1425. (if alist
  1426. (if name
  1427. ;; if name is given search that from alist and return the command
  1428. (nth 2 (assoc name
  1429. alist))
  1430. ;; if name is not given try test in 1th elem
  1431. (let ((car (car alist))
  1432. (cdr (cdr alist)))
  1433. (if (eval (nth 1 car))
  1434. ;; if the condition is true return the command
  1435. (nth 2 car)
  1436. ;; try next one
  1437. (and cdr
  1438. (my-rgrep-grep-command name cdr)))))
  1439. ;; if alist is not given set default value
  1440. (my-rgrep-grep-command name my-rgrep-alist)))
  1441. (defun my-rgrep (command-args)
  1442. "My recursive grep. Run COMMAND-ARGS.
  1443. If prefix argument is given, use current symbol as default search target
  1444. and search from projectile root (if projectile is available)."
  1445. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1446. nil)))
  1447. (if cmd
  1448. (list (read-shell-command "grep command: "
  1449. (concat cmd
  1450. (if current-prefix-arg
  1451. (thing-at-point 'symbol t)
  1452. ""))
  1453. 'grep-find-history))
  1454. (error "My-Rgrep: Command for rgrep not found")
  1455. )))
  1456. (if (and current-prefix-arg
  1457. (safe-require-or-eval 'projectile)
  1458. (projectile-project-p))
  1459. (projectile-with-default-dir (projectile-project-root)
  1460. (compilation-start command-args
  1461. 'grep-mode))
  1462. (compilation-start command-args
  1463. 'grep-mode)))
  1464. (defun my-rgrep-thing-at-point-projectile-root ()
  1465. "My recursive grep to find thing at point from project root."
  1466. (interactive)
  1467. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1468. nil))
  1469. (command-args
  1470. (if cmd
  1471. (concat cmd
  1472. (or (thing-at-point 'symbol t)
  1473. (error "No symbol at point")))
  1474. (error "My-Rgrep: Command for rgrep not found"))))
  1475. (if (safe-require-or-eval 'projectile)
  1476. (projectile-with-default-dir (or (projectile-project-root)
  1477. default-directory)
  1478. (compilation-start command-args
  1479. 'grep-mode))
  1480. (compilation-start command-args
  1481. 'grep-mode))))
  1482. (defmacro define-my-rgrep (name)
  1483. "Define rgrep for NAME."
  1484. `(defun ,(intern (concat "my-rgrep-"
  1485. name)) ()
  1486. ,(format "My recursive grep by %s."
  1487. name)
  1488. (interactive)
  1489. (let ((my-rgrep-default ,name))
  1490. (if (called-interactively-p 'any)
  1491. (call-interactively 'my-rgrep)
  1492. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1493. )
  1494. (define-my-rgrep "ack")
  1495. (define-my-rgrep "ag")
  1496. (define-my-rgrep "rg")
  1497. (define-my-rgrep "sift")
  1498. (define-my-rgrep "gitgrep")
  1499. (define-my-rgrep "grep")
  1500. (define-my-rgrep "global")
  1501. (define-key ctl-x-map "s" 'my-rgrep)
  1502. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1503. (defun my-occur (regexp &optional region)
  1504. "My occur command to search REGEXP."
  1505. (interactive (list (read-string "List lines matching regexp: "
  1506. (thing-at-point 'symbol t))))
  1507. (occur regexp nil region))
  1508. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1509. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1510. (defalias 'make 'compile)
  1511. (define-key ctl-x-map "c" 'compile)
  1512. ;;;;;;;;;;;;;;;;;;;;;;;
  1513. ;; adoc-simple-mode
  1514. (when (safe-require-or-eval 'adoc-mode)
  1515. (defvar adoc-simple-font-lock-keywords
  1516. nil)
  1517. (define-derived-mode adoc-simple-mode adoc-mode
  1518. "Adoc-Simple"
  1519. "Major mode for editing AsciiDoc text files.
  1520. This mode is a simplified version of `adoc-mode'."
  1521. '(set (make-local-variable 'font-lock-defaults)
  1522. '(adoc-simple-font-lock-keywords
  1523. nil nil nil nil
  1524. (font-lock-multiline . t)
  1525. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1526. (add-to-list 'auto-mode-alist
  1527. '("\\.adoc\\'" . adoc-simple-mode)))
  1528. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1529. ;; awk-preview
  1530. (defgroup awk-preview nil
  1531. "Awk previewer."
  1532. :tag "Awk Preview"
  1533. :prefix "awk-preview-"
  1534. :group 'tools)
  1535. (defcustom awk-preview-program
  1536. (or (executable-find "gawk")
  1537. (executable-find "awk")
  1538. "awk")
  1539. "Awk program to execute."
  1540. :type 'string
  1541. :group 'awk-preview)
  1542. (defcustom awk-preview-switches
  1543. ;; '("--sandbox")
  1544. nil
  1545. "String of awk options appended when running awk preview."
  1546. :type '(repeat string)
  1547. :group 'awk-preview)
  1548. (defcustom awk-preview-default-program
  1549. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1550. {
  1551. print NR, $0
  1552. }
  1553. "
  1554. "Default awk command."
  1555. :type 'string
  1556. :group 'awk-preview)
  1557. (defvar awk-preview--point-beg
  1558. nil
  1559. "Point of beginning.")
  1560. (make-variable-buffer-local 'awk-preview--point-beg)
  1561. (defvar awk-preview--point-end
  1562. nil
  1563. "Point of end.")
  1564. (make-variable-buffer-local 'awk-preview--point-end)
  1565. (defvar awk-preview--preview-point-beg
  1566. nil
  1567. "Point of beginning.
  1568. Used by preview buffer and always same as awk-preview--point-beg.")
  1569. (make-variable-buffer-local 'awk-preview--preview-point-beg)
  1570. (defvar awk-preview--preview-point-end
  1571. nil
  1572. "Point of beginning.
  1573. Used by preview buffer and may defferent from awk-preview--point-end.")
  1574. (make-variable-buffer-local 'awk-preview--preview-point-end)
  1575. (defvar awk-preview--program-filename
  1576. nil
  1577. "Awk preview program file name.")
  1578. (make-variable-buffer-local 'awk-preview--program-filename)
  1579. (defvar awk-preview--source-buffer
  1580. nil
  1581. "Awk source buffer.")
  1582. (make-variable-buffer-local 'awk-preview--source-buffer)
  1583. (defvar awk-preview--preview-buffer
  1584. nil
  1585. "Awk preview buffer.")
  1586. (make-variable-buffer-local 'awk-preview--preview-buffer)
  1587. (defvar awk-preview--program-buffer
  1588. nil
  1589. "Awk program buffer.")
  1590. (make-variable-buffer-local 'awk-preview--program-buffer)
  1591. (defvar awk-preview--window-configuration
  1592. nil
  1593. "Awk preview window configuration.")
  1594. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1595. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1596. When OUTPUT is t the result will be output to BUF.
  1597. Delete the text between BEG and END when DELETE is non-nil."
  1598. (with-current-buffer buf
  1599. (let ((status (apply 'call-process-region
  1600. beg
  1601. end
  1602. awk-preview-program
  1603. delete
  1604. output
  1605. nil
  1606. `(,@awk-preview-switches "-f" ,progfile))))
  1607. (unless (eq status
  1608. 0)
  1609. (error "awk-preview: Awk program exited abnormally."))
  1610. output)))
  1611. (defvar awk-preview-program-buffer-name
  1612. "*AWK Preview Program<%s>*"
  1613. "Buffer name of awk preview program.")
  1614. (defvar awk-preview-preview-buffer-name
  1615. "*AWK Preview<%s>*"
  1616. "Buffer name of awk preview.")
  1617. (defun awk-preview--create-program-buffer (source preview)
  1618. "Create and show awk program buffer for SOURCE and PREVIEW buffer.
  1619. Return that buffer."
  1620. (let ((source-name (buffer-name source)))
  1621. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1622. source-name))
  1623. (erase-buffer)
  1624. (insert awk-preview-default-program)
  1625. (awk-mode)
  1626. (awk-preview-program-mode 1)
  1627. (setq awk-preview--source-buffer source)
  1628. (setq awk-preview--preview-buffer preview)
  1629. (setq awk-preview--program-buffer (current-buffer))
  1630. (setq awk-preview--program-filename (make-temp-file "awk-preview-"
  1631. nil
  1632. ".awk"))
  1633. (current-buffer))))
  1634. (defun awk-preview-program-buffer-kill-hook ()
  1635. "Cleanup for awk-preview program buffer."
  1636. (when awk-preview--program-filename
  1637. (delete-file awk-preview--program-filename)))
  1638. (add-hook 'kill-buffer-hook
  1639. 'awk-preview-program-buffer-kill-hook)
  1640. (defun awk-preview--create-preview-buffer (source)
  1641. "Create preview buffer of SOURCE buffer and return it."
  1642. (with-current-buffer source
  1643. (let ((buffer-file-name nil)
  1644. (beg awk-preview--point-beg)
  1645. (end awk-preview--point-end))
  1646. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1647. (buffer-name)))
  1648. (awk-preview-program-mode 1)
  1649. (setq awk-preview--preview-point-beg beg)
  1650. (setq awk-preview--preview-point-end end)
  1651. (setq awk-preview--source-buffer source)
  1652. (setq awk-preview--preview-buffer (current-buffer))
  1653. (goto-char end)
  1654. (setq buffer-read-only t)
  1655. (current-buffer)))))
  1656. (defun awk-preview (beg end)
  1657. "Run awk and preview result."
  1658. (interactive "r")
  1659. (setq awk-preview--point-beg beg)
  1660. (setq awk-preview--point-end end)
  1661. (setq awk-preview--source-buffer (current-buffer))
  1662. (setq awk-preview--preview-buffer
  1663. (awk-preview--create-preview-buffer (current-buffer)))
  1664. (setq awk-preview--program-buffer
  1665. (awk-preview--create-program-buffer (current-buffer)
  1666. awk-preview--preview-buffer))
  1667. ;; At creation of preview buffer, program buffer is not available so set this
  1668. ;; here
  1669. (let ((b awk-preview--program-buffer))
  1670. (with-current-buffer awk-preview--preview-buffer
  1671. (setq awk-preview--program-buffer b)))
  1672. (setq awk-preview--window-configuration
  1673. (current-window-configuration))
  1674. (set-window-buffer (get-buffer-window awk-preview--source-buffer)
  1675. awk-preview--preview-buffer)
  1676. (pop-to-buffer awk-preview--program-buffer)
  1677. (switch-to-buffer awk-preview--program-buffer)
  1678. (awk-preview-update-preview)
  1679. )
  1680. (defun awk-preview-update-preview ()
  1681. "Update awk-preview."
  1682. (interactive)
  1683. (with-current-buffer awk-preview--program-buffer
  1684. (write-region (point-min)
  1685. (point-max)
  1686. awk-preview--program-filename))
  1687. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1688. (erase-buffer)
  1689. (current-buffer)))
  1690. (progfile (with-current-buffer awk-preview--program-buffer
  1691. awk-preview--program-filename)))
  1692. (with-current-buffer awk-preview--source-buffer
  1693. (awk-preview--invoke-awk (current-buffer)
  1694. awk-preview--point-beg
  1695. awk-preview--point-end
  1696. progfile
  1697. output)
  1698. (with-current-buffer awk-preview--preview-buffer
  1699. (let ((inhibit-read-only t))
  1700. (goto-char awk-preview--preview-point-end)
  1701. (delete-region awk-preview--preview-point-beg (point))
  1702. (insert-buffer-substring output)
  1703. (setq awk-preview--preview-point-end (point))))
  1704. )))
  1705. (defun awk-preview-commit ()
  1706. "Exit awk-preview and update buffer."
  1707. (interactive)
  1708. (awk-preview--cleanup))
  1709. (defun awk-preview-abort ()
  1710. "Discard result and exit awk-preview."
  1711. (interactive)
  1712. (awk-preview--cleanup))
  1713. (defun awk-preview--cleanup()
  1714. "Cleanup awk preview buffers and variables.")
  1715. (defvar awk-preview-program-mode-map
  1716. (let ((map (make-sparse-keymap)))
  1717. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1718. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1719. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1720. map)
  1721. "Keymap for `awk-preview-program-mode'.")
  1722. (define-minor-mode awk-preview-program-mode
  1723. "Minor mode for awk-preview program buffer."
  1724. :lighter " AWKPrev"
  1725. :keymap awk-preview-program-mode-map)
  1726. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1727. ;; editorconfig-auto-apply
  1728. (define-minor-mode editorconfig-auto-apply-mode
  1729. "When saving .editorconfig file update buffer configs."
  1730. :global t
  1731. :lighter ""
  1732. (if editorconfig-auto-apply-mode
  1733. (add-hook 'after-save-hook
  1734. 'editorconfig-auto-apply-mode--run)
  1735. (remove-hook 'after-save-hook
  1736. 'editorconfig-auto-apply-mode--run)))
  1737. (defun editorconfig-auto-apply-mode--run ()
  1738. "When saving .editorconfig file walk all buffers and update configs."
  1739. (when (eq major-mode
  1740. 'editorconfig-conf-mode)
  1741. (let ((dir (file-name-directory buffer-file-name)))
  1742. (cl-dolist (buf (buffer-list))
  1743. (when (and (buffer-file-name buf)
  1744. (file-in-directory-p (buffer-file-name buf)
  1745. dir))
  1746. (with-current-buffer buf
  1747. (editorconfig-mode-apply)))))))
  1748. (editorconfig-auto-apply-mode 1)
  1749. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1750. ;; recently
  1751. (defgroup recently nil
  1752. "Recently visited files"
  1753. :tag "Recently"
  1754. :prefix "recently-"
  1755. :group 'tools)
  1756. (defcustom recently-file
  1757. (concat user-emacs-directory
  1758. "recently.el")
  1759. "Recently file."
  1760. :type 'string
  1761. :group 'recently)
  1762. (defcustom recently-max
  1763. 100
  1764. "Recently list max length."
  1765. :type 'int
  1766. :group 'recently)
  1767. (defcustom recently-excludes
  1768. '()
  1769. "List of regexps for filenames excluded from the recent list."
  1770. :type '(repeat string)
  1771. :group 'recently)
  1772. (add-to-list 'recently-excludes
  1773. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1774. (defvar recently-list
  1775. '()
  1776. "Recently list.")
  1777. (defvar recently-file-mtime
  1778. nil
  1779. "Modified time of file when last read file.")
  1780. (defun recently-write ()
  1781. "Write to file."
  1782. (with-temp-buffer
  1783. (prin1 recently-list
  1784. (current-buffer))
  1785. (write-region (point-min)
  1786. (point-max)
  1787. recently-file)))
  1788. (defun recently-read ()
  1789. "Read file."
  1790. (when (file-readable-p recently-file)
  1791. (with-temp-buffer
  1792. (insert-file-contents recently-file)
  1793. (goto-char (point-min))
  1794. (setq recently-list
  1795. (read (current-buffer))))
  1796. (setq recently-file-mtime
  1797. (nth 5
  1798. (file-attributes recently-file)))))
  1799. (defun recently-reload ()
  1800. "Reload file."
  1801. (when (and (file-readable-p recently-file)
  1802. (not (equal recently-file-mtime
  1803. (nth 5
  1804. (file-attributes recently-file)))))
  1805. (recently-read)
  1806. (cl-assert (equal recently-file-mtime
  1807. (nth 5
  1808. (file-attributes recently-file))))))
  1809. (defun recently-add (path)
  1810. "Add PATH to list."
  1811. (cl-assert (string= path
  1812. (expand-file-name path)))
  1813. (when (cl-loop for re in recently-excludes
  1814. if (string-match re path) return nil
  1815. finally return t)
  1816. (recently-reload)
  1817. (let* ((l (copy-list recently-list))
  1818. (l (delete path
  1819. l))
  1820. (l (cl-loop for e in l
  1821. unless (file-in-directory-p path e)
  1822. collect e))
  1823. (l (cons path
  1824. l))
  1825. (l (recently--truncate l
  1826. recently-max)))
  1827. (unless (equal recently-list
  1828. l)
  1829. (setq recently-list l)
  1830. (recently-write)))))
  1831. (defun recently--truncate (list len)
  1832. "Truncate LIST to LEN."
  1833. (if (> (length list)
  1834. len)
  1835. (cl-subseq list
  1836. 0
  1837. len)
  1838. list))
  1839. (defun recently-find-file-hook ()
  1840. "Add current file."
  1841. (when buffer-file-name
  1842. (recently-add buffer-file-name)))
  1843. (defun recently-dired-mode-hook ()
  1844. "Add current directory."
  1845. (recently-add (expand-file-name default-directory)))
  1846. (add-hook 'find-file-hook
  1847. 'recently-find-file-hook)
  1848. (add-hook 'dired-mode-hook
  1849. 'recently-dired-mode-hook)
  1850. ;; recently-show
  1851. (defvar recently-show-window-height 10
  1852. "Max height of window of `recently-show'")
  1853. (defvar recently-show-mode-map
  1854. (let ((map (make-sparse-keymap)))
  1855. (suppress-keymap map)
  1856. (define-key map "n" 'next-line)
  1857. (define-key map "p" 'previous-line)
  1858. (define-key map (kbd "C-m") 'recently-show-find-file)
  1859. (define-key map (kbd "SPC") 'recently-show-find-file)
  1860. (define-key map "v" 'recently-show-view-file)
  1861. (define-key map "@" 'recently-show-dired)
  1862. (define-key map "q" 'recently-show-close)
  1863. (define-key map (kbd "C-g") 'recently-show-close)
  1864. (define-key map "?" 'describe-mode)
  1865. (define-key map "/" 'isearch-forward)
  1866. map))
  1867. (defvar recently-show-before-listing-hook nil
  1868. "Hook run before creating buffer of `recently-show'.")
  1869. (defvar recently-show-window-configuration nil
  1870. "Used for internal")
  1871. (defvar recently-show-abbreviate t
  1872. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1873. (define-derived-mode recently-show-mode special-mode "recently-show"
  1874. "Major mode for `recently-show'."
  1875. ;; (set (make-local-variable 'scroll-margin)
  1876. ;; 0)
  1877. )
  1878. ;;;###autoload
  1879. (defun recently-show (&optional files buffer-name)
  1880. "Show simplified list of recently opened files.
  1881. If optional argument FILES is non-nil, it is a list of recently-opened
  1882. files to choose from. It defaults to the whole recent list.
  1883. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1884. use for the buffer. It defaults to \"*recetf-show*\"."
  1885. (interactive)
  1886. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1887. (if bf
  1888. (progn
  1889. ;; (recently-save-list)
  1890. (setq recently-show-window-configuration (current-window-configuration))
  1891. ;;(pop-to-buffer bf t t)
  1892. (display-buffer bf)
  1893. ;; (set-window-text-height (selected-window)
  1894. ;; recently-show-window-height)
  1895. (shrink-window-if-larger-than-buffer (selected-window)))
  1896. (message "No recent file!"))))
  1897. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1898. "Create buffer listing recently files FILES."
  1899. (let ((bname (or buffer-name
  1900. "*recently-show-tabulated*"))
  1901. (list (or files
  1902. (progn
  1903. (recently-reload)
  1904. recently-list))))
  1905. (when list
  1906. (when (get-buffer bname)
  1907. (kill-buffer bname))
  1908. (with-current-buffer (get-buffer-create bname)
  1909. (setq tabulated-list-format
  1910. `[("Name" 30 t)
  1911. ("Full Path" 0 t)])
  1912. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1913. (setq tabulated-list-entries
  1914. (mapcar (lambda (f)
  1915. (list f
  1916. (vector (file-name-nondirectory f) f)))
  1917. ;; list
  1918. recently-list
  1919. ))
  1920. (recently-show-tabulated-mode)
  1921. (current-buffer)))))
  1922. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1923. "Major mode for browsing recently opened files and directories."
  1924. (setq tabulated-list-padding 2)
  1925. ;; TODO: Implement revert
  1926. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  1927. ;; TODO: Implement open
  1928. (tabulated-list-init-header)
  1929. (tabulated-list-print nil nil))
  1930. (defun recently-show-create-buffer (&optional files buffer-name)
  1931. "Create buffer listing recently files."
  1932. (let ((bname (or buffer-name
  1933. "*recently-show*"))
  1934. (list (or files
  1935. (progn
  1936. (recently-reload)
  1937. recently-list))))
  1938. (when list
  1939. (and (get-buffer bname)
  1940. (kill-buffer bname))
  1941. (let ((bf (get-buffer-create bname)))
  1942. (with-current-buffer bf
  1943. (recently-show-mode)
  1944. (let ((inhibit-read-only t))
  1945. (mapc (lambda (f)
  1946. (insert (if recently-show-abbreviate
  1947. (abbreviate-file-name f)
  1948. f)
  1949. "\n"))
  1950. list))
  1951. (goto-char (point-min))
  1952. ;; (setq buffer-read-only t)
  1953. )
  1954. bf))))
  1955. (defun recently-show-close ()
  1956. "Close recently-show window."
  1957. (interactive)
  1958. (kill-buffer (current-buffer))
  1959. (set-window-configuration recently-show-window-configuration))
  1960. (defun recently-show-find-file ()
  1961. "Fine file of current line."
  1962. (interactive)
  1963. (let ((f (recently-show-get-filename)))
  1964. (recently-show-close)
  1965. (find-file f)))
  1966. (defun recently-show-view-file ()
  1967. "view file of current line."
  1968. (interactive)
  1969. (let ((f (recently-show-get-filename)))
  1970. (recently-show-close)
  1971. (view-file f)))
  1972. (defun recently-show-get-filename ()
  1973. "Get filename of current line."
  1974. (buffer-substring-no-properties (point-at-bol)
  1975. (point-at-eol)))
  1976. (defun recently-show-dired()
  1977. "Open dired buffer of directory containing file of current line."
  1978. (interactive)
  1979. (let ((f (recently-show-get-filename)))
  1980. (recently-show-close)
  1981. (dired (if (file-directory-p f)
  1982. f
  1983. (or (file-name-directory f)
  1984. ".")))))
  1985. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1986. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1987. ;; git walktree
  1988. (defgroup git-walktree nil
  1989. "Git Walktree."
  1990. :tag "GitWalktree"
  1991. :prefix "git-walktree-"
  1992. :group 'tools)
  1993. (defvar git-walktree-current-committish nil
  1994. "Committish name of currently browsing.")
  1995. (make-variable-buffer-local 'git-walktree-current-committish)
  1996. (defvar git-walktree-current-path nil
  1997. "Path name currently visiting without leading and trailing slash.
  1998. This path is always relative to repository root.")
  1999. (make-variable-buffer-local 'git-walktree-current-path)
  2000. (defvar git-walktree-buffer-file-name nil
  2001. "Psudo filename of current buffer.")
  2002. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2003. (defvar git-walktree-object-full-sha1 nil
  2004. "Object name in full sha1 format of current buffer.")
  2005. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2006. (defvar git-walktree-repository-root nil
  2007. "Repository root path of current buffer.")
  2008. (make-variable-buffer-local 'git-walktree-repository-root)
  2009. (put 'git-walktree-repository-root
  2010. 'permanent-local
  2011. t)
  2012. (defun git-walktree--committish-fordisplay (committish)
  2013. "Convert COMMITTISH and return is a suitable format for displaying."
  2014. (if (and committish
  2015. (string-match-p "\\`[0-9a-f]+\\'"
  2016. committish)
  2017. (>= (length committish) 32))
  2018. (git-walktree--git-plumbing "rev-parse"
  2019. "--short"
  2020. committish)
  2021. committish))
  2022. (defun git-walktree--create-buffer (committish name type)
  2023. "Create and return buffer for COMMITTISH:NAME.
  2024. TYPE is target object type."
  2025. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2026. "--show-toplevel"))
  2027. (committish-display (git-walktree--committish-fordisplay committish))
  2028. (name (format "%s:%s"
  2029. (or committish-display "")
  2030. name)))
  2031. (if (and git-walktree-reuse-tree-buffer
  2032. (string= type "tree"))
  2033. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2034. (setq git-walktree-tree-buffer-for-reuse
  2035. (generate-new-buffer "gitwalktreebuf")))
  2036. (setq git-walktree-repository-root root)
  2037. (rename-buffer name t)
  2038. (current-buffer))
  2039. (with-current-buffer (get-buffer-create name)
  2040. (if git-walktree-repository-root
  2041. (if (string= root
  2042. git-walktree-repository-root)
  2043. (current-buffer)
  2044. ;; If the buffer is for another repository, create new buffer
  2045. (with-current-buffer (generate-new-buffer name)
  2046. (setq git-walktree-repository-root root)
  2047. (current-buffer)))
  2048. ;; New buffer
  2049. (setq git-walktree-repository-root root)
  2050. (current-buffer))))))
  2051. (defun git-walktree--replace-into-buffer (target)
  2052. "Replace TARGET buffer contents with that of current buffer.
  2053. It also copy text overlays."
  2054. (let ((src (current-buffer)))
  2055. (with-current-buffer target
  2056. (replace-buffer-contents src)))
  2057. ;; Copy color overlays
  2058. (let ((overlays (overlays-in (point-min) (point-max))))
  2059. (dolist (o overlays)
  2060. (let ((beg (overlay-start o))
  2061. (end (overlay-end o)))
  2062. (move-overlay (copy-overlay o)
  2063. beg
  2064. end
  2065. target)))))
  2066. (require 'ansi-color)
  2067. (defun git-walktree--open-treeish (committish path treeish)
  2068. "Open git tree buffer of COMMITISH:PATH.
  2069. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2070. (cl-assert path)
  2071. (cl-assert treeish)
  2072. (let* (point-tree-start
  2073. (type (git-walktree--git-plumbing "cat-file"
  2074. "-t"
  2075. treeish))
  2076. (buf (git-walktree--create-buffer committish path type))
  2077. )
  2078. (cl-assert (member type
  2079. '("commit" "tree")))
  2080. (with-current-buffer buf
  2081. (unless (and (string= treeish
  2082. git-walktree-object-full-sha1)
  2083. (or (eq committish
  2084. git-walktree-current-committish)
  2085. (string= committish
  2086. git-walktree-current-committish)))
  2087. (buffer-disable-undo)
  2088. ;; For running git command go back to repository root
  2089. (cd git-walktree-repository-root)
  2090. (save-excursion
  2091. (let ((inhibit-read-only t))
  2092. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2093. (remove-overlays)
  2094. (with-temp-buffer
  2095. (if committish
  2096. (progn (git-walktree--call-process nil
  2097. "show"
  2098. ;; TODO: Make this args configurable
  2099. ;; "--no-patch"
  2100. "--color=always"
  2101. "--pretty=short"
  2102. "--decorate"
  2103. "--stat"
  2104. committish)
  2105. (ansi-color-apply-on-region (point-min)
  2106. (point))
  2107. (insert "\n")
  2108. (insert (format "Contents of '%s:%s':\n"
  2109. (git-walktree--committish-fordisplay committish)
  2110. path)))
  2111. (insert (format "Contents of treeish object '%s:\n"
  2112. treeish)))
  2113. (setq point-tree-start (point))
  2114. (git-walktree--call-process nil
  2115. "ls-tree"
  2116. ;; "-r"
  2117. "--abbrev"
  2118. treeish)
  2119. (git-walktree--replace-into-buffer buf))
  2120. ))
  2121. (git-walktree-mode)
  2122. (set-buffer-modified-p nil)
  2123. (setq git-walktree-current-committish committish)
  2124. (setq git-walktree-current-path path)
  2125. (setq git-walktree-object-full-sha1 treeish)
  2126. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2127. (when (and git-walktree-try-cd
  2128. (file-directory-p dir))
  2129. (cd dir)))
  2130. (when (= (point) (point-min))
  2131. (goto-char point-tree-start)
  2132. (git-walktree-mode--move-to-file)
  2133. )
  2134. ))
  2135. buf))
  2136. (defun git-walktree--call-process (&optional infile &rest args)
  2137. "Call git command with input from INFILE and args ARGS.
  2138. Result will be inserted into current buffer."
  2139. (let ((status (apply 'call-process
  2140. git-walktree-git-executable
  2141. infile
  2142. t
  2143. nil
  2144. args)))
  2145. (unless (eq 0
  2146. status)
  2147. (error "Failed to call git process %S %S"
  2148. infile
  2149. args))))
  2150. ?w
  2151. (defun git-walktree--open-blob (committish path blob)
  2152. "Open blob object of COMMITISH:PATH.
  2153. BLOB should be a object full sha1 of COMMITISH:PATH."
  2154. (cl-assert committish)
  2155. (cl-assert path)
  2156. (cl-assert blob)
  2157. (let* ((type (git-walktree--git-plumbing "cat-file"
  2158. "-t"
  2159. blob))
  2160. (buf (git-walktree--create-buffer committish path type)))
  2161. (cl-assert (string= type "blob"))
  2162. (with-current-buffer buf
  2163. (unless (string= blob
  2164. git-walktree-object-full-sha1)
  2165. ;; For running git command go back to repository root
  2166. (cd git-walktree-repository-root)
  2167. (let ((inhibit-read-only t))
  2168. (with-temp-buffer
  2169. (git-walktree--call-process nil
  2170. "cat-file"
  2171. "-p"
  2172. blob)
  2173. (git-walktree--replace-into-buffer buf)))
  2174. (setq git-walktree-buffer-file-name
  2175. (concat git-walktree-repository-root "/git@" committish ":" path))
  2176. (setq buffer-file-name
  2177. (concat git-walktree-repository-root "/" path))
  2178. (normal-mode t)
  2179. ;; For asking filename when C-xC-s
  2180. (setq buffer-file-name nil)
  2181. (set-buffer-modified-p t)
  2182. (setq git-walktree-current-committish committish)
  2183. (setq git-walktree-current-path path)
  2184. (setq git-walktree-object-full-sha1 blob)
  2185. (let ((dir (expand-file-name (or (file-name-directory path)
  2186. ".")
  2187. git-walktree-repository-root)))
  2188. (when (and git-walktree-try-cd
  2189. (file-directory-p dir))
  2190. (cd dir)))
  2191. (view-mode 1)
  2192. ))
  2193. buf))
  2194. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2195. "Open git object of COMMITTISH:PATH.
  2196. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2197. When PATH is omitted or nil, it is calculated from current file or directory."
  2198. (cl-assert committish)
  2199. (let ((type (git-walktree--git-plumbing "cat-file"
  2200. "-t"
  2201. committish)))
  2202. (cl-assert (string= type "commit")))
  2203. (setq path
  2204. (or path
  2205. (git-walktree--path-in-repository (or buffer-file-name
  2206. default-directory))))
  2207. ;; PATH must not start with and end with slashes
  2208. (cl-assert (not (string-match-p "\\`/" path)))
  2209. (cl-assert (not (string-match-p "/\\'" path)))
  2210. (let ((obj (git-walktree--resolve-object committish path)))
  2211. (while (not obj)
  2212. (setq path
  2213. (git-walktree--parent-directory path))
  2214. (setq obj
  2215. (git-walktree--resolve-object committish path)))
  2216. (git-walktree--open-noselect committish
  2217. path
  2218. obj)))
  2219. ;; TODO: Store view history
  2220. (defun git-walktree--open-noselect (committish path object)
  2221. "Open buffer to view git object of COMMITTISH:PATH.
  2222. When PATH was given and non-nil open that, otherwise open root tree.
  2223. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2224. COMMITTISH:PATH without checking it."
  2225. (cl-assert committish)
  2226. (let ((type (git-walktree--git-plumbing "cat-file"
  2227. "-t"
  2228. committish)))
  2229. (cl-assert (string= type "commit")))
  2230. (setq path (or path
  2231. "."))
  2232. ;; PATH must not start with and end with slashes
  2233. (cl-assert (not (string-match-p "\\`/" path)))
  2234. (cl-assert (not (string-match-p "/\\'" path)))
  2235. (setq object (or object
  2236. (git-walktree--resolve-object committish path)))
  2237. (setq object (git-walktree--git-plumbing "rev-parse"
  2238. object))
  2239. (cl-assert object)
  2240. (let ((type (git-walktree--git-plumbing "cat-file"
  2241. "-t"
  2242. object)))
  2243. (pcase type
  2244. ((or "commit" "tree")
  2245. (git-walktree--open-treeish committish path object))
  2246. ("blob"
  2247. (git-walktree--open-blob committish path object))
  2248. (_
  2249. (error "Type cannot handle: %s" type)))))
  2250. (defun git-walktree--resolve-object (committish path)
  2251. "Return object full sha1 name of COMMITISIH:PATH.
  2252. If path is equal to \".\" return COMMITTISH's root tree object.
  2253. PATH will be always treated as relative to repository root."
  2254. (cl-assert committish)
  2255. (cl-assert path)
  2256. (cl-assert (not (string-match-p "\\`/" path)))
  2257. (cl-assert (not (string-match-p "/\\'" path)))
  2258. (if (string= path ".")
  2259. (git-walktree--git-plumbing "show"
  2260. "--no-patch"
  2261. "--pretty=format:%T"
  2262. committish)
  2263. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2264. "--full-tree"
  2265. committish
  2266. path))))
  2267. (plist-get info :object))))
  2268. (defun git-walktree-open (committish &optional path)
  2269. "Open git tree buffer of COMMITTISH.
  2270. When PATH was given and non-nil open that, otherwise try to open current path.
  2271. If target path is not found in COMMITISH tree, go up path and try again until found."
  2272. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2273. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2274. (defalias 'git-walktree 'git-walktree-open)
  2275. (defun git-walktree--path-in-repository (path)
  2276. "Convert PATH into relative path to repository root.
  2277. Result will not have leading and trailing slashes."
  2278. (with-temp-buffer
  2279. (cd (if (file-directory-p path)
  2280. path
  2281. (file-name-directory path)))
  2282. (let ((root (git-walktree--git-plumbing "rev-parse"
  2283. "--show-toplevel")))
  2284. (file-relative-name (directory-file-name path)
  2285. root))))
  2286. (defcustom git-walktree-git-executable "git"
  2287. "Git executable."
  2288. :type 'string
  2289. :group 'git-walktree)
  2290. (defcustom git-walktree-try-cd t
  2291. "Try to cd if directory exists in current working directory if non-nil.
  2292. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2293. :type 'boolean
  2294. :group 'git-walktree)
  2295. (defcustom git-walktree-reuse-tree-buffer t
  2296. "Non-nil to reuse buffer for treeish object."
  2297. :type 'boolean
  2298. :group 'git-walktree)
  2299. (defvar git-walktree-tree-buffer-for-reuse nil
  2300. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2301. (defun git-walktree--git-plumbing (&rest args)
  2302. "Run git plubming command with ARGS.
  2303. Returns first line of output without newline."
  2304. (with-temp-buffer
  2305. (let ((status (apply 'call-process
  2306. git-walktree-git-executable
  2307. nil
  2308. t
  2309. nil
  2310. args)))
  2311. (unless (eq 0
  2312. status)
  2313. (error "Faild to run git %S:\n%s"
  2314. args
  2315. (buffer-substring-no-properties (point-min)
  2316. (point-max))))
  2317. (buffer-substring-no-properties (point-min)
  2318. (progn
  2319. (goto-char (point-min))
  2320. (point-at-eol))))))
  2321. (defconst git-walktree-ls-tree-line-regexp
  2322. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2323. "Regexp for one line of output of git ls-tree.")
  2324. (defconst git-walktree-ls-tree-line-tree-regexp
  2325. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2326. "Regexp for tree line of output of git ls-tree.")
  2327. (defconst git-walktree-ls-tree-line-commit-regexp
  2328. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2329. "Regexp for commit line of output of git ls-tree.")
  2330. (defun git-walktree--parse-lstree-line (str)
  2331. "Extract object info from STR.
  2332. STR should be a string like following without newline.:
  2333. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2334. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2335. (let (result mode type object file)
  2336. (save-match-data
  2337. (with-temp-buffer
  2338. (insert str)
  2339. (goto-char (point-min))
  2340. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2341. nil
  2342. t)
  2343. (list :mode (match-string 1)
  2344. :type (match-string 2)
  2345. :object (match-string 3)
  2346. :file (match-string 4)))))))
  2347. (defun git-walktree-mode-open-this ()
  2348. "Open git object of current line."
  2349. (interactive)
  2350. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2351. (point-at-eol)))))
  2352. (if info
  2353. (switch-to-buffer
  2354. (if (string= (plist-get info
  2355. :type)
  2356. "commit")
  2357. ;; For submodule cd to that directory and intialize
  2358. ;; TODO: Provide way to go back to known "parent" repository
  2359. (with-temp-buffer
  2360. (cd (plist-get info :file))
  2361. (git-walktree--open-noselect (plist-get info
  2362. :object)
  2363. nil
  2364. (plist-get info
  2365. :object)))
  2366. (git-walktree--open-noselect git-walktree-current-committish
  2367. (git-walktree--join-path (plist-get info
  2368. :file))
  2369. (plist-get info
  2370. :object))))
  2371. (message "No object on current line."))))
  2372. (defun git-walktree--join-path (name &optional base)
  2373. "Make path from NAME and BASE.
  2374. If base is omitted or nil use value of `git-walktree-current-path'."
  2375. (setq base (or base
  2376. git-walktree-current-path))
  2377. (cl-assert base)
  2378. (if (string= base ".")
  2379. name
  2380. (concat base "/" name)))
  2381. (defun git-walktree--parent-directory (path)
  2382. "Return parent directory of PATH without trailing slash.
  2383. For root directory return \".\".
  2384. If PATH is equal to \".\", return nil."
  2385. (if (string-match-p "/" path)
  2386. (directory-file-name (file-name-directory path))
  2387. (if (string= "." path)
  2388. nil
  2389. ".")))
  2390. (defun git-walktree-up (&optional committish path)
  2391. "Open parent directory of COMMITTISH and PATH.
  2392. If not given, value of current buffer will be used."
  2393. (interactive)
  2394. (setq committish
  2395. (or committish git-walktree-current-committish))
  2396. (setq path
  2397. (or path git-walktree-current-path))
  2398. (let ((parent (git-walktree--parent-directory path)))
  2399. (if parent
  2400. (switch-to-buffer (git-walktree--open-noselect committish
  2401. parent
  2402. nil))
  2403. (message "Cannot find parent directory for current tree."))))
  2404. (defun git-walktree-mode--move-to-file ()
  2405. "Move point to file field of ls-tree output in current line.
  2406. This function do nothing when current line is not ls-tree output."
  2407. (interactive)
  2408. (save-match-data
  2409. (when (save-excursion
  2410. (goto-char (point-at-bol))
  2411. (re-search-forward git-walktree-ls-tree-line-regexp
  2412. (point-at-eol) t))
  2413. (goto-char (match-beginning 4)))))
  2414. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2415. "Move cursor vertically down ARG lines and move to file field if found."
  2416. (interactive "^p\np")
  2417. (or arg (setq arg 1))
  2418. (line-move arg nil nil try-vscroll)
  2419. (git-walktree-mode--move-to-file)
  2420. )
  2421. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2422. "Move cursor vertically up ARG lines and move to file field if found."
  2423. (interactive "^p\np")
  2424. (or arg (setq arg 1))
  2425. (line-move (- arg) nil nil try-vscroll)
  2426. (git-walktree-mode--move-to-file)
  2427. )
  2428. (defgroup git-walktree-faces nil
  2429. "Faces used by git-walktree."
  2430. :group 'git-walktree
  2431. :group 'faces)
  2432. (defface git-walktree-tree-face
  2433. ;; Same as dired-directory
  2434. '((t (:inherit font-lock-function-name-face)))
  2435. "Face used for tree objects."
  2436. :group 'git-walktree-faces)
  2437. (defface git-walktree-commit-face
  2438. ;; Same as dired-symlink face
  2439. '((t (:inherit font-lock-keyword-face)))
  2440. "Face used for commit objects."
  2441. :group 'git-walktree-faces)
  2442. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2443. "Hash of already known pair of commitid -> list of child commitid.
  2444. Both values should be object full sha1 names.")
  2445. (defun git-walktree--put-child (parent child)
  2446. "Register PARENT and CHILD relationship.
  2447. PARENT should be a full sha1 object name."
  2448. ;; Any way to check if PARENT is a full SHA-1 object name?
  2449. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2450. (unless (member child current)
  2451. (puthash parent
  2452. (cons child
  2453. current)
  2454. git-walktree-known-child-revisions))))
  2455. ;; TODO: Add aggressive search mode
  2456. ;; https://stackoverflow.com/a/9870218
  2457. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2458. (defun git-walktree--get-children (parent)
  2459. "Get known children list of PARENT commit.
  2460. PARENT should be a full sha1 object name."
  2461. (gethash parent git-walktree-known-child-revisions))
  2462. (defun git-walktree--choose-committish (prompt-format collection)
  2463. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2464. When collection has just one element, return the first element without asking."
  2465. (cl-assert collection)
  2466. (if (< (length collection) 2)
  2467. (car collection)
  2468. (completing-read (format prompt-format
  2469. (mapconcat 'git-walktree--committish-fordisplay
  2470. collection
  2471. " "))
  2472. collection
  2473. nil
  2474. t)))
  2475. (defun git-walktree-parent-revision ()
  2476. "Open parent revision of current path.
  2477. If current path was not found in the parent revision try to go up path."
  2478. (interactive)
  2479. (cl-assert git-walktree-current-committish)
  2480. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2481. git-walktree-current-committish))
  2482. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2483. (dolist (parent parents)
  2484. (git-walktree--put-child parent
  2485. commit-full-sha1))
  2486. (if (< (length parents)
  2487. 1)
  2488. (message "This revision has no parent revision")
  2489. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2490. parents))
  2491. (path git-walktree-current-path))
  2492. (cl-assert path)
  2493. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2494. path))))))
  2495. (defun git-walktree--parent-full-sha1 (committish)
  2496. "Return list of parent commits of COMMITTISH in sha1 string."
  2497. (let ((type (git-walktree--git-plumbing "cat-file"
  2498. "-t"
  2499. committish)))
  2500. (cl-assert (string= type "commit")))
  2501. (let ((parents (git-walktree--git-plumbing "show"
  2502. "--no-patch"
  2503. "--pretty=format:%P"
  2504. committish)))
  2505. (split-string parents)))
  2506. (defun git-walktree-known-child-revision ()
  2507. "Open known revision of current path."
  2508. (interactive)
  2509. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2510. git-walktree-current-committish))
  2511. (children (git-walktree--get-children commit-full-sha1)))
  2512. (if (< (length children)
  2513. 1)
  2514. (message "There are no known child revision")
  2515. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2516. children))
  2517. (path git-walktree-current-path))
  2518. (cl-assert path)
  2519. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2520. path))))))
  2521. (defvar git-walktree-mode-map
  2522. (let ((map (make-sparse-keymap)))
  2523. (define-key map "n" 'git-walktree-mode-next-line)
  2524. (define-key map "p" 'git-walktree-mode-previous-line)
  2525. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2526. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2527. ;; TODO: Review keybind
  2528. (define-key map "P" 'git-walktree-parent-revision)
  2529. (define-key map "N" 'git-walktree-known-child-revision)
  2530. (define-key map "^" 'git-walktree-up)
  2531. ;; TODO: implement
  2532. (define-key map (kbd "DEL") 'git-walktree-back)
  2533. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2534. map))
  2535. (defvar git-walktree-mode-font-lock-keywords
  2536. `(
  2537. (,git-walktree-ls-tree-line-regexp . (
  2538. (1 'shadow)
  2539. (3 'shadow)
  2540. ))
  2541. (,git-walktree-ls-tree-line-tree-regexp . (
  2542. (2 'git-walktree-tree-face)
  2543. (4 'git-walktree-tree-face)
  2544. ))
  2545. (,git-walktree-ls-tree-line-commit-regexp . (
  2546. (2 'git-walktree-commit-face)
  2547. (4 'git-walktree-commit-face)
  2548. ))
  2549. )
  2550. "Syntax highlighting for git-walktree mode.")
  2551. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2552. "Major-mode for `git-walktree-open'."
  2553. (set (make-local-variable 'font-lock-defaults)
  2554. '(git-walktree-mode-font-lock-keywords
  2555. nil nil nil nil
  2556. ))
  2557. )
  2558. (require 'magit nil t)
  2559. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2560. ;; Local Variables:
  2561. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2562. ;; flycheck-checker: emacs-lisp
  2563. ;; End:
  2564. ;;; emancs.el ends here