Skip to content

Commit

Permalink
Search only first 10 lines of the buffer to auto-detect diff
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed May 2, 2017
1 parent 22fe75f commit 252efc1
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions eless
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Time-stamp: <2017-04-28 15:54:17 kmodi>
# Time-stamp: <2017-05-02 13:17:58 kmodi>
#
# This script uses the unofficial strict mode as explained in
# http://redsymbol.net/articles/unofficial-bash-strict-mode
Expand Down Expand Up @@ -292,24 +292,29 @@ function emacs_Q_view_mode {
(defun eless/enable-diff-mode-maybe ()
(interactive)
(save-excursion
(goto-char (point-min))
(when (re-search-forward "^\\(?:[0-9]+,\\)?[0-9]+\\(?1:[adc]\\)\\(?:[0-9]+,\\)?[0-9]+$" nil :noerror)
(forward-line 1)
(let ((diff-type (match-string-no-properties 1))
(diff-mode-enable nil))
(cond
;; Line(s) added
((string= diff-type "a")
(when (re-search-forward "^> " nil :noerror)
(setq diff-mode-enable t)))
;; Line(s) deleted or changed
(t
(when (re-search-forward "^< " nil :noerror)
(setq diff-mode-enable t))))
(when diff-mode-enable
(diff-mode)
(rename-buffer "*Diff*" :unique)
(view-mode 1)))))) ;Re-enable view-mode
(let* ((max-line 10) ;Search first MAX-LINE lines of the buffer
(bound (progn
(goto-char (point-min))
(forward-line max-line)
(point))))
(goto-char (point-min))
(when (re-search-forward "^\\(?:[0-9]+,\\)?[0-9]+\\(?1:[adc]\\)\\(?:[0-9]+,\\)?[0-9]+$" bound :noerror)
(forward-line 1)
(let ((diff-type (match-string-no-properties 1))
(diff-mode-enable nil))
(cond
;; Line(s) added
((string= diff-type "a")
(when (re-search-forward "^> " nil :noerror)
(setq diff-mode-enable t)))
;; Line(s) deleted or changed
(t
(when (re-search-forward "^< " nil :noerror)
(setq diff-mode-enable t))))
(when diff-mode-enable
(diff-mode)
(rename-buffer "*Diff*" :unique)
(view-mode 1))))))) ;Re-enable view-mode
(defun eless/kill-emacs-or-buffer ()
(interactive)
Expand Down

0 comments on commit 252efc1

Please sign in to comment.