nav.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. ;;; nav.el --- Emacs mode for filesystem navigation
  2. ;;
  3. ;; Copyright 2010 Google Inc. All Rights Reserved.
  4. ;;
  5. ;; Author: issactrotts@google.com (Issac Trotts)
  6. ;; Version: 49
  7. ;;
  8. ;;; License:
  9. ;; Emacs Nav is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; Emacs Nav is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; To use this file, put something like the following in your
  22. ;; ~/.emacs:
  23. ;;
  24. ;; (add-to-list 'load-path "/directory/containing/nav/")
  25. ;; (require 'nav)
  26. ;; (nav-disable-overeager-window-splitting)
  27. ;;
  28. ;; Type M-x nav to start navigating.
  29. ;;
  30. ;; To set options for Nav, type M-x customize, then select
  31. ;; Applications, Nav.
  32. ;;; Key Bindings
  33. ;;
  34. ;; Press ? in the Nav window to display a list of key bindings.
  35. ;;
  36. ;;; History:
  37. ;;
  38. ;; See http://code.google.com/p/emacs-nav/source/list
  39. ;;
  40. ;;; Code:
  41. (if (not (featurep 'full-ack))
  42. (condition-case err
  43. (require 'ack)
  44. (error
  45. (message "Could not load ack."))))
  46. (defgroup nav nil
  47. "A lightweight filesystem navigator."
  48. :group 'applications)
  49. (defcustom nav-filtered-p t
  50. "*If true, Nav will filter out files and directories such as
  51. hidden files, backups and .elc files. The result depends on
  52. `nav-boring-file-regexps'.
  53. "
  54. :type 'boolean
  55. :group 'nav)
  56. (defcustom nav-width 25
  57. "*If non-nil, Nav will change its width to this when it opens files in
  58. other windows.
  59. "
  60. :type 'integer
  61. :group 'nav)
  62. (defcustom nav-boring-file-regexps
  63. (list "^[.][^.].*$" ; hidden files such as .foo
  64. "^[.]$" ; current directory
  65. "~$"
  66. "[.]elc$"
  67. "[.]pyc$"
  68. "[.]o$"
  69. "[.]bak$"
  70. ;; Stolen from Ack:
  71. "^_MTN$" ; Monotone
  72. "^blib$" ; Perl module building
  73. "^CVS$" ; CVS
  74. "^RCS$" ; RCS
  75. "^SCCS$" ; SCCS
  76. "^_darcs$" ; darcs
  77. "^_sgbak$" ; Vault/Fortress
  78. "^autom4te.cache$" ; autoconf
  79. "^cover_db$" ; Devel::Cover
  80. "^_build$" ; Module::Build
  81. )
  82. "*Nav ignores filenames that match any regular expression in this list."
  83. :type '(repeat string)
  84. :group 'nav)
  85. ;;
  86. ;; Fontification
  87. ;;
  88. (require 'dired)
  89. (defvar nav-directory-face 'dired-directory
  90. "Face name used for directories.")
  91. (defvar nav-font-lock-keywords
  92. '(("^.*/$" . nav-directory-face))
  93. "Regexes and associated faces used by Nav to fontify files and
  94. directories."
  95. )
  96. (defun nav-make-mode-map ()
  97. "Creates and returns a mode map with nav's key bindings."
  98. (let ((keymap (make-sparse-keymap)))
  99. (define-key keymap "a" 'ack)
  100. (define-key keymap "c" 'nav-copy-file-or-dir)
  101. (define-key keymap "C" 'nav-customize)
  102. (define-key keymap "d" 'nav-delete-file-or-dir-on-this-line)
  103. (define-key keymap "e" 'nav-invoke-dired)
  104. (define-key keymap "f" 'nav-find-files)
  105. (define-key keymap "g" 'grep-find)
  106. (define-key keymap "h" 'nav-jump-to-home)
  107. (define-key keymap "j" 'nav-jump-to-dir)
  108. (define-key keymap "m" 'nav-move-file-or-dir)
  109. (define-key keymap "n" 'nav-make-new-directory)
  110. (define-key keymap "o" 'nav-open-file-under-cursor)
  111. (define-key keymap "p" 'nav-pop-dir)
  112. (define-key keymap "P" 'nav-print-current-dir)
  113. (define-key keymap "q" 'nav-unsplit-window-horizontally)
  114. (define-key keymap "r" 'nav-refresh)
  115. (define-key keymap "s" 'nav-shell)
  116. (define-key keymap "u" 'nav-go-up-one-dir)
  117. (define-key keymap "w" 'nav-shrink-wrap)
  118. (define-key keymap "!" 'nav-shell-command)
  119. (define-key keymap "." 'nav-toggle-hidden-files)
  120. (define-key keymap "?" 'nav-help-screen)
  121. (define-key keymap "-" 'nav-shrink-a-bit)
  122. (define-key keymap "_" 'nav-shrink-a-bit)
  123. (define-key keymap "+" 'nav-expand-a-bit)
  124. (define-key keymap "=" 'nav-expand-a-bit)
  125. (define-key keymap " " 'nav-jump-to-name)
  126. (define-key keymap [(control ?x) (control ?f)] 'nav-find-file-other-window)
  127. (define-key keymap [(control ?x) ?b] 'nav-switch-to-buffer-other-window)
  128. keymap))
  129. (defun nav-shrink-window-horizontally (delta)
  130. "First, compute a new value for the delta to make sure we don't
  131. make the window too small, according to the following equation:
  132. window-width - delta' = max(window-min-width, window-width - delta)
  133. "
  134. (let ((delta (- (window-width)
  135. (max window-min-width
  136. (- (window-width) delta)))))
  137. (shrink-window-horizontally delta)
  138. (nav-remember-current-width-during-this-session)))
  139. (defun nav-enlarge-window-horizontally (delta)
  140. (enlarge-window-horizontally delta)
  141. (nav-remember-current-width-during-this-session))
  142. (defun nav-remember-current-width-during-this-session ()
  143. (customize-set-variable 'nav-width (window-width)))
  144. (defun nav-shrink-a-bit ()
  145. "Decreases the width of the nav window by one character."
  146. (interactive)
  147. (nav-shrink-window-horizontally 1))
  148. (defun nav-expand-a-bit ()
  149. "Increases the width of the nav window by one character."
  150. (interactive)
  151. (nav-enlarge-window-horizontally 1))
  152. (defun nav-quit-help ()
  153. "Exits the nav help screen."
  154. (interactive)
  155. (kill-buffer (current-buffer)))
  156. (defun nav-help-screen ()
  157. "Displays the help screen."
  158. (interactive)
  159. (switch-to-buffer-other-window "*nav-help*")
  160. (insert "\
  161. Nav Key Bindings
  162. ================
  163. Enter/Return: Go to directory under cursor, or open file under
  164. cursor in other window.
  165. Tab: Move forward through filenames.
  166. Shift-Tab: Move backward through filenames.
  167. Space: Press spacebar, then any other letter to jump to filename
  168. that starts with that letter.
  169. a\t Recursively grep for a Perl regex using Ack (http://betterthangrep.com/).
  170. c\t Copy file or directory under cursor.
  171. C\t Customize Nav settings and bookmarks.
  172. d\t Delete file or directory under cursor (asks to confirm first).
  173. e\t Edit current directory in dired.
  174. f\t Recursively find files whose titles match a Perl regex (using Ack).
  175. g\t Grep recursively from current directory using grep-find
  176. h\t Jump to home (~).
  177. j\t Jump to another directory.
  178. m\t Move or rename file or directory.
  179. n\t Make a new directory.
  180. o\t Open file under cursor in the nav window.
  181. p\t Pop directory stack to go back to the previous directory.
  182. P\t Print full path of current displayed directory.
  183. q\t Quit nav.
  184. r\t Refresh.
  185. s\t Start a shell in an emacs window in the current directory.
  186. u\t Go up to parent directory.
  187. w\t Shrink-wrap Nav's window to fit the longest filename in the current directory.
  188. \t Hit C-x + to roughly undo this by balancing windows.
  189. !\t Run shell command.
  190. .\t Toggle display of hidden files.
  191. ?\t Show this help screen.
  192. -\t Narrow Nav window by one character.
  193. +\t Widen Nav window by one character.
  194. ")
  195. (nav-goto-line 1)
  196. (view-mode -1)
  197. (toggle-read-only 1))
  198. (defvar nav-mode-map
  199. (nav-make-mode-map)
  200. "Mode map for nav mode")
  201. (defvar nav-dir-stack '())
  202. (defvar nav-map-dir-to-line-number (make-hash-table :test 'equal)
  203. "Hash table from dir paths to most recent cursor pos in them.")
  204. (defvar nav-button-face nil)
  205. (defconst nav-default-line-num 2
  206. "Line where cursor starts in directories that have not yet been
  207. visited. A value of 1 would start the cursor off on ../.")
  208. (defconst nav-shell-buffer-name "*nav-shell*"
  209. "Name of the buffer used for the command line shell spawned by
  210. nav on the 's' key.")
  211. (defconst nav-buffer-name "*nav*"
  212. "Name of the buffer where nav shows directory contents.")
  213. (defconst nav-buffer-name-for-find-results "*nav-find*"
  214. "Name of the buffer where nav shows find results.")
  215. (defun nav-join (sep string-list)
  216. (mapconcat 'identity string-list sep))
  217. (defun nav-toggle-hidden-files ()
  218. (interactive)
  219. (setq nav-filtered-p (not nav-filtered-p))
  220. (nav-refresh))
  221. (defun nav-filename-matches-some-regexp (filename regexps)
  222. (let ((matches-p nil))
  223. (dolist (rx regexps)
  224. (if (string-match rx filename)
  225. (setq matches-p t)))
  226. matches-p))
  227. ;; http://www.emacswiki.org/emacs/ElispCookbook#toc41
  228. (defun nav-filter (condp lst)
  229. (delq nil
  230. (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
  231. (defun nav-filter-out-boring-filenames (filenames boring-regexps)
  232. (nav-filter
  233. (lambda (filename)
  234. (not (nav-filename-matches-some-regexp filename boring-regexps)))
  235. filenames))
  236. (defun nav-get-line-for-cur-dir ()
  237. (gethash (nav-get-working-dir) nav-map-dir-to-line-number))
  238. (defun nav-cd (dirname)
  239. "Changes to a different directory and pushes it onto the stack."
  240. (let ((dirname (file-name-as-directory (file-truename dirname))))
  241. (nav-save-cursor-line)
  242. (setq default-directory dirname)
  243. (nav-show-dir dirname)
  244. (nav-restore-cursor-line)))
  245. (defun nav-save-cursor-line ()
  246. "Updates line number hash table."
  247. (let ((line-num (nav-line-number-at-pos (point))))
  248. (puthash (nav-get-working-dir) line-num nav-map-dir-to-line-number)))
  249. (defun nav-goto-line (line)
  250. "Jumps point to the given line."
  251. (goto-char (point-min)) (forward-line (1- line)))
  252. (defun nav-restore-cursor-line ()
  253. "Remembers what line we were on last time we visited this directory."
  254. (let ((line-num (or (nav-get-line-for-cur-dir)
  255. nav-default-line-num)))
  256. (nav-goto-line line-num)))
  257. (defun nav-open-file (filename)
  258. "Opens a file or directory from Nav."
  259. (interactive "FFilename:")
  260. (if (file-directory-p filename)
  261. (nav-push-dir filename)
  262. (find-file filename)))
  263. (defun nav-open-file-other-window (filename)
  264. "Opens a file or directory from Nav."
  265. (interactive "FFilename:")
  266. (if (file-directory-p filename)
  267. (nav-push-dir filename)
  268. (let ((path (concat default-directory filename)))
  269. (nav-find-file-other-window path))))
  270. (defun nav-find-file-other-window (filename)
  271. "Opens the file FILENAME in the window to the right."
  272. (interactive "FFilename:")
  273. (message filename)
  274. (nav-must-windmove-right)
  275. (find-file filename))
  276. (defun nav-switch-to-buffer-other-window (buffer-name)
  277. (interactive "bSwitch to buffer:")
  278. (nav-must-windmove-right)
  279. (switch-to-buffer buffer-name))
  280. (defun nav-must-windmove-right ()
  281. "Moves point to the window to the right of the current one.
  282. If there is no such window, it first splits the current window
  283. and then moves to the new window just formed on the right."
  284. (condition-case err
  285. (windmove-right)
  286. (error
  287. (split-window-horizontally)
  288. (windmove-right))))
  289. (defun nav-open-file-under-cursor ()
  290. "Finds the file under the cursor."
  291. (interactive)
  292. (let ((filename (nav-get-cur-line-str)))
  293. (nav-open-file filename)))
  294. (defun nav-get-current-window ()
  295. "Returns the currently selected window."
  296. (get-buffer-window (current-buffer)))
  297. (defun nav-get-current-window-width ()
  298. "Returns the width of the currently selected window."
  299. (window-width (nav-get-current-window)))
  300. (defun nav-go-up-one-dir ()
  301. "Points Nav to ../."
  302. (interactive)
  303. (nav-push-dir ".."))
  304. (defun nav-get-shrink-wrap-width ()
  305. (let* ((lines (split-string (buffer-string) "\n" t))
  306. (num-lines (length lines))
  307. (line-lengths (mapcar 'length lines))
  308. (desired-width (+ 1 (apply 'max line-lengths)))
  309. (max-width (/ (frame-width) 2))
  310. (new-width (min desired-width max-width)))
  311. new-width))
  312. (defun nav-shrink-wrap ()
  313. "Updates the width of the Nav window to fit the longest filename in the
  314. current directory. Updates the global variable nav-width as a side effect."
  315. (interactive)
  316. (nav-set-window-width (nav-get-shrink-wrap-width)))
  317. (defun nav-push-dir (dirname)
  318. (let ((dirname (file-truename dirname)))
  319. (when (not (string= dirname default-directory))
  320. (push (file-truename default-directory) nav-dir-stack)
  321. (nav-cd dirname))))
  322. (defun nav-pop-dir ()
  323. "Goes to the previous directory in Nav's history.
  324. This works like a web browser's back button."
  325. (interactive)
  326. (let ((dir nil))
  327. (while (and nav-dir-stack
  328. (or (not dir)
  329. (equal dir (file-name-as-directory (file-truename ".")))
  330. (not (file-exists-p dir))))
  331. (setq dir (pop nav-dir-stack)))
  332. (setq dir (or dir "."))
  333. (nav-cd dir)))
  334. (defun nav-get-cur-line-str ()
  335. (buffer-substring-no-properties (point-at-bol)
  336. (point-at-eol)))
  337. (defun nav-non-boring-directory-files (dir)
  338. (nav-filter-out-boring-filenames (directory-files dir)
  339. (if nav-filtered-p
  340. nav-boring-file-regexps
  341. '()
  342. )))
  343. (defun nav-dir-suffix (dir)
  344. (replace-regexp-in-string ".*/" "" (directory-file-name dir)))
  345. (defun nav-line-number-at-pos (p)
  346. (let ((line-num 1))
  347. (dotimes (i p line-num)
  348. (if (eq ?\n (char-after i))
  349. (setq line-num (+ line-num 1))))))
  350. (defun nav-replace-buffer-contents (new-contents)
  351. (let ((saved-line-number (nav-line-number-at-pos (point)))
  352. ;; Setting inhibit-read-only to t here lets us edit the buffer
  353. ;; in this let-block.
  354. (inhibit-read-only t))
  355. (erase-buffer)
  356. (insert new-contents)
  357. (nav-make-filenames-clickable)
  358. (nav-goto-line saved-line-number)))
  359. (defun nav-select-window (window)
  360. (if window
  361. (select-window window)
  362. (message "Attempted to select nil window")))
  363. (defun nav-button-action-to-open-file (button)
  364. "Opens a file or directory in response to a button."
  365. (let* ((buffer (overlay-buffer button))
  366. (window-with-nav (get-buffer-window buffer)))
  367. (nav-select-window window-with-nav)
  368. (if (= 1 (count-windows))
  369. (split-window-horizontally))
  370. (nav-open-file-other-window (button-label button))
  371. (if nav-width
  372. (let ((other-window (nav-get-current-window)))
  373. (select-window window-with-nav)
  374. (nav-set-window-width nav-width)
  375. (select-window other-window)))))
  376. (defun nav-button-action-to-open-dir (button)
  377. (let ((buffer (overlay-buffer button)))
  378. (pop-to-buffer buffer)
  379. (nav-push-dir (button-label button))))
  380. (defun nav-make-filenames-clickable ()
  381. (condition-case err
  382. (save-excursion
  383. (nav-goto-line 1)
  384. (dotimes (i (count-lines 1 (point-max)))
  385. (let* ((start (line-beginning-position))
  386. (end (line-end-position))
  387. (filename (buffer-substring-no-properties start end))
  388. (action (if (file-directory-p filename)
  389. 'nav-button-action-to-open-dir
  390. 'nav-button-action-to-open-file)))
  391. (make-button start end
  392. 'action action
  393. 'follow-link t
  394. 'face nav-button-face
  395. 'help-echo nil))
  396. (forward-line 1)))
  397. (error
  398. ;; This can happen for versions of emacs that don't have
  399. ;; make-button defined.
  400. 'failed)))
  401. (defun nav-string< (s1 s2)
  402. "Tells whether S1 comes lexically before S2, ignoring case."
  403. (string< (downcase s1) (downcase s2)))
  404. (defun nav-show-dir (dir)
  405. (let ((new-contents '()))
  406. (dolist (filename (nav-non-boring-directory-files dir))
  407. (let ((line (concat filename
  408. (if (file-directory-p filename)
  409. "/"
  410. "")
  411. )))
  412. (push line new-contents)))
  413. (let* ((new-contents (sort new-contents 'nav-string<))
  414. (new-contents (nav-join "\n" new-contents)))
  415. (nav-replace-buffer-contents new-contents))
  416. (setq mode-line-format (nav-make-mode-line "d" dir))
  417. (force-mode-line-update)))
  418. (defun nav-set-window-width (n)
  419. (let ((n (max n window-min-width)))
  420. (if (> (window-width) n)
  421. (nav-shrink-window-horizontally (- (window-width) n)))
  422. (if (< (window-width) n)
  423. (nav-enlarge-window-horizontally (- n (window-width))))))
  424. (defun nav-save-window-width ()
  425. "Saves the width of the current window as the default width for Nav."
  426. (interactive)
  427. (let ((width (window-width (nav-get-current-window))))
  428. (customize-save-variable 'nav-width width)))
  429. (defun nav-get-working-dir ()
  430. (file-name-as-directory (file-truename default-directory)))
  431. (defun nav-invoke-dired ()
  432. "Invokes dired on the current directory."
  433. (interactive)
  434. (dired (nav-get-working-dir)))
  435. (defun nav-find-files (pattern)
  436. "Recursively finds files whose names match a Perl regular expression."
  437. (interactive "sPattern: ")
  438. (let* ((pattern (format "%s[^/]*$" pattern))
  439. (find-command (format "ack -a -l '.' | ack %s" pattern))
  440. (inhibit-read-only t))
  441. (erase-buffer)
  442. (call-process-shell-command find-command nil (current-buffer))
  443. (nav-make-filenames-clickable)
  444. (message "Hit r to bring back Nav directory listing.")
  445. (cond ((string= "" (buffer-string))
  446. (insert "No matching files found."))
  447. (t
  448. ;; Enable nav keyboard shortcuts, mainly so hitting enter will open
  449. ;; files.
  450. (use-local-map nav-mode-map))
  451. )
  452. (forward-line -1)
  453. ))
  454. (defun nav-refresh ()
  455. "Resizes Nav window to original size, updates its contents."
  456. (interactive)
  457. (nav-show-dir ".")
  458. (nav-restore-cursor-line))
  459. (defun nav-jump-to-home ()
  460. "Show home directory in Nav."
  461. (interactive)
  462. (nav-push-dir "~"))
  463. (defun nav-jump-to-name (arg)
  464. (interactive "K")
  465. (nav-goto-line 2)
  466. (let ((nav-search-string (concat "^" arg)))
  467. (search-forward-regexp nav-search-string)))
  468. (defun nav-jump-to-dir (dirname)
  469. "Shows a specified directory in Nav."
  470. (interactive "fDirectory: ")
  471. (nav-push-dir dirname))
  472. (defun nav-make-mode-line (mode dir)
  473. (concat "-(nav)"
  474. (nav-dir-suffix (file-truename dir))
  475. "/"
  476. " "
  477. (format "[%s]"
  478. (if nav-filtered-p
  479. "filtered"
  480. "unfiltered"))
  481. )
  482. )
  483. (defun nav-delete-file-or-dir (filename)
  484. (nav-save-cursor-line)
  485. (if (and (file-directory-p filename)
  486. (not (file-symlink-p (directory-file-name filename))))
  487. (when (yes-or-no-p (format "Really delete directory %s ?" filename))
  488. (delete-directory filename t)
  489. (nav-refresh))
  490. ;; We first use directory-file-name to strip the trailing slash
  491. ;; if it's a symlink to a directory.
  492. (let ((filename (directory-file-name filename)))
  493. (when (y-or-n-p (format "Really delete file %s ? " filename))
  494. (delete-file filename)
  495. (nav-refresh))))
  496. (nav-restore-cursor-line))
  497. (defun nav-delete-file-or-dir-on-this-line ()
  498. "Deletes a file or directory."
  499. (interactive)
  500. (nav-delete-file-or-dir (nav-get-cur-line-str)))
  501. (defun nav-copy-file-or-dir (target-name)
  502. "Copies a file or directory."
  503. (interactive "FCopy to: ")
  504. (let ((filename (nav-get-cur-line-str)))
  505. (if (file-directory-p filename)
  506. (copy-directory filename target-name)
  507. (copy-file filename target-name)))
  508. (nav-refresh))
  509. (defun nav-customize ()
  510. "Starts customization for Nav."
  511. (interactive)
  512. (customize-group "nav"))
  513. (defun nav-move-file-or-dir (target-name)
  514. "Moves a file or directory."
  515. (interactive "FMove to: ")
  516. (let ((filename (nav-get-cur-line-str)))
  517. (rename-file filename target-name))
  518. (nav-refresh))
  519. (defun nav-append-slashes-to-dir-names (names)
  520. (mapcar (lambda (name)
  521. (if (file-directory-p name)
  522. (concat name "/")
  523. name))
  524. names))
  525. (defun nav-make-new-directory (name)
  526. "Creates a new directory."
  527. (interactive "sMake directory: ")
  528. (make-directory name)
  529. (nav-refresh))
  530. (defun nav-shell ()
  531. "Starts up a shell on the current nav directory.
  532. Thanks to claudio.bley for this new, improved version.
  533. http://code.google.com/p/emacs-nav/issues/detail?id=78
  534. "
  535. (interactive)
  536. (let ((default-directory (nav-get-working-dir)))
  537. (shell nav-shell-buffer-name)))
  538. (defun nav-shell-command (command)
  539. "Runs a shell command and then refreshes the Nav window."
  540. (interactive "sShell command: ")
  541. (shell-command command)
  542. (nav-refresh))
  543. (defun nav-print-current-dir ()
  544. "Shows the full path that nav is currently displaying"
  545. (interactive)
  546. (print default-directory))
  547. (define-derived-mode nav-mode fundamental-mode
  548. "Nav mode navigates filesystems."
  549. (setq mode-name "Nav")
  550. (use-local-map nav-mode-map)
  551. (setq buffer-read-only t)
  552. (setq truncate-lines t)
  553. (setq font-lock-defaults '(nav-font-lock-keywords))
  554. (set-window-dedicated-p (selected-window) t)
  555. (nav-refresh))
  556. (defun nav-disable-overeager-window-splitting ()
  557. "Turns off the new feature where Emacs 23 automatically splits windows when
  558. opening files in a large frame."
  559. (interactive)
  560. (setq split-width-threshold most-positive-fixnum)
  561. (setq split-height-threshold most-positive-fixnum))
  562. (defun nav-window-width-with-chrome ()
  563. "Width of the current window, including decoration."
  564. (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))))
  565. (defun nav-current-window-has-left-window-neighbor ()
  566. "Returns non-nil of the current window is not the leftmost."
  567. (> (nth 0 (window-edges)) 0))
  568. (defun nav-width-of-window-to-left ()
  569. "Returns the width of the window to the left, or 0 if there is
  570. none."
  571. (if (nav-current-window-has-left-window-neighbor)
  572. (save-selected-window
  573. (windmove-left)
  574. (window-width))
  575. 0))
  576. ;; Copied from subr.el
  577. (defmacro nav-ignore-errors (&rest body)
  578. "Execute BODY; if an error occurs, return nil.
  579. Otherwise, return result of last form in BODY."
  580. (declare (debug t) (indent 0))
  581. `(condition-case nil (progn ,@body) (error nil)))
  582. (defun nav-unsplit-window-horizontally ()
  583. "Attempts to reverse the effect of split-window-horizontally."
  584. (interactive)
  585. (let ((nav-width (nav-window-width-with-chrome))
  586. (left-width (nav-width-of-window-to-left))
  587. (buf (current-buffer)))
  588. (nav-ignore-errors
  589. (windmove-right))
  590. (kill-buffer buf)
  591. (let* ((new-left-width (nav-width-of-window-to-left))
  592. (left-window-expanded (> new-left-width left-width)))
  593. (if left-window-expanded
  594. (enlarge-window-horizontally nav-width)))))
  595. ;; Copied from subr.el
  596. (defun nav-string-prefix-p (str1 str2 &optional ignore-case)
  597. "Returns non-nil if STR1 is a prefix of STR2.
  598. If IGNORE-CASE is non-nil, the comparison is done without paying attention
  599. to case differences."
  600. (eq t (compare-strings str1 nil nil
  601. str2 0 (length str1) ignore-case)))
  602. (defun nav-current-buffer-is-nav ()
  603. "Returns non-nil if the current buffer is a Nav instance."
  604. (nav-string-prefix-p nav-buffer-name (buffer-name)))
  605. (defun nav-left-neighbor-is-nav ()
  606. "Returns non-nil if there is a window to the left with a Nav
  607. instance."
  608. (when (nav-current-window-has-left-window-neighbor)
  609. (windmove-left)
  610. (let ((is-nav (nav-current-buffer-is-nav)))
  611. (windmove-right)
  612. is-nav)))
  613. (defun nav-toggle ()
  614. "Toggles the nav panel."
  615. (interactive)
  616. (if (nav-current-buffer-is-nav)
  617. (nav-unsplit-window-horizontally)
  618. (if (nav-left-neighbor-is-nav)
  619. (progn
  620. (windmove-left)
  621. (nav-unsplit-window-horizontally))
  622. (nav))))
  623. (defun nav-in-place ()
  624. "Starts Nav in the current window."
  625. (interactive)
  626. (switch-to-buffer (generate-new-buffer-name nav-buffer-name))
  627. (nav-mode)
  628. (nav-refresh))
  629. ;; The next line is for ELPA, the Emacs Lisp Package Archive.
  630. ;;;###autoload
  631. (defun nav ()
  632. "Opens Nav in a new window to the left of the current one."
  633. (interactive)
  634. (let ((default-directory (nav-get-working-dir)))
  635. (split-window-horizontally)
  636. (nav-in-place)
  637. (nav-set-window-width nav-width)))
  638. (provide 'nav)
  639. ;;; nav.el ends here