Mercurial and Git clients can push and pull from this alias URL to interact with this repository. You can change to which repository an alias points by going to the Aliases link on the project page.
# qscilib.py - Utility codes for QsciScintilla## Copyright 2010 Steve Borho <steve@borho.org># Copyright 2010 Yuya Nishihara <yuya@tcha.org>## This software may be used and distributed according to the terms of the# GNU General Public License version 2 or any later version.importrefrommercurialimportutilfromtortoisehg.utilimporthglibfromtortoisehg.hgqtimportqtlibfromtortoisehg.hgqt.i18nimport_fromPyQt4.QtCoreimport*fromPyQt4.QtGuiimport*fromPyQt4.Qsciimport*class_SciImSupport(object):"""Patch for QsciScintilla to implement improved input method support See http://doc.trolltech.com/4.7/qinputmethodevent.html """PREEDIT_INDIC_ID=QsciScintilla.INDIC_MAX"""indicator for highlighting preedit text"""def__init__(self,sci):self._sci=sciself._preeditpos=(0,0)# (line, index) where preedit text startsself._preeditlen=0self._preeditcursorpos=0# relative pos where preedit cursor existsself._undoactionbegun=Falseself._setuppreeditindic()defremovepreedit(self):"""Remove the previous preedit text original pos: preedit cursor final pos: target cursor """l,i=self._sci.getCursorPosition()i-=self._preeditcursorposself._preeditcursorpos=0try:self._sci.setSelection(self._preeditpos[0],self._preeditpos[1],self._preeditpos[0],self._preeditpos[1]+self._preeditlen)self._sci.removeSelectedText()finally:self._sci.setCursorPosition(l,i)defcommitstr(self,start,repllen,commitstr):"""Remove the repl string followed by insertion of the commit string original pos: target cursor final pos: end of committed text (= start of preedit text) """l,i=self._sci.getCursorPosition()i+=startself._sci.setSelection(l,i,l,i+repllen)self._sci.removeSelectedText()self._sci.insert(commitstr)self._sci.setCursorPosition(l,i+len(commitstr))ifcommitstr:self.endundo()definsertpreedit(self,text):"""Insert preedit text original pos: start of preedit text final pos: start of preedit text (unchanged) """iftextandnotself._preeditlen:self.beginundo()l,i=self._sci.getCursorPosition()self._sci.insert(text)self._updatepreeditpos(l,i,len(text))ifnotself._preeditlen:self.endundo()defmovepreeditcursor(self,pos):"""Move the cursor to the relative pos inside preedit text"""self._preeditcursorpos=min(pos,self._preeditlen)l,i=self._preeditposself._sci.setCursorPosition(l,i+self._preeditcursorpos)defbeginundo(self):ifself._undoactionbegun:returnself._sci.beginUndoAction()self._undoactionbegun=Truedefendundo(self):ifnotself._undoactionbegun:returnself._sci.endUndoAction()self._undoactionbegun=Falsedef_updatepreeditpos(self,l,i,len):"""Update the indicator and internal state for preedit text"""self._sci.SendScintilla(QsciScintilla.SCI_SETINDICATORCURRENT,self.PREEDIT_INDIC_ID)self._preeditpos=(l,i)self._preeditlen=leniflen<=0:# have problem on scireturnp=self._sci.positionFromLineIndex(*self._preeditpos)q=self._sci.positionFromLineIndex(self._preeditpos[0],self._preeditpos[1]+len)self._sci.SendScintilla(QsciScintilla.SCI_INDICATORFILLRANGE,p,q-p)# q - p != lendef_setuppreeditindic(self):"""Configure the style of preedit text indicator"""self._sci.SendScintilla(QsciScintilla.SCI_INDICSETSTYLE,self.PREEDIT_INDIC_ID,QsciScintilla.INDIC_PLAIN)classScintilla(QsciScintilla):_stdMenu=Nonedef__init__(self,parent=None):super(Scintilla,self).__init__(parent)self.setUtf8(True)self.textChanged.connect(self._resetfindcond)self._resetfindcond()definputMethodQuery(self,query):ifquery==Qt.ImMicroFocus:returnself.cursorRect()returnsuper(Scintilla,self).inputMethodQuery(query)definputMethodEvent(self,event):ifself.isReadOnly():returnself.removeSelectedText()self._imsupport.removepreedit()self._imsupport.commitstr(event.replacementStart(),event.replacementLength(),event.commitString())self._imsupport.insertpreedit(event.preeditString())forainevent.attributes():ifa.type==QInputMethodEvent.Cursor:self._imsupport.movepreeditcursor(a.start)# TODO TextFormatevent.accept()@util.propertycachedef_imsupport(self):return_SciImSupport(self)defcursorRect(self):"""Return a rectangle (in viewport coords) including the cursor"""l,i=self.getCursorPosition()p=self.positionFromLineIndex(l,i)x=self.SendScintilla(QsciScintilla.SCI_POINTXFROMPOSITION,0,p)y=self.SendScintilla(QsciScintilla.SCI_POINTYFROMPOSITION,0,p)w=self.SendScintilla(QsciScintilla.SCI_GETCARETWIDTH)returnQRect(x,y,w,self.textHeight(l))defcreateStandardContextMenu(self):"""Create standard context menu"""ifnotself._stdMenu:self._stdMenu=QMenu(self)else:self._stdMenu.clear()ifnotself.isReadOnly():a=self._stdMenu.addAction(_('Undo'),self.undo,QKeySequence.Undo)a.setEnabled(self.isUndoAvailable())a=self._stdMenu.addAction(_('Redo'),self.redo,QKeySequence.Redo)a.setEnabled(self.isRedoAvailable())self._stdMenu.addSeparator()a=self._stdMenu.addAction(_('Cut'),self.cut,QKeySequence.Cut)a.setEnabled(self.hasSelectedText())a=self._stdMenu.addAction(_('Copy'),self.copy,QKeySequence.Copy)a.setEnabled(self.hasSelectedText())ifnotself.isReadOnly():self._stdMenu.addAction(_('Paste'),self.paste,QKeySequence.Paste)a=self._stdMenu.addAction(_('Delete'),self.removeSelectedText,QKeySequence.Delete)a.setEnabled(self.hasSelectedText())self._stdMenu.addSeparator()self._stdMenu.addAction(_('Select All'),self.selectAll,QKeySequence.SelectAll)self._stdMenu.addSeparator()qsci=QsciScintillawrapmenu=QMenu(_('Wrap'),self)forname,modein((_('None','wrap mode'),qsci.WrapNone),(_('Word'),qsci.WrapWord),(_('Character'),qsci.WrapCharacter)):defmkaction(n,m):a=wrapmenu.addAction(n)a.setCheckable(True)a.setChecked(self.wrapMode()==m)a.triggered.connect(lambda:self.setWrapMode(m))mkaction(name,mode)wsmenu=QMenu(_('Whitespace'),self)forname,modein((_('Visible'),qsci.WsVisible),(_('Invisible'),qsci.WsInvisible),(_('AfterIndent'),qsci.WsVisibleAfterIndent)):defmkaction(n,m):a=wsmenu.addAction(n)a.setCheckable(True)a.setChecked(self.whitespaceVisibility()==m)a.triggered.connect(lambda:self.setWhitespaceVisibility(m))mkaction(name,mode)vsmenu=QMenu(_('EolnVisibility'),self)forname,modein((_('Visible'),True),(_('Invisible'),False)):defmkaction(n,m):a=vsmenu.addAction(n)a.setCheckable(True)a.setChecked(self.eolVisibility()==m)a.triggered.connect(lambda:self.setEolVisibility(m))mkaction(name,mode)self._stdMenu.addMenu(wrapmenu)self._stdMenu.addMenu(wsmenu)self._stdMenu.addMenu(vsmenu)returnself._stdMenudefsaveSettings(self,qs,prefix):qs.setValue(prefix+'/wrap',self.wrapMode())qs.setValue(prefix+'/whitespace',self.whitespaceVisibility())qs.setValue(prefix+'/eol',self.eolVisibility())defloadSettings(self,qs,prefix):self.setWrapMode(qs.value(prefix+'/wrap').toInt()[0])self.setWhitespaceVisibility(qs.value(prefix+'/whitespace').toInt()[0])self.setEolVisibility(qs.value(prefix+'/eol').toBool())@pyqtSlot(unicode,bool,bool,bool)deffind(self,exp,icase=True,wrap=False,forward=True):"""Find the next/prev occurence; returns True if found This method tries to imitate the behavior of QTextEdit.find(), unlike combo of QsciScintilla.findFirst() and findNext(). """cond=(exp,True,noticase,False,wrap,forward)ifcond==self.__findcond:returnself.findNext()else:self.__findcond=condreturnself.findFirst(*cond)@pyqtSlot()def_resetfindcond(self):self.__findcond=()@pyqtSlot(unicode,bool)defhighlightText(self,match,icase=False):"""Highlight text matching to the given regexp pattern [unicode] The previous highlight is cleared automatically. """try:flags=0ificase:flags|=re.IGNORECASEpat=re.compile(unicode(match).encode('utf-8'),flags)exceptre.error:return# it could be partial pattern while user typingself.clearHighlightText() self.SendScintilla(self.SCI_SETINDICATORCURRENT,
self._highlightIndicator)
+ if len(match) == 0:+ return+ # NOTE: pat and target text are *not* unicode because scintilla
# requires positions in byte. For accuracy, it should do pattern
# match in unicode, then calculating byte length of substring::
## text = unicode(self.text())# for m in pat.finditer(text):# p = len(text[:m.start()].encode('utf-8'))# self.SendScintilla(self.SCI_INDICATORFILLRANGE,# p, len(m.group(0).encode('utf-8')))## but it doesn't to avoid possible performance issue.forminpat.finditer(unicode(self.text()).encode('utf-8')):self.SendScintilla(self.SCI_INDICATORFILLRANGE,m.start(),m.end()-m.start())@pyqtSlot()defclearHighlightText(self):self.SendScintilla(self.SCI_SETINDICATORCURRENT,self._highlightIndicator)self.SendScintilla(self.SCI_INDICATORCLEARRANGE,0,self.length())@util.propertycachedef_highlightIndicator(self):"""Return indicator number for highlight after initializing it"""id=self._imsupport.PREEDIT_INDIC_ID-1self.SendScintilla(self.SCI_INDICSETSTYLE,id,self.INDIC_ROUNDBOX)self.SendScintilla(self.SCI_INDICSETUNDER,id,True)self.SendScintilla(self.SCI_INDICSETFORE,id,0x00ffff)# 0xbbggrr# document says alpha value is 0 to 255, but it looks 0 to 100self.SendScintilla(self.SCI_INDICSETALPHA,id,100)returnidclassSearchToolBar(QToolBar):conditionChanged=pyqtSignal(unicode,bool,bool)"""Emitted (pattern, icase, wrap) when search condition changed"""searchRequested=pyqtSignal(unicode,bool,bool,bool)"""Emitted (pattern, icase, wrap, forward) when requested"""def__init__(self,parent=None,hidable=False,settings=None):super(SearchToolBar,self).__init__(_('Search'),parent,objectName='search',iconSize=QSize(16,16))ifhidable:self._close_button=QToolButton(icon=qtlib.geticon('window-close'),shortcut=Qt.Key_Escape)self._close_button.clicked.connect(self.hide)self.addWidget(self._close_button)self._le=QLineEdit()ifhasattr(self._le,'setPlaceholderText'):# Qt >= 4.7self._le.setPlaceholderText(_('### regular expression ###'))else:self._lbl=QLabel(_('Regexp:'),toolTip=_('Regular expression search pattern'))self.addWidget(self._lbl)self._lbl.setBuddy(self._le)self._le.returnPressed.connect(self._emitSearchRequested)self.addWidget(self._le)self._chk=QCheckBox(_('Ignore case'))self.addWidget(self._chk)self._wrapchk=QCheckBox(_('Wrap search'))self.addWidget(self._wrapchk)self._bt=QPushButton(_('Search'),enabled=False)self._bt.clicked.connect(self._emitSearchRequested)self._le.textChanged.connect(lambdas:self._bt.setEnabled(bool(s)))self.addWidget(self._bt)self.setFocusProxy(self._le)defdefaultsettings():s=QSettings()s.beginGroup('searchtoolbar')returnsself._settings=settingsordefaultsettings()self.searchRequested.connect(self._writesettings)self._readsettings()self._le.textChanged.connect(self._emitConditionChanged)self._chk.toggled.connect(self._emitConditionChanged)self._wrapchk.toggled.connect(self._emitConditionChanged)defkeyPressEvent(self,event):ifevent.matches(QKeySequence.FindNext):self._emitSearchRequested(forward=True)returnifevent.matches(QKeySequence.FindPrevious):self._emitSearchRequested(forward=False)returnifevent.key()in(Qt.Key_Enter,Qt.Key_Return):return# handled by returnPressedsuper(SearchToolBar,self).keyPressEvent(event)defwheelEvent(self,event):ifevent.delta()>0:self._emitSearchRequested(forward=False)returnifevent.delta()<0:self._emitSearchRequested(forward=True)returnsuper(SearchToolBar,self).wheelEvent(event)defsetVisible(self,visible=True):super(SearchToolBar,self).setVisible(visible)ifvisible:self._le.setFocus()self._le.selectAll()def_readsettings(self):self.setCaseInsensitive(self._settings.value('icase',False).toBool())self.setWrapAround(self._settings.value('wrap',False).toBool())@pyqtSlot()def_writesettings(self):self._settings.setValue('icase',self.caseInsensitive())self._settings.setValue('wrap',self.wrapAround())@pyqtSlot()def_emitConditionChanged(self):self.conditionChanged.emit(self.pattern(),self.caseInsensitive(),self.wrapAround())@pyqtSlot()def_emitSearchRequested(self,forward=True):self.searchRequested.emit(self.pattern(),self.caseInsensitive(),self.wrapAround(),forward)defpattern(self):"""Returns the current search pattern [unicode]"""returnself._le.text()defsetPattern(self,text):"""Set the search pattern [unicode]"""self._le.setText(text)defcaseInsensitive(self):"""True if case-insensitive search is requested"""returnself._chk.isChecked()defsetCaseInsensitive(self,icase):self._chk.setChecked(icase)defwrapAround(self):"""True if wrap search is requested"""returnself._wrapchk.isChecked()defsetWrapAround(self,wrap):self._wrapchk.setChecked(wrap)@pyqtSlot(unicode)defsearch(self,text):"""Request search with the given pattern"""self.setPattern(text)self._emitSearchRequested()classKeyPressInterceptor(QObject):"""Grab key press events important for dialogs Usage:: sci = qscilib.Scintilla(self) sci.installEventFilter(KeyPressInterceptor(self)) """def__init__(self,parent=None,keys=None,keyseqs=None):super(KeyPressInterceptor,self).__init__(parent)self._keys=set((Qt.Key_Escape,))self._keyseqs=set((QKeySequence.Refresh,))ifkeys:self._keys.update(keys)ifkeyseqs:self._keyseqs.update(keyseqs)defeventFilter(self,watched,event):ifevent.type()!=QEvent.KeyPress:returnsuper(KeyPressInterceptor,self).eventFilter(watched,event)ifself._isinterceptable(event):event.ignore()returnTruereturnFalsedef_isinterceptable(self,event):ifevent.key()inself._keys:returnTrueifutil.any(event.matches(e)foreinself._keyseqs):returnTruereturnFalsedeffileEditor(filename,**opts):'Open a simple modal file editing dialog'dialog=QDialog()dialog.setWindowFlags(dialog.windowFlags()&~Qt.WindowContextHelpButtonHint)dialog.setWindowTitle(filename)dialog.setLayout(QVBoxLayout())editor=Scintilla()editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)editor.installEventFilter(KeyPressInterceptor(dialog))editor.setMarginLineNumbers(1,True)editor.setMarginWidth(1,'000')editor.setLexer(QsciLexerProperties())ifopts.get('foldable'):editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)dialog.layout().addWidget(editor)searchbar=SearchToolBar(dialog,hidable=True)searchbar.searchRequested.connect(editor.find)searchbar.conditionChanged.connect(editor.highlightText)searchbar.hide()defshowsearchbar():searchbar.show()searchbar.setFocus(Qt.OtherFocusReason)QShortcut(QKeySequence.Find,dialog,showsearchbar)dialog.layout().addWidget(searchbar)BB=QDialogButtonBoxbb=QDialogButtonBox(BB.Save|BB.Cancel)bb.accepted.connect(dialog.accept)bb.rejected.connect(dialog.reject)dialog.layout().addWidget(bb)s=QSettings()geomname='editor-geom'desktopgeom=qApp.desktop().availableGeometry()dialog.resize(desktopgeom.size()*0.5)dialog.restoreGeometry(s.value(geomname).toByteArray())ret=QDialog.Rejectedtry:f=QFile(filename)f.open(QIODevice.ReadOnly)editor.read(f)editor.setModified(False)ret=dialog.exec_()ifret==QDialog.Accepted:f=QFile(filename)f.open(QIODevice.WriteOnly)editor.write(f)s.setValue(geomname,dialog.saveGeometry())exceptEnvironmentError,e:qtlib.WarningMsgBox(_('Unable to read/write config file'),hglib.tounicode(str(e)),parent=dialog)returnret
Attach a Trello Card
Add a tag
Your session has expired
You are no longer logged in. Please log in and try your request again.
Filter RSS Feed
This RSS feed URL allows you to see the contents of your current filter using any feed reader.
This link includes a special authentication token. If you share the URL with anyone else, they can see this RSS feed's activity. You can disable these tokens when needed.
Your current filter is unsaved; changing it won't affect this RSS feed.