You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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