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.
 
 
 
 
 
 

2649 regels
90 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. `(progn
  47. (when (locate-library ,libname)
  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. (set-variable '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. (set-variable '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. (set-variable 'defauit-input-method "japanese-mozc")
  736. (set-variable '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. (with-eval-after-load 'cc-vars
  1022. (defvar c-default-style)
  1023. (add-to-list 'c-default-style
  1024. '(c-mode . "k&r"))
  1025. (add-to-list 'c-default-style
  1026. '(c++-mode . "k&r"))
  1027. (add-hook 'c-mode-common-hook
  1028. (lambda ()
  1029. ;; why c-basic-offset in k&r style defaults to 5 ???
  1030. (set-variable 'c-basic-offset 4)
  1031. (set-variable 'indent-tabs-mode nil)
  1032. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1033. (c-toggle-hungry-state -1)
  1034. ;; (and (require 'gtags nil t)
  1035. ;; (gtags-mode 1))
  1036. )))
  1037. (when (autoload-eval-lazily 'php-mode)
  1038. (add-hook 'php-mode-hook
  1039. (lambda ()
  1040. (setq c-basic-offset 2))))
  1041. (when (autoload-eval-lazily 'js2-mode)
  1042. ;; currently do not use js2-mode
  1043. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1044. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1045. (add-hook 'js2-mode-hook
  1046. (lambda ()
  1047. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1048. (interactive)
  1049. (js2-enter-key)
  1050. (indent-for-tab-command)))
  1051. ;; (add-hook (kill-local-variable 'before-save-hook)
  1052. ;; 'js2-before-save)
  1053. ;; (add-hook 'before-save-hook
  1054. ;; 'my-indent-buffer
  1055. ;; nil
  1056. ;; t)
  1057. )))
  1058. (eval-after-load "js"
  1059. (set-variable 'js-indent-level 2))
  1060. (add-to-list 'interpreter-mode-alist
  1061. '("node" . js-mode))
  1062. (when (autoload-eval-lazily 'flymake-jslint
  1063. '(flymake-jslint-load))
  1064. (autoload-eval-lazily 'js nil
  1065. (add-hook 'js-mode-hook
  1066. 'flymake-jslint-load)))
  1067. (safe-require-or-eval 'js-doc)
  1068. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1069. (when (safe-require-or-eval 'uniquify)
  1070. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1071. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1072. (setq uniquify-min-dir-content 1))
  1073. (add-hook 'view-mode-hook
  1074. (lambda()
  1075. (defvar view-mode-map)
  1076. (define-key view-mode-map "j" 'scroll-up-line)
  1077. (define-key view-mode-map "k" 'scroll-down-line)
  1078. (define-key view-mode-map "v" 'toggle-read-only)
  1079. (define-key view-mode-map "q" 'bury-buffer)
  1080. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1081. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1082. ;; (define-key view-mode-map
  1083. ;; "n" 'nonincremental-repeat-search-forward)
  1084. ;; (define-key view-mode-map
  1085. ;; "N" 'nonincremental-repeat-search-backward)
  1086. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1087. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1088. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1089. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1090. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1091. ))
  1092. (global-set-key "\M-r" 'view-mode)
  1093. ;; (setq view-read-only t)
  1094. ;; (defun my-view-mode-search-word (word)
  1095. ;; "Search for word current directory and subdirectories.
  1096. ;; If called intearctively, find word at point."
  1097. ;; (interactive (list (thing-at-point 'symbol)))
  1098. ;; (if word
  1099. ;; (if (and (require 'gtags nil t)
  1100. ;; (gtags-get-rootpath))
  1101. ;; (gtags-goto-tag word "s")
  1102. ;; (my-rgrep word))
  1103. ;; (message "No word at point.")
  1104. ;; nil))
  1105. (add-hook 'Man-mode-hook
  1106. (lambda ()
  1107. (view-mode 1)
  1108. (setq truncate-lines nil)))
  1109. (set-variable 'Man-notify-method (if window-system
  1110. 'newframe
  1111. 'aggressive))
  1112. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1113. "woman_cache.el")))
  1114. (defalias 'man 'woman)
  1115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1116. ;; python
  1117. (when (autoload-eval-lazily 'python '(python-mode))
  1118. (set-variable 'python-python-command (or (executable-find "python3")
  1119. (executable-find "python")))
  1120. ;; (defun my-python-run-as-command ()
  1121. ;; ""
  1122. ;; (interactive)
  1123. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1124. (defun my-python-display-python-buffer ()
  1125. ""
  1126. (interactive)
  1127. (defvar python-buffer)
  1128. (set-window-text-height (display-buffer python-buffer
  1129. t)
  1130. 7))
  1131. (add-hook 'python-mode-hook
  1132. (lambda ()
  1133. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1134. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1135. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1136. (add-hook 'inferior-python-mode-hook
  1137. (lambda ()
  1138. (my-python-display-python-buffer)
  1139. (local-set-key (kbd "<up>") 'comint-previous-input)
  1140. (local-set-key (kbd "<down>") 'comint-next-input))))
  1141. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1142. ;; GNU GLOBAL(gtags)
  1143. ;; http://uguisu.skr.jp/Windows/gtags.html
  1144. ;; http://eigyr.dip.jp/gtags.html
  1145. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1146. (let ((d "/opt/local/share/gtags/"))
  1147. (and (file-directory-p d)
  1148. (add-to-list 'load-path
  1149. d)))
  1150. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1151. (add-hook 'gtags-mode-hook
  1152. (lambda ()
  1153. (view-mode gtags-mode)
  1154. (setq gtags-select-buffer-single t)
  1155. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1156. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1157. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1158. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1159. (define-key gtags-mode-map (kbd "C-x t h")
  1160. 'gtags-find-tag-from-here)
  1161. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1162. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1163. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1164. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1165. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1166. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1167. ))
  1168. (add-hook 'gtags-select-mode-hook
  1169. (lambda ()
  1170. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1171. ))
  1172. )
  1173. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1174. ;; term mode
  1175. ;; (setq multi-term-program shell-file-name)
  1176. (when (autoload-eval-lazily 'multi-term)
  1177. (setq multi-term-switch-after-close nil)
  1178. (setq multi-term-dedicated-select-after-open-p t)
  1179. (setq multi-term-dedicated-window-height 20))
  1180. (when (autoload-eval-lazily 'term '(term ansi-term))
  1181. (defun my-term-quit-or-send-raw ()
  1182. ""
  1183. (interactive)
  1184. (if (get-buffer-process (current-buffer))
  1185. (call-interactively 'term-send-raw)
  1186. (kill-buffer)))
  1187. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1188. ;; (setq term-ansi-default-program shell-file-name)
  1189. (add-hook 'term-setup-hook
  1190. (lambda ()
  1191. (set-variable 'term-display-table (make-display-table))))
  1192. (add-hook 'term-mode-hook
  1193. (lambda ()
  1194. (defvar term-raw-map)
  1195. (unless (memq (current-buffer)
  1196. (and (featurep 'multi-term)
  1197. (defvar multi-term-buffer-list)
  1198. ;; current buffer is not multi-term buffer
  1199. multi-term-buffer-list))
  1200. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1201. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1202. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1203. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1204. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1205. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1206. (define-key term-raw-map
  1207. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1208. (define-key term-raw-map
  1209. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1210. )
  1211. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1212. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1213. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1214. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1215. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1216. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1217. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1218. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1219. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1220. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1221. (define-key term-raw-map [delete] 'term-send-raw)
  1222. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1223. (define-key term-raw-map "\C-y" 'term-paste)
  1224. (define-key term-raw-map
  1225. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1226. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1227. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1228. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1229. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1230. (set (make-local-variable 'scroll-margin) 0)
  1231. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1232. ;; (cua-mode 0)
  1233. ;; (and cua-mode
  1234. ;; (local-unset-key (kbd "C-c")))
  1235. ;; (define-key cua--prefix-override-keymap
  1236. ;;"\C-c" 'term-interrupt-subjob)
  1237. (set (make-local-variable (defvar hl-line-range-function))
  1238. (lambda ()
  1239. '(0 . 0)))
  1240. ))
  1241. ;; (add-hook 'term-exec-hook 'forward-char)
  1242. )
  1243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1244. ;; buffer switching
  1245. (defvar bs-configurations)
  1246. (when (autoload-eval-lazily 'bs '(bs-show)
  1247. ;; (add-to-list 'bs-configurations
  1248. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1249. (add-to-list 'bs-configurations
  1250. '("files-and-terminals" nil nil nil
  1251. (lambda (buf)
  1252. (and (bs-visits-non-file buf)
  1253. (save-excursion
  1254. (set-buffer buf)
  1255. (not (memq major-mode
  1256. '(term-mode
  1257. eshell-mode))))))))
  1258. ;; (setq bs-configurations (list
  1259. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1260. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1261. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1262. )
  1263. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1264. (defalias 'list-buffers 'bs-show)
  1265. (set-variable 'bs-default-configuration "files-and-terminals")
  1266. (set-variable 'bs-default-sort-name "by nothing")
  1267. (add-hook 'bs-mode-hook
  1268. (lambda ()
  1269. ;; (setq bs-default-configuration "files")
  1270. ;; (and bs--show-all
  1271. ;; (call-interactively 'bs-toggle-show-all))
  1272. (set (make-local-variable 'scroll-margin) 0))))
  1273. ;;(iswitchb-mode 1)
  1274. (icomplete-mode)
  1275. (defun iswitchb-buffer-display-other-window ()
  1276. "Do iswitchb in other window."
  1277. (interactive)
  1278. (let ((iswitchb-default-method 'display))
  1279. (call-interactively 'iswitchb-buffer)))
  1280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1281. ;; sdic
  1282. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1283. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1284. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1285. (defun sdic-describe-word-at-point-echo ()
  1286. ""
  1287. (interactive)
  1288. (save-window-excursion
  1289. (sdic-describe-word-at-point))
  1290. (save-excursion
  1291. (set-buffer sdic-buffer-name)
  1292. (message (buffer-substring (point-min)
  1293. (progn (goto-char (point-min))
  1294. (or (and (re-search-forward "^\\w"
  1295. nil
  1296. t
  1297. 4)
  1298. (progn (previous-line) t)
  1299. (point-at-eol))
  1300. (point-max)))))))
  1301. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1302. (setq sdic-waei-dictionary-list
  1303. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1304. (setq sdic-disable-select-window t)
  1305. (setq sdic-window-height 7))
  1306. ;;;;;;;;;;;;;;;;;;;;;;;;
  1307. ;; ilookup
  1308. (with-eval-after-load 'ilookup
  1309. (setq ilookup-dict-alist
  1310. '(
  1311. ("sdcv" . (lambda (word)
  1312. (shell-command-to-string
  1313. (format "sdcv -n '%s'"
  1314. word))))
  1315. ("en" . (lambda (word)
  1316. (shell-command-to-string
  1317. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1318. word))))
  1319. ("ja" . (lambda (word)
  1320. (shell-command-to-string
  1321. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1322. word))))
  1323. ("jaj" . (lambda (word)
  1324. (shell-command-to-string
  1325. (format "sdcv -n -u jmdict-en-ja '%s'"
  1326. word))))
  1327. ("jag" .
  1328. (lambda (word)
  1329. (with-temp-buffer
  1330. (insert (shell-command-to-string
  1331. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1332. word)))
  1333. (html2text)
  1334. (buffer-substring (point-min)
  1335. (point-max)))))
  1336. ("alc" . (lambda (word)
  1337. (shell-command-to-string
  1338. (format "alc '%s' | head -n 20"
  1339. word))))
  1340. ("app" . (lambda (word)
  1341. (shell-command-to-string
  1342. (format "dict_app '%s'"
  1343. word))))
  1344. ;; letters broken
  1345. ("ms" .
  1346. (lambda (word)
  1347. (let ((url (concat
  1348. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1349. "Translate?appId=%s&text=%s&to=%s"))
  1350. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1351. (target "ja")
  1352. (eword (url-hexify-string word)))
  1353. (with-current-buffer (url-retrieve-synchronously
  1354. (format url
  1355. apikey
  1356. eword
  1357. target))
  1358. (message "")
  1359. (goto-char (point-min))
  1360. (search-forward-regexp "^$"
  1361. nil
  1362. t)
  1363. (url-unhex-string (buffer-substring-no-properties
  1364. (point)
  1365. (point-max)))))))
  1366. ))
  1367. ;; (funcall (cdr (assoc "ms"
  1368. ;; ilookup-alist))
  1369. ;; "dictionary")
  1370. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1371. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1372. (setq ilookup-default "ja")
  1373. (when (locate-library "google-translate")
  1374. (add-to-list 'ilookup-dict-alist
  1375. '("gt" .
  1376. (lambda (word)
  1377. (save-excursion
  1378. (google-translate-translate "auto"
  1379. "ja"
  1380. word))
  1381. (with-current-buffer "*Google Translate*"
  1382. (buffer-substring-no-properties (point-min)
  1383. (point-max)))))))
  1384. )
  1385. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1386. google-translate-at-point))
  1387. (setq google-translate-default-source-language "auto")
  1388. (setq google-translate-default-target-language "ja"))
  1389. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1390. ;; vc
  1391. (require 'vc)
  1392. (setq vc-handled-backends '())
  1393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1394. ;; gauche-mode
  1395. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1396. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1397. (when (and (fetch-library
  1398. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1399. t)
  1400. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1401. (let ((s (executable-find "gosh")))
  1402. (setq scheme-program-name s
  1403. gauche-program-name s))
  1404. (defun run-gauche-other-window ()
  1405. "Run gauche on other window"
  1406. (interactive)
  1407. (switch-to-buffer-other-window
  1408. (get-buffer-create "*scheme*"))
  1409. (run-gauche))
  1410. (defun run-gauche ()
  1411. "run gauche"
  1412. (interactive)
  1413. (run-scheme gauche-program-name)
  1414. )
  1415. (defun scheme-send-buffer ()
  1416. ""
  1417. (interactive)
  1418. (scheme-send-region (point-min) (point-max))
  1419. (my-scheme-display-scheme-buffer)
  1420. )
  1421. (defun my-scheme-display-scheme-buffer ()
  1422. ""
  1423. (interactive)
  1424. (set-window-text-height (display-buffer scheme-buffer
  1425. t)
  1426. 7))
  1427. (add-hook 'scheme-mode-hook
  1428. (lambda ()
  1429. nil))
  1430. (add-hook 'inferior-scheme-mode-hook
  1431. (lambda ()
  1432. ;; (my-scheme-display-scheme-buffer)
  1433. ))
  1434. (setq auto-mode-alist
  1435. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1436. (setq auto-mode-alist
  1437. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1438. (add-hook 'gauche-mode-hook
  1439. (lambda ()
  1440. (define-key gauche-mode-map
  1441. (kbd "C-c C-z") 'run-gauche-other-window)
  1442. (define-key scheme-mode-map
  1443. (kbd "C-c C-c") 'scheme-send-buffer)
  1444. (define-key scheme-mode-map
  1445. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1446. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1447. ;; recentf-mode
  1448. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1449. "recentf"))
  1450. recentf-max-menu-items 20
  1451. recentf-max-saved-items 30
  1452. recentf-show-file-shortcuts-flag nil)
  1453. (when (safe-require-or-eval 'recentf)
  1454. (add-to-list 'recentf-exclude
  1455. (regexp-quote recentf-save-file))
  1456. (add-to-list 'recentf-exclude
  1457. (regexp-quote (expand-file-name user-emacs-directory)))
  1458. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1459. (remove-hook 'find-file-hook
  1460. 'recentf-track-opened-file)
  1461. (defun my-recentf-load-track-save-list ()
  1462. "Load current recentf list from file, track current visiting file, then save
  1463. the list."
  1464. (recentf-load-list)
  1465. (recentf-track-opened-file)
  1466. (recentf-save-list))
  1467. (add-hook 'find-file-hook
  1468. 'my-recentf-load-track-save-list)
  1469. (add-hook 'kill-emacs-hook
  1470. 'recentf-load-list)
  1471. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1472. ;; (add-hook 'find-file-hook
  1473. ;; (lambda ()
  1474. ;; (recentf-add-file default-directory)))
  1475. (and (autoload-eval-lazily 'recentf-show)
  1476. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1477. (add-hook 'recentf-show-before-listing-hook
  1478. 'recentf-load-list))
  1479. (recentf-mode 1)
  1480. (add-hook 'recentf-dialog-mode-hook
  1481. (lambda ()
  1482. ;; (recentf-save-list)
  1483. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1484. ;; 'my-recentf-cd-and-find-file)
  1485. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1486. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1487. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1488. (define-key recentf-dialog-mode-map "n" 'next-line)
  1489. (cd "~/"))))
  1490. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1491. ;; dired
  1492. (when (autoload-eval-lazily 'dired nil)
  1493. (defun my-dired-echo-file-head (arg)
  1494. ""
  1495. (interactive "P")
  1496. (let ((f (dired-get-filename)))
  1497. (message "%s"
  1498. (with-temp-buffer
  1499. (insert-file-contents f)
  1500. (buffer-substring-no-properties
  1501. (point-min)
  1502. (progn (goto-line (if arg
  1503. (prefix-numeric-value arg)
  1504. 7))
  1505. (point-at-eol)))))))
  1506. (defun my-dired-diff ()
  1507. ""
  1508. (interactive)
  1509. (let ((files (dired-get-marked-files nil nil nil t)))
  1510. (if (eq (car files)
  1511. t)
  1512. (diff (cadr files) (dired-get-filename))
  1513. (message "One files must be marked!"))))
  1514. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1515. "pop up buffer using `display-buffer' and return that buffer."
  1516. (let ((bf (get-buffer-create buffer-or-name)))
  1517. (with-current-buffer bf
  1518. (cd ".")
  1519. (erase-buffer))
  1520. (display-buffer bf)
  1521. bf))
  1522. (defun my-replace-nasi-none ()
  1523. ""
  1524. (save-excursion
  1525. (let ((buffer-read-only nil))
  1526. (goto-char (point-min))
  1527. (while (search-forward "なし" nil t)
  1528. (replace-match "none")))))
  1529. (defun dired-get-file-info ()
  1530. "dired get file info"
  1531. (interactive)
  1532. (let ((f (shell-quote-argument (dired-get-filename t))))
  1533. (if (file-directory-p f)
  1534. (progn
  1535. (message "Calculating disk usage...")
  1536. (shell-command (concat "du -hsD "
  1537. f)))
  1538. (shell-command (concat "file "
  1539. f)))))
  1540. (defun my-dired-scroll-up ()
  1541. ""
  1542. (interactive)
  1543. (my-dired-previous-line (- (window-height) 1)))
  1544. (defun my-dired-scroll-down ()
  1545. ""
  1546. (interactive)
  1547. (my-dired-next-line (- (window-height) 1)))
  1548. ;; (defun my-dired-forward-line (arg)
  1549. ;; ""
  1550. ;; (interactive "p"))
  1551. (defun my-dired-previous-line (arg)
  1552. ""
  1553. (interactive "p")
  1554. (if (> arg 0)
  1555. (progn
  1556. (if (eq (line-number-at-pos)
  1557. 1)
  1558. (goto-char (point-max))
  1559. (forward-line -1))
  1560. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1561. (dired-get-subdir))
  1562. (- arg 1)
  1563. arg)))
  1564. (dired-move-to-filename)))
  1565. (defun my-dired-next-line (arg)
  1566. ""
  1567. (interactive "p")
  1568. (if (> arg 0)
  1569. (progn
  1570. (if (eq (point)
  1571. (point-max))
  1572. (goto-char (point-min))
  1573. (forward-line 1))
  1574. (my-dired-next-line (if (or (dired-get-filename nil t)
  1575. (dired-get-subdir))
  1576. (- arg 1)
  1577. arg)))
  1578. (dired-move-to-filename)))
  1579. (defun my-dired-print-current-dir-and-file ()
  1580. (message "%s %s"
  1581. default-directory
  1582. (buffer-substring-no-properties (point-at-bol)
  1583. (point-at-eol))))
  1584. (defun dired-do-execute-as-command ()
  1585. ""
  1586. (interactive)
  1587. (let ((file (dired-get-filename t)))
  1588. (if (file-executable-p file)
  1589. (start-process file nil file)
  1590. (when (y-or-n-p
  1591. "This file cant be executed. Mark as executable and go? ")
  1592. (set-file-modes file
  1593. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1594. (start-process file nil file)))))
  1595. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1596. (defun my-dired-x-open ()
  1597. ""
  1598. (interactive)
  1599. (my-x-open (dired-get-filename t t)))
  1600. (if (eq window-system 'mac)
  1601. (setq dired-listing-switches "-lhF")
  1602. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1603. )
  1604. (setq dired-listing-switches "-lhF")
  1605. (put 'dired-find-alternate-file 'disabled nil)
  1606. ;; when using dired-find-alternate-file
  1607. ;; reuse current dired buffer for the file to open
  1608. (set-variable 'dired-ls-F-marks-symlinks t)
  1609. (when (safe-require-or-eval 'ls-lisp)
  1610. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1611. (setq ls-lisp-dirs-first t)
  1612. (setq ls-lisp-use-localized-time-format t)
  1613. (setq ls-lisp-format-time-list
  1614. '("%Y-%m-%d %H:%M"
  1615. "%Y-%m-%d ")))
  1616. (set-variable 'dired-dwim-target t)
  1617. (set-variable 'dired-isearch-filenames t)
  1618. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1619. (set-variable 'dired-hide-details-hide-information-lines nil)
  1620. ;; (add-hook 'dired-after-readin-hook
  1621. ;; 'my-replace-nasi-none)
  1622. ;; (add-hook 'after-init-hook
  1623. ;; (lambda ()
  1624. ;; (dired ".")))
  1625. (add-hook 'dired-mode-hook
  1626. (lambda ()
  1627. (local-set-key "o" 'my-dired-x-open)
  1628. (local-set-key "i" 'dired-get-file-info)
  1629. (local-set-key "f" 'find-file)
  1630. (local-set-key "!" 'shell-command)
  1631. (local-set-key "&" 'async-shell-command)
  1632. (local-set-key "X" 'dired-do-async-shell-command)
  1633. (local-set-key "=" 'my-dired-diff)
  1634. (local-set-key "B" 'gtkbm-add-current-dir)
  1635. (local-set-key "b" 'gtkbm)
  1636. (local-set-key "h" 'my-dired-echo-file-head)
  1637. (local-set-key "@" (lambda ()
  1638. (interactive) (my-x-open ".")))
  1639. (local-set-key (kbd "TAB") 'other-window)
  1640. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1641. (local-set-key "/" 'dired-isearch-filenames)
  1642. (local-set-key (kbd "DEL") 'dired-up-directory)
  1643. (local-set-key (kbd "C-h") 'dired-up-directory)
  1644. (substitute-key-definition 'dired-next-line
  1645. 'my-dired-next-line
  1646. (current-local-map))
  1647. (substitute-key-definition 'dired-previous-line
  1648. 'my-dired-previous-line
  1649. (current-local-map))
  1650. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1651. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1652. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1653. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1654. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1655. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1656. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1657. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1658. (when (fboundp 'dired-hide-details-mode)
  1659. (dired-hide-details-mode t)
  1660. (local-set-key "l" 'dired-hide-details-mode))
  1661. (let ((file "._Icon\015"))
  1662. (when nil (file-readable-p file)
  1663. (delete-file file)))))
  1664. (and (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1665. (add-hook 'dired-mode-hook
  1666. (lambda ()
  1667. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1668. (and (autoload-eval-lazily 'dired-list-all-mode)
  1669. (setq dired-listing-switches "-lhF")
  1670. (add-hook 'dired-mode-hook
  1671. (lambda ()
  1672. (local-set-key "a" 'dired-list-all-mode)
  1673. )))
  1674. ) ; when dired locate
  1675. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1676. (defvar dired-marker-char)
  1677. (defun my-dired-toggle-mark()
  1678. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1679. (t dired-marker-char))))
  1680. (delete-char 1)
  1681. (insert cur)))
  1682. (defun my-dired-mark (arg)
  1683. "Toggle mark the current (or next ARG) files.
  1684. If on a subdir headerline, mark all its files except `.' and `..'.
  1685. Use \\[dired-unmark-all-files] to remove all marks
  1686. and \\[dired-unmark] on a subdir to remove the marks in
  1687. this subdir."
  1688. (interactive "P")
  1689. (if (dired-get-subdir)
  1690. (save-excursion (dired-mark-subdir-files))
  1691. (let ((inhibit-read-only t))
  1692. (dired-repeat-over-lines
  1693. (prefix-numeric-value arg)
  1694. 'my-dired-toggle-mark))))
  1695. (defun my-dired-mark-backward (arg)
  1696. "In Dired, move up lines and toggle mark there.
  1697. Optional prefix ARG says how many lines to unflag; default is one line."
  1698. (interactive "p")
  1699. (my-dired-mark (- arg)))
  1700. (add-hook 'dired-mode-hook
  1701. (lambda ()
  1702. (local-set-key (kbd "SPC") 'my-dired-mark)
  1703. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1704. )
  1705. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1706. ;; eshell
  1707. (autoload-eval-lazily 'eshell nil
  1708. (set-variable 'eshell-banner-message (format "Welcome to the Emacs shell
  1709. %s
  1710. C-x t to toggling emacs-text-mode
  1711. "
  1712. (shell-command-to-string "uname -a")
  1713. ))
  1714. (defvar eshell-text-mode-map
  1715. (let ((map (make-sparse-keymap)))
  1716. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1717. map))
  1718. (define-derived-mode eshell-text-mode text-mode
  1719. "Eshell-Text"
  1720. "Text-mode for Eshell."
  1721. nil)
  1722. (defun eshell-text-mode-toggle ()
  1723. "Toggle eshell-text-mode and eshell-mode."
  1724. (interactive)
  1725. (cond ((eq major-mode
  1726. 'eshell-text-mode)
  1727. (goto-char (point-max))
  1728. (message "Eshell text mode disabled")
  1729. (eshell-mode))
  1730. ((eq major-mode
  1731. 'eshell-mode)
  1732. (message "Eshell text mode enabled")
  1733. (eshell-write-history)
  1734. (eshell-text-mode))
  1735. (t
  1736. (message "Not in eshell buffer")
  1737. nil)))
  1738. (defun my-eshell-backward-delete-char ()
  1739. (interactive)
  1740. (when (< (save-excursion
  1741. (eshell-bol)
  1742. (point))
  1743. (point))
  1744. (backward-delete-char 1)))
  1745. (defun my-file-owner-p (file)
  1746. "t if FILE is owned by me."
  1747. (eq (user-uid) (nth 2 (file-attributes file))))
  1748. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1749. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1750. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1751. ;; (defun eshell/less (&rest args)
  1752. ;; "Invoke `view-file' on the file.
  1753. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1754. ;; (if args
  1755. ;; (while args
  1756. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1757. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1758. ;; (file (pop args)))
  1759. ;; (view-file file)
  1760. ;; (goto-line line))
  1761. ;; (view-file (pop args))))))
  1762. (defun eshell/o (&optional file)
  1763. (my-x-open (or file ".")))
  1764. ;; (defun eshell/vi (&rest args)
  1765. ;; "Invoke `find-file' on the file.
  1766. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1767. ;; (while args
  1768. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1769. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1770. ;; (file (pop args)))
  1771. ;; (find-file file)
  1772. ;; (goto-line line))
  1773. ;; (find-file (pop args)))))
  1774. (defun eshell/clear ()
  1775. "Clear the current buffer, leaving one prompt at the top."
  1776. (interactive)
  1777. (let ((inhibit-read-only t))
  1778. (erase-buffer)))
  1779. (defun eshell-clear ()
  1780. (interactive)
  1781. (let ((inhibit-read-only t))
  1782. (erase-buffer)
  1783. (insert (funcall eshell-prompt-function))))
  1784. (defun eshell/d (&optional dirname switches)
  1785. "if first arg is omitted open current directory."
  1786. (dired (or dirname ".") switches))
  1787. (defun eshell/v ()
  1788. (view-mode 1))
  1789. ;; (defun eshell/aaa (&rest args)
  1790. ;; (message "%S"
  1791. ;; args))
  1792. (defalias 'eshell/: 'ignore)
  1793. (defalias 'eshell/type 'eshell/which)
  1794. ;; (defalias 'eshell/vim 'eshell/vi)
  1795. (defalias 'eshell/ff 'find-file)
  1796. (defalias 'eshell/q 'eshell/exit)
  1797. (defun eshell-goto-prompt ()
  1798. ""
  1799. (interactive)
  1800. (goto-char (point-max)))
  1801. (defun eshell-delete-char-or-logout (n)
  1802. (interactive "p")
  1803. (if (equal (eshell-get-old-input)
  1804. "")
  1805. (progn
  1806. (insert "exit")
  1807. (eshell-send-input))
  1808. (delete-char n)))
  1809. (defun eshell-kill-input ()
  1810. (interactive)
  1811. (delete-region (point)
  1812. (progn (eshell-bol)
  1813. (point))))
  1814. (defalias 'eshell/logout 'eshell/exit)
  1815. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1816. "open eshell and change wd
  1817. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1818. (interactive)
  1819. (let ((dir (expand-file-name default-directory)))
  1820. (switch-to-buffer (or eshell-buffer-or-name
  1821. (eshell t)))
  1822. (unless (equal dir (expand-file-name default-directory))
  1823. ;; (cd dir)
  1824. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1825. ;; (eshell-emit-prompt)
  1826. (goto-char (point-max))
  1827. (eshell-kill-input)
  1828. (insert "cd " dir)
  1829. (eshell-send-input))))
  1830. (defadvice eshell-next-matching-input-from-input
  1831. ;; do not cycle history
  1832. (around eshell-history-do-not-cycle activate)
  1833. (if (= 0
  1834. (or eshell-history-index
  1835. 0))
  1836. (progn
  1837. (delete-region eshell-last-output-end (point))
  1838. (insert-and-inherit eshell-matching-input-from-input-string)
  1839. (setq eshell-history-index nil))
  1840. ad-do-it))
  1841. (set-variable 'eshell-directory-name (concat user-emacs-directory
  1842. "eshell/"))
  1843. (set-variable 'eshell-term-name "eterm-color")
  1844. (set-variable 'eshell-scroll-to-bottom-on-input t)
  1845. (set-variable 'eshell-cmpl-ignore-case t)
  1846. (set-variable 'eshell-cmpl-cycle-completions nil)
  1847. (set-variable 'eshell-highlight-prompt nil)
  1848. (if (eq system-type 'darwin)
  1849. (set-variable 'eshell-ls-initial-args '("-hCFG")
  1850. (set-variable 'eshell-ls-initial-args '("-hCFG"
  1851. "--color=auto"
  1852. "--time-style=long-iso")) ; "-hF")
  1853. ))
  1854. (set-variable 'eshell-prompt-function
  1855. 'my-eshell-prompt-function)
  1856. (defvar eshell-last-command-status)
  1857. (defun my-eshell-prompt-function()
  1858. "Prompt function.
  1859. It looks like:
  1860. :: [10sr@darwin:~/][ESHELL]
  1861. :: $
  1862. "
  1863. (concat ":: ["
  1864. (let ((str (concat user-login-name
  1865. "@"
  1866. (car (split-string system-name
  1867. "\\."))
  1868. )))
  1869. (put-text-property 0
  1870. (length str)
  1871. 'face
  1872. 'underline
  1873. str)
  1874. str)
  1875. ":"
  1876. (let ((str (abbreviate-file-name default-directory)))
  1877. (put-text-property 0
  1878. (length str)
  1879. 'face
  1880. 'underline
  1881. str)
  1882. str)
  1883. "][ESHELL]\n:: "
  1884. (if (eq 0
  1885. eshell-last-command-status)
  1886. ""
  1887. (format "[STATUS:%d] "
  1888. eshell-last-command-status))
  1889. (if (= (user-uid)
  1890. 0)
  1891. "# "
  1892. "$ ")
  1893. ))
  1894. (add-hook 'eshell-mode-hook
  1895. (lambda ()
  1896. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1897. ;; (interactive)
  1898. ;; (switch-to-buffer (other-buffer))))
  1899. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1900. ;; (interactive)
  1901. ;; (eshell-goto-prompt)
  1902. ;; (keyboard-quit)))
  1903. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1904. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1905. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1906. ;; (define-key eshell-mode-map (kbd "C-l")
  1907. ;; 'eshell-clear)
  1908. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  1909. (local-set-key (kbd "<up>") 'scroll-down-line)
  1910. (local-set-key (kbd "<down>") 'scroll-up-line)
  1911. ;; (define-key eshell-mode-map
  1912. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1913. ;; (define-key eshell-mode-map
  1914. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  1915. (apply 'eshell/addpath exec-path)
  1916. (set (make-local-variable 'scroll-margin) 0)
  1917. ;; (eshell/export "GIT_PAGER=")
  1918. ;; (eshell/export "GIT_EDITOR=")
  1919. (eshell/export "LC_MESSAGES=C")
  1920. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1921. (set (make-local-variable (defvar hl-line-range-function))
  1922. (lambda ()
  1923. '(0 . 0)))
  1924. (defvar eshell-virtual-targets)
  1925. (add-to-list 'eshell-virtual-targets
  1926. '("/dev/less"
  1927. (lambda (str)
  1928. (if str
  1929. (with-current-buffer nil)))
  1930. nil))
  1931. ))
  1932. (add-hook 'eshell-mode-hook
  1933. (lambda ()
  1934. (defvar eshell-visual-commands)
  1935. (defvar eshell-output-filter-functions)
  1936. (defvar eshell-command-aliases-list)
  1937. (add-to-list 'eshell-visual-commands "vim")
  1938. ;; (add-to-list 'eshell-visual-commands "git")
  1939. (add-to-list 'eshell-output-filter-functions
  1940. 'eshell-truncate-buffer)
  1941. (mapcar (lambda (alias)
  1942. (add-to-list 'eshell-command-aliases-list
  1943. alias))
  1944. '(
  1945. ;; ("ll" "ls -l $*")
  1946. ;; ("la" "ls -a $*")
  1947. ;; ("lla" "ls -al $*")
  1948. ("git" "git -c color.ui=always $*")
  1949. ("g" "git $*")
  1950. ("eless"
  1951. (concat "cat >>> (with-current-buffer "
  1952. "(get-buffer-create \"*eshell output\") "
  1953. "(erase-buffer) "
  1954. "(setq buffer-read-only nil) "
  1955. "(current-buffer)) "
  1956. "(view-buffer (get-buffer \"*eshell output*\"))"))
  1957. )
  1958. )))
  1959. ) ; eval after load eshell
  1960. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1961. ;; my-term
  1962. (defvar my-term nil
  1963. "My terminal buffer.")
  1964. (defvar my-term-function nil
  1965. "Function to create terminal buffer.
  1966. This function accept no argument and return newly created buffer of terminal.")
  1967. (defun my-term (&optional arg)
  1968. "Open terminal buffer and return that buffer.
  1969. If ARG is given or called with prefix argument, create new buffer."
  1970. (interactive "P")
  1971. (if (and (not arg)
  1972. my-term
  1973. (buffer-name my-term))
  1974. (pop-to-buffer my-term)
  1975. (setq my-term
  1976. (save-window-excursion
  1977. (funcall my-term-function)))
  1978. (and my-term
  1979. (my-term))))
  1980. ;; (setq my-term-function
  1981. ;; (lambda ()
  1982. ;; (if (eq system-type 'windows-nt)
  1983. ;; (eshell)
  1984. ;; (if (require 'multi-term nil t)
  1985. ;; (multi-term)
  1986. ;; (ansi-term shell-file-name)))))
  1987. (setq my-term-function (lambda () (eshell t)))
  1988. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  1989. (define-key ctl-x-map "i" 'my-term)
  1990. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1991. ;; x open
  1992. (defvar my-filer nil)
  1993. (setq my-filer (or (executable-find "pcmanfm")
  1994. (executable-find "nautilus")))
  1995. (defun my-x-open (file)
  1996. "Open FILE."
  1997. (interactive "FOpen File: ")
  1998. (setq file (expand-file-name file))
  1999. (message "Opening %s..." file)
  2000. (cond ((eq system-type 'windows-nt)
  2001. (call-process "cmd.exe" nil 0 nil
  2002. "/c" "start" "" (convert-standard-filename file)))
  2003. ((eq system-type 'darwin)
  2004. (call-process "open" nil 0 nil file))
  2005. ((getenv "DISPLAY")
  2006. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2007. (t
  2008. (find-file file))
  2009. )
  2010. ;; (recentf-add-file file)
  2011. (message "Opening %s...done" file))
  2012. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2013. ;; misc funcs
  2014. (defun my-git-apply-index-from-buffer (&optional buf)
  2015. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2016. (interactive)
  2017. (let ((buf (or buf
  2018. (current-buffer)))
  2019. (file (make-temp-file "git-apply-diff.emacs")))
  2020. (with-current-buffer buf
  2021. (write-region (point-min)
  2022. (point-max)
  2023. file)
  2024. (call-process "git"
  2025. nil
  2026. nil
  2027. nil
  2028. "apply"
  2029. "--cached"
  2030. file))))
  2031. (defun memo (&optional dir)
  2032. "Open memo.txt in DIR."
  2033. (interactive)
  2034. (pop-to-buffer (find-file-noselect (concat (if dir
  2035. (file-name-as-directory dir)
  2036. "")
  2037. "memo.txt"))))
  2038. (defvar my-rgrep-alist
  2039. `(
  2040. ;; the silver searcher
  2041. ("ag"
  2042. (executable-find "ag")
  2043. "ag --nocolor --nogroup --nopager --filename ")
  2044. ;; ack
  2045. ("ack"
  2046. (executable-find "ack")
  2047. "ack --nocolor --nogroup --nopager --with-filename ")
  2048. ;; gnu global
  2049. ("global"
  2050. (and (require 'gtags nil t)
  2051. (executable-find "global")
  2052. (gtags-get-rootpath))
  2053. "global --result grep ")
  2054. ;; git grep
  2055. ("gitgrep"
  2056. (eq 0
  2057. (shell-command "git rev-parse --git-dir"))
  2058. "git --no-pager -c color.grep=false grep -nH -e ")
  2059. ;; grep
  2060. ("grep"
  2061. t
  2062. ,(concat "find . "
  2063. "-path '*/.git' -prune -o "
  2064. "-path '*/.svn' -prune -o "
  2065. "-type f -print0 | "
  2066. "xargs -0 grep -nH -e "))
  2067. )
  2068. "Alist of rgrep command.
  2069. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2070. condition to choose COMMAND when evaluated.")
  2071. (defvar my-rgrep-default nil
  2072. "Default command name for my-rgrep.")
  2073. (defun my-rgrep-grep-command (&optional name alist)
  2074. "Return recursive grep command for current directory or nil.
  2075. If NAME is given, use that without testing.
  2076. Commands are searched from ALIST."
  2077. (if alist
  2078. (if name
  2079. ;; if name is given search that from alist and return the command
  2080. (nth 2 (assoc name
  2081. alist))
  2082. ;; if name is not given try test in 1th elem
  2083. (let ((car (car alist))
  2084. (cdr (cdr alist)))
  2085. (if (eval (nth 1 car))
  2086. ;; if the condition is true return the command
  2087. (nth 2 car)
  2088. ;; try next one
  2089. (and cdr
  2090. (my-rgrep-grep-command name cdr)))))
  2091. ;; if alist is not given set default value
  2092. (my-rgrep-grep-command name my-rgrep-alist)))
  2093. (defun my-rgrep (command-args)
  2094. "My recursive grep. Run COMMAND-ARGS."
  2095. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2096. nil)))
  2097. (if cmd
  2098. (list (read-shell-command "grep command: "
  2099. cmd
  2100. 'grep-find-history))
  2101. (error "My-Rgrep: Command for rgrep not found")
  2102. )))
  2103. (compilation-start command-args
  2104. 'grep-mode))
  2105. ;; (defun my-rgrep-symbol-at-point (command-args)
  2106. ;; "My recursive grep. Run COMMAND-ARGS."
  2107. ;; (interactive (list (read-shell-command "grep command: "
  2108. ;; (concat (my-rgrep-grep-command)
  2109. ;; " "
  2110. ;; (thing-at-point 'symbol))
  2111. ;; 'grep-find-history)))
  2112. ;; (compilation-start command-args
  2113. ;; 'grep-mode))
  2114. (defmacro define-my-rgrep (name)
  2115. "Define rgrep for NAME."
  2116. `(defun ,(intern (concat "my-rgrep-"
  2117. name)) ()
  2118. ,(format "My recursive grep by %s."
  2119. name)
  2120. (interactive)
  2121. (let ((my-rgrep-default ,name))
  2122. (if (called-interactively-p 'any)
  2123. (call-interactively 'my-rgrep)
  2124. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2125. )
  2126. (define-my-rgrep "ack")
  2127. (define-my-rgrep "ag")
  2128. (define-my-rgrep "gitgrep")
  2129. (define-my-rgrep "grep")
  2130. (define-my-rgrep "global")
  2131. (define-key ctl-x-map "s" 'my-rgrep)
  2132. ;; (defun make ()
  2133. ;; "Run \"make -k\" in current directory."
  2134. ;; (interactive)
  2135. ;; (compile "make -k"))
  2136. (defalias 'make 'compile)
  2137. (define-key ctl-x-map "c" 'compile)
  2138. (defvar sed-in-place-history nil
  2139. "History of `sed-in-place'.")
  2140. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2141. (defun sed-in-place (command)
  2142. "Issue sed in place COMMAND."
  2143. (interactive (list (read-shell-command "sed in place: "
  2144. (concat sed-in-place-command " ")
  2145. 'sed-in-place-history)))
  2146. (shell-command command
  2147. "*sed in place*"))
  2148. (defun dired-do-sed-in-place (&optional arg)
  2149. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2150. (interactive "p")
  2151. (require 'dired-aux)
  2152. (let* ((files (dired-get-marked-files t arg))
  2153. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2154. nil
  2155. 'sed-in-place
  2156. arg
  2157. files)))
  2158. (if (equal expr
  2159. "")
  2160. (error "No expression specified")
  2161. (shell-command (concat sed-in-place-command
  2162. " '"
  2163. expr
  2164. "' "
  2165. (mapconcat 'shell-quote-argument
  2166. files
  2167. " "))
  2168. "*sed in place*"))))
  2169. (defun dir-show (&optional dir)
  2170. "Show DIR list."
  2171. (interactive)
  2172. (let ((bf (get-buffer-create "*dir show*"))
  2173. (list-directory-brief-switches "-C"))
  2174. (with-current-buffer bf
  2175. (list-directory (or nil
  2176. default-directory)
  2177. nil))
  2178. ))
  2179. (defun my-convmv-sjis2utf8-test ()
  2180. "Run `convmv -r -f sjis -t utf8 *'.
  2181. this is test, does not rename files."
  2182. (interactive)
  2183. (shell-command "convmv -r -f sjis -t utf8 *"))
  2184. (defun my-convmv-sjis2utf8-notest ()
  2185. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2186. (interactive)
  2187. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2188. (defun kill-ring-save-buffer-file-name ()
  2189. "Get current filename."
  2190. (interactive)
  2191. (let ((file buffer-file-name))
  2192. (if file
  2193. (progn (kill-new file)
  2194. (message file))
  2195. (message "not visiting file."))))
  2196. (defvar kill-ring-buffer-name "*kill-ring*"
  2197. "Buffer name for `kill-ring-buffer'.")
  2198. (defun open-kill-ring-buffer ()
  2199. "Open kill- ring buffer."
  2200. (interactive)
  2201. (pop-to-buffer
  2202. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2203. (erase-buffer)
  2204. (yank)
  2205. (text-mode)
  2206. (current-local-map)
  2207. (goto-char (point-min))
  2208. (yank)
  2209. (current-buffer))))
  2210. (defun set-terminal-header (string)
  2211. "Set terminal header STRING."
  2212. (let ((savepos "\033[s")
  2213. (restorepos "\033[u")
  2214. (movecursor "\033[0;%dH")
  2215. (inverse "\033[7m")
  2216. (restorecolor "\033[0m")
  2217. (cols (frame-parameter nil 'width))
  2218. (length (length string)))
  2219. ;; (redraw-frame (selected-frame))
  2220. (send-string-to-terminal (concat savepos
  2221. (format movecursor
  2222. (1+ (- cols length)))
  2223. inverse
  2224. string
  2225. restorecolor
  2226. restorepos))
  2227. ))
  2228. (defun my-set-terminal-header ()
  2229. "Set terminal header."
  2230. (set-terminal-header (concat " "
  2231. user-login-name
  2232. "@"
  2233. (car (split-string system-name
  2234. "\\."))
  2235. " "
  2236. (format-time-string "%Y/%m/%d %T %z")
  2237. " ")))
  2238. ;; (run-with-timer
  2239. ;; 0.1
  2240. ;; 1
  2241. ;; 'my-set-terminal-header)
  2242. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2243. ;; ;; savage emacs
  2244. ;; ;; when enabled emacs fails to complete
  2245. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2246. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2247. ;; (setq arg
  2248. ;; (concat arg
  2249. ;; (if (eq nil
  2250. ;; (string-match "\\. *$"
  2251. ;; arg))
  2252. ;; ".")
  2253. ;; " Stupid!")))
  2254. ;; TODO: make these a library
  2255. (defvar info-in-prompt
  2256. nil
  2257. "System info in the form of \"[user@host] \".")
  2258. (setq info-in-prompt
  2259. (concat "["
  2260. user-login-name
  2261. "@"
  2262. (car (split-string system-name
  2263. "\\."))
  2264. "]"))
  2265. (defun my-real-function-subr-p (function)
  2266. "Return t if FUNCTION is a built-in function even if it is advised."
  2267. (let* ((advised (and (symbolp function)
  2268. (featurep 'advice)
  2269. (ad-get-advice-info function)))
  2270. (real-function
  2271. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2272. (and (fboundp origname)
  2273. origname)))
  2274. function))
  2275. (def (if (symbolp real-function)
  2276. (symbol-function real-function)
  2277. function)))
  2278. (subrp def)))
  2279. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2280. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2281. ;; "Show system info when use `read-from-minibuffer'."
  2282. ;; (ad-set-arg 0
  2283. ;; (concat my-system-info
  2284. ;; (ad-get-arg 0))))
  2285. ;; (defadvice read-string (before info-in-prompt activate)
  2286. ;; "Show system info when use `read-string'."
  2287. ;; (ad-set-arg 0
  2288. ;; (concat my-system-info
  2289. ;; (ad-get-arg 0))))
  2290. ;; (when (< emacs-major-version 24)
  2291. ;; (defadvice completing-read (before info-in-prompt activate)
  2292. ;; "Show system info when use `completing-read'."
  2293. ;; (ad-set-arg 0
  2294. ;; (concat my-system-info
  2295. ;; (ad-get-arg 0)))))
  2296. (defmacro info-in-prompt-set (&rest functions)
  2297. "Set info-in-prompt advices for FUNCTIONS."
  2298. `(progn
  2299. ,@(mapcar (lambda (f)
  2300. `(defadvice ,f (before info-in-prompt activate)
  2301. "Show info in prompt."
  2302. (let ((orig (ad-get-arg 0)))
  2303. (unless (string-match-p (regexp-quote info-in-prompt)
  2304. orig)
  2305. (ad-set-arg 0
  2306. (concat info-in-prompt
  2307. " "
  2308. orig))))))
  2309. functions)))
  2310. (info-in-prompt-set read-from-minibuffer
  2311. read-string
  2312. completing-read)
  2313. ;;; emacs.el ends here