home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / alt / lucidem / help / 258 < prev    next >
Encoding:
Text File  |  1992-08-13  |  9.9 KB  |  304 lines

  1. Xref: sparky alt.lucid-emacs.help:258 gnu.emacs.help:3715
  2. Newsgroups: alt.lucid-emacs.help,gnu.emacs.help
  3. Path: sparky!uunet!sun-barr!ames!elroy.jpl.nasa.gov!usc!sol.ctr.columbia.edu!destroyer!ncar!sunny!southern
  4. From: southern@sunny.NoSubdomain.NoDomain (Lawrence Buja)
  5. Subject: Re: hiding and exposing text
  6. Message-ID: <1992Aug13.143132.5540@ncar.ucar.edu>
  7. Sender: news@ncar.ucar.edu (USENET Maintenance)
  8. Reply-To: southern@ncar.ucar.edu
  9. Organization: National Center for Atmospheric Research
  10. References:  <MCCLEN.92Aug12205450@alder.uswest.com>
  11. Distribution: usa
  12. Date: Thu, 13 Aug 1992 14:31:32 GMT
  13. Lines: 289
  14.  
  15. In article <MCCLEN.92Aug12205450@alder.uswest.com>, mcclen@uswest.com ( Chris McClenaghan) writes:
  16. >I'm looking for a package of emacs lisp code that allows hiding
  17. >of regions of text and subsequent exposure of the hidden text.
  18. >I'd like to have more than one region of text hidden at a time.
  19. >This should be interactive, but having the ability to predefine
  20. >regular expressions or syntactic elements that would
  21. >automatically be hidden would also be nice.
  22. >
  23. >As an example, I'd like to collapse the body of a while statement
  24. >in C or C++ leaving just the while and condition with some sort
  25. >of ellipsis mark that could be selected for exposing the text.
  26. >Also, certain #ifdef's might automatically be hidden, leaving a
  27. >mark for expansion.
  28. >
  29. >--
  30. >Chris McClenaghan    mcclen@uswest.com
  31. >
  32.  
  33.  
  34. Below is some functions which I scooped off the comp.emacs.help
  35. awhile back and modified to my tastes.  I really only ever use the
  36. first function, hide-all-non-matching-lines.  It's quite powerful
  37. when bound to a hot key or used in conjunction with
  38. narrow-to-region (see function "lock") for isolating the definition
  39. and uses of variables in large codes. 
  40.  
  41. With this available, I rarely do isearches anymore.  Instead I just
  42. hide all the lines that don't contain target, go to the occurance
  43. of target that I'm interested in, then redisplay the entire buffer.
  44.  
  45. To check for two or more targets, separate them with a \| 
  46. (i.e. targeta\|targetb\|targetc ).
  47.  
  48. Cautions:
  49.  
  50. 1. Be careful about deleting hidden text.  It's still there
  51.    (marked by three dots ...) even though you can't see it.  The
  52.    hidden text is conceptionally appended to the end of the previous
  53.    visible line.  If you delete the whole previous line, including the
  54.    ...'s, you've deleted all the hidden text up to the next visible line.
  55.  
  56. 2. String searches will also "find" target strings even if they are
  57.    hidden.  So be careful when doing global changes or running
  58.    search&modify marcros.  
  59.  
  60. Note that 1. and 2. apply only to text hidden via hide-region.
  61. 1. and 2. do NOT apply to text hidden via narrow-to-region.
  62.  
  63. Good luck.
  64.  
  65. /\      Lawrence Buja           Climate and Global Dynamics Division
  66.   \_][  southern@ncar.ucar.edu  National Center for Atmospheric Research
  67.       \_________________________Boulder,_Colorado___80307-3000__________
  68.  
  69. ;;Date: Sat, 3 Dec 88 14:08:15 cst
  70. ;;From: Daniel LaLiberte <liberte@M.CS.UIUC.EDU>
  71. ;;To: sboisen@IZAR.BBN.COM
  72. ;;Subject: Re:  elisp structure editing
  73. ;;
  74. ;;There are two ways to use selective display.  It can be used to
  75. ;;hide everything indented past some number of leading whitespaces,
  76. ;;or to hide only those lines that are preceded by CR.  My functions,
  77. ;;below, only use the latter.  They need some work, but maybe you
  78. ;;will be inspired to do that.  Also, keep in mind the problem with
  79. ;;comments.  You may suggest to RMS that comments should end with
  80. ;;either \n or \r; that would solve the problem.
  81. ;;
  82. ;;dan
  83. ;;
  84. ;;; Modified substantially 11/1/89 Aaron Larson. (alarson@src.honeywell.com)
  85.  
  86.  
  87. (provide 'hide-sexp)
  88.  
  89. (defun hide-all-non-matching-lines (regexp)
  90. "Similar to the CMS XEDIT ALL command.                  (all.el)
  91.    - Hide all lines except those containing matches for REGEXP.
  92.    - A match split across lines preserves all the lines it lies in.
  93.    - Applies to all lines in buffer.
  94.    - Entering nothing at the query will redisplay all lines."
  95.   (interactive "sShow lines (regexp): ")
  96.   (show-all)  
  97.   (if (string-equal regexp "")     ()
  98.   (save-excursion
  99.     (goto-line 1)
  100.     (or (bolp) (forward-line 1))
  101.     (let ((start (point)))
  102.       (while (not (eobp))
  103.     ;; Start is first char not preserved by previous match.
  104.     (if (not (re-search-forward regexp nil 'move))
  105.         (hide-region start (point-max))
  106.       (let ((end (save-excursion (goto-char (match-beginning 0))
  107.                      (forward-line -1)
  108.                      (point))))
  109.         ;; Now end is first char preserved by the new match.
  110.         (if (< start end)
  111.         (hide-region start end))))
  112.     (setq start (point)))))))
  113.  
  114. (defun show-all ()
  115.   "Show any hidden text in buffer.        (all.el)"
  116.   (interactive)
  117.   (show-region (point-min) (point-max))
  118.   (message " "))
  119.  
  120.  
  121. (setq locked 'unlocked)
  122. (defun lock ()
  123.   "If currently unlocked
  124.      Narrow region to display only the current fortran subprogram,
  125.      defun or paragraph depending on major-mode of the file.
  126.    If currently locked
  127.      widen and recenter to restore all text.   (all.el)"
  128.   (interactive)
  129.   (save-excursion
  130.  
  131.   (if (eq major-mode 'fortran-mode)     (mark-fortran-subprogram))
  132.   (if (eq major-mode 'emacs-lisp-mode)  (mark-defun))
  133.   (if (eq major-mode 'text-mode)        (mark-paragraph))
  134.   (if (eq locked 'locked) (widen)    (narrow-to-region (point) (mark)))
  135. ;;(if (eq locked 'locked) (recenter) ())
  136.   (if (eq locked 'locked) (setq locked 'unlocked) (setq locked 'locked))
  137.     (setq start (point)) ))
  138.  
  139.  
  140.  
  141. (defun set-mark-widen ()
  142.   "Set mark then show any hidden text in buffer.   (all.el)"
  143.   (interactive)
  144.   (show-region (point-min) (point-max))
  145.   (set-mark-command nil)
  146. )
  147.  
  148.  
  149. (defun hide-non-top-level (&optional except-comments) 
  150.   "Hide all non top level forms, with arguments doesn't hide top level 
  151. comments (i.e. ';' in column 1)."
  152.   (interactive "P")
  153.   (hide-region (point-min) (point-max)
  154.            (if except-comments "(;" "(")))
  155.     
  156.     
  157.  
  158. (defun hide-sexp ()
  159.   "Hide the lines within the current sexp.  Current sexp is defined as either 
  160. the one immediately following point, or the surrounding one."
  161.   (interactive)
  162.   (save-excursion
  163.     (forward-char)
  164.     (backward-up-list 1)
  165.     (hide-region
  166.       (save-excursion (beginning-of-line)
  167.               (point))
  168.       (progn (forward-sexp 1)
  169.          (point)))))
  170.  
  171. (defun show-sexp ()
  172.   "Show the lines within the current sexp.  Current sexp is defined as either 
  173. the one immediately following point, or the surrounding one."
  174.   (interactive)
  175.   (save-excursion
  176.     (forward-char)
  177.     (backward-up-list 1)
  178.     (show-region
  179.      (save-excursion
  180.        (beginning-of-line)
  181.        (point))
  182.      (progn
  183.        (forward-sexp 1)
  184.        (point)))))
  185.  
  186. (defun hide-sub-sexps ()
  187.   "Hide all the sub sexps in the current form."
  188.   (interactive)
  189.   (save-excursion
  190.     (condition-case err
  191.     (progn
  192.       (backward-up-list 1)
  193.       (down-list 1)
  194.       (forward-sexp 1)        ; after function name
  195.       (while t            ; run til end of list
  196.         (forward-sexp 1)
  197.         (hide-region 
  198.           (progn
  199.         (forward-sexp -1) (point))
  200.           (progn
  201.         (forward-sexp 1) (point))))
  202.       )
  203.       (error nil))))
  204.       
  205.  
  206.  
  207.  
  208. ;(setq minor-mode-alist (cons (quote (selective-display " Hiden"))
  209. ;                 minor-mode-alist))
  210. (defun hide-text (arg)
  211.   "Hide all text in buffer indented by more than the current column.
  212. If an argument is given, then the argument is used instead of the current 
  213. column.  In either case, a value less than 1 makes all text visible."
  214.   (interactive "P")
  215.   (setq arg (or arg (current-column)))
  216.   (setq selective-display (if (< (prefix-numeric-value arg) 1)
  217.                   nil
  218.                 (prefix-numeric-value arg))))
  219.  
  220.  
  221. (defun hide-region (from to &optional flag)
  222.   (interactive "r")
  223.   (hide-or-show-region from to ?\r flag))
  224.  
  225. (defun show-region (from to &optional flag)
  226.   (interactive "r")
  227.   (hide-or-show-region from to ?\n flag))
  228.  
  229. (defun hide-or-show-region (from to flag &optional show-lines-starting)
  230.   "Hides or shows lines from FROM to TO, according to FLAG.  If FLAG
  231. is \\n (newline character) then text is shown, while if FLAG is \\r
  232. \(control-M) the text is hidden. Also twiddles the buffer-modified
  233. flag to make up for a deficiency (in the documentation at least) of
  234. subst-char-in-region.  Show-lines-starting should be a string containing
  235. characters indicating that the lines starting with one of the characters
  236. should not be hiden."
  237.   (let ((modifiedp (buffer-modified-p))
  238.     (buffer-read-only nil))
  239.     (subst-char-in-region from to
  240.               (if (= flag ?\n) ?\r ?\n)
  241.               flag 'noundo)
  242.     (if show-lines-starting
  243.     (save-excursion
  244.       (goto-char from)
  245.       (let ((chars (concat (char-to-string ?\r) "[" (regexp-quote show-lines-starting) "]")))
  246.         (while (re-search-forward chars to t)
  247.           (backward-char 1)
  248.           (subst-char-in-region (- (point) 1) (point) ?\r ?\n 'noundo)))))
  249.     (set-buffer-modified-p modifiedp))
  250.   (setq selective-display t)
  251.   (modify-syntax-entry ?\r ">")
  252.   )
  253.  
  254.  
  255. (defun hide-matching-lines (regexp)
  256.   "Hide lines, from point to end of buffer, containing matches for REGEXP.
  257. If a match is split across lines, all the lines it lies in are hidden."
  258.   (interactive "sHide lines (containing match for regexp): ")
  259.   (save-excursion
  260.     (while (and (not (eobp))
  261.         (re-search-forward regexp nil t))
  262.       (hide-region (save-excursion (goto-char (match-beginning 0))
  263.                    (forward-line -1)
  264.                    (point))
  265.            (match-end 0))
  266.       (forward-line 1))))
  267.  
  268.  
  269. (defun forward-maybe-hidden-line (n)
  270.   "Move forward a ``logical'' line, hidden or not."
  271.   (goto-char
  272.    (let* ((l (save-excursion
  273.           (forward-line n)
  274.           (point)))
  275.       (p (progn
  276.            (search-forward "\r" l 'move
  277.                    (if (plusp n)
  278.                    n
  279.                    (- n 1)))
  280.            (point))))
  281.      
  282.      (if (plusp n)
  283.      (min l p)
  284.      (max l p)))))
  285.  
  286. (defun show-matching-lines (regexp)
  287.   "Show lines, from point to end of buffer, containing matches for REGEXP.
  288. If a match is split across lines, all the lines it lies in are shown.  (all.el)"
  289.   (interactive "sShow lines (containing match for regexp): ")
  290.   (save-excursion
  291.     (while (and (not (eobp))
  292.         (re-search-forward regexp nil t))
  293.       (show-region (save-excursion (forward-maybe-hidden-line 0)
  294.                    (point))
  295.            (progn
  296.              (forward-maybe-hidden-line 1)
  297.              (backward-char 1)
  298.              (point))))))
  299.  
  300.  
  301.  
  302.  
  303.  
  304.