您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

2651 行
91 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (let ((d (expand-file-name (concat user-emacs-directory
  11. "lisp"))))
  12. (unless (file-directory-p d)
  13. (make-directory d))
  14. (add-to-list 'load-path d))
  15. (eval-when-compile
  16. ;; TODO: Use cl-lib intead
  17. (require 'cl nil t))
  18. ;; (add-hook 'after-change-major-mode-hook
  19. ;; (lambda ()
  20. ;; (message "cmm: %S %s"
  21. ;; major-mode
  22. ;; buffer-file-name)))
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; Some macros for internals
  25. (defun call-after-init (func)
  26. "If `after-init-hook' has been run, call FUNC immediately.
  27. Otherwize hook it."
  28. (if after-init-time
  29. (funcall func)
  30. (add-hook 'after-init-hook
  31. func)))
  32. (defmacro safe-require-or-eval (feature)
  33. "Require FEATURE if available.
  34. At compile time the feature will be loaded immediately."
  35. `(eval-and-compile
  36. (require ,feature nil t)))
  37. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  38. "Define autoloading FEATURE that defines FUNCTIONS.
  39. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  40. the function same as FEATURE is defined as autoloaded function. BODY is passed
  41. to `eval-after-load'.
  42. After this macro is expanded, this returns the path to library if FEATURE
  43. found, otherwise returns nil."
  44. (let* ((libname (symbol-name (eval feature)))
  45. (libpath (locate-library libname)))
  46. (and libpath
  47. `(progn
  48. ,@(mapcar (lambda (f)
  49. (unless (fboundp f)
  50. `(progn
  51. (message "Autoloaded function `%S' defined (%s)"
  52. (quote ,f)
  53. ,libpath)
  54. (autoload (quote ,f)
  55. ,libname
  56. ,(concat "Autoloaded function defined in \""
  57. libpath
  58. "\".")
  59. t))))
  60. (or (eval functions)
  61. `(,(eval feature))))
  62. (eval-after-load ,feature
  63. (quote (progn
  64. ,@body)))
  65. (locate-library ,libname)))))
  66. (put 'autoload-eval-lazily 'lisp-indent-function 2)
  67. (when (autoload-eval-lazily 'tetris nil
  68. (message "Tetris loaded!"))
  69. (message "Tetris found!"))
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;; download library from web
  72. (defvar fetch-library-enabled-p t
  73. "Set nil to skip downloading with `fetch-library'.")
  74. (defun fetch-library (url &optional byte-compile-p force-download-p)
  75. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  76. Return nil if library unfound and failed to download,
  77. otherwise the path where the library installed.
  78. If BYTE-COMPILE-P is t byte compile the file after downloading.
  79. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download.
  80. This function also checks the value of `fetch-library-enabled-p' and do not
  81. fetch libraries if this value is nil. In this case all arguments (including
  82. FORCE-DOWNLOAD-P) will be ignored."
  83. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  84. (lib (file-name-sans-extension (file-name-nondirectory url)))
  85. (lpath (concat dir lib ".el"))
  86. (locate-p (locate-library lib)))
  87. (if (and fetch-library-enabled-p
  88. (or force-download-p
  89. (not locate-p)))
  90. (if (progn (message "Downloading %s..."
  91. url)
  92. (download-file url
  93. lpath
  94. t))
  95. (progn (message "Downloading %s...done"
  96. url)
  97. (when (and byte-compile-p
  98. (require 'bytecomp nil t))
  99. (and (file-exists-p (byte-compile-dest-file lpath))
  100. (delete-file (byte-compile-dest-file lpath)))
  101. (message "Byte-compiling %s..."
  102. lpath)
  103. (byte-compile-file lpath)
  104. (message "Byte-compiling %s...done"
  105. lpath)))
  106. (progn (and (file-writable-p lpath)
  107. (delete-file lpath))
  108. (message "Downloading %s...failed"
  109. url))))
  110. (locate-library lib)))
  111. ;; If EMACS_EL_DRY_RUN is set and it is not an empty string, fetch-library
  112. ;; does not actually fetch library.
  113. (let ((dryrun (getenv "EMACS_EL_DRY_RUN")))
  114. (when (and dryrun
  115. (< 0
  116. (length dryrun)))
  117. (setq fetch-library-enabled-p
  118. nil)
  119. (message "EMACS_EL_DRY_RUN is set. Skip fetching libraries.")))
  120. (defun download-file (url path &optional ok-if-already-exists)
  121. "Download file from URL and output to PATH.
  122. IF OK-IF-ALREADY-EXISTS is true force download."
  123. (let ((curl (executable-find "curl"))
  124. (wget (executable-find "wget")))
  125. (cond (wget
  126. (if (and (not ok-if-already-exists)
  127. (file-exists-p path))
  128. nil
  129. (and (eq 0
  130. (call-process wget
  131. nil
  132. nil
  133. nil
  134. "-O"
  135. path
  136. url
  137. ))
  138. path)))
  139. (curl
  140. (if (and (not ok-if-already-exists)
  141. (file-exists-p path))
  142. nil
  143. (and (eq 0
  144. (call-process curl
  145. nil
  146. nil
  147. nil
  148. "--output"
  149. path
  150. "-L"
  151. url
  152. ))
  153. path)))
  154. (t
  155. (ignore-errors
  156. (require 'url)
  157. (url-copy-file url
  158. path
  159. ok-if-already-exists)
  160. path)))))
  161. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  162. ;; package
  163. (set-variable 'my-package-list
  164. '(
  165. markdown-mode
  166. yaml-mode
  167. gnuplot-mode
  168. erlang
  169. js2-mode
  170. git-commit
  171. gitignore-mode
  172. ;; ack
  173. color-moccur
  174. ggtags
  175. flycheck
  176. ;; is flymake installs are required?
  177. ;;flymake-jshint
  178. ;;flymake-python-pyflakes
  179. xclip
  180. foreign-regexp
  181. multi-term
  182. dirtree
  183. term-run
  184. editorconfig
  185. git-ps1-mode
  186. restart-emacs
  187. fill-column-indicator
  188. scala-mode2
  189. ensime
  190. editorconfig
  191. git-command
  192. ;; 10sr repository
  193. terminal-title
  194. recentf-show
  195. dired-list-all-mode
  196. pack
  197. set-modeline-color
  198. read-only-only-mode
  199. smart-revert
  200. autosave
  201. ;;window-organizer
  202. remember-major-modes-mode
  203. ilookup
  204. pasteboard
  205. )
  206. "Package list just for me.")
  207. (when (safe-require-or-eval 'package)
  208. ;; (add-to-list 'package-archives
  209. ;; '("ELPA" . "http://tromey.com/elpa/"))
  210. (setq package-archives
  211. `(,@package-archives
  212. ("melpa" . "https://melpa.org/packages/")
  213. ("10sr-el" . "https://10sr.github.io/emacs-lisp/p/")))
  214. ;; (add-to-list 'package-archives
  215. ;; '("melpa" . "https://melpa.org/packages/")
  216. ;; t)
  217. ;; (add-to-list 'package-archives
  218. ;; '("marmalade" . "http://marmalade-repo.org/packages/"))
  219. (package-initialize)
  220. (defun my-auto-install-package ()
  221. "Install packages semi-automatically."
  222. (interactive)
  223. (package-refresh-contents)
  224. (mapc (lambda (pkg)
  225. (or (package-installed-p pkg)
  226. (locate-library (symbol-name pkg))
  227. (package-install pkg)))
  228. my-package-list))
  229. )
  230. ;; (lazy-load-eval 'sudoku)
  231. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  232. ;; my-idle-hook
  233. (defvar my-idle-hook nil
  234. "Hook run when idle for several secs.")
  235. (defvar my-idle-hook-sec 5
  236. "Second to run `my-idle-hook'.")
  237. (run-with-idle-timer my-idle-hook-sec
  238. t
  239. (lambda ()
  240. (run-hooks 'my-idle-hook)))
  241. ;; (add-hook 'my-idle-hook
  242. ;; (lambda ()
  243. ;; (message "idle hook message")))
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. ;; start and quit
  246. (setq inhibit-startup-message t)
  247. (setq confirm-kill-emacs 'y-or-n-p)
  248. (setq gc-cons-threshold (* 1024 1024 4))
  249. (when window-system
  250. (add-to-list 'default-frame-alist '(cursor-type . box))
  251. (add-to-list 'default-frame-alist '(background-color . "white"))
  252. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  253. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  254. ;; does not work?
  255. )
  256. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  257. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  258. (and (fboundp 'tool-bar-mode)
  259. (tool-bar-mode 0))
  260. (and (fboundp 'set-scroll-bar-mode)
  261. (set-scroll-bar-mode nil))
  262. (add-hook 'kill-emacs-hook
  263. ;; load init file when terminating emacs to ensure file is not broken
  264. 'reload-init-file)
  265. (defun my-force-kill-emacs ()
  266. "My force kill Emacs."
  267. (interactive)
  268. (let ((kill-emacs-hook nil))
  269. (kill-emacs)))
  270. (call-after-init
  271. (lambda ()
  272. (message "%s %s" invocation-name emacs-version)
  273. (message "%s was taken to initialize emacs." (emacs-init-time))
  274. (switch-to-buffer "*Messages*")))
  275. (cd ".") ; when using windows use / instead of \ in `default-directory'
  276. ;; locale
  277. (set-language-environment "Japanese")
  278. (set-default-coding-systems 'utf-8-unix)
  279. (prefer-coding-system 'utf-8-unix)
  280. (setq system-time-locale "C")
  281. ;; my prefix map
  282. (defvar my-prefix-map nil
  283. "My prefix map.")
  284. (define-prefix-command 'my-prefix-map)
  285. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  286. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  287. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  288. ;; (comint-show-maximum-output)
  289. ;; kill scratch
  290. (call-after-init (lambda ()
  291. (let ((buf (get-buffer "*scratch*")))
  292. (when buf
  293. (kill-buffer buf)))))
  294. ;; modifier keys
  295. ;; (setq mac-option-modifier 'control)
  296. ;; display
  297. (setq visible-bell t)
  298. (setq ring-bell-function 'ignore)
  299. (mouse-avoidance-mode 'banish)
  300. (defun reload-init-file ()
  301. "Reload Emacs init file."
  302. (interactive)
  303. (when (and user-init-file
  304. (file-readable-p user-init-file))
  305. (load-file user-init-file)))
  306. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  307. ;; for windows
  308. (defun start-ckw-bash ()
  309. "Start ckw in windows."
  310. (interactive)
  311. (start-process
  312. "ckw_bash"
  313. nil
  314. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  315. ;; command seems to have to be in c drive
  316. (defun my-w32-add-export-path (&rest args)
  317. "Add pathes ARGS for windows."
  318. (mapc (lambda (path)
  319. (add-to-list 'exec-path (expand-file-name path)))
  320. (reverse args))
  321. (setenv "PATH"
  322. (mapconcat 'convert-standard-filename
  323. exec-path
  324. ";")))
  325. (when (eq system-type 'windows-nt)
  326. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  327. ;; (setq python-python-command "c:/Python26/python.exe")
  328. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  329. (my-w32-add-export-path "c:/Windows/system"
  330. "c:/Windows/System32"
  331. "c:/Program Files/Git/bin"
  332. "c:/MinGW/bin"
  333. "c:/MinGW/mingw32/bin"
  334. (expand-file-name "~/.local/bin")
  335. (expand-file-name "~/dbx/apps/bin"))
  336. (when window-system
  337. (set-variable 'w32-enable-synthesized-fonts t))
  338. (set-variable 'w32-apps-modifier 'meta)
  339. (setq file-name-coding-system 'sjis))
  340. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  341. ;; global keys
  342. (global-set-key (kbd "<up>") 'scroll-down-line)
  343. (global-set-key (kbd "<down>") 'scroll-up-line)
  344. (global-set-key (kbd "<left>") 'scroll-down)
  345. (global-set-key (kbd "<right>") 'scroll-up)
  346. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  347. (global-set-key (kbd "C-\\") help-map)
  348. (define-key ctl-x-map (kbd "DEL") help-map)
  349. (define-key ctl-x-map (kbd "C-h") help-map)
  350. (define-key help-map "a" 'apropos)
  351. ;; disable annoying keys
  352. (global-set-key [prior] 'ignore)
  353. (global-set-key (kbd "<next>") 'ignore)
  354. (global-set-key [menu] 'ignore)
  355. (global-set-key [down-mouse-1] 'ignore)
  356. (global-set-key [down-mouse-2] 'ignore)
  357. (global-set-key [down-mouse-3] 'ignore)
  358. (global-set-key [mouse-1] 'ignore)
  359. (global-set-key [mouse-2] 'ignore)
  360. (global-set-key [mouse-3] 'ignore)
  361. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  362. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  363. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  364. ;; title and mode-line
  365. (when (safe-require-or-eval 'terminal-title)
  366. ;; if TERM is not screen use default value
  367. (if (getenv "TMUX")
  368. ;; if use tmux locally just basename of current dir
  369. (setq terminal-title-format
  370. '((file-name-nondirectory (directory-file-name
  371. default-directory))))
  372. (if (and (let ((tty-type (frame-parameter nil
  373. 'tty-type)))
  374. (and tty-type
  375. (equal (car (split-string tty-type
  376. "-"))
  377. "screen")))
  378. (not (getenv "SSH_CONNECTION")))
  379. (setq terminal-title-format
  380. '((file-name-nondirectory (directory-file-name
  381. default-directory))))
  382. ;; seems that TMUX is used locally and ssh to remote host
  383. (setq terminal-title-format
  384. `("em:"
  385. ,user-login-name
  386. "@"
  387. ,(car (split-string system-name
  388. "\\."))
  389. ":"
  390. default-directory))
  391. )
  392. )
  393. (terminal-title-mode))
  394. (setq eol-mnemonic-dos "\\r\\n")
  395. (setq eol-mnemonic-mac "\\r")
  396. (setq eol-mnemonic-unix "\\n")
  397. (which-function-mode 0)
  398. (line-number-mode 0)
  399. (column-number-mode 0)
  400. (size-indication-mode 0)
  401. (setq mode-line-position
  402. '(:eval (format "L%%l/%d,C%%c"
  403. (count-lines (point-max)
  404. (point-min)))))
  405. (when (require 'git-ps1-mode nil t)
  406. (git-ps1-mode))
  407. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  408. ;; display date
  409. (call-after-init (lambda ()
  410. (when display-time-mode
  411. (display-time-update))))
  412. (when (safe-require-or-eval 'time)
  413. (setq display-time-interval 29)
  414. (setq display-time-day-and-date t)
  415. (setq display-time-format "%a, %d %b %Y %T")
  416. (if window-system
  417. (display-time-mode 0)
  418. (display-time-mode 1)))
  419. ;; ;; current directory
  420. ;; (let ((ls (member 'mode-line-buffer-identification
  421. ;; mode-line-format)))
  422. ;; (setcdr ls
  423. ;; (cons '(:eval (concat " ("
  424. ;; (abbreviate-file-name default-directory)
  425. ;; ")"))
  426. ;; (cdr ls))))
  427. ;; ;; display last modified time
  428. ;; (let ((ls (member 'mode-line-buffer-identification
  429. ;; mode-line-format)))
  430. ;; (setcdr ls
  431. ;; (cons '(:eval (concat " "
  432. ;; my-buffer-file-last-modified-time))
  433. ;; (cdr ls))))
  434. (defun buffer-list-not-start-with-space ()
  435. "Return a list of buffers that not start with whitespaces."
  436. (let ((bl (buffer-list))
  437. b nbl)
  438. (while bl
  439. (setq b (pop bl))
  440. (unless (string-equal " "
  441. (substring (buffer-name b)
  442. 0
  443. 1))
  444. (add-to-list 'nbl b)))
  445. nbl))
  446. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  447. ;; (add-to-list 'minor-mode-alist
  448. ;; '(global-whitespace-mode ""))
  449. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  450. ;; system info
  451. (defun my-message-current-info ()
  452. "Echo current login name, hostname and directory."
  453. (interactive)
  454. (message "%s@%s:%s"
  455. user-login-name
  456. system-name
  457. (abbreviate-file-name default-directory)))
  458. ;; (run-with-idle-timer 3
  459. ;; t
  460. ;; 'my-message-current-info)
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; minibuffer
  463. (setq insert-default-directory t)
  464. (setq completion-ignore-case t
  465. read-file-name-completion-ignore-case t
  466. read-buffer-completion-ignore-case t)
  467. (setq resize-mini-windows t)
  468. (temp-buffer-resize-mode 1)
  469. (savehist-mode 1)
  470. (fset 'yes-or-no-p 'y-or-n-p)
  471. ;; complete symbol when `eval'
  472. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  473. (define-key minibuffer-local-map (kbd "C-u")
  474. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  475. ;; I dont know these bindings are good
  476. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  477. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  478. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  479. ;; letters, font-lock mode and fonts
  480. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  481. ;; (set-window-margins (selected-window) 1 1)
  482. (and (or (eq system-type 'Darwin)
  483. (eq system-type 'darwin))
  484. (fboundp 'mac-set-input-method-parameter)
  485. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  486. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  487. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  488. (boundp 'input-method-inactivate-hook))
  489. (add-hook 'input-method-activate-hook
  490. (lambda () (set-cursor-color "red")))
  491. (add-hook 'input-method-inactivate-hook
  492. (lambda () (set-cursor-color "black"))))
  493. (when (safe-require-or-eval 'paren)
  494. (show-paren-mode 1)
  495. (setq show-paren-delay 0.5
  496. show-paren-style 'parenthesis) ; mixed is hard to read
  497. ;; (set-face-background 'show-paren-match
  498. ;; "black")
  499. ;; ;; (face-foreground 'default))
  500. ;; (set-face-foreground 'show-paren-match
  501. ;; "white")
  502. ;; (set-face-inverse-video-p 'show-paren-match
  503. ;; t)
  504. )
  505. (transient-mark-mode 1)
  506. (global-font-lock-mode 1)
  507. (setq font-lock-global-modes
  508. '(not
  509. help-mode
  510. eshell-mode
  511. ;;term-mode
  512. Man-mode))
  513. ;; (standard-display-ascii ?\n "$\n")
  514. (defvar my-eol-face
  515. '(("\n" . (0 font-lock-comment-face t nil)))
  516. )
  517. (defvar my-tab-face
  518. '(("\t" . '(0 highlight t nil))))
  519. (defvar my-jspace-face
  520. '(("\u3000" . '(0 highlight t nil))))
  521. (add-hook 'font-lock-mode-hook
  522. (lambda ()
  523. ;; (font-lock-add-keywords nil my-eol-face)
  524. (font-lock-add-keywords nil my-jspace-face)
  525. ))
  526. (when (safe-require-or-eval 'whitespace)
  527. (add-to-list 'whitespace-display-mappings ; not work
  528. `(tab-mark ?\t ,(vconcat "^I\t")))
  529. (add-to-list 'whitespace-display-mappings
  530. `(newline-mark ?\n ,(vconcat "$\n")))
  531. (setq whitespace-style '(face
  532. trailing ; trailing blanks
  533. newline ; newlines
  534. newline-mark ; use display table for newline
  535. ;; tab-mark
  536. empty ; empty lines at beg or end of buffer
  537. lines-tail ; lines over 80
  538. ))
  539. ;; (setq whitespace-newline 'font-lock-comment-face)
  540. (global-whitespace-mode t)
  541. (if (eq (display-color-cells)
  542. 256)
  543. (set-face-foreground 'whitespace-newline "brightblack")
  544. ;; (progn
  545. ;; (set-face-bold-p 'whitespace-newline
  546. ;; t))
  547. ))
  548. (and nil
  549. (safe-require-or-eval 'fill-column-indicator)
  550. (setq fill-column-indicator))
  551. ;; highlight current line
  552. ;; http://wiki.riywo.com/index.php?Meadow
  553. (defface my-hl-line
  554. '((((min-colors 256)
  555. (background dark))
  556. (:background "color-234"))
  557. (((min-colors 256)
  558. (background light))
  559. (:background "color-234"))
  560. (t
  561. (:underline "black")))
  562. "*Face used by hl-line.")
  563. (set-variable 'hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  564. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  565. (set-variable 'hl-line-global-modes
  566. '(not
  567. term-mode))
  568. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  569. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  570. ;; fonts
  571. (defun my-set-ascii-and-jp-font (list)
  572. "Set font configuration to LIST."
  573. (let ((fspec1 (if (> emacs-major-version 22)
  574. ;; font spec is available in emacs23 and later
  575. (font-spec :family (nth 2 list) :size (nth 3 list))
  576. (cons (nth 2 list) "jisx0208.*")))
  577. (fspec2 (if (> emacs-major-version 22)
  578. (font-spec :family (nth 2 list) :size (nth 3 list))
  579. (cons (nth 2 list) "jisx0201.*"))))
  580. (set-face-attribute 'default nil
  581. :family (nth 0 list)
  582. :height (nth 1 list))
  583. (set-fontset-font "fontset-default"
  584. 'japanese-jisx0208
  585. fspec1)
  586. (set-fontset-font "fontset-default"
  587. 'katakana-jisx0201
  588. fspec2)))
  589. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  590. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  591. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  592. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  593. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  594. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  595. ;; あ a
  596. (safe-require-or-eval 'set-modeline-color)
  597. (let ((fg (face-foreground 'default))
  598. (bg (face-background 'default)))
  599. (set-face-background 'mode-line-inactive
  600. (if (face-inverse-video-p 'mode-line) fg bg))
  601. (set-face-foreground 'mode-line-inactive
  602. (if (face-inverse-video-p 'mode-line) bg fg)))
  603. (set-face-underline 'mode-line-inactive
  604. t)
  605. (set-face-underline 'vertical-border
  606. nil)
  607. ;; Not found in MELPA nor any other package repositories
  608. (and (fetch-library
  609. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  610. t)
  611. (safe-require-or-eval 'end-mark)
  612. (global-end-mark-mode))
  613. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  614. ;; file handling
  615. (when (safe-require-or-eval 'editorconfig)
  616. (editorconfig-mode 1))
  617. (setq revert-without-query '(".+"))
  618. ;; save cursor position
  619. (when (safe-require-or-eval 'saveplace)
  620. (setq-default save-place t)
  621. (setq save-place-file (concat user-emacs-directory
  622. "places")))
  623. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  624. (setq make-backup-files t)
  625. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  626. (setq backup-directory-alist
  627. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  628. "backup")))
  629. backup-directory-alist))
  630. (setq version-control 'never)
  631. (setq delete-old-versions t)
  632. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  633. "auto-save/")))
  634. (setq delete-auto-save-files t)
  635. (add-to-list 'completion-ignored-extensions ".bak")
  636. ;; (setq delete-by-moving-to-trash t
  637. ;; trash-directory "~/.emacs.d/trash")
  638. (add-hook 'after-save-hook
  639. 'executable-make-buffer-file-executable-if-script-p)
  640. (set-variable 'bookmark-default-file (concat user-emacs-directory
  641. "bmk"))
  642. (add-hook 'recentf-load-hook
  643. (lambda ()
  644. (defvar recentf-exclude)
  645. (add-to-list 'recentf-exclude
  646. (regexp-quote bookmark-default-file))))
  647. (when (safe-require-or-eval 'smart-revert)
  648. (smart-revert-on))
  649. ;; autosave
  650. (when (safe-require-or-eval 'autosave)
  651. (autosave-set 2))
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. ;; editting
  654. (defun my-copy-whole-line ()
  655. "Copy whole line."
  656. (interactive)
  657. (kill-new (concat (buffer-substring (point-at-bol)
  658. (point-at-eol))
  659. "\n")))
  660. (setq require-final-newline t)
  661. (setq kill-whole-line t)
  662. (setq scroll-conservatively 35
  663. scroll-margin 2
  664. scroll-step 0)
  665. (setq-default major-mode 'text-mode)
  666. (setq next-line-add-newlines nil)
  667. (setq kill-read-only-ok t)
  668. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  669. ;; (setq-default line-spacing 0.2)
  670. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  671. (setq-default tab-width 4)
  672. (setq-default indent-tabs-mode nil)
  673. (setq-default indent-line-function nil)
  674. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  675. (delete-selection-mode 1)
  676. (cua-mode 0)
  677. (setq line-move-visual nil)
  678. ;; key bindings
  679. ;; moving around
  680. ;; (global-set-key (kbd "M-j") 'next-line)
  681. ;; (global-set-key (kbd "M-k") 'previous-line)
  682. ;; (global-set-key (kbd "M-h") 'backward-char)
  683. ;; (global-set-key (kbd "M-l") 'forward-char)
  684. ;;(keyboard-translate ?\M-j ?\C-j)
  685. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  686. (define-key esc-map "p" 'backward-paragraph)
  687. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  688. (define-key esc-map "n" 'forward-paragraph)
  689. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  690. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  691. (global-set-key (kbd "C-<left>") 'scroll-down)
  692. (global-set-key (kbd "C-<right>") 'scroll-up)
  693. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  694. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  695. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  696. ;; C-h and DEL
  697. (global-set-key (kbd "C-h") (kbd "DEL"))
  698. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  699. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  700. (define-key esc-map "k" 'my-copy-whole-line)
  701. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  702. (define-key esc-map "u" 'undo)
  703. (define-key esc-map "i" (kbd "ESC TAB"))
  704. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  705. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  706. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  707. (define-key my-prefix-map (kbd "C-o") 'occur)
  708. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  709. ;; japanese input method
  710. (defun my-load-scim ()
  711. "Use scim-bridge.el as japanese im."
  712. ;; Load scim-bridge.
  713. (when (safe-require-or-eval 'scim-bridge)
  714. ;; Turn on scim-mode automatically after loading .emacs
  715. (call-after-init 'scim-mode-on)
  716. (setq scim-cursor-color "red")
  717. (scim-define-preedit-key ?\^h t)
  718. (scim-define-common-key ?\* nil)
  719. (scim-define-common-key ?\^/ nil)))
  720. (defun my-load-anthy ()
  721. "Use anthy.el as japanese im."
  722. ;; anthy
  723. (when (safe-require-or-eval 'anthy)
  724. (global-set-key
  725. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  726. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  727. (when (>= emacs-major-version 23)
  728. (setq anthy-accept-timeout 1))))
  729. ;; quail
  730. ;; aproposs input-method for some information
  731. ;; (setq default-input-method "japanese")
  732. (defun my-load-mozc-el ()
  733. "Use mozc.el as japanese im."
  734. (when (safe-require-or-eval 'mozc)
  735. (setq defauit-input-method "japanese-mozc")
  736. (setq mozc-leim-title "[MZ]")
  737. ))
  738. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  739. ;; gmail
  740. (setq mail-interactive t
  741. send-mail-function 'smtpmail-send-it
  742. ;; message-send-mail-function 'smtpmail-send-it
  743. smtpmail-smtp-server "smtp.gmail.com"
  744. smtpmail-smtp-service 587
  745. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  746. "8.slashes@gmail.com" nil))
  747. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  748. "8.slashes@gmail.com" nil))
  749. user-mail-address "8.slashes@gmail.com")
  750. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  751. ;; buffer killing
  752. ;; (defun my-delete-window-killing-buffer () nil)
  753. (defun my-query-kill-current-buffer ()
  754. "Interactively kill current buffer."
  755. (interactive)
  756. (if (y-or-n-p (concat "kill current buffer? :"))
  757. (kill-buffer (current-buffer))))
  758. (substitute-key-definition 'kill-buffer
  759. 'my-query-kill-current-buffer
  760. global-map)
  761. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  762. (defun my-kill-buffers ()
  763. "Kill buffers that visit files."
  764. (interactive)
  765. (mapcar (lambda (buf)
  766. (when (buffer-file-name buf)
  767. (kill-buffer buf)))
  768. (buffer-list)))
  769. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  770. ;; share clipboard with x
  771. ;; this page describes this in details, but only these sexps seem to be needed
  772. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  773. (and (not window-system)
  774. (not (eq window-system 'mac))
  775. (getenv "DISPLAY")
  776. (not (equal (getenv "DISPLAY") ""))
  777. (executable-find "xclip")
  778. ;; (< emacs-major-version 24)
  779. (safe-require-or-eval 'xclip)
  780. nil
  781. (turn-on-xclip))
  782. (and (eq system-type 'darwin)
  783. (safe-require-or-eval 'pasteboard)
  784. (turn-on-pasteboard)
  785. (getenv "TMUX")
  786. (pasteboard-enable-rtun))
  787. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  788. ;; https://github.com/lunaryorn/flycheck
  789. (when (safe-require-or-eval 'flycheck)
  790. (call-after-init 'global-flycheck-mode))
  791. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  792. ;; window
  793. ;; (when (autoload-eval-lazily 'window-organizer)
  794. ;; (define-key ctl-x-map (kbd "w") 'window-organizer))
  795. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  796. ;; server
  797. (when (safe-require-or-eval 'server)
  798. (setq server-name (concat "server"
  799. (number-to-string (emacs-pid))))
  800. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  801. ;; In Darwin environment, init fails with message like 'Service name too long'
  802. ;; when server-use-tcp is nil
  803. (when (or (eq system-type
  804. 'cygwin)
  805. (eq system-type
  806. 'darwin))
  807. (setq server-use-tcp t))
  808. (defun my-construct-emacsclient-editor-command ()
  809. "Construct and return command in a string to connect to current Emacs server."
  810. (if server-use-tcp
  811. (format "%s -f \"%s/%s\""
  812. "emacsclient"
  813. (expand-file-name server-auth-dir)
  814. server-name)
  815. (format "%s -s \"%s/%s\""
  816. "emacsclient"
  817. server-socket-dir
  818. server-name)))
  819. (setq process-environment
  820. `(,(concat "EDITOR="
  821. (my-construct-emacsclient-editor-command))
  822. ,(concat "GIT_EDITOR="
  823. (my-construct-emacsclient-editor-command))
  824. ,@process-environment))
  825. (server-start))
  826. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  827. ;; some modes and hooks
  828. (set-variable 'ac-ignore-case nil)
  829. ;; (when (require 'ensime nil t)
  830. ;; (set-variable 'ensime-ac-case-sensitive t)
  831. ;; (set-variable 'ensime-company-case-sensitive t)
  832. ;; (add-hook 'scala-mode-hook
  833. ;; 'ensime-scala-mode-hook)
  834. ;; (add-hook 'ensime-scala-mode-hook
  835. ;; 'ac-stop))
  836. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  837. (define-key ctl-x-map "t" 'term-run-shell-command))
  838. (add-to-list 'safe-local-variable-values
  839. '(encoding utf-8))
  840. (setq enable-local-variables :safe)
  841. (when (autoload-eval-lazily 'dirtree nil
  842. (defun my-dirtree-current-line-directory-p ()
  843. "Return nil if element on current line is not a directory."
  844. (file-directory-p (widget-get (tree-mode-button-current-line)
  845. :file)))
  846. ;; This fix is actually a little strange. Strictly speaking
  847. ;; judging tree should be done by whether the widget is a tree one.
  848. (defun my-dirtree-next-node (arg)
  849. "Fix the problem that `tree-mode-next-node' moves cursor 2 lines."
  850. (interactive "p")
  851. (if (my-dirtree-current-line-directory-p)
  852. (widget-forward (* arg 2))
  853. (widget-forward arg)))
  854. (defun my-dirtree-previous-node (arg)
  855. "Fix the problem that `tree-mode-previous-node' moves cursor 2 lines."
  856. (interactive "p")
  857. (my-dirtree-next-node (- arg)))
  858. (define-key dirtree-mode-map "n" 'my-dirtree-next-node)
  859. (define-key dirtree-mode-map "p" 'my-dirtree-previous-node))
  860. (define-key ctl-x-map "d" 'dirtree))
  861. (when (safe-require-or-eval 'remember-major-modes-mode)
  862. (remember-major-modes-mode 1))
  863. ;; Detect file type from shebang and set major-mode.
  864. (add-to-list 'interpreter-mode-alist
  865. '("python3" . python-mode))
  866. (add-to-list 'interpreter-mode-alist
  867. '("python2" . python-mode))
  868. ;; http://fukuyama.co/foreign-regexp
  869. '(and (safe-require-or-eval 'foreign-regexp)
  870. (progn
  871. (setq foreign-regexp/regexp-type 'perl)
  872. '(setq reb-re-syntax 'foreign-regexp)
  873. ))
  874. (safe-require-or-eval 'session)
  875. (autoload-eval-lazily 'sql '(sql-mode)
  876. (safe-require-or-eval 'sql-indent))
  877. (when (autoload-eval-lazily 'git-command)
  878. (define-key ctl-x-map "g" 'git-command))
  879. (and (fetch-library
  880. "http://www.emacswiki.org/emacs/download/sl.el"
  881. t)
  882. (autoload-eval-lazily 'sl))
  883. (defalias 'qcalc 'quick-calc)
  884. (safe-require-or-eval 'simple)
  885. (add-hook 'makefile-mode-hook
  886. (lambda ()
  887. (local-set-key (kbd "C-m") 'newline-and-indent)
  888. ;; this functions is set in write-file-functions, i cannot find any
  889. ;; good way to remove this.
  890. (fset 'makefile-warn-suspicious-lines 'ignore)
  891. ))
  892. (add-hook 'verilog-mode-hook
  893. (lambda ()
  894. (local-set-key ";" 'self-insert-command)))
  895. (setq diff-switches "-u")
  896. (add-hook 'diff-mode-hook
  897. (lambda ()
  898. ;; (when (and (eq major-mode
  899. ;; 'diff-mode)
  900. ;; (not buffer-file-name))
  901. ;; ;; do not pass when major-mode is derived mode of diff-mode
  902. ;; (view-mode 1))
  903. (set-face-attribute 'diff-header nil
  904. :foreground nil
  905. :background nil
  906. :weight 'bold)
  907. (set-face-attribute 'diff-file-header nil
  908. :foreground nil
  909. :background nil
  910. :weight 'bold)
  911. (set-face-foreground 'diff-index-face "blue")
  912. (set-face-attribute 'diff-hunk-header nil
  913. :foreground "cyan"
  914. :weight 'normal)
  915. (set-face-attribute 'diff-context nil
  916. ;; :foreground "white"
  917. :foreground nil
  918. :weight 'normal)
  919. (set-face-foreground 'diff-removed-face "red")
  920. (set-face-foreground 'diff-added-face "green")
  921. (set-face-background 'diff-removed-face nil)
  922. (set-face-background 'diff-added-face nil)
  923. (set-face-attribute 'diff-changed nil
  924. :foreground "magenta"
  925. :weight 'normal)
  926. (set-face-attribute 'diff-refine-change nil
  927. :foreground nil
  928. :background nil
  929. :weight 'bold
  930. :inverse-video t)
  931. ;; Annoying !
  932. ;;(diff-auto-refine-mode)
  933. ))
  934. ;; (ffap-bindings)
  935. (add-hook 'sh-mode-hook
  936. (lambda ()
  937. (local-set-key
  938. (kbd "C-x C-e")
  939. 'my-execute-shell-command-current-line)))
  940. (set-variable 'sh-here-document-word "__EOC__")
  941. (defun my-execute-shell-command-current-line ()
  942. "Run current line as shell command."
  943. (interactive)
  944. (shell-command (buffer-substring-no-properties (point-at-bol)
  945. (point))))
  946. (setq auto-mode-alist
  947. `(("autostart\\'" . sh-mode)
  948. ("xinitrc\\'" . sh-mode)
  949. ("xprograms\\'" . sh-mode)
  950. ("PKGBUILD\\'" . sh-mode)
  951. ,@auto-mode-alist))
  952. (and (autoload-eval-lazily 'pkgbuild-mode)
  953. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  954. auto-mode-alist)))
  955. (and (autoload-eval-lazily 'groovy-mode)
  956. (add-to-list 'auto-mode-alist
  957. '("build.gradle\\'" . groovy-mode)))
  958. (add-hook 'yaml-mode-hook
  959. (lambda ()
  960. (local-set-key(kbd "C-m") 'newline)))
  961. (add-hook 'html-mode-hook
  962. (lambda ()
  963. (local-set-key(kbd "C-m") 'reindent-then-newline-and-indent)))
  964. (add-hook 'text-mode-hook
  965. (lambda ()
  966. (local-set-key (kbd "C-m") 'newline)))
  967. (add-to-list 'Info-default-directory-list
  968. (expand-file-name "~/.info/emacs-ja"))
  969. (add-hook 'apropos-mode-hook
  970. (lambda ()
  971. (local-set-key "n" 'next-line)
  972. (local-set-key "p" 'previous-line)
  973. ))
  974. (add-hook 'isearch-mode-hook
  975. (lambda ()
  976. ;; (define-key isearch-mode-map
  977. ;; (kbd "C-j") 'isearch-other-control-char)
  978. ;; (define-key isearch-mode-map
  979. ;; (kbd "C-k") 'isearch-other-control-char)
  980. ;; (define-key isearch-mode-map
  981. ;; (kbd "C-h") 'isearch-other-control-char)
  982. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  983. (define-key isearch-mode-map (kbd "M-r")
  984. 'isearch-query-replace-regexp)))
  985. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  986. (setq lazy-highlight-cleanup nil)
  987. ;; face for isearch highlighing
  988. (set-face-attribute 'lazy-highlight
  989. nil
  990. :foreground `unspecified
  991. :background `unspecified
  992. :underline t
  993. ;; :weight `bold
  994. )
  995. (add-hook 'outline-mode-hook
  996. (lambda ()
  997. (if (string-match "\\.md\\'" buffer-file-name)
  998. (set (make-local-variable 'outline-regexp) "#+ "))))
  999. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1000. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1001. (when (autoload-eval-lazily 'markdown-mode
  1002. '(markdown-mode gfm-mode))
  1003. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1004. (setq markdown-command (or (executable-find "markdown")
  1005. (executable-find "markdown.pl")))
  1006. (add-hook 'markdown-mode-hook
  1007. (lambda ()
  1008. (outline-minor-mode 1)
  1009. (flyspell-mode)
  1010. (set (make-local-variable 'comment-start) ";")))
  1011. (add-hook 'gfm-mode-hook
  1012. (lambda ()
  1013. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1014. ;;(electric-indent-mode 0)
  1015. )))
  1016. ;; c-mode
  1017. ;; http://www.emacswiki.org/emacs/IndentingC
  1018. ;; http://en.wikipedia.org/wiki/Indent_style
  1019. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1020. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1021. (when (autoload-eval-lazily 'cc-vars
  1022. nil
  1023. (defvar c-default-style)
  1024. (add-to-list 'c-default-style
  1025. '(c-mode . "k&r"))
  1026. (add-to-list 'c-default-style
  1027. '(c++-mode . "k&r"))
  1028. (add-hook 'c-mode-common-hook
  1029. (lambda ()
  1030. ;; why c-basic-offset in k&r style defaults to 5 ???
  1031. (set-variable 'c-basic-offset 4)
  1032. (set-variable 'indent-tabs-mode nil)
  1033. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1034. (c-toggle-hungry-state -1)
  1035. ;; (and (require 'gtags nil t)
  1036. ;; (gtags-mode 1))
  1037. ))))
  1038. (when (autoload-eval-lazily 'php-mode)
  1039. (add-hook 'php-mode-hook
  1040. (lambda ()
  1041. (setq c-basic-offset 2))))
  1042. (when (autoload-eval-lazily 'js2-mode)
  1043. ;; currently do not use js2-mode
  1044. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1045. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1046. (add-hook 'js2-mode-hook
  1047. (lambda ()
  1048. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1049. (interactive)
  1050. (js2-enter-key)
  1051. (indent-for-tab-command)))
  1052. ;; (add-hook (kill-local-variable 'before-save-hook)
  1053. ;; 'js2-before-save)
  1054. ;; (add-hook 'before-save-hook
  1055. ;; 'my-indent-buffer
  1056. ;; nil
  1057. ;; t)
  1058. )))
  1059. (eval-after-load "js"
  1060. (set-variable 'js-indent-level 2))
  1061. (add-to-list 'interpreter-mode-alist
  1062. '("node" . js-mode))
  1063. (when (autoload-eval-lazily 'flymake-jslint
  1064. '(flymake-jslint-load))
  1065. (autoload-eval-lazily 'js nil
  1066. (add-hook 'js-mode-hook
  1067. 'flymake-jslint-load)))
  1068. (safe-require-or-eval 'js-doc)
  1069. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1070. (when (safe-require-or-eval 'uniquify)
  1071. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1072. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1073. (setq uniquify-min-dir-content 1))
  1074. (add-hook 'view-mode-hook
  1075. (lambda()
  1076. (defvar view-mode-map)
  1077. (define-key view-mode-map "j" 'scroll-up-line)
  1078. (define-key view-mode-map "k" 'scroll-down-line)
  1079. (define-key view-mode-map "v" 'toggle-read-only)
  1080. (define-key view-mode-map "q" 'bury-buffer)
  1081. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1082. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1083. ;; (define-key view-mode-map
  1084. ;; "n" 'nonincremental-repeat-search-forward)
  1085. ;; (define-key view-mode-map
  1086. ;; "N" 'nonincremental-repeat-search-backward)
  1087. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1088. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1089. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1090. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1091. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1092. ))
  1093. (global-set-key "\M-r" 'view-mode)
  1094. ;; (setq view-read-only t)
  1095. ;; (defun my-view-mode-search-word (word)
  1096. ;; "Search for word current directory and subdirectories.
  1097. ;; If called intearctively, find word at point."
  1098. ;; (interactive (list (thing-at-point 'symbol)))
  1099. ;; (if word
  1100. ;; (if (and (require 'gtags nil t)
  1101. ;; (gtags-get-rootpath))
  1102. ;; (gtags-goto-tag word "s")
  1103. ;; (my-rgrep word))
  1104. ;; (message "No word at point.")
  1105. ;; nil))
  1106. (add-hook 'Man-mode-hook
  1107. (lambda ()
  1108. (view-mode 1)
  1109. (setq truncate-lines nil)))
  1110. (set-variable 'Man-notify-method (if window-system
  1111. 'newframe
  1112. 'aggressive))
  1113. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1114. "woman_cache.el")))
  1115. (defalias 'man 'woman)
  1116. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1117. ;; python
  1118. (when (autoload-eval-lazily 'python '(python-mode))
  1119. (set-variable 'python-python-command (or (executable-find "python3")
  1120. (executable-find "python")))
  1121. ;; (defun my-python-run-as-command ()
  1122. ;; ""
  1123. ;; (interactive)
  1124. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1125. (defun my-python-display-python-buffer ()
  1126. ""
  1127. (interactive)
  1128. (defvar python-buffer)
  1129. (set-window-text-height (display-buffer python-buffer
  1130. t)
  1131. 7))
  1132. (add-hook 'python-mode-hook
  1133. (lambda ()
  1134. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1135. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1136. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1137. (add-hook 'inferior-python-mode-hook
  1138. (lambda ()
  1139. (my-python-display-python-buffer)
  1140. (local-set-key (kbd "<up>") 'comint-previous-input)
  1141. (local-set-key (kbd "<down>") 'comint-next-input))))
  1142. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1143. ;; GNU GLOBAL(gtags)
  1144. ;; http://uguisu.skr.jp/Windows/gtags.html
  1145. ;; http://eigyr.dip.jp/gtags.html
  1146. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1147. (let ((d "/opt/local/share/gtags/"))
  1148. (and (file-directory-p d)
  1149. (add-to-list 'load-path
  1150. d)))
  1151. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1152. (add-hook 'gtags-mode-hook
  1153. (lambda ()
  1154. (view-mode gtags-mode)
  1155. (setq gtags-select-buffer-single t)
  1156. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1157. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1158. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1159. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1160. (define-key gtags-mode-map (kbd "C-x t h")
  1161. 'gtags-find-tag-from-here)
  1162. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1163. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1164. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1165. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1166. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1167. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1168. ))
  1169. (add-hook 'gtags-select-mode-hook
  1170. (lambda ()
  1171. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1172. ))
  1173. )
  1174. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1175. ;; term mode
  1176. ;; (setq multi-term-program shell-file-name)
  1177. (when (autoload-eval-lazily 'multi-term)
  1178. (setq multi-term-switch-after-close nil)
  1179. (setq multi-term-dedicated-select-after-open-p t)
  1180. (setq multi-term-dedicated-window-height 20))
  1181. (when (autoload-eval-lazily 'term '(term ansi-term))
  1182. (defun my-term-quit-or-send-raw ()
  1183. ""
  1184. (interactive)
  1185. (if (get-buffer-process (current-buffer))
  1186. (call-interactively 'term-send-raw)
  1187. (kill-buffer)))
  1188. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1189. ;; (setq term-ansi-default-program shell-file-name)
  1190. (add-hook 'term-setup-hook
  1191. (lambda ()
  1192. (set-variable 'term-display-table (make-display-table))))
  1193. (add-hook 'term-mode-hook
  1194. (lambda ()
  1195. (defvar term-raw-map)
  1196. (unless (memq (current-buffer)
  1197. (and (featurep 'multi-term)
  1198. (defvar multi-term-buffer-list)
  1199. ;; current buffer is not multi-term buffer
  1200. multi-term-buffer-list))
  1201. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1202. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1203. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1204. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1205. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1206. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1207. (define-key term-raw-map
  1208. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1209. (define-key term-raw-map
  1210. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1211. )
  1212. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1213. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1214. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1215. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1216. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1217. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1218. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1219. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1220. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1221. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1222. (define-key term-raw-map [delete] 'term-send-raw)
  1223. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1224. (define-key term-raw-map "\C-y" 'term-paste)
  1225. (define-key term-raw-map
  1226. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1227. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1228. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1229. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1230. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1231. (set (make-local-variable 'scroll-margin) 0)
  1232. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1233. ;; (cua-mode 0)
  1234. ;; (and cua-mode
  1235. ;; (local-unset-key (kbd "C-c")))
  1236. ;; (define-key cua--prefix-override-keymap
  1237. ;;"\C-c" 'term-interrupt-subjob)
  1238. (set (make-local-variable (defvar hl-line-range-function))
  1239. (lambda ()
  1240. '(0 . 0)))
  1241. ))
  1242. ;; (add-hook 'term-exec-hook 'forward-char)
  1243. )
  1244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1245. ;; buffer switching
  1246. (defvar bs-configurations)
  1247. (when (autoload-eval-lazily 'bs '(bs-show)
  1248. ;; (add-to-list 'bs-configurations
  1249. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1250. (add-to-list 'bs-configurations
  1251. '("files-and-terminals" nil nil nil
  1252. (lambda (buf)
  1253. (and (bs-visits-non-file buf)
  1254. (save-excursion
  1255. (set-buffer buf)
  1256. (not (memq major-mode
  1257. '(term-mode
  1258. eshell-mode))))))))
  1259. ;; (setq bs-configurations (list
  1260. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1261. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1262. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1263. )
  1264. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1265. (defalias 'list-buffers 'bs-show)
  1266. (set-variable 'bs-default-configuration "files-and-terminals")
  1267. (set-variable 'bs-default-sort-name "by nothing")
  1268. (add-hook 'bs-mode-hook
  1269. (lambda ()
  1270. ;; (setq bs-default-configuration "files")
  1271. ;; (and bs--show-all
  1272. ;; (call-interactively 'bs-toggle-show-all))
  1273. (set (make-local-variable 'scroll-margin) 0))))
  1274. ;;(iswitchb-mode 1)
  1275. (icomplete-mode)
  1276. (defun iswitchb-buffer-display-other-window ()
  1277. "Do iswitchb in other window."
  1278. (interactive)
  1279. (let ((iswitchb-default-method 'display))
  1280. (call-interactively 'iswitchb-buffer)))
  1281. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1282. ;; sdic
  1283. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1284. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1285. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1286. (defun sdic-describe-word-at-point-echo ()
  1287. ""
  1288. (interactive)
  1289. (save-window-excursion
  1290. (sdic-describe-word-at-point))
  1291. (save-excursion
  1292. (set-buffer sdic-buffer-name)
  1293. (message (buffer-substring (point-min)
  1294. (progn (goto-char (point-min))
  1295. (or (and (re-search-forward "^\\w"
  1296. nil
  1297. t
  1298. 4)
  1299. (progn (previous-line) t)
  1300. (point-at-eol))
  1301. (point-max)))))))
  1302. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1303. (setq sdic-waei-dictionary-list
  1304. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1305. (setq sdic-disable-select-window t)
  1306. (setq sdic-window-height 7))
  1307. ;;;;;;;;;;;;;;;;;;;;;;;;
  1308. ;; ilookup
  1309. (autoload-eval-lazily 'ilookup
  1310. '(ilookup-open)
  1311. (setq ilookup-dict-alist
  1312. '(
  1313. ("sdcv" . (lambda (word)
  1314. (shell-command-to-string
  1315. (format "sdcv -n '%s'"
  1316. word))))
  1317. ("en" . (lambda (word)
  1318. (shell-command-to-string
  1319. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1320. word))))
  1321. ("ja" . (lambda (word)
  1322. (shell-command-to-string
  1323. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1324. word))))
  1325. ("jaj" . (lambda (word)
  1326. (shell-command-to-string
  1327. (format "sdcv -n -u jmdict-en-ja '%s'"
  1328. word))))
  1329. ("jag" .
  1330. (lambda (word)
  1331. (with-temp-buffer
  1332. (insert (shell-command-to-string
  1333. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1334. word)))
  1335. (html2text)
  1336. (buffer-substring (point-min)
  1337. (point-max)))))
  1338. ("alc" . (lambda (word)
  1339. (shell-command-to-string
  1340. (format "alc '%s' | head -n 20"
  1341. word))))
  1342. ("app" . (lambda (word)
  1343. (shell-command-to-string
  1344. (format "dict_app '%s'"
  1345. word))))
  1346. ;; letters broken
  1347. ("ms" .
  1348. (lambda (word)
  1349. (let ((url (concat
  1350. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1351. "Translate?appId=%s&text=%s&to=%s"))
  1352. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1353. (target "ja")
  1354. (eword (url-hexify-string word)))
  1355. (with-current-buffer (url-retrieve-synchronously
  1356. (format url
  1357. apikey
  1358. eword
  1359. target))
  1360. (message "")
  1361. (goto-char (point-min))
  1362. (search-forward-regexp "^$"
  1363. nil
  1364. t)
  1365. (url-unhex-string (buffer-substring-no-properties
  1366. (point)
  1367. (point-max)))))))
  1368. ))
  1369. ;; (funcall (cdr (assoc "ms"
  1370. ;; ilookup-alist))
  1371. ;; "dictionary")
  1372. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1373. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1374. (setq ilookup-default "ja")
  1375. (when (locate-library "google-translate")
  1376. (add-to-list 'ilookup-dict-alist
  1377. '("gt" .
  1378. (lambda (word)
  1379. (save-excursion
  1380. (google-translate-translate "auto"
  1381. "ja"
  1382. word))
  1383. (with-current-buffer "*Google Translate*"
  1384. (buffer-substring-no-properties (point-min)
  1385. (point-max)))))))
  1386. )
  1387. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1388. google-translate-at-point))
  1389. (setq google-translate-default-source-language "auto")
  1390. (setq google-translate-default-target-language "ja"))
  1391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1392. ;; vc
  1393. (require 'vc)
  1394. (setq vc-handled-backends '())
  1395. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1396. ;; gauche-mode
  1397. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1398. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1399. (when (and (fetch-library
  1400. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1401. t)
  1402. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1403. (let ((s (executable-find "gosh")))
  1404. (setq scheme-program-name s
  1405. gauche-program-name s))
  1406. (defun run-gauche-other-window ()
  1407. "Run gauche on other window"
  1408. (interactive)
  1409. (switch-to-buffer-other-window
  1410. (get-buffer-create "*scheme*"))
  1411. (run-gauche))
  1412. (defun run-gauche ()
  1413. "run gauche"
  1414. (interactive)
  1415. (run-scheme gauche-program-name)
  1416. )
  1417. (defun scheme-send-buffer ()
  1418. ""
  1419. (interactive)
  1420. (scheme-send-region (point-min) (point-max))
  1421. (my-scheme-display-scheme-buffer)
  1422. )
  1423. (defun my-scheme-display-scheme-buffer ()
  1424. ""
  1425. (interactive)
  1426. (set-window-text-height (display-buffer scheme-buffer
  1427. t)
  1428. 7))
  1429. (add-hook 'scheme-mode-hook
  1430. (lambda ()
  1431. nil))
  1432. (add-hook 'inferior-scheme-mode-hook
  1433. (lambda ()
  1434. ;; (my-scheme-display-scheme-buffer)
  1435. ))
  1436. (setq auto-mode-alist
  1437. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1438. (setq auto-mode-alist
  1439. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1440. (add-hook 'gauche-mode-hook
  1441. (lambda ()
  1442. (define-key gauche-mode-map
  1443. (kbd "C-c C-z") 'run-gauche-other-window)
  1444. (define-key scheme-mode-map
  1445. (kbd "C-c C-c") 'scheme-send-buffer)
  1446. (define-key scheme-mode-map
  1447. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1448. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1449. ;; recentf-mode
  1450. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1451. "recentf"))
  1452. recentf-max-menu-items 20
  1453. recentf-max-saved-items 30
  1454. recentf-show-file-shortcuts-flag nil)
  1455. (when (safe-require-or-eval 'recentf)
  1456. (add-to-list 'recentf-exclude
  1457. (regexp-quote recentf-save-file))
  1458. (add-to-list 'recentf-exclude
  1459. (regexp-quote (expand-file-name user-emacs-directory)))
  1460. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1461. (remove-hook 'find-file-hook
  1462. 'recentf-track-opened-file)
  1463. (defun my-recentf-load-track-save-list ()
  1464. "Load current recentf list from file, track current visiting file, then save
  1465. the list."
  1466. (recentf-load-list)
  1467. (recentf-track-opened-file)
  1468. (recentf-save-list))
  1469. (add-hook 'find-file-hook
  1470. 'my-recentf-load-track-save-list)
  1471. (add-hook 'kill-emacs-hook
  1472. 'recentf-load-list)
  1473. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1474. ;; (add-hook 'find-file-hook
  1475. ;; (lambda ()
  1476. ;; (recentf-add-file default-directory)))
  1477. (and (autoload-eval-lazily 'recentf-show)
  1478. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1479. (add-hook 'recentf-show-before-listing-hook
  1480. 'recentf-load-list))
  1481. (recentf-mode 1)
  1482. (add-hook 'recentf-dialog-mode-hook
  1483. (lambda ()
  1484. ;; (recentf-save-list)
  1485. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1486. ;; 'my-recentf-cd-and-find-file)
  1487. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1488. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1489. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1490. (define-key recentf-dialog-mode-map "n" 'next-line)
  1491. (cd "~/"))))
  1492. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1493. ;; dired
  1494. (when (autoload-eval-lazily 'dired nil)
  1495. (defun my-dired-echo-file-head (arg)
  1496. ""
  1497. (interactive "P")
  1498. (let ((f (dired-get-filename)))
  1499. (message "%s"
  1500. (with-temp-buffer
  1501. (insert-file-contents f)
  1502. (buffer-substring-no-properties
  1503. (point-min)
  1504. (progn (goto-line (if arg
  1505. (prefix-numeric-value arg)
  1506. 7))
  1507. (point-at-eol)))))))
  1508. (defun my-dired-diff ()
  1509. ""
  1510. (interactive)
  1511. (let ((files (dired-get-marked-files nil nil nil t)))
  1512. (if (eq (car files)
  1513. t)
  1514. (diff (cadr files) (dired-get-filename))
  1515. (message "One files must be marked!"))))
  1516. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1517. "pop up buffer using `display-buffer' and return that buffer."
  1518. (let ((bf (get-buffer-create buffer-or-name)))
  1519. (with-current-buffer bf
  1520. (cd ".")
  1521. (erase-buffer))
  1522. (display-buffer bf)
  1523. bf))
  1524. (defun my-replace-nasi-none ()
  1525. ""
  1526. (save-excursion
  1527. (let ((buffer-read-only nil))
  1528. (goto-char (point-min))
  1529. (while (search-forward "なし" nil t)
  1530. (replace-match "none")))))
  1531. (defun dired-get-file-info ()
  1532. "dired get file info"
  1533. (interactive)
  1534. (let ((f (shell-quote-argument (dired-get-filename t))))
  1535. (if (file-directory-p f)
  1536. (progn
  1537. (message "Calculating disk usage...")
  1538. (shell-command (concat "du -hsD "
  1539. f)))
  1540. (shell-command (concat "file "
  1541. f)))))
  1542. (defun my-dired-scroll-up ()
  1543. ""
  1544. (interactive)
  1545. (my-dired-previous-line (- (window-height) 1)))
  1546. (defun my-dired-scroll-down ()
  1547. ""
  1548. (interactive)
  1549. (my-dired-next-line (- (window-height) 1)))
  1550. ;; (defun my-dired-forward-line (arg)
  1551. ;; ""
  1552. ;; (interactive "p"))
  1553. (defun my-dired-previous-line (arg)
  1554. ""
  1555. (interactive "p")
  1556. (if (> arg 0)
  1557. (progn
  1558. (if (eq (line-number-at-pos)
  1559. 1)
  1560. (goto-char (point-max))
  1561. (forward-line -1))
  1562. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1563. (dired-get-subdir))
  1564. (- arg 1)
  1565. arg)))
  1566. (dired-move-to-filename)))
  1567. (defun my-dired-next-line (arg)
  1568. ""
  1569. (interactive "p")
  1570. (if (> arg 0)
  1571. (progn
  1572. (if (eq (point)
  1573. (point-max))
  1574. (goto-char (point-min))
  1575. (forward-line 1))
  1576. (my-dired-next-line (if (or (dired-get-filename nil t)
  1577. (dired-get-subdir))
  1578. (- arg 1)
  1579. arg)))
  1580. (dired-move-to-filename)))
  1581. (defun my-dired-print-current-dir-and-file ()
  1582. (message "%s %s"
  1583. default-directory
  1584. (buffer-substring-no-properties (point-at-bol)
  1585. (point-at-eol))))
  1586. (defun dired-do-execute-as-command ()
  1587. ""
  1588. (interactive)
  1589. (let ((file (dired-get-filename t)))
  1590. (if (file-executable-p file)
  1591. (start-process file nil file)
  1592. (when (y-or-n-p
  1593. "This file cant be executed. Mark as executable and go? ")
  1594. (set-file-modes file
  1595. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1596. (start-process file nil file)))))
  1597. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1598. (defun my-dired-x-open ()
  1599. ""
  1600. (interactive)
  1601. (my-x-open (dired-get-filename t t)))
  1602. (if (eq window-system 'mac)
  1603. (setq dired-listing-switches "-lhF")
  1604. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1605. )
  1606. (setq dired-listing-switches "-lhF")
  1607. (put 'dired-find-alternate-file 'disabled nil)
  1608. ;; when using dired-find-alternate-file
  1609. ;; reuse current dired buffer for the file to open
  1610. (set-variable 'dired-ls-F-marks-symlinks t)
  1611. (when (safe-require-or-eval 'ls-lisp)
  1612. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1613. (setq ls-lisp-dirs-first t)
  1614. (setq ls-lisp-use-localized-time-format t)
  1615. (setq ls-lisp-format-time-list
  1616. '("%Y-%m-%d %H:%M"
  1617. "%Y-%m-%d ")))
  1618. (set-variable 'dired-dwim-target t)
  1619. (set-variable 'dired-isearch-filenames t)
  1620. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1621. (set-variable 'dired-hide-details-hide-information-lines nil)
  1622. ;; (add-hook 'dired-after-readin-hook
  1623. ;; 'my-replace-nasi-none)
  1624. ;; (add-hook 'after-init-hook
  1625. ;; (lambda ()
  1626. ;; (dired ".")))
  1627. (add-hook 'dired-mode-hook
  1628. (lambda ()
  1629. (local-set-key "o" 'my-dired-x-open)
  1630. (local-set-key "i" 'dired-get-file-info)
  1631. (local-set-key "f" 'find-file)
  1632. (local-set-key "!" 'shell-command)
  1633. (local-set-key "&" 'async-shell-command)
  1634. (local-set-key "X" 'dired-do-async-shell-command)
  1635. (local-set-key "=" 'my-dired-diff)
  1636. (local-set-key "B" 'gtkbm-add-current-dir)
  1637. (local-set-key "b" 'gtkbm)
  1638. (local-set-key "h" 'my-dired-echo-file-head)
  1639. (local-set-key "@" (lambda ()
  1640. (interactive) (my-x-open ".")))
  1641. (local-set-key (kbd "TAB") 'other-window)
  1642. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1643. (local-set-key "/" 'dired-isearch-filenames)
  1644. (local-set-key (kbd "DEL") 'dired-up-directory)
  1645. (local-set-key (kbd "C-h") 'dired-up-directory)
  1646. (substitute-key-definition 'dired-next-line
  1647. 'my-dired-next-line
  1648. (current-local-map))
  1649. (substitute-key-definition 'dired-previous-line
  1650. 'my-dired-previous-line
  1651. (current-local-map))
  1652. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1653. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1654. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1655. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1656. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1657. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1658. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1659. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1660. (when (fboundp 'dired-hide-details-mode)
  1661. (dired-hide-details-mode t)
  1662. (local-set-key "l" 'dired-hide-details-mode))
  1663. (let ((file "._Icon\015"))
  1664. (when nil (file-readable-p file)
  1665. (delete-file file)))))
  1666. (and (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1667. (add-hook 'dired-mode-hook
  1668. (lambda ()
  1669. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1670. (and (autoload-eval-lazily 'dired-list-all-mode)
  1671. (setq dired-listing-switches "-lhF")
  1672. (add-hook 'dired-mode-hook
  1673. (lambda ()
  1674. (local-set-key "a" 'dired-list-all-mode)
  1675. )))
  1676. ) ; when dired locate
  1677. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1678. (defvar dired-marker-char)
  1679. (defun my-dired-toggle-mark()
  1680. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1681. (t dired-marker-char))))
  1682. (delete-char 1)
  1683. (insert cur)))
  1684. (defun my-dired-mark (arg)
  1685. "Toggle mark the current (or next ARG) files.
  1686. If on a subdir headerline, mark all its files except `.' and `..'.
  1687. Use \\[dired-unmark-all-files] to remove all marks
  1688. and \\[dired-unmark] on a subdir to remove the marks in
  1689. this subdir."
  1690. (interactive "P")
  1691. (if (dired-get-subdir)
  1692. (save-excursion (dired-mark-subdir-files))
  1693. (let ((inhibit-read-only t))
  1694. (dired-repeat-over-lines
  1695. (prefix-numeric-value arg)
  1696. 'my-dired-toggle-mark))))
  1697. (defun my-dired-mark-backward (arg)
  1698. "In Dired, move up lines and toggle mark there.
  1699. Optional prefix ARG says how many lines to unflag; default is one line."
  1700. (interactive "p")
  1701. (my-dired-mark (- arg)))
  1702. (add-hook 'dired-mode-hook
  1703. (lambda ()
  1704. (local-set-key (kbd "SPC") 'my-dired-mark)
  1705. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1706. )
  1707. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1708. ;; eshell
  1709. (autoload-eval-lazily 'eshell nil
  1710. (set-variable 'eshell-banner-message (format "Welcome to the Emacs shell
  1711. %s
  1712. C-x t to toggling emacs-text-mode
  1713. "
  1714. (shell-command-to-string "uname -a")
  1715. ))
  1716. (defvar eshell-text-mode-map
  1717. (let ((map (make-sparse-keymap)))
  1718. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1719. map))
  1720. (define-derived-mode eshell-text-mode text-mode
  1721. "Eshell-Text"
  1722. "Text-mode for Eshell."
  1723. nil)
  1724. (defun eshell-text-mode-toggle ()
  1725. "Toggle eshell-text-mode and eshell-mode."
  1726. (interactive)
  1727. (cond ((eq major-mode
  1728. 'eshell-text-mode)
  1729. (goto-char (point-max))
  1730. (message "Eshell text mode disabled")
  1731. (eshell-mode))
  1732. ((eq major-mode
  1733. 'eshell-mode)
  1734. (message "Eshell text mode enabled")
  1735. (eshell-write-history)
  1736. (eshell-text-mode))
  1737. (t
  1738. (message "Not in eshell buffer")
  1739. nil)))
  1740. (defun my-eshell-backward-delete-char ()
  1741. (interactive)
  1742. (when (< (save-excursion
  1743. (eshell-bol)
  1744. (point))
  1745. (point))
  1746. (backward-delete-char 1)))
  1747. (defun my-file-owner-p (file)
  1748. "t if FILE is owned by me."
  1749. (eq (user-uid) (nth 2 (file-attributes file))))
  1750. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1751. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1752. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1753. ;; (defun eshell/less (&rest args)
  1754. ;; "Invoke `view-file' on the file.
  1755. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1756. ;; (if args
  1757. ;; (while args
  1758. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1759. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1760. ;; (file (pop args)))
  1761. ;; (view-file file)
  1762. ;; (goto-line line))
  1763. ;; (view-file (pop args))))))
  1764. (defun eshell/o (&optional file)
  1765. (my-x-open (or file ".")))
  1766. ;; (defun eshell/vi (&rest args)
  1767. ;; "Invoke `find-file' on the file.
  1768. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1769. ;; (while args
  1770. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1771. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1772. ;; (file (pop args)))
  1773. ;; (find-file file)
  1774. ;; (goto-line line))
  1775. ;; (find-file (pop args)))))
  1776. (defun eshell/clear ()
  1777. "Clear the current buffer, leaving one prompt at the top."
  1778. (interactive)
  1779. (let ((inhibit-read-only t))
  1780. (erase-buffer)))
  1781. (defun eshell-clear ()
  1782. (interactive)
  1783. (let ((inhibit-read-only t))
  1784. (erase-buffer)
  1785. (insert (funcall eshell-prompt-function))))
  1786. (defun eshell/d (&optional dirname switches)
  1787. "if first arg is omitted open current directory."
  1788. (dired (or dirname ".") switches))
  1789. (defun eshell/v ()
  1790. (view-mode 1))
  1791. ;; (defun eshell/aaa (&rest args)
  1792. ;; (message "%S"
  1793. ;; args))
  1794. (defalias 'eshell/: 'ignore)
  1795. (defalias 'eshell/type 'eshell/which)
  1796. ;; (defalias 'eshell/vim 'eshell/vi)
  1797. (defalias 'eshell/ff 'find-file)
  1798. (defalias 'eshell/q 'eshell/exit)
  1799. (defun eshell-goto-prompt ()
  1800. ""
  1801. (interactive)
  1802. (goto-char (point-max)))
  1803. (defun eshell-delete-char-or-logout (n)
  1804. (interactive "p")
  1805. (if (equal (eshell-get-old-input)
  1806. "")
  1807. (progn
  1808. (insert "exit")
  1809. (eshell-send-input))
  1810. (delete-char n)))
  1811. (defun eshell-kill-input ()
  1812. (interactive)
  1813. (delete-region (point)
  1814. (progn (eshell-bol)
  1815. (point))))
  1816. (defalias 'eshell/logout 'eshell/exit)
  1817. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1818. "open eshell and change wd
  1819. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1820. (interactive)
  1821. (let ((dir (expand-file-name default-directory)))
  1822. (switch-to-buffer (or eshell-buffer-or-name
  1823. (eshell t)))
  1824. (unless (equal dir (expand-file-name default-directory))
  1825. ;; (cd dir)
  1826. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1827. ;; (eshell-emit-prompt)
  1828. (goto-char (point-max))
  1829. (eshell-kill-input)
  1830. (insert "cd " dir)
  1831. (eshell-send-input))))
  1832. (defadvice eshell-next-matching-input-from-input
  1833. ;; do not cycle history
  1834. (around eshell-history-do-not-cycle activate)
  1835. (if (= 0
  1836. (or eshell-history-index
  1837. 0))
  1838. (progn
  1839. (delete-region eshell-last-output-end (point))
  1840. (insert-and-inherit eshell-matching-input-from-input-string)
  1841. (setq eshell-history-index nil))
  1842. ad-do-it))
  1843. (set-variable 'eshell-directory-name (concat user-emacs-directory
  1844. "eshell/"))
  1845. (set-variable 'eshell-term-name "eterm-color")
  1846. (set-variable 'eshell-scroll-to-bottom-on-input t)
  1847. (set-variable 'eshell-cmpl-ignore-case t)
  1848. (set-variable 'eshell-cmpl-cycle-completions nil)
  1849. (set-variable 'eshell-highlight-prompt nil)
  1850. (if (eq system-type 'darwin)
  1851. (set-variable 'eshell-ls-initial-args '("-hCFG")
  1852. (set-variable 'eshell-ls-initial-args '("-hCFG"
  1853. "--color=auto"
  1854. "--time-style=long-iso")) ; "-hF")
  1855. ))
  1856. (set-variable 'eshell-prompt-function
  1857. 'my-eshell-prompt-function)
  1858. (defvar eshell-last-command-status)
  1859. (defun my-eshell-prompt-function()
  1860. "Prompt function.
  1861. It looks like:
  1862. :: [10sr@darwin:~/][ESHELL]
  1863. :: $
  1864. "
  1865. (concat ":: ["
  1866. (let ((str (concat user-login-name
  1867. "@"
  1868. (car (split-string system-name
  1869. "\\."))
  1870. )))
  1871. (put-text-property 0
  1872. (length str)
  1873. 'face
  1874. 'underline
  1875. str)
  1876. str)
  1877. ":"
  1878. (let ((str (abbreviate-file-name default-directory)))
  1879. (put-text-property 0
  1880. (length str)
  1881. 'face
  1882. 'underline
  1883. str)
  1884. str)
  1885. "][ESHELL]\n:: "
  1886. (if (eq 0
  1887. eshell-last-command-status)
  1888. ""
  1889. (format "[STATUS:%d] "
  1890. eshell-last-command-status))
  1891. (if (= (user-uid)
  1892. 0)
  1893. "# "
  1894. "$ ")
  1895. ))
  1896. (add-hook 'eshell-mode-hook
  1897. (lambda ()
  1898. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1899. ;; (interactive)
  1900. ;; (switch-to-buffer (other-buffer))))
  1901. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1902. ;; (interactive)
  1903. ;; (eshell-goto-prompt)
  1904. ;; (keyboard-quit)))
  1905. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1906. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1907. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1908. ;; (define-key eshell-mode-map (kbd "C-l")
  1909. ;; 'eshell-clear)
  1910. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  1911. (local-set-key (kbd "<up>") 'scroll-down-line)
  1912. (local-set-key (kbd "<down>") 'scroll-up-line)
  1913. ;; (define-key eshell-mode-map
  1914. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1915. ;; (define-key eshell-mode-map
  1916. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  1917. (apply 'eshell/addpath exec-path)
  1918. (set (make-local-variable 'scroll-margin) 0)
  1919. ;; (eshell/export "GIT_PAGER=")
  1920. ;; (eshell/export "GIT_EDITOR=")
  1921. (eshell/export "LC_MESSAGES=C")
  1922. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1923. (set (make-local-variable (defvar hl-line-range-function))
  1924. (lambda ()
  1925. '(0 . 0)))
  1926. (defvar eshell-virtual-targets)
  1927. (add-to-list 'eshell-virtual-targets
  1928. '("/dev/less"
  1929. (lambda (str)
  1930. (if str
  1931. (with-current-buffer nil)))
  1932. nil))
  1933. ))
  1934. (add-hook 'eshell-mode-hook
  1935. (lambda ()
  1936. (defvar eshell-visual-commands)
  1937. (defvar eshell-output-filter-functions)
  1938. (defvar eshell-command-aliases-list)
  1939. (add-to-list 'eshell-visual-commands "vim")
  1940. ;; (add-to-list 'eshell-visual-commands "git")
  1941. (add-to-list 'eshell-output-filter-functions
  1942. 'eshell-truncate-buffer)
  1943. (mapcar (lambda (alias)
  1944. (add-to-list 'eshell-command-aliases-list
  1945. alias))
  1946. '(
  1947. ;; ("ll" "ls -l $*")
  1948. ;; ("la" "ls -a $*")
  1949. ;; ("lla" "ls -al $*")
  1950. ("git" "git -c color.ui=always $*")
  1951. ("g" "git $*")
  1952. ("eless"
  1953. (concat "cat >>> (with-current-buffer "
  1954. "(get-buffer-create \"*eshell output\") "
  1955. "(erase-buffer) "
  1956. "(setq buffer-read-only nil) "
  1957. "(current-buffer)) "
  1958. "(view-buffer (get-buffer \"*eshell output*\"))"))
  1959. )
  1960. )))
  1961. ) ; eval after load eshell
  1962. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1963. ;; my-term
  1964. (defvar my-term nil
  1965. "My terminal buffer.")
  1966. (defvar my-term-function nil
  1967. "Function to create terminal buffer.
  1968. This function accept no argument and return newly created buffer of terminal.")
  1969. (defun my-term (&optional arg)
  1970. "Open terminal buffer and return that buffer.
  1971. If ARG is given or called with prefix argument, create new buffer."
  1972. (interactive "P")
  1973. (if (and (not arg)
  1974. my-term
  1975. (buffer-name my-term))
  1976. (pop-to-buffer my-term)
  1977. (setq my-term
  1978. (save-window-excursion
  1979. (funcall my-term-function)))
  1980. (and my-term
  1981. (my-term))))
  1982. ;; (setq my-term-function
  1983. ;; (lambda ()
  1984. ;; (if (eq system-type 'windows-nt)
  1985. ;; (eshell)
  1986. ;; (if (require 'multi-term nil t)
  1987. ;; (multi-term)
  1988. ;; (ansi-term shell-file-name)))))
  1989. (setq my-term-function (lambda () (eshell t)))
  1990. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  1991. (define-key ctl-x-map "i" 'my-term)
  1992. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1993. ;; x open
  1994. (defvar my-filer nil)
  1995. (setq my-filer (or (executable-find "pcmanfm")
  1996. (executable-find "nautilus")))
  1997. (defun my-x-open (file)
  1998. "Open FILE."
  1999. (interactive "FOpen File: ")
  2000. (setq file (expand-file-name file))
  2001. (message "Opening %s..." file)
  2002. (cond ((eq system-type 'windows-nt)
  2003. (call-process "cmd.exe" nil 0 nil
  2004. "/c" "start" "" (convert-standard-filename file)))
  2005. ((eq system-type 'darwin)
  2006. (call-process "open" nil 0 nil file))
  2007. ((getenv "DISPLAY")
  2008. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2009. (t
  2010. (find-file file))
  2011. )
  2012. ;; (recentf-add-file file)
  2013. (message "Opening %s...done" file))
  2014. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2015. ;; misc funcs
  2016. (defun my-git-apply-index-from-buffer (&optional buf)
  2017. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2018. (interactive)
  2019. (let ((buf (or buf
  2020. (current-buffer)))
  2021. (file (make-temp-file "git-apply-diff.emacs")))
  2022. (with-current-buffer buf
  2023. (write-region (point-min)
  2024. (point-max)
  2025. file)
  2026. (call-process "git"
  2027. nil
  2028. nil
  2029. nil
  2030. "apply"
  2031. "--cached"
  2032. file))))
  2033. (defun memo (&optional dir)
  2034. "Open memo.txt in DIR."
  2035. (interactive)
  2036. (pop-to-buffer (find-file-noselect (concat (if dir
  2037. (file-name-as-directory dir)
  2038. "")
  2039. "memo.txt"))))
  2040. (defvar my-rgrep-alist
  2041. `(
  2042. ;; the silver searcher
  2043. ("ag"
  2044. (executable-find "ag")
  2045. "ag --nocolor --nogroup --nopager --filename ")
  2046. ;; ack
  2047. ("ack"
  2048. (executable-find "ack")
  2049. "ack --nocolor --nogroup --nopager --with-filename ")
  2050. ;; gnu global
  2051. ("global"
  2052. (and (require 'gtags nil t)
  2053. (executable-find "global")
  2054. (gtags-get-rootpath))
  2055. "global --result grep ")
  2056. ;; git grep
  2057. ("gitgrep"
  2058. (eq 0
  2059. (shell-command "git rev-parse --git-dir"))
  2060. "git --no-pager -c color.grep=false grep -nH -e ")
  2061. ;; grep
  2062. ("grep"
  2063. t
  2064. ,(concat "find . "
  2065. "-path '*/.git' -prune -o "
  2066. "-path '*/.svn' -prune -o "
  2067. "-type f -print0 | "
  2068. "xargs -0 grep -nH -e "))
  2069. )
  2070. "Alist of rgrep command.
  2071. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2072. condition to choose COMMAND when evaluated.")
  2073. (defvar my-rgrep-default nil
  2074. "Default command name for my-rgrep.")
  2075. (defun my-rgrep-grep-command (&optional name alist)
  2076. "Return recursive grep command for current directory or nil.
  2077. If NAME is given, use that without testing.
  2078. Commands are searched from ALIST."
  2079. (if alist
  2080. (if name
  2081. ;; if name is given search that from alist and return the command
  2082. (nth 2 (assoc name
  2083. alist))
  2084. ;; if name is not given try test in 1th elem
  2085. (let ((car (car alist))
  2086. (cdr (cdr alist)))
  2087. (if (eval (nth 1 car))
  2088. ;; if the condition is true return the command
  2089. (nth 2 car)
  2090. ;; try next one
  2091. (and cdr
  2092. (my-rgrep-grep-command name cdr)))))
  2093. ;; if alist is not given set default value
  2094. (my-rgrep-grep-command name my-rgrep-alist)))
  2095. (defun my-rgrep (command-args)
  2096. "My recursive grep. Run COMMAND-ARGS."
  2097. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2098. nil)))
  2099. (if cmd
  2100. (list (read-shell-command "grep command: "
  2101. cmd
  2102. 'grep-find-history))
  2103. (error "My-Rgrep: Command for rgrep not found")
  2104. )))
  2105. (compilation-start command-args
  2106. 'grep-mode))
  2107. ;; (defun my-rgrep-symbol-at-point (command-args)
  2108. ;; "My recursive grep. Run COMMAND-ARGS."
  2109. ;; (interactive (list (read-shell-command "grep command: "
  2110. ;; (concat (my-rgrep-grep-command)
  2111. ;; " "
  2112. ;; (thing-at-point 'symbol))
  2113. ;; 'grep-find-history)))
  2114. ;; (compilation-start command-args
  2115. ;; 'grep-mode))
  2116. (defmacro define-my-rgrep (name)
  2117. "Define rgrep for NAME."
  2118. `(defun ,(intern (concat "my-rgrep-"
  2119. name)) ()
  2120. ,(format "My recursive grep by %s."
  2121. name)
  2122. (interactive)
  2123. (let ((my-rgrep-default ,name))
  2124. (if (called-interactively-p 'any)
  2125. (call-interactively 'my-rgrep)
  2126. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2127. )
  2128. (define-my-rgrep "ack")
  2129. (define-my-rgrep "ag")
  2130. (define-my-rgrep "gitgrep")
  2131. (define-my-rgrep "grep")
  2132. (define-my-rgrep "global")
  2133. (define-key ctl-x-map "s" 'my-rgrep)
  2134. ;; (defun make ()
  2135. ;; "Run \"make -k\" in current directory."
  2136. ;; (interactive)
  2137. ;; (compile "make -k"))
  2138. (defalias 'make 'compile)
  2139. (define-key ctl-x-map "c" 'compile)
  2140. (defvar sed-in-place-history nil
  2141. "History of `sed-in-place'.")
  2142. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2143. (defun sed-in-place (command)
  2144. "Issue sed in place COMMAND."
  2145. (interactive (list (read-shell-command "sed in place: "
  2146. (concat sed-in-place-command " ")
  2147. 'sed-in-place-history)))
  2148. (shell-command command
  2149. "*sed in place*"))
  2150. (defun dired-do-sed-in-place (&optional arg)
  2151. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2152. (interactive "p")
  2153. (require 'dired-aux)
  2154. (let* ((files (dired-get-marked-files t arg))
  2155. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2156. nil
  2157. 'sed-in-place
  2158. arg
  2159. files)))
  2160. (if (equal expr
  2161. "")
  2162. (error "No expression specified")
  2163. (shell-command (concat sed-in-place-command
  2164. " '"
  2165. expr
  2166. "' "
  2167. (mapconcat 'shell-quote-argument
  2168. files
  2169. " "))
  2170. "*sed in place*"))))
  2171. (defun dir-show (&optional dir)
  2172. "Show DIR list."
  2173. (interactive)
  2174. (let ((bf (get-buffer-create "*dir show*"))
  2175. (list-directory-brief-switches "-C"))
  2176. (with-current-buffer bf
  2177. (list-directory (or nil
  2178. default-directory)
  2179. nil))
  2180. ))
  2181. (defun my-convmv-sjis2utf8-test ()
  2182. "Run `convmv -r -f sjis -t utf8 *'.
  2183. this is test, does not rename files."
  2184. (interactive)
  2185. (shell-command "convmv -r -f sjis -t utf8 *"))
  2186. (defun my-convmv-sjis2utf8-notest ()
  2187. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2188. (interactive)
  2189. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2190. (defun kill-ring-save-buffer-file-name ()
  2191. "Get current filename."
  2192. (interactive)
  2193. (let ((file buffer-file-name))
  2194. (if file
  2195. (progn (kill-new file)
  2196. (message file))
  2197. (message "not visiting file."))))
  2198. (defvar kill-ring-buffer-name "*kill-ring*"
  2199. "Buffer name for `kill-ring-buffer'.")
  2200. (defun open-kill-ring-buffer ()
  2201. "Open kill- ring buffer."
  2202. (interactive)
  2203. (pop-to-buffer
  2204. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2205. (erase-buffer)
  2206. (yank)
  2207. (text-mode)
  2208. (current-local-map)
  2209. (goto-char (point-min))
  2210. (yank)
  2211. (current-buffer))))
  2212. (defun set-terminal-header (string)
  2213. "Set terminal header STRING."
  2214. (let ((savepos "\033[s")
  2215. (restorepos "\033[u")
  2216. (movecursor "\033[0;%dH")
  2217. (inverse "\033[7m")
  2218. (restorecolor "\033[0m")
  2219. (cols (frame-parameter nil 'width))
  2220. (length (length string)))
  2221. ;; (redraw-frame (selected-frame))
  2222. (send-string-to-terminal (concat savepos
  2223. (format movecursor
  2224. (1+ (- cols length)))
  2225. inverse
  2226. string
  2227. restorecolor
  2228. restorepos))
  2229. ))
  2230. (defun my-set-terminal-header ()
  2231. "Set terminal header."
  2232. (set-terminal-header (concat " "
  2233. user-login-name
  2234. "@"
  2235. (car (split-string system-name
  2236. "\\."))
  2237. " "
  2238. (format-time-string "%Y/%m/%d %T %z")
  2239. " ")))
  2240. ;; (run-with-timer
  2241. ;; 0.1
  2242. ;; 1
  2243. ;; 'my-set-terminal-header)
  2244. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2245. ;; ;; savage emacs
  2246. ;; ;; when enabled emacs fails to complete
  2247. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2248. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2249. ;; (setq arg
  2250. ;; (concat arg
  2251. ;; (if (eq nil
  2252. ;; (string-match "\\. *$"
  2253. ;; arg))
  2254. ;; ".")
  2255. ;; " Stupid!")))
  2256. ;; TODO: make these a library
  2257. (defvar info-in-prompt
  2258. nil
  2259. "System info in the form of \"[user@host] \".")
  2260. (setq info-in-prompt
  2261. (concat "["
  2262. user-login-name
  2263. "@"
  2264. (car (split-string system-name
  2265. "\\."))
  2266. "]"))
  2267. (defun my-real-function-subr-p (function)
  2268. "Return t if FUNCTION is a built-in function even if it is advised."
  2269. (let* ((advised (and (symbolp function)
  2270. (featurep 'advice)
  2271. (ad-get-advice-info function)))
  2272. (real-function
  2273. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2274. (and (fboundp origname)
  2275. origname)))
  2276. function))
  2277. (def (if (symbolp real-function)
  2278. (symbol-function real-function)
  2279. function)))
  2280. (subrp def)))
  2281. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2282. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2283. ;; "Show system info when use `read-from-minibuffer'."
  2284. ;; (ad-set-arg 0
  2285. ;; (concat my-system-info
  2286. ;; (ad-get-arg 0))))
  2287. ;; (defadvice read-string (before info-in-prompt activate)
  2288. ;; "Show system info when use `read-string'."
  2289. ;; (ad-set-arg 0
  2290. ;; (concat my-system-info
  2291. ;; (ad-get-arg 0))))
  2292. ;; (when (< emacs-major-version 24)
  2293. ;; (defadvice completing-read (before info-in-prompt activate)
  2294. ;; "Show system info when use `completing-read'."
  2295. ;; (ad-set-arg 0
  2296. ;; (concat my-system-info
  2297. ;; (ad-get-arg 0)))))
  2298. (defmacro info-in-prompt-set (&rest functions)
  2299. "Set info-in-prompt advices for FUNCTIONS."
  2300. `(progn
  2301. ,@(mapcar (lambda (f)
  2302. `(defadvice ,f (before info-in-prompt activate)
  2303. "Show info in prompt."
  2304. (let ((orig (ad-get-arg 0)))
  2305. (unless (string-match-p (regexp-quote info-in-prompt)
  2306. orig)
  2307. (ad-set-arg 0
  2308. (concat info-in-prompt
  2309. " "
  2310. orig))))))
  2311. functions)))
  2312. (info-in-prompt-set read-from-minibuffer
  2313. read-string
  2314. completing-read)
  2315. ;;; emacs.el ends here