def genall(): remem = lisp.point() lisp.beginning_of_buffer() res = True while res: res = lisp.re_search_forward("$$[[:ascii:][:nonascii:]]*?$$",None,lisp.t) lisp.backward_char(3) show() lisp.goto_char(remem)
def genall(): remem = lisp.point() lisp.beginning_of_buffer() res = True while res: res = lisp.re_search_forward("$$[[:ascii:][:nonascii:]]*?$$", None, lisp.t) lisp.backward_char(3) show() lisp.goto_char(remem)
def find_comment(self): """\ Find and return the limits of the block of comments following or enclosing the cursor, or return an error if the cursor is not within such a block of comments. Extend it as far as possible in both directions. """ let = Let().push_excursion() try: # Find the start of the current or immediately following comment. lisp.beginning_of_line() lisp.skip_chars_forward(' \t\n') lisp.beginning_of_line() if not language_matcher[0](self.remainder_of_line()): temp = lisp.point() if not lisp.re_search_forward('\\*/', None, lisp.t): lisp.error("outside any comment block") lisp.re_search_backward('/\\*') if lisp.point() > temp: lisp.error("outside any comment block") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(' \t') if lisp.point() != temp: lisp.error("text before start of comment") lisp.beginning_of_line() start = lisp.point() language = guess_language(self.remainder_of_line()) # Find the end of this comment. if language == 2: lisp.search_forward('*/') if not lisp.looking_at('[ \t]*$'): lisp.error("text after end of comment") lisp.end_of_line() if lisp.eobp(): lisp.insert('\n') else: lisp.forward_char(1) end = lisp.point() # Try to extend the comment block backwards. lisp.goto_char(start) while not lisp.bobp(): if language == 2: lisp.skip_chars_backward(' \t\n') if not lisp.looking_at('[ \t]*\n[ \t]*/\\*'): break if lisp.point() < 2: break lisp.backward_char(2) if not lisp.looking_at('\\*/'): break lisp.re_search_backward('/\\*') temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(' \t') if lisp.point() != temp: break lisp.beginning_of_line() else: lisp.previous_line(1) if not language_matcher[language]( self.remainder_of_line()): break start = lisp.point() # Try to extend the comment block forward. lisp.goto_char(end) while language_matcher[language](self.remainder_of_line()): if language == 2: lisp.re_search_forward('[ \t]*/\\*') lisp.re_search_forward('\\*/') if lisp.looking_at('[ \t]*$'): lisp.beginning_of_line() lisp.forward_line(1) end = lisp.point() else: lisp.forward_line(1) end = lisp.point() return start, end finally: let.pops()
def pmfetch(pmid): """ Fetch and insert a BibTeX reference for PubMed article w/ given PMID at beginning of buffer. """ try: # Init vars key = '' author = '' title = '' journal = '' year = '' volume = '' number = '' pages = '' month = '' note = '' abstract = '' url = '' # Format author def format_author(author): suffix = '' if author[-2:] in ('Jr', 'Sr'): suffix = author[-2:] + '.' author = author[:-3] elif author[-3:] in ('2nd', '3rd'): suffix = author[-3:] author = author[:-4] author = split(author, maxsplit=-1) last_name = author[0] initials = author[1] initials = list(initials) if suffix: initials.append(suffix) else: initials.append('') return strip(last_name + ', ' + '. '.join(initials)) # Format and check pmid, and fetch article from PubMed pmid = str(pmid).strip() if not pmid.isdigit(): raise ValueError pm = PubMed.Dictionary(parser=Medline.RecordParser()) article = pm[pmid] # Fetch article from PubMed if not article or not article.pubmed_id == pmid: raise ValueError if not article.title: raise ValueError else: title = article.title if not article.no_author and article.authors: authors = [format_author(author) for author in article.authors] author = ' and '.join(authors) if not article.publication_date: raise ValueError else: year = article.publication_date[:4] if not article.title_abbreviation: raise ValueError else: journal = article.title_abbreviation if not article.volume_issue: raise ValueError else: volume = article.volume_issue if not article.pagination: raise ValueError else: pages = article.pagination.replace('-','--') if article.abstract: abstract = article.abstract if article.issue_part_supplement: number = article.issue_part_supplement # Create BibTeX ref bib = """ @ARTICLE{%s, pmid = {%s}, author = {%s}, title = {{%s}}, journal = {%s}, year = {%s}, volume = {%s}, number = {%s}, pages = {%s}, month = {%s}, abstract = {%s}, note = {%s}, url = {%s}, } """ % (key,pmid,author,title,journal,year,volume,number,pages,month,abstract,note,url) # Insert BibTeX ref lisp.goto_char(lisp.point_min()) if lisp.re_search_forward(pmid,None,True,1): lisp.message('Article with PMID = %s is already in buffer.' % pmid) return lisp.insert(bib) lisp.bibtex_clean_entry() lisp.bibtex_fill_entry() return except: lisp.message('Unable to fetch article from PubMed. Check PMID and try again.') return
def find_comment(self): """\ Find and return the limits of the block of comments following or enclosing the cursor, or return an error if the cursor is not within such a block of comments. Extend it as far as possible in both directions. """ let = Let().push_excursion() try: # Find the start of the current or immediately following comment. lisp.beginning_of_line() lisp.skip_chars_forward(" \t\n") lisp.beginning_of_line() if not language_matcher[0](self.remainder_of_line()): temp = lisp.point() if not lisp.re_search_forward("\\*/", None, lisp.t): lisp.error("outside any comment block") lisp.re_search_backward("/\\*") if lisp.point() > temp: lisp.error("outside any comment block") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(" \t") if lisp.point() != temp: lisp.error("text before start of comment") lisp.beginning_of_line() start = lisp.point() language = guess_language(self.remainder_of_line()) # Find the end of this comment. if language == 2: lisp.search_forward("*/") if not lisp.looking_at("[ \t]*$"): lisp.error("text after end of comment") lisp.end_of_line() if lisp.eobp(): lisp.insert("\n") else: lisp.forward_char(1) end = lisp.point() # Try to extend the comment block backwards. lisp.goto_char(start) while not lisp.bobp(): if language == 2: lisp.skip_chars_backward(" \t\n") if not lisp.looking_at("[ \t]*\n[ \t]*/\\*"): break if lisp.point() < 2: break lisp.backward_char(2) if not lisp.looking_at("\\*/"): break lisp.re_search_backward("/\\*") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(" \t") if lisp.point() != temp: break lisp.beginning_of_line() else: lisp.previous_line(1) if not language_matcher[language](self.remainder_of_line()): break start = lisp.point() # Try to extend the comment block forward. lisp.goto_char(end) while language_matcher[language](self.remainder_of_line()): if language == 2: lisp.re_search_forward("[ \t]*/\\*") lisp.re_search_forward("\\*/") if lisp.looking_at("[ \t]*$"): lisp.beginning_of_line() lisp.forward_line(1) end = lisp.point() else: lisp.forward_line(1) end = lisp.point() return start, end finally: let.pops()