Skip to content

Commit

Permalink
cleanup embarrassing code and increasing robustness
Browse files Browse the repository at this point in the history
  note: this will break calls to defjump as it is now written as a
        macro.  To use this new code you must unquote the first two
        arguments to defjump.

* jump.el (jump-remove-unwanted-files): whitespace fixes and adding
  documentation string
  (jump-to-file): now ensuring that directories exist before calling
  findr
  (defjump): rewrote this macro with defmacro rather than as a
  function with a quoted body
  • Loading branch information
eschulte committed Oct 15, 2010
1 parent 0def544 commit 820bb7d
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ Return the path selected or nil if files was empty."
(if file (find-file (cdr (assoc file files))))))

(defun jump-remove-unwanted-files (files)
(delete-if nil (mapcar (lambda (file-cons)
(unless (string-match
jump-ignore-file-regexp (cdr file-cons)) file-cons))
files)))
"Remove file matching `jump-ignore-file-regexp' from the list
of possible jumps."
(delete-if nil
(mapcar
(lambda (file-cons)
(unless (string-match jump-ignore-file-regexp (cdr file-cons))
file-cons))
files)))

(defun jump-to-file (&optional file)
"Open the file located at file if file ends in a / then look in
Expand All @@ -141,9 +145,14 @@ from all matches."
(jump-uniqueify file-cons))
(add-to-list 'file-alist file-cons)
file-cons))
(findr (car file-cons)
(expand-file-name (or (file-name-directory
(cdr file-cons)) "") root)))))))))
(let ((dir (expand-file-name
(or (file-name-directory (cdr file-cons)) "")
root)))
(when (and (file-exists-p dir) (file-directory-p dir))
(findr (car file-cons)
(expand-file-name
(or (file-name-directory
(cdr file-cons)) "") root)))))))))))

(defun jump-to-method (&optional method)
"If `jump-method' returns method in buffer, go to the first
Expand Down Expand Up @@ -251,7 +260,7 @@ MAKE to create the target file."
(t (message (format "unrecognized jump-from specification format %s")))))

;;;###autoload
(defun defjump (name specs root &optional doc make method-command)
(defmacro defjump (name specs root &optional doc make method-command)
"Define NAME as a function with behavior determined by SPECS.
SPECS should be a list of cons cells of the form
Expand All @@ -277,36 +286,37 @@ MAKE `find-file' will be used to open the path.
Optional argument METHOD-COMMAND overrides the function used to
find the current method which defaults to `which-function'."
(eval
`(defun ,name (&optional create) ,(concat doc "\n\nautomatically created by `defjump'")
(interactive "P")
(let ((root ,(if (functionp root) `(,root) root))
(method-command (quote ,(or method-command 'which-function)))
matches)
(loop ;; try every rule in mappings
for spec in (quote ,(mapcar
(lambda (spec)
(if (stringp (car spec))
;;xemacs did not understand :digit: class
(if (featurep 'xemacs)
(cons (replace-regexp-in-string
"\\\\[0-9]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec))
(cons (replace-regexp-in-string
"\\\\[[:digit:]]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec)))
spec))
specs))
;; don't stop until both the front and the back match
;;
;; the back should match if the user is presented with a list
;; of files, or a single file is jumped to
until (and (setf matches (jump-from (car spec)))
(cond
((equal t matches)
(jump-to (cdr spec) nil (if create (quote ,make))))
((consp matches)
(jump-to (cdr spec) matches (if create (quote ,make)))))))))))
`(defun ,name (&optional create)
,(concat doc "\n\nautomatically created by `defjump'")
(interactive "P")
(let ((root ,(if (functionp root) (root) root))
(method-command ,(or method-command 'which-function))
matches)
(loop ;; try every rule in mappings
for spec in (quote ,(mapcar
(lambda (spec)
(if (stringp (car spec))
;;xemacs did not understand :digit: class
(if (featurep 'xemacs)
(cons (replace-regexp-in-string
"\\\\[0-9]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec))
(cons (replace-regexp-in-string
"\\\\[[:digit:]]+" "\\\\(.*?\\\\)"
(car spec)) (cdr spec)))
spec))
specs))
;; don't stop until both the front and the back match
;;
;; the back should match if the user is presented with a list
;; of files, or a single file is jumped to
until
(and (setf matches (jump-from (car spec)))
(cond
((equal t matches)
(jump-to (cdr spec) nil (when create ,make)))
((consp matches)
(jump-to (cdr spec) matches (when create ,make)))))))))

(provide 'jump)
;;; jump.el ends here

0 comments on commit 820bb7d

Please sign in to comment.