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.

emacs.el 64 KiB

13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. ;; 外部ファイル読み込み
  2. ;; (load-file "~/filepath")
  3. ;; (thing-at-point 'word)
  4. ;; (define-generic-mode)
  5. (unless (file-directory-p (expand-file-name "~/.emacs.d"))
  6. (make-directory (expand-file-name "~/.emacs.d")))
  7. (unless (file-directory-p (expand-file-name "~/.emacs.d/lisp"))
  8. (make-directory (expand-file-name "~/.emacs.d/lisp")))
  9. (add-to-list 'load-path "~/.emacs.d/lisp")
  10. (require 'cl nil t)
  11. (progn ; hook run when directory changed
  12. (defvar buffer-file-changed-function nil "Hook run when buffer file changed.
  13. Each function is called with two args, the filename before changing and after changing.")
  14. (declare-function run-buffer-file-change-function "emacs.el")
  15. (add-hook 'post-command-hook
  16. 'run-buffer-file-changed-function)
  17. (lexical-let (previous-file)
  18. (defun run-buffer-file-changed-function ()
  19. ""
  20. (unless (and previous-file
  21. (equal previous-file
  22. (expand-file-name (or buffer-file-name
  23. default-directory))))
  24. (let ((pfile previous-file)
  25. (cfile (expand-file-name (or buffer-file-name
  26. default-directory))))
  27. (setq previous-file cfile)
  28. (run-hook-with-args 'directory-changed-function pfile cfile)))))
  29. ;; (add-hook 'directory-changed-function
  30. ;; (lambda (pdir cdir)
  31. ;; (message "dir changed %s to %s !" pdir cdir)))
  32. )
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (require 'url)
  36. (defun dllib-if-unfound (lib url &optional bite-compile-p force-download-p)
  37. "if LIB does not exist, download it from URL and locate it to \"~/emacs.d/lisp/LIB.el\".
  38. return nil if LIB unfound and downloading failed, otherwise the path of LIB."
  39. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  40. (lpath (concat dir lib ".el"))
  41. (locate-p (locate-library lib)))
  42. (if (or force-download-p (not locate-p))
  43. (progn (condition-case nil
  44. (progn (message "downloading %s..." url)
  45. (url-copy-file url
  46. lpath
  47. t)
  48. (when bite-compile-p
  49. (byte-compile-file lpath)))
  50. (error (and (file-readable-p lpath)
  51. (delete-file lpath))
  52. (message "downloading %s...something wrong happened!" url)
  53. nil))
  54. (locate-library lib))
  55. locate-p)))
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;; start and quit
  58. (setq inhibit-startup-message t)
  59. (setq frame-title-format (list '(:eval (format-time-string (or display-time-format "")))
  60. " | %b "
  61. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  62. " buffers ["
  63. invocation-name
  64. " "
  65. emacs-version
  66. " "
  67. (symbol-name system-type)
  68. "] "
  69. '(:eval (symbol-name last-command))))
  70. (defun set-terminal-title (&rest args)
  71. ""
  72. (interactive "sString to set as title: ")
  73. (let ((tty (frame-parameter nil
  74. 'tty-type)))
  75. (when (and tty
  76. (eq t (compare-strings "xterm" 0 5
  77. tty 0 5)))
  78. (send-string-to-terminal (apply 'concat
  79. "\033]0;"
  80. `(,@args "\007"))))))
  81. (defun my-set-terminal-title ()
  82. ""
  83. (set-terminal-title "["
  84. invocation-name
  85. " "
  86. emacs-version
  87. " "
  88. (symbol-name system-type)
  89. "] "
  90. (abbreviate-file-name (or buffer-file-name default-directory))))
  91. (add-hook 'directory-changed-function
  92. (lambda (p c)
  93. (my-set-terminal-title)))
  94. (add-hook 'suspend-resume-hook
  95. 'my-set-terminal-title)
  96. (defun buffer-list-not-start-with-space ()
  97. (let ((bl (buffer-list))
  98. b nbl)
  99. (while bl
  100. (setq b (pop bl))
  101. (unless (string-equal " "
  102. (substring (buffer-name b)
  103. 0
  104. 1))
  105. (add-to-list 'nbl b)))
  106. nbl))
  107. (setq confirm-kill-emacs 'y-or-n-p)
  108. (setq gc-cons-threshold (* 1024 1024 4))
  109. (when window-system
  110. (add-to-list 'default-frame-alist '(cursor-type . box))
  111. (add-to-list 'default-frame-alist '(background-color . "white"))
  112. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  113. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ;聞いてないみたい
  114. )
  115. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  116. (tool-bar-mode 0)
  117. (set-scroll-bar-mode nil)
  118. (add-hook 'kill-emacs-hook ; 終了時に読み込んで壊れてないか調べる
  119. (lambda ()
  120. (when (file-readable-p "~/.emacs")
  121. (load-file "~/.emacs"))
  122. ))
  123. (add-hook 'after-init-hook
  124. (lambda ()
  125. (message (emacs-init-time))
  126. (switch-to-buffer "*Messages*")
  127. ))
  128. (cd ".") ; when using windows use / instead of \ in `default-directory'
  129. ;; locale
  130. (set-language-environment "Japanese")
  131. (set-default-coding-systems 'utf-8-unix)
  132. (prefer-coding-system 'utf-8-unix)
  133. (setq system-time-locale "C")
  134. ;; my prefix map
  135. (define-prefix-command 'my-prefix-map)
  136. (add-hook 'after-init-hook
  137. (lambda ()
  138. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  139. ))
  140. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  141. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  142. ;; (comint-show-maximum-output)
  143. ;; kill scratch
  144. (add-hook 'after-init-hook
  145. (lambda ()
  146. (kill-buffer "*scratch*")))
  147. ;; modifier keys
  148. (setq mac-option-modifier 'control)
  149. (setq w32-apps-modifier 'meta)
  150. ;; display
  151. (setq redisplay-dont-pause t)
  152. (setq visible-bell t)
  153. (setq ring-bell-function 'ignore)
  154. (mouse-avoidance-mode 'banish)
  155. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  156. ;; global keys
  157. (define-key my-prefix-map (kbd "C-o") 'occur)
  158. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  159. (global-set-key (kbd "C-\\") help-map)
  160. (define-key ctl-x-map (kbd "DEL") help-map)
  161. (define-key ctl-x-map (kbd "C-h") help-map)
  162. (define-key help-map "a" 'apropos)
  163. ;; compose window
  164. (global-set-key [?\C--] 'other-window)
  165. (global-set-key [?\C-0] 'delete-window)
  166. (global-set-key [?\C-1] 'delete-other-windows)
  167. (global-set-key [?\C-2] 'split-window-vertically)
  168. (global-set-key [?\C-3] 'split-window-horizontally)
  169. ;; disable annoying keys
  170. (global-set-key [prior] 'ignore)
  171. (global-set-key (kbd "<next>") 'ignore)
  172. (global-set-key [menu] 'ignore)
  173. (global-set-key [down-mouse-1] 'ignore)
  174. (global-set-key [down-mouse-2] 'ignore)
  175. (global-set-key [down-mouse-3] 'ignore)
  176. (global-set-key [mouse-1] 'ignore)
  177. (global-set-key [mouse-2] 'ignore)
  178. (global-set-key [mouse-3] 'ignore)
  179. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  180. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;; mode-line
  183. (setq eol-mnemonic-dos "crlf")
  184. (setq eol-mnemonic-mac "cr")
  185. (setq eol-mnemonic-unix "lf")
  186. (which-function-mode 0)
  187. (line-number-mode 0)
  188. (column-number-mode 0)
  189. (size-indication-mode 0)
  190. (setq mode-line-position
  191. '(:eval (format "L%%l/%d,C%%c"
  192. (count-lines (point-max)
  193. (point-min)))))
  194. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  195. ;; display date
  196. (add-hook 'after-init-hook
  197. (lambda ()
  198. (when display-time-mode
  199. (display-time-update))
  200. ))
  201. (setq display-time-interval 29)
  202. (setq display-time-day-and-date t)
  203. (setq display-time-format "%a, %d %b %Y %T")
  204. (if window-system
  205. (display-time-mode 0)
  206. (display-time-mode 1))
  207. ;; ;; current directory
  208. ;; (let ((ls (member 'mode-line-buffer-identification
  209. ;; mode-line-format)))
  210. ;; (setcdr ls
  211. ;; (cons '(:eval (concat " ("
  212. ;; (abbreviate-file-name default-directory)
  213. ;; ")"))
  214. ;; (cdr ls))))
  215. ;; ;; display last modified time
  216. ;; (let ((ls (member 'mode-line-buffer-identification
  217. ;; mode-line-format)))
  218. ;; (setcdr ls
  219. ;; (cons '(:eval (concat " "
  220. ;; my-buffer-file-last-modified-time))
  221. ;; (cdr ls))))
  222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  223. ;; minibuffer
  224. (setq insert-default-directory t)
  225. (setq completion-ignore-case t
  226. read-file-name-completion-ignore-case t
  227. read-buffer-completion-ignore-case t)
  228. (setq resize-mini-window t)
  229. (temp-buffer-resize-mode 1)
  230. (savehist-mode 1)
  231. (fset 'yes-or-no-p 'y-or-n-p)
  232. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  233. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  234. ;; letters, font-lock mode and fonts
  235. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  236. (when (eq system-type 'Darwin)
  237. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  238. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  239. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  240. (boundp 'input-method-inactivate-hook))
  241. (add-hook 'input-method-activate-hook
  242. (lambda () (set-cursor-color "red")))
  243. (add-hook 'input-method-inactivate-hook
  244. (lambda () (set-cursor-color "black"))))
  245. (show-paren-mode 1)
  246. (setq show-paren-style 'mixed)
  247. (transient-mark-mode 1)
  248. (global-font-lock-mode 1)
  249. (setq font-lock-global-modes
  250. '(not
  251. help-mode
  252. eshell-mode
  253. term-mode
  254. Man-mode))
  255. (standard-display-ascii ?\n "$\n")
  256. (standard-display-ascii ?\f "---------------------------------------------------------------------------------------^L")
  257. (defface my-pagebreak-face
  258. '((t (:foreground "gray")))
  259. "pagebreak.")
  260. (defvar my-eol-face
  261. '(("\n" . (0 font-lock-comment-face t nil)))
  262. )
  263. (defvar my-pagebreak-face
  264. '(("\f" . 'my-pagebreak-face)))
  265. (defvar my-highlight-face
  266. '(("\t" . '(0 highlight t nil))
  267. (" " . '(0 highlight t nil))))
  268. ;; highlight current line
  269. ;; http://wiki.riywo.com/index.php?Meadow
  270. (defface hlline-face
  271. '((((type x w32)
  272. (class color)
  273. (background dark))
  274. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  275. (((type x w32)
  276. (class color)
  277. (background light))
  278. (:background "gainsboro"))
  279. (t
  280. (:underline "black")))
  281. "*Face used by hl-line.")
  282. ;; (defface hlline-ul-face
  283. ;; '((t (:underline "yellow")))
  284. ;; "underline yellow")
  285. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  286. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  287. (setq hl-line-global-modes
  288. '(not
  289. term-mode))
  290. (add-hook 'font-lock-mode-hook
  291. (lambda ()
  292. (font-lock-add-keywords nil my-eol-face)
  293. ;; (font-lock-add-keywords nil my-highlight-face)
  294. ))
  295. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  296. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  297. ;; fonts
  298. (defun my-set-ascii-and-jp-font-with-size (list)
  299. ""
  300. (if (> emacs-major-version 22)
  301. (progn ; 23 or later
  302. (set-face-attribute 'default nil
  303. :family (nth 0 list)
  304. :height (nth 1 list))
  305. (set-fontset-font "fontset-default"
  306. 'japanese-jisx0208
  307. (font-spec :family (nth 2 list) :size (nth 3 list)))
  308. (set-fontset-font "fontset-default"
  309. 'katakana-jisx0201
  310. (font-spec :family (nth 2 list) :size (nth 3 list))))
  311. ; font spec is available in emacs23 and later, cannot used in emacs22
  312. (progn ; 22
  313. (set-face-attribute 'default nil
  314. :family (nth 0 list)
  315. :height (nth 1 list))
  316. (set-fontset-font "fontset-default"
  317. 'japanese-jisx0208
  318. (cons (nth 2 list) "jisx0208.*"))
  319. (set-fontset-font "fontset-default"
  320. 'katakana-jisx0201
  321. (cons (nth 2 list) "jisx0201.*"))
  322. )))
  323. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  324. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  325. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  326. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  327. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  328. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  329. ;; あ a
  330. (and (dllib-if-unfound "set-modeline-color"
  331. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  332. t)
  333. (require 'set-modeline-color nil t))
  334. ;; (set-face-foreground 'mode-line-inactive (if window-system "gray" "white"))
  335. (set-face-background 'mode-line-inactive (if window-system "white" "green"))
  336. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  337. ;; file handling
  338. (setq revert-without-query '(".+"))
  339. ;; カーソルの場所を保存する
  340. (setq save-place-file (concat user-emacs-directory
  341. "places"))
  342. (when (require 'saveplace nil t)
  343. (setq-default save-place t))
  344. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  345. (setq make-backup-files t)
  346. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  347. (setq backup-directory-alist
  348. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  349. backup-directory-alist))
  350. (setq version-control 'never)
  351. (setq delete-old-versions t)
  352. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  353. (setq delete-auto-save-files t)
  354. (add-to-list 'completion-ignored-extensions ".bak")
  355. ;; (setq delete-by-moving-to-trash t
  356. ;; trash-directory "~/.emacs.d/trash")
  357. (add-hook 'after-save-hook
  358. 'executable-make-buffer-file-executable-if-script-p)
  359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360. ;; editting
  361. (setq require-final-newline t)
  362. (setq kill-whole-line t)
  363. (setq scroll-conservatively 35
  364. scroll-margin 2
  365. scroll-step 0)
  366. (setq default-major-mode 'text-mode)
  367. (setq next-line-add-newlines nil)
  368. (setq kill-read-only-ok t)
  369. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  370. ;; (setq-default line-spacing 0.2)
  371. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  372. (setq-default tab-width 4)
  373. (setq-default indent-tabs-mode nil)
  374. (setq-default indent-line-function nil)
  375. (pc-selection-mode 1)
  376. (delete-selection-mode 1)
  377. (cua-mode 0)
  378. ;; key bindings
  379. ;; moving around
  380. ;; (global-set-key (kbd "M-j") 'next-line)
  381. ;; (global-set-key (kbd "M-k") 'previous-line)
  382. ;; (global-set-key (kbd "M-h") 'backward-char)
  383. ;; (global-set-key (kbd "M-l") 'forward-char)
  384. ;;(keyboard-translate ?\M-j ?\C-j)
  385. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  386. (define-key esc-map "p" 'backward-paragraph)
  387. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  388. (define-key esc-map "n" 'forward-paragraph)
  389. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  390. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  391. (global-set-key (kbd "C-<left>") 'scroll-down)
  392. (global-set-key (kbd "C-<right>") 'scroll-up)
  393. (global-set-key (kbd "<select>") 'previous-line-mark)
  394. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  395. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  396. ;; C-h and DEL
  397. ;(global-set-key (kbd "C-h") (kbd "DEL"))
  398. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  399. (global-set-key (kbd "C-o")
  400. ;; (lambda ()
  401. ;; (interactive)
  402. ;; (move-end-of-line nil)
  403. ;; (newline-and-indent))
  404. (kbd "C-e C-m")
  405. )
  406. (global-set-key (kbd "C-k") 'kill-whole-line)
  407. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  408. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  409. (global-set-key (kbd "M-u") 'undo)
  410. (global-set-key (kbd "C-r") 'query-replace-regexp)
  411. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  412. (global-set-key (kbd "M-i") (kbd "ESC TAB"))
  413. '(and (dllib-if-unfound "drill-instructor"
  414. "https://raw.github.com/k1LoW/emacs-drill-instructor/master/drill-instructor.el"
  415. t)
  416. (require 'drill-instructor nil t)
  417. (setq drill-instructor-global t))
  418. (mapc (lambda (key)
  419. (global-set-key (read-kbd-macro key) 'ignore))
  420. '("<up>" "<down>" "<right>" "<left>"))
  421. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  422. ;; gmail
  423. (setq mail-interactive t
  424. send-mail-function 'smtpmail-send-it
  425. ;; message-send-mail-function 'smtpmail-send-it
  426. smtpmail-smtp-server "smtp.gmail.com"
  427. smtpmail-smtp-service 587
  428. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  429. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  430. user-mail-address "8.slashes@gmail.com")
  431. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  432. ;; buffer killing
  433. (defun my-query-kill-this-buffer ()
  434. ""
  435. (interactive)
  436. (if (y-or-n-p (concat "kill this buffer? :"))
  437. (kill-buffer (current-buffer))))
  438. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  439. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  440. '(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
  441. ("gnu" . "http://elpa.gnu.org/packages/")
  442. ("marmalade" . "http://marmalade-repo.org/packages/")))
  443. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  444. ;;requireが必要なelispおよびhook
  445. (require 'simple nil t)
  446. (and window-system
  447. (dllib-if-unfound "save-window-size" "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el" t)
  448. (require 'save-window-size nil t))
  449. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  450. ;; share clipboard with x
  451. (when (and window-system
  452. (not (eq window-system 'mac))
  453. )
  454. (setq x-select-enable-clipboard t ; these settings seems to be useless when using emacs in terminal
  455. x-select-enable-primary nil)
  456. ;; (global-set-key "\C-y" 'x-clipboard-yank)
  457. )
  458. ;; urlに細かい説明あり。でも設定は上記だけでよさそう
  459. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  460. (and (not x-select-enable-clipboard)
  461. (getenv "DISPLAY")
  462. (executable-find "xclip")
  463. (dllib-if-unfound "xclip" "http://www.emacswiki.org/emacs/download/xclip.el" t)
  464. (require 'xclip nil t)
  465. (turn-on-xclip))
  466. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  467. ;; mode
  468. (add-hook 'verilog-mode-hook
  469. (lambda ()
  470. (define-key verilog-mode-map ";" 'self-insert-command)))
  471. (setq diff-switches "-u")
  472. (add-hook 'diff-mode-hook
  473. (lambda ()
  474. (view-mode 1)
  475. (set-face-foreground 'diff-file-header-face nil)
  476. (set-face-foreground 'diff-header-face nil)
  477. (set-face-foreground 'diff-index-face "blue")
  478. (set-face-foreground 'diff-hunk-header-face "cyan")
  479. (set-face-foreground 'diff-context-face nil)
  480. (set-face-foreground 'diff-removed-face "red")
  481. (set-face-foreground 'diff-added-face "green")
  482. (set-face-foreground 'diff-changed-face "magenda")
  483. ))
  484. ;; (ffap-bindings)
  485. (add-hook 'sh-mode-hook
  486. (lambda ()
  487. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  488. (defun my-execute-shell-command-current-line ()
  489. ""
  490. (interactive)
  491. (shell-command (buffer-substring-no-properties (point-at-bol)
  492. (point))))
  493. (add-to-list 'auto-mode-alist
  494. '("\\(xinitrc\\|xprograms\\)\\'" . sh-mode))
  495. (setq python-python-command (or (executable-find "python3")
  496. (executable-find "python")))
  497. (defun my-python-run-as-command ()
  498. ""
  499. (interactive)
  500. (shell-command (concat python-python-command " " buffer-file-name)))
  501. (defun my-python-display-python-buffer ()
  502. ""
  503. (interactive)
  504. (set-window-text-height (display-buffer python-buffer
  505. t)
  506. 7))
  507. (add-hook 'python-mode-hook
  508. (lambda ()
  509. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  510. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  511. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  512. (add-hook 'inferior-python-mode-hook
  513. (lambda ()
  514. (my-python-display-python-buffer)
  515. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  516. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  517. (add-hook 'text-mode-hook
  518. (lambda ()
  519. (define-key text-mode-map (kbd "C-m") 'newline)))
  520. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  521. (setq bookmark-default-file "~/.emacs.d/bmk")
  522. (add-hook 'apropos-mode-hook
  523. (lambda ()
  524. (define-key apropos-mode-map "j" 'next-line)
  525. (define-key apropos-mode-map "k" 'previous-line)))
  526. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point))))
  527. (add-hook 'isearch-mode-hook
  528. (lambda ()
  529. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  530. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  531. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  532. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  533. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  534. (add-hook 'outline-mode-hook
  535. (lambda ()
  536. (if (string-match "\\.md\\'" buffer-file-name)
  537. (set (make-local-variable 'outline-regexp) "#+ "))))
  538. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  539. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  540. (setq markdown-command (or (executable-find "markdown")
  541. (executable-find "markdown.pl")))
  542. (when (dllib-if-unfound "markdown-mode"
  543. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  544. t)
  545. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  546. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  547. (add-hook 'markdown-mode-hook
  548. (lambda ()
  549. (outline-minor-mode 1)
  550. (set (make-local-variable 'comment-start) ";"))))
  551. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  552. ;; c-mode
  553. ;; (setq c-default-style "bsd")
  554. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  555. (add-hook 'c-mode-common-hook
  556. (lambda ()
  557. (setq c-basic-offset 2
  558. indent-tabs-mode nil)
  559. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  560. (c-toggle-hungry-state 1)
  561. ))
  562. (when (dllib-if-unfound "js2-mode"
  563. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  564. t)
  565. (autoload 'js2-mode "js2-mode" nil t)
  566. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  567. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode)))
  568. ;; (add-hook 'js2-mode-hook
  569. ;; (lambda ()
  570. ;; (add-hook 'before-save-hook
  571. ;; 'my-indent-buffer
  572. ;; nil
  573. ;; t)))
  574. (add-hook 'js2-mode-hook
  575. (lambda ()
  576. (define-key js2-mode-map (kbd "C-m") (lambda ()
  577. (interactive)
  578. (js2-enter-key)
  579. (indent-for-tab-command)))
  580. (add-hook (kill-local-variable 'before-save-hook)
  581. 'js2-before-save)))
  582. (and nil
  583. (require 'zone nil t)
  584. (not (eq system-type 'windows-nt))
  585. ;; (zone-when-idle 180)
  586. (run-with-idle-timer 180 t (lambda ()
  587. (unless (memq major-mode
  588. '(term-mode))
  589. (zone)))))
  590. (when (require 'uniquify nil t)
  591. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  592. (setq uniquify-ignore-buffers-re "*[^*]+*")
  593. (setq uniquify-min-dir-content 1))
  594. (add-hook 'view-mode-hook
  595. (lambda()
  596. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  597. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  598. (define-key view-mode-map "/" 'isearch-forward)
  599. (define-key view-mode-map "v" 'toggle-read-only)
  600. (define-key view-mode-map "q" 'bury-buffer)))
  601. (global-set-key "\M-r" 'view-mode)
  602. (setq view-read-only t)
  603. (add-hook 'Man-mode-hook
  604. (lambda ()
  605. (view-mode 1)
  606. (setq truncate-lines nil)))
  607. (setq Man-notify-method (if window-system
  608. 'newframe
  609. 'pushy))
  610. (require 'session nil t)
  611. (and (dllib-if-unfound "gtkbm"
  612. "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  613. t)
  614. (require 'gtkbm nil t)
  615. (global-set-key (kbd "C-x C-d") 'gtkbm))
  616. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  617. ;; term mode
  618. ;; (setq multi-term-program shell-file-name)
  619. (and (dllib-if-unfound "multi-term"
  620. "http://www.emacswiki.org/emacs/download/multi-term.el"
  621. t)
  622. (require 'multi-term nil t)
  623. (setq multi-term-switch-after-close nil))
  624. (defun my-term-quit-or-send-raw ()
  625. ""
  626. (interactive)
  627. (if (get-buffer-process (current-buffer))
  628. (call-interactively 'term-send-raw)
  629. (kill-buffer)))
  630. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  631. ;; (setq term-ansi-default-program shell-file-name)
  632. (add-hook 'term-setup-hook
  633. (lambda ()
  634. (setq term-display-table (make-display-table))))
  635. (add-hook 'term-mode-hook
  636. (lambda ()
  637. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  638. (multi-term-list)))
  639. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  640. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  641. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  642. ;; (define-key term-raw-map "\C-f" 'forward-char)
  643. ;; (define-key term-raw-map "\C-b" 'backward-char)
  644. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  645. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  646. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  647. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  648. (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  649. (define-key term-raw-map [delete] 'term-send-raw)
  650. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  651. (define-key term-raw-map "\C-y" 'term-paste)
  652. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  653. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  654. ;; (define-key term-raw-map (kbd key) 'term-send-raw))
  655. ;; (define-key term-raw-map "\C-d" 'delete-char)
  656. (set (make-local-variable 'scroll-margin) 0)
  657. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  658. ;; (cua-mode 0)
  659. ;; (and cua-mode
  660. ;; (local-unset-key (kbd "C-c")))
  661. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  662. (set (make-local-variable 'hl-line-range-function)
  663. (lambda ()
  664. '(0 . 0)))
  665. ))
  666. ;; (add-hook 'term-exec-hook 'forward-char)
  667. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  668. ;; buffer switching
  669. (when (require 'bs nil t)
  670. ;; (global-set-key "\C-x\C-b" 'bs-show)
  671. (defalias 'list-buffers 'bs-show))
  672. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  673. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  674. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  675. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  676. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  677. (setq bs-default-configuration "this-frame")
  678. (setq bs-default-sort-name "by name")
  679. (add-hook 'bs-mode-hook
  680. (lambda ()
  681. (setq bs-default-configuration "this-frame")
  682. ;; (and bs--show-all
  683. ;; (call-interactively 'bs-toggle-show-all))
  684. (set (make-local-variable 'scroll-margin) 0)
  685. ))
  686. (defun buffer-same-dir-p (bf)
  687. "return t if BF's dir is same as current dir, otherwise nil."
  688. (let ((cdir (expand-file-name default-directory)))
  689. (with-current-buffer bf
  690. (equal (expand-file-name default-directory) cdir))))
  691. (iswitchb-mode 1)
  692. (defun iswitchb-buffer-display-other-window ()
  693. ""
  694. (interactive)
  695. (let ((iswitchb-default-method 'display))
  696. (call-interactively 'iswitchb-buffer)))
  697. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  698. ;; sdic
  699. (defun sdic-describe-word-at-point-echo ()
  700. ""
  701. (interactive)
  702. (save-window-excursion
  703. (sdic-describe-word-at-point))
  704. (save-excursion
  705. (set-buffer sdic-buffer-name)
  706. (message (buffer-substring (point-min)
  707. (progn (goto-char (point-min))
  708. (or (and (re-search-forward "^\\w" nil t 4)
  709. (progn (previous-line) t)
  710. (point-at-eol))
  711. (point-max)))))))
  712. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  713. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  714. (setq sdic-disable-select-window t)
  715. (setq sdic-window-height 7)
  716. (when (require 'sdic nil t)
  717. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  718. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  719. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  720. ;; vc
  721. ;; (require 'vc)
  722. (setq vc-handled-backends '())
  723. (and (executable-find "git")
  724. (add-to-list 'vc-handled-backends 'GIT))
  725. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  726. ;; gauche-mode
  727. (let ((s (executable-find "gosh")))
  728. (setq scheme-program-name s
  729. gauche-program-name s))
  730. (defun run-gauche-other-window ()
  731. "Run gauche on other window"
  732. (interactive)
  733. (switch-to-buffer-other-window
  734. (get-buffer-create "*scheme*"))
  735. (run-gauche))
  736. (defun run-gauche ()
  737. "run gauche"
  738. (run-scheme gauche-program-name)
  739. )
  740. (defun scheme-send-buffer ()
  741. ""
  742. (interactive)
  743. (scheme-send-region (point-min) (point-max))
  744. (my-scheme-display-scheme-buffer)
  745. )
  746. (defun my-scheme-display-scheme-buffer ()
  747. ""
  748. (interactive)
  749. (set-window-text-height (display-buffer scheme-buffer
  750. t)
  751. 7))
  752. (add-hook 'scheme-mode-hook
  753. (lambda ()
  754. nil))
  755. (add-hook 'inferior-scheme-mode-hook
  756. (lambda ()
  757. ;; (my-scheme-display-scheme-buffer)
  758. ))
  759. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  760. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  761. (when (dllib-if-unfound "gauche-mode"
  762. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  763. t)
  764. (setq auto-mode-alist
  765. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  766. (setq auto-mode-alist
  767. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  768. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  769. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  770. (add-hook 'gauche-mode-hook
  771. (lambda ()
  772. (define-key gauche-mode-map (kbd "C-c C-z") 'run-gauche-other-window)
  773. (define-key scheme-mode-map (kbd "C-c C-c") 'scheme-send-buffer)
  774. (define-key scheme-mode-map (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)
  775. )))
  776. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  777. ;; recentf-mode
  778. ;; (add-hook 'recentf-dialog-mode-hook
  779. ;; 'my-recentf-abbrev-list)
  780. (defun my-recentf-delete-entry ()
  781. ""
  782. (interactive)
  783. (let ((p (point)))
  784. (setq recentf-list
  785. (delete (my-recentf-get-filename) recentf-list))
  786. (recentf-open-files)
  787. (goto-char p)))
  788. (defun my-recentf-abbrev-list ()
  789. ""
  790. (setq recentf-list
  791. (mapcar 'abbreviate-file-name
  792. recentf-list)))
  793. (defun my-recentf-view-file ()
  794. ""
  795. (interactive)
  796. (view-file (my-recentf-get-filename)))
  797. (defun my-recentf-dired ()
  798. ""
  799. (interactive)
  800. (let ((file (my-recentf-get-filename)))
  801. (if (file-directory-p file)
  802. (dired file)
  803. (dired (or (file-name-directory file)
  804. ".")))))
  805. (defun my-recentf-x-open ()
  806. ""
  807. (interactive)
  808. (my-x-open (my-recentf-get-filename)))
  809. (defun my-recentf-cd-and-find-file ()
  810. ""
  811. (interactive)
  812. (cd (file-name-directory (my-recentf-get-filename)))
  813. (call-interactively 'find-file))
  814. (defun my-recentf-get-filename ()
  815. "get file name in recentf-mode"
  816. (replace-regexp-in-string " \\(\\[.+?\\] \\)?" ; " " or " [\\d] "
  817. ""
  818. (buffer-substring-no-properties (point-at-bol)
  819. (point-at-eol))))
  820. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  821. recentf-max-menu-items 20
  822. recentf-max-saved-items 30
  823. recentf-show-file-shortcuts-flag nil)
  824. (defun my-recentf-pop-to-buffer ()
  825. ""
  826. (interactive)
  827. (let ((bf (save-excursion
  828. (recentf-open-files))))
  829. (pop-to-buffer bf)))
  830. (when (require 'recentf nil t)
  831. (global-set-key "\C-x\C-r" 'recentf-open-files)
  832. ;; (add-hook 'find-file-hook
  833. ;; (lambda ()
  834. ;; (recentf-add-file default-directory)))
  835. (recentf-mode 1)
  836. ;; (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  837. (add-to-list 'recentf-exclude (rx-to-string recentf-save-file)))
  838. (add-hook 'recentf-dialog-mode-hook
  839. (lambda ()
  840. (recentf-save-list)
  841. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  842. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  843. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  844. (define-key recentf-dialog-mode-map "o" 'my-recentf-x-open)
  845. (define-key recentf-dialog-mode-map "d" 'my-recentf-delete-entry)
  846. (define-key recentf-dialog-mode-map "@" 'my-recentf-dired)
  847. (define-key recentf-dialog-mode-map "v" 'my-recentf-view-file)
  848. (cd "~/")))
  849. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  850. ;; dired
  851. (require 'dired)
  852. (defun my-dired-echo-file-head (arg)
  853. ""
  854. (interactive "P")
  855. (let ((f (dired-get-filename)))
  856. (message "%s"
  857. (with-temp-buffer
  858. (insert-file-contents f)
  859. (buffer-substring-no-properties (point-min)
  860. (progn (goto-line (if arg
  861. (prefix-numeric-value arg)
  862. 10))
  863. (point-at-eol)))))))
  864. (defun my-dired-diff ()
  865. ""
  866. (interactive)
  867. (let ((files (dired-get-marked-files nil nil nil t)))
  868. (if (eq (car files)
  869. t)
  870. (diff (cadr files) (dired-get-filename))
  871. (message "One files must be marked!"))))
  872. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  873. "pop up buffer using `display-buffer' and return that buffer."
  874. (let ((bf (get-buffer-create buffer-or-name)))
  875. (with-current-buffer bf
  876. (cd ".")
  877. (erase-buffer))
  878. (display-buffer bf)
  879. bf))
  880. (defun my-replace-nasi-none ()
  881. ""
  882. (save-excursion
  883. (let ((buffer-read-only nil))
  884. (goto-char (point-min))
  885. (while (search-forward "なし" nil t)
  886. (replace-match "none")))))
  887. (defun dired-get-file-info ()
  888. "dired get disk usage"
  889. (interactive)
  890. (let ((f (dired-get-filename)))
  891. (if (file-directory-p f)
  892. (progn
  893. (message "calculating du...")
  894. (shell-command (concat "du -hsD "
  895. f)))
  896. (shell-command (concat "file "
  897. f)))))
  898. (defun my-dired-scroll-up ()
  899. ""
  900. (interactive)
  901. (my-dired-previous-line (- (window-height) 1)))
  902. (defun my-dired-scroll-down ()
  903. ""
  904. (interactive)
  905. (my-dired-next-line (- (window-height) 1)))
  906. (defun my-dired-previous-line (arg)
  907. ""
  908. (interactive "p")
  909. (when (> arg 0)
  910. ;; (ignore 'my-dired-print-current-dir-and-file)
  911. (dired-previous-line 1)
  912. (when (eq (line-number-at-pos)
  913. 2)
  914. (goto-line (- (line-number-at-pos (point-max))
  915. 1))
  916. (dired-move-to-filename))
  917. (my-dired-previous-line (- arg 1))
  918. ))
  919. (defun my-dired-next-line (arg)
  920. ""
  921. (interactive "p")
  922. (when (> arg 0)
  923. ;; (ignore 'my-dired-print-current-dir-and-file)
  924. (dired-next-line 1)
  925. (when (eq (point)
  926. (point-max))
  927. (goto-line 3)
  928. (dired-move-to-filename))
  929. (my-dired-next-line (- arg 1))
  930. ))
  931. (defun my-dired-print-current-dir-and-file ()
  932. (message "%s %s"
  933. default-directory
  934. (buffer-substring-no-properties (point-at-bol)
  935. (point-at-eol))))
  936. (defun dired-do-execute-as-command ()
  937. ""
  938. (interactive)
  939. (let ((file (dired-get-filename t)))
  940. (if (file-executable-p file)
  941. (start-process file nil file)
  942. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  943. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  944. (start-process file nil file)))))
  945. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  946. (defun my-dired-x-open ()
  947. ""
  948. (interactive)
  949. (my-x-open (dired-get-filename t t)))
  950. (if (eq window-system 'mac)
  951. (setq dired-listing-switches "-lhFG")
  952. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  953. )
  954. (setq dired-listing-switches "-lhFG")
  955. (put 'dired-find-alternate-file 'disabled nil) ; when using dired-find-alternate-file reuse current dired buffer for the file to open
  956. (setq dired-ls-F-marks-symlinks t)
  957. (require 'ls-lisp)
  958. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  959. (setq ls-lisp-dirs-first t)
  960. (setq ls-lisp-use-localized-time-format t)
  961. (setq ls-lisp-format-time-list
  962. '("%Y-%m-%d %H:%M"
  963. "%Y-%m-%d "))
  964. (setq dired-dwim-target t)
  965. ;; (add-hook 'dired-after-readin-hook
  966. ;; 'my-replace-nasi-none)
  967. ;; (add-hook 'after-init-hook
  968. ;; (lambda ()
  969. ;; (dired ".")))
  970. (add-hook 'dired-mode-hook
  971. (lambda ()
  972. (define-key dired-mode-map "o" 'my-dired-x-open)
  973. (define-key dired-mode-map "i" 'dired-get-file-info)
  974. (define-key dired-mode-map "f" 'find-file)
  975. (define-key dired-mode-map "!" 'shell-command)
  976. (define-key dired-mode-map "&" 'async-shell-command)
  977. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  978. (define-key dired-mode-map "=" 'my-dired-diff)
  979. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  980. (define-key dired-mode-map "b" 'gtkbm)
  981. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  982. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  983. (define-key dired-mode-map (kbd "TAB") 'other-window)
  984. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  985. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  986. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  987. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  988. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  989. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  990. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  991. (let ((file "._Icon\015"))
  992. (when (file-readable-p file)
  993. (delete-file file)))))
  994. (and (dllib-if-unfound "pack"
  995. "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  996. t)
  997. (require 'pack nil t)
  998. (add-hook 'dired-mode-hook
  999. (lambda ()
  1000. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1001. (and (dllib-if-unfound "dired-list-all-mode"
  1002. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1003. t)
  1004. (require 'dired-list-all-mode nil t)
  1005. (setq dired-listing-switches "-lhFG")
  1006. (add-hook 'dired-mode-hook
  1007. (lambda ()
  1008. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1009. )))
  1010. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1011. (defun my-dired-toggle-mark()
  1012. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1013. (t dired-marker-char))))
  1014. (delete-char 1)
  1015. (insert cur)))
  1016. (defun my-dired-mark (arg)
  1017. "toggle mark the current (or next ARG) files.
  1018. If on a subdir headerline, mark all its files except `.' and `..'.
  1019. Use \\[dired-unmark-all-files] to remove all marks
  1020. and \\[dired-unmark] on a subdir to remove the marks in
  1021. this subdir."
  1022. (interactive "P")
  1023. (if (dired-get-subdir)
  1024. (save-excursion (dired-mark-subdir-files))
  1025. (let ((inhibit-read-only t))
  1026. (dired-repeat-over-lines
  1027. (prefix-numeric-value arg)
  1028. 'my-dired-toggle-mark))))
  1029. (defun my-dired-mark-backward (arg)
  1030. "In Dired, move up lines and toggle mark there.
  1031. Optional prefix ARG says how many lines to unflag; default is one line."
  1032. (interactive "p")
  1033. (my-dired-mark (- arg)))
  1034. (add-hook 'dired-mode-hook
  1035. (lambda ()
  1036. (local-set-key (kbd "SPC") 'my-dired-mark)
  1037. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1038. )
  1039. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1040. ;; eshell
  1041. (defun my-eshell-backward-delete-char ()
  1042. (interactive)
  1043. (when (< (save-excursion
  1044. (eshell-bol)
  1045. (point))
  1046. (point))
  1047. (backward-delete-char 1)))
  1048. (defun my-file-owner-p (file)
  1049. "t if FILE is owned by me."
  1050. (eq (user-uid) (nth 2 (file-attributes file))))
  1051. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1052. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1053. ;; (defun eshell/less (&rest args)
  1054. ;; "Invoke `view-file' on the file.
  1055. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1056. ;; (if args
  1057. ;; (while args
  1058. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1059. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1060. ;; (file (pop args)))
  1061. ;; (view-file file)
  1062. ;; (goto-line line))
  1063. ;; (view-file (pop args))))))
  1064. (defun eshell/o (&optional file)
  1065. (my-x-open (or file ".")))
  1066. ;; (defun eshell/vi (&rest args)
  1067. ;; "Invoke `find-file' on the file.
  1068. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1069. ;; (while args
  1070. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1071. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1072. ;; (file (pop args)))
  1073. ;; (find-file file)
  1074. ;; (goto-line line))
  1075. ;; (find-file (pop args)))))
  1076. (defun eshell/clear ()
  1077. "Clear the current buffer, leaving one prompt at the top."
  1078. (let ((inhibit-read-only t))
  1079. (erase-buffer)))
  1080. (defun eshell/d (&optional dirname switches)
  1081. "if first arg is omitted open current directory."
  1082. (dired (or dirname ".") switches))
  1083. (defun eshell/v ()
  1084. (view-mode 1))
  1085. (defun eshell/git (&rest args)
  1086. ""
  1087. (if (member (car args)
  1088. '("di" "diff" "log" "show"))
  1089. (apply 'eshell-exec-visual "git" args)
  1090. (shell-command (mapconcat 'shell-quote-argument
  1091. `("git" ,@args)
  1092. " ")
  1093. t)
  1094. ;; (eshell-external-command "git" args)
  1095. ))
  1096. (defalias 'eshell/: 'ignore)
  1097. (defalias 'eshell/type 'eshell/which)
  1098. ;; (defalias 'eshell/vim 'eshell/vi)
  1099. (defalias 'eshell/ff 'find-file)
  1100. (defalias 'eshell/q 'eshell/exit)
  1101. (defun eshell-goto-prompt ()
  1102. ""
  1103. (interactive)
  1104. (goto-char (point-max)))
  1105. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1106. "open eshell and change wd
  1107. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1108. (interactive)
  1109. (let ((dir (expand-file-name default-directory)))
  1110. (switch-to-buffer (or eshell-buffer-or-name
  1111. (eshell t)))
  1112. (unless (equal dir (expand-file-name default-directory))
  1113. ;; (cd dir)
  1114. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1115. ;; (eshell-emit-prompt)
  1116. (goto-char (point-max))
  1117. (eshell-kill-input)
  1118. (insert "cd " dir)
  1119. (eshell-send-input))))
  1120. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1121. (setq eshell-term-name "eterm-color")
  1122. (setq eshell-scroll-to-bottom-on-input t)
  1123. (setq eshell-cmpl-ignore-case t)
  1124. (setq eshell-cmpl-cycle-completions nil)
  1125. (setq eshell-highlight-prompt nil)
  1126. (setq eshell-ls-initial-args '("-hCFG"
  1127. "--color=auto"
  1128. "--time-style=long-iso")) ; "-hF")
  1129. (setq eshell-prompt-function
  1130. (lambda ()
  1131. (with-temp-buffer
  1132. (let (p1 p2 p3 p4)
  1133. (insert " [")
  1134. (setq p1 (point))
  1135. (insert (abbreviate-file-name default-directory))
  1136. (setq p2 (point))
  1137. (insert "]"
  1138. "\n")
  1139. (setq p3 (point))
  1140. (insert user-login-name
  1141. "@"
  1142. (or (getenv "HOSTNAME")
  1143. (substring (shell-command-to-string (or (executable-find "hostname")
  1144. "echo ''"))
  1145. 0
  1146. -1)))
  1147. (setq p4 (point))
  1148. (insert " "
  1149. (format-time-string "%a, %d %b %Y %T %z")
  1150. " eshell\n"
  1151. "last:"
  1152. (number-to-string eshell-last-command-status)
  1153. (if (= (user-uid)
  1154. 0)
  1155. " # "
  1156. " $ "))
  1157. (add-text-properties p1
  1158. p2
  1159. '(face ((foreground-color . "yellow"))))
  1160. (add-text-properties p3
  1161. p4
  1162. '(face ((foreground-color . "cyan"))))
  1163. (buffer-substring (point-min)
  1164. (point-max))))))
  1165. (add-hook 'eshell-mode-hook
  1166. (lambda ()
  1167. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1168. ;; (interactive)
  1169. ;; (switch-to-buffer (other-buffer))))
  1170. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1171. (interactive)
  1172. (eshell-goto-prompt)
  1173. (eshell-kill-input)))
  1174. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1175. (interactive)
  1176. (eshell-goto-prompt)
  1177. (my-keyboard-quit)))
  1178. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1179. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1180. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1181. (apply 'eshell/addpath exec-path)
  1182. (set (make-local-variable 'scroll-margin) 0)
  1183. ;; (eshell/export "GIT_PAGER=")
  1184. ;; (eshell/export "GIT_EDITOR=")
  1185. (eshell/export "LC_MESSAGES=C")
  1186. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1187. (set (make-local-variable 'hl-line-range-function)
  1188. (lambda ()
  1189. '(0 . 0)))
  1190. (add-to-list 'eshell-virtual-targets
  1191. '("/dev/less"
  1192. (lambda (str)
  1193. (if str
  1194. (with-current-buffer nil)))
  1195. nil))
  1196. ))
  1197. (add-hook 'eshell-mode-hook
  1198. (lambda ()
  1199. (add-to-list 'eshell-visual-commands "vim")
  1200. ;; (add-to-list 'eshell-visual-commands "git")
  1201. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1202. (mapcar (lambda (alias)
  1203. (add-to-list 'eshell-command-aliases-list
  1204. alias))
  1205. '(
  1206. ; ("ll" "ls -l $*")
  1207. ; ("la" "ls -a $*")
  1208. ; ("lla" "ls -al $*")
  1209. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp $*")
  1210. ("aptin" "apt-get install $*")
  1211. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1212. ("g" "git $*")
  1213. ))
  1214. ))
  1215. ;; (eval-after-load "em-alias"
  1216. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1217. ;; ;; (eshell/alias "la" "ls -a")
  1218. ;; ;; (eshell/alias "lla" "ls -al")
  1219. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1220. ;; "gcc -o win.$1.exe $1"
  1221. ;; "gcc -o ${uname}.$1.out $1"))
  1222. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1223. ;; "gcc -lm -o win.$1.exe $1"
  1224. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1225. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1226. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1227. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1228. ;; 最終更新日時を得る
  1229. (defvar my-buffer-file-last-modified-time nil "")
  1230. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1231. (defun my-get-file-last-modified-time (file)
  1232. ""
  1233. (nth 5
  1234. (file-attributes file)))
  1235. (defun my-set-buffer-file-last-modified-time ()
  1236. ""
  1237. (make-local-variable 'my-buffer-file-last-modified-time)
  1238. (setq my-buffer-file-last-modified-time
  1239. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1240. (add-hook 'find-file-hook
  1241. 'my-set-buffer-file-last-modified-time)
  1242. (add-hook 'after-save-hook
  1243. 'my-set-buffer-file-last-modified-time)
  1244. (add-hook 'after-revert-hook
  1245. 'my-set-buffer-file-last-modified-time)
  1246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1247. ;; frame buffer
  1248. ;; todo: work well when opening the file that was already opened on another window
  1249. (add-hook 'after-make-frame-functions
  1250. (lambda (f)
  1251. (set-window-buffer (frame-selected-window f)
  1252. "*Messages*")))
  1253. (defun make-frame-command-with-name (name)
  1254. "Make frame with name specified."
  1255. (interactive "sName for new frame: ")
  1256. (set-frame-parameter (make-frame-command)
  1257. 'name
  1258. name))
  1259. (defvar my-frame-buffer-plist nil)
  1260. (defun my-frame-buffer-add (&optional buf frame)
  1261. ""
  1262. (setq my-frame-buffer-plist
  1263. (plist-put my-frame-buffer-plist
  1264. (or frame
  1265. (selected-frame))
  1266. (let ((lst (my-frame-buffer-get frame)))
  1267. (if lst
  1268. (add-to-list 'lst
  1269. (or buf
  1270. (current-buffer)))
  1271. (list (or buf
  1272. (current-buffer))))))))
  1273. (defun my-frame-buffer-remove (&optional buf frame)
  1274. ""
  1275. (setq my-frame-buffer-plist
  1276. (plist-put my-frame-buffer-plist
  1277. (or frame
  1278. (selected-frame))
  1279. (delq (or buf
  1280. (current-buffer))
  1281. (my-frame-buffer-get frame)))))
  1282. (defun my-frame-buffer-get (&optional frame)
  1283. ""
  1284. (plist-get my-frame-buffer-plist
  1285. (or frame
  1286. (selected-frame))))
  1287. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1288. ""
  1289. (mapcar 'kill-buffer
  1290. (my-frame-buffer-get frame)))
  1291. (add-hook 'find-file-hook
  1292. 'my-frame-buffer-add)
  1293. (add-hook 'term-mode-hook
  1294. 'my-frame-buffer-add)
  1295. (add-hook 'eshell-mode-hook
  1296. 'my-frame-buffer-add)
  1297. (add-hook 'Man-mode-hook
  1298. 'my-frame-buffer-add)
  1299. (add-hook 'kill-buffer-hook
  1300. 'my-frame-buffer-remove)
  1301. (add-hook 'delete-frame-functions
  1302. 'my-frame-buffer-kill-all-buffer)
  1303. (defvar my-desktop-terminal "roxterm")
  1304. (defun my-execute-terminal ()
  1305. ""
  1306. (interactive)
  1307. (if (and (or (eq system-type 'windows-nt)
  1308. window-system)
  1309. my-desktop-terminal
  1310. )
  1311. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1312. (start-process "terminal"
  1313. nil
  1314. my-desktop-terminal))
  1315. (my-term)))
  1316. (defun my-term ()
  1317. "open terminal buffer and return that buffer."
  1318. (interactive)
  1319. (if (eq system-type 'windows-nt)
  1320. (eshell t)
  1321. (if (featurep 'multi-term)
  1322. (multi-term)
  1323. (ansi-term "/bin/bash"))))
  1324. (defun my-delete-frame-or-kill-emacs ()
  1325. "delete frame when opening multiple frame, kill emacs when only one."
  1326. (interactive)
  1327. (if (eq 1
  1328. (length (frame-list)))
  1329. (save-buffers-kill-emacs)
  1330. (delete-frame)))
  1331. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1332. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1333. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1334. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1335. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1336. ;; auto saving
  1337. (defun my-save-this-buffer (silent-p)
  1338. "save current buffer if can without asking"
  1339. (let ((cm (if (current-message)
  1340. (format "%s\n" (current-message))
  1341. ""))
  1342. (fun (symbol-function (if silent-p
  1343. 'ignore
  1344. 'message))))
  1345. (cond ((active-minibuffer-window) nil)
  1346. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1347. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1348. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1349. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1350. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1351. (t (funcall fun "%ssaving..." cm)
  1352. (save-buffer)
  1353. (funcall fun "%ssaving... done." cm)))))
  1354. ;; (if (and buffer-file-name
  1355. ;; (not buffer-read-only)
  1356. ;; (buffer-modified-p)
  1357. ;; (file-writable-p buffer-file-name))
  1358. ;; (save-buffer))) ; 静かな方
  1359. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1360. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1361. "auto save current buffer if idle for SEC.
  1362. when SEC is nil, stop auto save if enabled."
  1363. (if sec
  1364. (progn (when my-auto-save-this-buffer
  1365. (cancel-timer my-auto-save-this-buffer)
  1366. (setq my-auto-save-this-buffer nil))
  1367. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1368. (when my-auto-save-this-buffer
  1369. (cancel-timer my-auto-save-this-buffer)
  1370. (setq my-auto-save-this-buffer nil))))
  1371. (my-auto-save-this-buffer 2 t)
  1372. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1373. ;; misc funcs
  1374. (defalias 'qcalc 'quick-calc)
  1375. ;; (when (require 'ansi-color nil t)
  1376. ;; (ansi-color-for-comint-mode-on))
  1377. (defvar git-command-history nil
  1378. "History list for git command.")
  1379. (defun my-git-shell-command (cmd)
  1380. ""
  1381. (interactive (list (read-shell-command (format "[%s] $ git : "
  1382. (abbreviate-file-name default-directory))
  1383. nil
  1384. 'git-command-history)))
  1385. (let ((dir default-directory)
  1386. (bf (get-buffer-create "*Git Output*"))
  1387. )
  1388. (delete-windows-on bf t)
  1389. (shell-command (concat "git "
  1390. cmd)
  1391. bf)
  1392. (with-current-buffer bf
  1393. (cd dir)
  1394. (and (require 'ansi-color nil t)
  1395. (ansi-color-apply-on-region (point-min)
  1396. (point-max)))
  1397. (setq buffer-read-only t)
  1398. (view-mode 1)
  1399. )))
  1400. (define-key ctl-x-map "g" 'my-git-shell-command)
  1401. (defun my-kill-buffers ()
  1402. ""
  1403. (interactive)
  1404. (mapcar (lambda (buf)
  1405. (when (buffer-file-name buf)
  1406. (kill-buffer buf)))
  1407. (buffer-list)))
  1408. (defvar my-filer nil)
  1409. (setq my-filer (or (executable-find "pcmanfm")
  1410. (executable-find "nautilus")))
  1411. (defun my-x-open (file)
  1412. "open file."
  1413. (interactive "FOpen File: ")
  1414. (setq file (expand-file-name file))
  1415. (message "Opening %s..." file)
  1416. (cond ((eq system-type 'windows-nt)
  1417. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1418. ((eq system-type 'darwin)
  1419. (call-process "open" nil 0 nil file))
  1420. ((getenv "DISPLAY")
  1421. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1422. (t
  1423. (find-file file))
  1424. )
  1425. ;; (recentf-add-file file)
  1426. (message "Opening %s...done" file))
  1427. (defun my-keyboard-quit ()
  1428. ""
  1429. (interactive)
  1430. (run-hooks 'before-keyboard-quit-hook)
  1431. ;; (redisplay t)
  1432. (redraw-display)
  1433. ;; (run-hooks 'window-configuration-change-hook)
  1434. (my-revert-buffer-if-needed)
  1435. ;; (revert-buffer t t)
  1436. (keyboard-quit)
  1437. (insert "insert me")
  1438. (run-hooks 'after-keyboard-quit-hook))
  1439. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1440. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1441. (defun my-convmv-sjis2utf8-test ()
  1442. "run `convmv -r -f sjis -t utf8 *'
  1443. this is test, does not rename files"
  1444. (interactive)
  1445. (shell-command "convmv -r -f sjis -t utf8 *"))
  1446. (defun my-convmv-sjis2utf8-notest ()
  1447. "run `convmv -r -f sjis -t utf8 * --notest'"
  1448. (interactive)
  1449. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1450. (defun my-copy-whole-line ()
  1451. ""
  1452. (interactive)
  1453. (kill-new (concat (buffer-substring (point-at-bol)
  1454. (point-at-eol))
  1455. "\n")))
  1456. (defun kill-ring-save-buffer-file-name ()
  1457. "get current filename"
  1458. (interactive)
  1459. (let ((file buffer-file-name))
  1460. (if file
  1461. (progn (kill-new file)
  1462. (message file))
  1463. (message "not visiting file."))))
  1464. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1465. (defun my-revert-buffer-if-needed ()
  1466. ""
  1467. (interactive)
  1468. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1469. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1470. (when (or (and (eq major-mode 'dired-mode)
  1471. (dired-directory-changed-p default-directory))
  1472. (not (verify-visited-file-modtime (current-buffer))))
  1473. (revert-buffer t t)
  1474. (message "%s reverted." (buffer-name))
  1475. )))
  1476. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1477. 'my-revert-buffer-if-needed)
  1478. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1479. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1480. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1481. (defun my-window-organizer ()
  1482. "Control window size and position."
  1483. (interactive)
  1484. (save-selected-window
  1485. (select-window (window-at 0 0))
  1486. (let ( ;; (window-obj (selected-window))
  1487. ;; (current-width (window-width))
  1488. ;; (current-height (window-height))
  1489. action
  1490. c)
  1491. (catch 'end-flag
  1492. (while t
  1493. (setq action
  1494. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1495. (window-width)
  1496. (window-height))))
  1497. (setq c (aref action 0))
  1498. (cond ((= c ?l)
  1499. (unless (eq (window-width) (frame-width))
  1500. (enlarge-window-horizontally 1)))
  1501. ((= c ?h)
  1502. (unless (eq (window-width) (frame-width))
  1503. (shrink-window-horizontally 1)))
  1504. ((= c ?j)
  1505. (enlarge-window 1))
  1506. ((= c ?k)
  1507. (shrink-window 1))
  1508. ((= c ?o)
  1509. (other-window 1))
  1510. ((memq c '(?d ?0))
  1511. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1512. (delete-window (selected-window))))
  1513. ((= c ?1)
  1514. (delete-other-windows))
  1515. ((= c ?2)
  1516. (split-window-vertically))
  1517. ((= c ?3)
  1518. (split-window-horizontally))
  1519. ((memq c '(?q ?\C-g))
  1520. (message "Quit")
  1521. (throw 'end-flag t))
  1522. (t
  1523. (beep))))))))
  1524. ;; (aref (read-key-sequence-vector "aa") 0)
  1525. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1526. ;; ;; emacsを殺伐とさせる
  1527. ;; ;; 補完させるとき失敗するからなし
  1528. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1529. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1530. ;; (setq arg
  1531. ;; (concat arg
  1532. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1533. ;; " And You are a Coward!")))
  1534. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1535. ;; japanese input method
  1536. (defun my-load-scim ()
  1537. "use scim-bridge.el as japanese im."
  1538. ;; Load scim-bridge.
  1539. (when (require 'scim-bridge nil t)
  1540. ;; Turn on scim-mode automatically after loading .emacs
  1541. (add-hook 'after-init-hook 'scim-mode-on)
  1542. (setq scim-cursor-color "red")
  1543. (scim-define-preedit-key ?\^h t)
  1544. (scim-define-common-key ?\* nil)
  1545. (scim-define-common-key ?\^/ nil)))
  1546. (defun my-load-anthy ()
  1547. "use anthy.el as japanese im."
  1548. ;; anthy
  1549. (when (require 'anthy nil t)
  1550. (global-set-key (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1551. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1552. (when (>= emacs-major-version 23)
  1553. (setq anthy-accept-timeout 1))))
  1554. ;; quail
  1555. ;; aproposs input-method for some information
  1556. ;; (setq default-input-method "japanese")
  1557. (defun my-load-mozc-el ()
  1558. ""
  1559. (setq mozc-leim-title "[MZ]")
  1560. (when (require 'mozc nil t)
  1561. (setq defauit-input-method "japanese-mozc")
  1562. ))
  1563. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1564. ;; for windows
  1565. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1566. (defun start-ckw-bash ()
  1567. ""
  1568. (interactive)
  1569. (start-process "ckw_bash"
  1570. nil
  1571. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1572. (defun my-w32-add-export-path (&rest args)
  1573. ""
  1574. (mapcar (lambda (path)
  1575. (add-to-list 'exec-path (expand-file-name path)))
  1576. (reverse args))
  1577. (setenv "PATH"
  1578. (mapconcat 'convert-standard-filename
  1579. exec-path
  1580. ";")))
  1581. (when (eq system-type 'windows-nt)
  1582. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1583. ;; (setq python-python-command "c:/Python26/python.exe")
  1584. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1585. (my-w32-add-export-path "c:/WINDOWS"
  1586. (expand-file-name "~/bin")
  1587. (expand-file-name "~/dbx/apps/bin"))
  1588. (when window-system
  1589. (setq w32-enable-synthesized-fonts t))
  1590. (setq file-name-coding-system 'sjis))