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